Skip to content

Commit bbd9590

Browse files
Merge pull request #20194 from umohnani8/kube-mode
Add DefaultMode to kube play
2 parents 9560d36 + 17cebb3 commit bbd9590

File tree

3 files changed

+252
-27
lines changed

3 files changed

+252
-27
lines changed

pkg/domain/infra/abi/play.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,7 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
628628
if err != nil || mountPoint == "" {
629629
return nil, nil, fmt.Errorf("unable to get mountpoint of volume %q: %w", vol.Name(), err)
630630
}
631+
defaultMode := v.DefaultMode
631632
// Create files and add data to the volume mountpoint based on the Items in the volume
632633
for k, v := range v.Items {
633634
dataPath := filepath.Join(mountPoint, k)
@@ -640,6 +641,10 @@ func (ic *ContainerEngine) playKubePod(ctx context.Context, podName string, podY
640641
if err != nil {
641642
return nil, nil, err
642643
}
644+
// Set file permissions
645+
if err := os.Chmod(f.Name(), os.FileMode(defaultMode)); err != nil {
646+
return nil, nil, err
647+
}
643648
}
644649
}
645650
}

pkg/specgen/generate/kube/volume.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ type KubeVolume struct {
5151
// If the volume is optional, we can move on if it is not found
5252
// Only used when there are volumes in a yaml that refer to a configmap
5353
Optional bool
54+
// DefaultMode sets the permissions on files created for the volume
55+
// This is optional and defaults to 0644
56+
DefaultMode int32
5457
}
5558

5659
// Create a KubeVolume from an HostPathVolumeSource
@@ -135,9 +138,18 @@ func VolumeFromHostPath(hostPath *v1.HostPathVolumeSource, mountLabel string) (*
135138
// VolumeFromSecret creates a new kube volume from a kube secret.
136139
func VolumeFromSecret(secretSource *v1.SecretVolumeSource, secretsManager *secrets.SecretsManager) (*KubeVolume, error) {
137140
kv := &KubeVolume{
138-
Type: KubeVolumeTypeSecret,
139-
Source: secretSource.SecretName,
140-
Items: map[string][]byte{},
141+
Type: KubeVolumeTypeSecret,
142+
Source: secretSource.SecretName,
143+
Items: map[string][]byte{},
144+
DefaultMode: v1.SecretVolumeSourceDefaultMode,
145+
}
146+
// Set the defaultMode if set in the kube yaml
147+
validMode, err := isValidDefaultMode(secretSource.DefaultMode)
148+
if err != nil {
149+
return nil, fmt.Errorf("invalid DefaultMode for secret %q: %w", secretSource.SecretName, err)
150+
}
151+
if validMode {
152+
kv.DefaultMode = *secretSource.DefaultMode
141153
}
142154

143155
// returns a byte array of a kube secret data, meaning this needs to go into a string map
@@ -191,8 +203,9 @@ func VolumeFromPersistentVolumeClaim(claim *v1.PersistentVolumeClaimVolumeSource
191203
func VolumeFromConfigMap(configMapVolumeSource *v1.ConfigMapVolumeSource, configMaps []v1.ConfigMap) (*KubeVolume, error) {
192204
var configMap *v1.ConfigMap
193205
kv := &KubeVolume{
194-
Type: KubeVolumeTypeConfigMap,
195-
Items: map[string][]byte{},
206+
Type: KubeVolumeTypeConfigMap,
207+
Items: map[string][]byte{},
208+
DefaultMode: v1.ConfigMapVolumeSourceDefaultMode,
196209
}
197210
for _, cm := range configMaps {
198211
if cm.Name == configMapVolumeSource.Name {
@@ -203,6 +216,14 @@ func VolumeFromConfigMap(configMapVolumeSource *v1.ConfigMapVolumeSource, config
203216
break
204217
}
205218
}
219+
// Set the defaultMode if set in the kube yaml
220+
validMode, err := isValidDefaultMode(configMapVolumeSource.DefaultMode)
221+
if err != nil {
222+
return nil, fmt.Errorf("invalid DefaultMode for configMap %q: %w", configMapVolumeSource.Name, err)
223+
}
224+
if validMode {
225+
kv.DefaultMode = *configMapVolumeSource.DefaultMode
226+
}
206227

207228
if configMap == nil {
208229
// If the volumeSource was optional, move on even if a matching configmap wasn't found
@@ -279,3 +300,14 @@ func InitializeVolumes(specVolumes []v1.Volume, configMaps []v1.ConfigMap, secre
279300

280301
return volumes, nil
281302
}
303+
304+
// isValidDefaultMode returns true if mode is between 0 and 0777
305+
func isValidDefaultMode(mode *int32) (bool, error) {
306+
if mode == nil {
307+
return false, nil
308+
}
309+
if *mode >= 0 && *mode <= int32(os.ModePerm) {
310+
return true, nil
311+
}
312+
return false, errors.New("must be between 0000 and 0777")
313+
}

0 commit comments

Comments
 (0)