Skip to content

Commit 6f3b6b9

Browse files
authored
KEP-3721: Support for env files (kubernetes#132626)
* Add FileKeyRef field and struct to the Pod API * Add the implementation code in the kubelet. * Add validation code * Add basic functionality e2e tests * add codes for drop disabled pod fields * update go.mod
1 parent 08362f0 commit 6f3b6b9

File tree

94 files changed

+6397
-1204
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

94 files changed

+6397
-1204
lines changed

api/openapi-spec/swagger.json

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/api__v1_openapi.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/apis__apps__v1_openapi.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/openapi-spec/v3/apis__batch__v1_openapi.json

Lines changed: 40 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ require (
2323
github.com/coreos/go-oidc v2.3.0+incompatible
2424
github.com/coreos/go-systemd/v22 v22.5.0
2525
github.com/cpuguy83/go-md2man/v2 v2.0.6
26+
github.com/cyphar/filepath-securejoin v0.4.1
2627
github.com/distribution/reference v0.6.0
2728
github.com/docker/go-units v0.5.0
2829
github.com/emicklei/go-restful/v3 v3.12.2
@@ -142,7 +143,6 @@ require (
142143
github.com/containerd/typeurl/v2 v2.2.2 // indirect
143144
github.com/coredns/caddy v1.1.1 // indirect
144145
github.com/coreos/go-semver v0.3.1 // indirect
145-
github.com/cyphar/filepath-securejoin v0.4.1 // indirect
146146
github.com/davecgh/go-spew v1.1.1 // indirect
147147
github.com/dustin/go-humanize v1.0.1 // indirect
148148
github.com/euank/go-kmsg-parser v2.0.0+incompatible // indirect

pkg/api/pod/util.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ func GetValidationOptionsFromPodSpecAndMeta(podSpec, oldPodSpec *api.PodSpec, po
432432
// we must allow it
433433
opts.AllowRelaxedEnvironmentVariableValidation = useRelaxedEnvironmentVariableValidation(podSpec, oldPodSpec)
434434
opts.AllowRelaxedDNSSearchValidation = useRelaxedDNSSearchValidation(oldPodSpec)
435+
opts.AllowEnvFilesValidation = useAllowEnvFilesValidation(oldPodSpec)
435436

436437
opts.AllowOnlyRecursiveSELinuxChangePolicy = useOnlyRecursiveSELinuxChangePolicy(oldPodSpec)
437438

@@ -526,6 +527,44 @@ func hasDotOrUnderscore(searches []string) bool {
526527
return false
527528
}
528529

530+
func useAllowEnvFilesValidation(oldPodSpec *api.PodSpec) bool {
531+
// Return true early if feature gate is enabled
532+
if utilfeature.DefaultFeatureGate.Enabled(features.EnvFiles) {
533+
return true
534+
}
535+
536+
if oldPodSpec == nil {
537+
return false
538+
}
539+
540+
for _, container := range oldPodSpec.Containers {
541+
if hasEnvFileKeyRef(container.Env) {
542+
return true
543+
}
544+
}
545+
for _, container := range oldPodSpec.InitContainers {
546+
if hasEnvFileKeyRef(container.Env) {
547+
return true
548+
}
549+
}
550+
for _, container := range oldPodSpec.EphemeralContainers {
551+
if hasEnvFileKeyRef(container.Env) {
552+
return true
553+
}
554+
}
555+
556+
return false
557+
}
558+
559+
func hasEnvFileKeyRef(envs []api.EnvVar) bool {
560+
for _, env := range envs {
561+
if env.ValueFrom != nil && env.ValueFrom.FileKeyRef != nil {
562+
return true
563+
}
564+
}
565+
return false
566+
}
567+
529568
func gatherPodEnvVarNames(podSpec *api.PodSpec) sets.Set[string] {
530569
podEnvVarNames := sets.Set[string]{}
531570

@@ -725,12 +764,46 @@ func dropDisabledFields(
725764
}
726765
}
727766

767+
dropFileKeyRefInUse(podSpec, oldPodSpec)
728768
dropPodLifecycleSleepAction(podSpec, oldPodSpec)
729769
dropImageVolumes(podSpec, oldPodSpec)
730770
dropSELinuxChangePolicy(podSpec, oldPodSpec)
731771
dropContainerStopSignals(podSpec, oldPodSpec)
732772
}
733773

774+
func dropFileKeyRefInUse(podSpec, oldPodSpec *api.PodSpec) {
775+
if utilfeature.DefaultFeatureGate.Enabled(features.EnvFiles) || podFileKeyRefInUse(oldPodSpec) {
776+
return
777+
}
778+
779+
VisitContainers(podSpec, AllContainers, func(c *api.Container, _ ContainerType) bool {
780+
for i := range c.Env {
781+
if c.Env[i].ValueFrom != nil && c.Env[i].ValueFrom.FileKeyRef != nil {
782+
c.Env[i].ValueFrom.FileKeyRef = nil
783+
}
784+
}
785+
return true
786+
})
787+
}
788+
789+
func podFileKeyRefInUse(podSpec *api.PodSpec) bool {
790+
if podSpec == nil {
791+
return false
792+
}
793+
794+
var inUse bool
795+
VisitContainers(podSpec, AllContainers, func(c *api.Container, _ ContainerType) bool {
796+
for _, env := range c.Env {
797+
if env.ValueFrom != nil && env.ValueFrom.FileKeyRef != nil {
798+
inUse = true
799+
return false
800+
}
801+
}
802+
return true
803+
})
804+
return inUse
805+
}
806+
734807
func dropContainerStopSignals(podSpec, oldPodSpec *api.PodSpec) {
735808
if utilfeature.DefaultFeatureGate.Enabled(features.ContainerStopSignals) || containerStopSignalsInUse(oldPodSpec) {
736809
return

0 commit comments

Comments
 (0)