1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771 |
- /*
- Copyright 2017 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package pod
- import (
- "fmt"
- "reflect"
- "strings"
- "testing"
- "k8s.io/apimachinery/pkg/api/resource"
- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
- "k8s.io/apimachinery/pkg/util/diff"
- "k8s.io/apimachinery/pkg/util/sets"
- "k8s.io/apimachinery/pkg/util/validation/field"
- utilfeature "k8s.io/apiserver/pkg/util/feature"
- featuregatetesting "k8s.io/component-base/featuregate/testing"
- api "k8s.io/kubernetes/pkg/apis/core"
- "k8s.io/kubernetes/pkg/features"
- "k8s.io/kubernetes/pkg/security/apparmor"
- )
- func TestPodSecrets(t *testing.T) {
- // Stub containing all possible secret references in a pod.
- // The names of the referenced secrets match struct paths detected by reflection.
- pod := &api.Pod{
- Spec: api.PodSpec{
- Containers: []api.Container{{
- EnvFrom: []api.EnvFromSource{{
- SecretRef: &api.SecretEnvSource{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Containers[*].EnvFrom[*].SecretRef"}}}},
- Env: []api.EnvVar{{
- ValueFrom: &api.EnvVarSource{
- SecretKeyRef: &api.SecretKeySelector{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Containers[*].Env[*].ValueFrom.SecretKeyRef"}}}}}}},
- ImagePullSecrets: []api.LocalObjectReference{{
- Name: "Spec.ImagePullSecrets"}},
- InitContainers: []api.Container{{
- EnvFrom: []api.EnvFromSource{{
- SecretRef: &api.SecretEnvSource{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.InitContainers[*].EnvFrom[*].SecretRef"}}}},
- Env: []api.EnvVar{{
- ValueFrom: &api.EnvVarSource{
- SecretKeyRef: &api.SecretKeySelector{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.InitContainers[*].Env[*].ValueFrom.SecretKeyRef"}}}}}}},
- Volumes: []api.Volume{{
- VolumeSource: api.VolumeSource{
- AzureFile: &api.AzureFileVolumeSource{
- SecretName: "Spec.Volumes[*].VolumeSource.AzureFile.SecretName"}}}, {
- VolumeSource: api.VolumeSource{
- CephFS: &api.CephFSVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.CephFS.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- Cinder: &api.CinderVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.Cinder.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- FlexVolume: &api.FlexVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.FlexVolume.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- Projected: &api.ProjectedVolumeSource{
- Sources: []api.VolumeProjection{{
- Secret: &api.SecretProjection{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.Projected.Sources[*].Secret"}}}}}}}, {
- VolumeSource: api.VolumeSource{
- RBD: &api.RBDVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.RBD.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- Secret: &api.SecretVolumeSource{
- SecretName: "Spec.Volumes[*].VolumeSource.Secret.SecretName"}}}, {
- VolumeSource: api.VolumeSource{
- Secret: &api.SecretVolumeSource{
- SecretName: "Spec.Volumes[*].VolumeSource.Secret"}}}, {
- VolumeSource: api.VolumeSource{
- ScaleIO: &api.ScaleIOVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.ScaleIO.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- ISCSI: &api.ISCSIVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.ISCSI.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- StorageOS: &api.StorageOSVolumeSource{
- SecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.StorageOS.SecretRef"}}}}, {
- VolumeSource: api.VolumeSource{
- CSI: &api.CSIVolumeSource{
- NodePublishSecretRef: &api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.CSI.NodePublishSecretRef"}}}}},
- },
- }
- extractedNames := sets.NewString()
- VisitPodSecretNames(pod, func(name string) bool {
- extractedNames.Insert(name)
- return true
- })
- // excludedSecretPaths holds struct paths to fields with "secret" in the name that are not actually references to secret API objects
- excludedSecretPaths := sets.NewString(
- "Spec.Volumes[*].VolumeSource.CephFS.SecretFile",
- )
- // expectedSecretPaths holds struct paths to fields with "secret" in the name that are references to secret API objects.
- // every path here should be represented as an example in the Pod stub above, with the secret name set to the path.
- expectedSecretPaths := sets.NewString(
- "Spec.Containers[*].EnvFrom[*].SecretRef",
- "Spec.Containers[*].Env[*].ValueFrom.SecretKeyRef",
- "Spec.ImagePullSecrets",
- "Spec.InitContainers[*].EnvFrom[*].SecretRef",
- "Spec.InitContainers[*].Env[*].ValueFrom.SecretKeyRef",
- "Spec.Volumes[*].VolumeSource.AzureFile.SecretName",
- "Spec.Volumes[*].VolumeSource.CephFS.SecretRef",
- "Spec.Volumes[*].VolumeSource.Cinder.SecretRef",
- "Spec.Volumes[*].VolumeSource.FlexVolume.SecretRef",
- "Spec.Volumes[*].VolumeSource.Projected.Sources[*].Secret",
- "Spec.Volumes[*].VolumeSource.RBD.SecretRef",
- "Spec.Volumes[*].VolumeSource.Secret",
- "Spec.Volumes[*].VolumeSource.Secret.SecretName",
- "Spec.Volumes[*].VolumeSource.ScaleIO.SecretRef",
- "Spec.Volumes[*].VolumeSource.ISCSI.SecretRef",
- "Spec.Volumes[*].VolumeSource.StorageOS.SecretRef",
- "Spec.Volumes[*].VolumeSource.CSI.NodePublishSecretRef",
- )
- secretPaths := collectResourcePaths(t, "secret", nil, "", reflect.TypeOf(&api.Pod{}))
- secretPaths = secretPaths.Difference(excludedSecretPaths)
- if missingPaths := expectedSecretPaths.Difference(secretPaths); len(missingPaths) > 0 {
- t.Logf("Missing expected secret paths:\n%s", strings.Join(missingPaths.List(), "\n"))
- t.Error("Missing expected secret paths. Verify VisitPodSecretNames() is correctly finding the missing paths, then correct expectedSecretPaths")
- }
- if extraPaths := secretPaths.Difference(expectedSecretPaths); len(extraPaths) > 0 {
- t.Logf("Extra secret paths:\n%s", strings.Join(extraPaths.List(), "\n"))
- t.Error("Extra fields with 'secret' in the name found. Verify VisitPodSecretNames() is including these fields if appropriate, then correct expectedSecretPaths")
- }
- if missingNames := expectedSecretPaths.Difference(extractedNames); len(missingNames) > 0 {
- t.Logf("Missing expected secret names:\n%s", strings.Join(missingNames.List(), "\n"))
- t.Error("Missing expected secret names. Verify the pod stub above includes these references, then verify VisitPodSecretNames() is correctly finding the missing names")
- }
- if extraNames := extractedNames.Difference(expectedSecretPaths); len(extraNames) > 0 {
- t.Logf("Extra secret names:\n%s", strings.Join(extraNames.List(), "\n"))
- t.Error("Extra secret names extracted. Verify VisitPodSecretNames() is correctly extracting secret names")
- }
- }
- // collectResourcePaths traverses the object, computing all the struct paths that lead to fields with resourcename in the name.
- func collectResourcePaths(t *testing.T, resourcename string, path *field.Path, name string, tp reflect.Type) sets.String {
- resourcename = strings.ToLower(resourcename)
- resourcePaths := sets.NewString()
- if tp.Kind() == reflect.Ptr {
- resourcePaths.Insert(collectResourcePaths(t, resourcename, path, name, tp.Elem()).List()...)
- return resourcePaths
- }
- if strings.Contains(strings.ToLower(name), resourcename) {
- resourcePaths.Insert(path.String())
- }
- switch tp.Kind() {
- case reflect.Ptr:
- resourcePaths.Insert(collectResourcePaths(t, resourcename, path, name, tp.Elem()).List()...)
- case reflect.Struct:
- // ObjectMeta is generic and therefore should never have a field with a specific resource's name;
- // it contains cycles so it's easiest to just skip it.
- if name == "ObjectMeta" {
- break
- }
- for i := 0; i < tp.NumField(); i++ {
- field := tp.Field(i)
- resourcePaths.Insert(collectResourcePaths(t, resourcename, path.Child(field.Name), field.Name, field.Type).List()...)
- }
- case reflect.Interface:
- t.Errorf("cannot find %s fields in interface{} field %s", resourcename, path.String())
- case reflect.Map:
- resourcePaths.Insert(collectResourcePaths(t, resourcename, path.Key("*"), "", tp.Elem()).List()...)
- case reflect.Slice:
- resourcePaths.Insert(collectResourcePaths(t, resourcename, path.Key("*"), "", tp.Elem()).List()...)
- default:
- // all primitive types
- }
- return resourcePaths
- }
- func TestPodConfigmaps(t *testing.T) {
- // Stub containing all possible ConfigMap references in a pod.
- // The names of the referenced ConfigMaps match struct paths detected by reflection.
- pod := &api.Pod{
- Spec: api.PodSpec{
- Containers: []api.Container{{
- EnvFrom: []api.EnvFromSource{{
- ConfigMapRef: &api.ConfigMapEnvSource{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Containers[*].EnvFrom[*].ConfigMapRef"}}}},
- Env: []api.EnvVar{{
- ValueFrom: &api.EnvVarSource{
- ConfigMapKeyRef: &api.ConfigMapKeySelector{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Containers[*].Env[*].ValueFrom.ConfigMapKeyRef"}}}}}}},
- InitContainers: []api.Container{{
- EnvFrom: []api.EnvFromSource{{
- ConfigMapRef: &api.ConfigMapEnvSource{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.InitContainers[*].EnvFrom[*].ConfigMapRef"}}}},
- Env: []api.EnvVar{{
- ValueFrom: &api.EnvVarSource{
- ConfigMapKeyRef: &api.ConfigMapKeySelector{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.InitContainers[*].Env[*].ValueFrom.ConfigMapKeyRef"}}}}}}},
- Volumes: []api.Volume{{
- VolumeSource: api.VolumeSource{
- Projected: &api.ProjectedVolumeSource{
- Sources: []api.VolumeProjection{{
- ConfigMap: &api.ConfigMapProjection{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.Projected.Sources[*].ConfigMap"}}}}}}}, {
- VolumeSource: api.VolumeSource{
- ConfigMap: &api.ConfigMapVolumeSource{
- LocalObjectReference: api.LocalObjectReference{
- Name: "Spec.Volumes[*].VolumeSource.ConfigMap"}}}}},
- },
- }
- extractedNames := sets.NewString()
- VisitPodConfigmapNames(pod, func(name string) bool {
- extractedNames.Insert(name)
- return true
- })
- // expectedPaths holds struct paths to fields with "ConfigMap" in the name that are references to ConfigMap API objects.
- // every path here should be represented as an example in the Pod stub above, with the ConfigMap name set to the path.
- expectedPaths := sets.NewString(
- "Spec.Containers[*].EnvFrom[*].ConfigMapRef",
- "Spec.Containers[*].Env[*].ValueFrom.ConfigMapKeyRef",
- "Spec.InitContainers[*].EnvFrom[*].ConfigMapRef",
- "Spec.InitContainers[*].Env[*].ValueFrom.ConfigMapKeyRef",
- "Spec.Volumes[*].VolumeSource.Projected.Sources[*].ConfigMap",
- "Spec.Volumes[*].VolumeSource.ConfigMap",
- )
- collectPaths := collectResourcePaths(t, "ConfigMap", nil, "", reflect.TypeOf(&api.Pod{}))
- if missingPaths := expectedPaths.Difference(collectPaths); len(missingPaths) > 0 {
- t.Logf("Missing expected paths:\n%s", strings.Join(missingPaths.List(), "\n"))
- t.Error("Missing expected paths. Verify VisitPodConfigmapNames() is correctly finding the missing paths, then correct expectedPaths")
- }
- if extraPaths := collectPaths.Difference(expectedPaths); len(extraPaths) > 0 {
- t.Logf("Extra paths:\n%s", strings.Join(extraPaths.List(), "\n"))
- t.Error("Extra fields with resource in the name found. Verify VisitPodConfigmapNames() is including these fields if appropriate, then correct expectedPaths")
- }
- if missingNames := expectedPaths.Difference(extractedNames); len(missingNames) > 0 {
- t.Logf("Missing expected names:\n%s", strings.Join(missingNames.List(), "\n"))
- t.Error("Missing expected names. Verify the pod stub above includes these references, then verify VisitPodConfigmapNames() is correctly finding the missing names")
- }
- if extraNames := extractedNames.Difference(expectedPaths); len(extraNames) > 0 {
- t.Logf("Extra names:\n%s", strings.Join(extraNames.List(), "\n"))
- t.Error("Extra names extracted. Verify VisitPodConfigmapNames() is correctly extracting resource names")
- }
- }
- func TestDropAlphaVolumeDevices(t *testing.T) {
- podWithVolumeDevices := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{
- {
- Name: "container1",
- Image: "testimage",
- VolumeDevices: []api.VolumeDevice{
- {
- Name: "myvolume",
- DevicePath: "/usr/test",
- },
- },
- },
- },
- InitContainers: []api.Container{
- {
- Name: "container1",
- Image: "testimage",
- VolumeDevices: []api.VolumeDevice{
- {
- Name: "myvolume",
- DevicePath: "/usr/test",
- },
- },
- },
- },
- Volumes: []api.Volume{
- {
- Name: "myvolume",
- VolumeSource: api.VolumeSource{
- HostPath: &api.HostPathVolumeSource{
- Path: "/dev/xvdc",
- },
- },
- },
- },
- },
- }
- }
- podWithoutVolumeDevices := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{
- {
- Name: "container1",
- Image: "testimage",
- },
- },
- InitContainers: []api.Container{
- {
- Name: "container1",
- Image: "testimage",
- },
- },
- Volumes: []api.Volume{
- {
- Name: "myvolume",
- VolumeSource: api.VolumeSource{
- HostPath: &api.HostPathVolumeSource{
- Path: "/dev/xvdc",
- },
- },
- },
- },
- },
- }
- }
- podInfo := []struct {
- description string
- hasVolumeDevices bool
- pod func() *api.Pod
- }{
- {
- description: "has VolumeDevices",
- hasVolumeDevices: true,
- pod: podWithVolumeDevices,
- },
- {
- description: "does not have VolumeDevices",
- hasVolumeDevices: false,
- pod: podWithoutVolumeDevices,
- },
- {
- description: "is nil",
- hasVolumeDevices: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasVolumeDevices, oldPod := oldPodInfo.hasVolumeDevices, oldPodInfo.pod()
- newPodHasVolumeDevices, newPod := newPodInfo.hasVolumeDevices, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.BlockVolume, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasVolumeDevices:
- // new pod should not be changed if the feature is enabled, or if the old pod had VolumeDevices
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasVolumeDevices:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have VolumeDevices
- if !reflect.DeepEqual(newPod, podWithoutVolumeDevices()) {
- t.Errorf("new pod had VolumeDevices: %v", diff.ObjectReflectDiff(newPod, podWithoutVolumeDevices()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropSubPath(t *testing.T) {
- podWithSubpaths := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPath: "foo"}, {Name: "a", SubPath: "foo2"}, {Name: "a", SubPath: "foo3"}}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPath: "foo"}, {Name: "a", SubPath: "foo2"}}}},
- Volumes: []api.Volume{{Name: "a", VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/dev/xvdc"}}}},
- },
- }
- }
- podWithoutSubpaths := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPath: ""}, {Name: "a", SubPath: ""}, {Name: "a", SubPath: ""}}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPath: ""}, {Name: "a", SubPath: ""}}}},
- Volumes: []api.Volume{{Name: "a", VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/dev/xvdc"}}}},
- },
- }
- }
- podInfo := []struct {
- description string
- hasSubpaths bool
- pod func() *api.Pod
- }{
- {
- description: "has subpaths",
- hasSubpaths: true,
- pod: podWithSubpaths,
- },
- {
- description: "does not have subpaths",
- hasSubpaths: false,
- pod: podWithoutSubpaths,
- },
- {
- description: "is nil",
- hasSubpaths: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasSubpaths, oldPod := oldPodInfo.hasSubpaths, oldPodInfo.pod()
- newPodHasSubpaths, newPod := newPodInfo.hasSubpaths, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.VolumeSubpath, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasSubpaths:
- // new pod should not be changed if the feature is enabled, or if the old pod had subpaths
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasSubpaths:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have subpaths
- if !reflect.DeepEqual(newPod, podWithoutSubpaths()) {
- t.Errorf("new pod had subpaths: %v", diff.ObjectReflectDiff(newPod, podWithoutSubpaths()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropRuntimeClass(t *testing.T) {
- runtimeClassName := "some_container_engine"
- podWithoutRuntimeClass := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RuntimeClassName: nil,
- },
- }
- }
- podWithRuntimeClass := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RuntimeClassName: &runtimeClassName,
- },
- }
- }
- podInfo := []struct {
- description string
- hasPodRuntimeClassName bool
- pod func() *api.Pod
- }{
- {
- description: "pod Without RuntimeClassName",
- hasPodRuntimeClassName: false,
- pod: podWithoutRuntimeClass,
- },
- {
- description: "pod With RuntimeClassName",
- hasPodRuntimeClassName: true,
- pod: podWithRuntimeClass,
- },
- {
- description: "is nil",
- hasPodRuntimeClassName: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasRuntimeClassName, oldPod := oldPodInfo.hasPodRuntimeClassName, oldPodInfo.pod()
- newPodHasRuntimeClassName, newPod := newPodInfo.hasPodRuntimeClassName, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RuntimeClass, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasRuntimeClassName:
- // new pod should not be changed if the feature is enabled, or if the old pod had RuntimeClass
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasRuntimeClassName:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have RuntimeClass
- if !reflect.DeepEqual(newPod, podWithoutRuntimeClass()) {
- t.Errorf("new pod had PodRuntimeClassName: %v", diff.ObjectReflectDiff(newPod, podWithoutRuntimeClass()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropProcMount(t *testing.T) {
- procMount := api.UnmaskedProcMount
- defaultProcMount := api.DefaultProcMount
- podWithProcMount := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: &api.SecurityContext{ProcMount: &procMount}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: &api.SecurityContext{ProcMount: &procMount}}},
- },
- }
- }
- podWithDefaultProcMount := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: &api.SecurityContext{ProcMount: &defaultProcMount}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: &api.SecurityContext{ProcMount: &defaultProcMount}}},
- },
- }
- }
- podWithoutProcMount := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: &api.SecurityContext{ProcMount: nil}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: &api.SecurityContext{ProcMount: nil}}},
- },
- }
- }
- podInfo := []struct {
- description string
- hasProcMount bool
- pod func() *api.Pod
- }{
- {
- description: "has ProcMount",
- hasProcMount: true,
- pod: podWithProcMount,
- },
- {
- description: "has default ProcMount",
- hasProcMount: false,
- pod: podWithDefaultProcMount,
- },
- {
- description: "does not have ProcMount",
- hasProcMount: false,
- pod: podWithoutProcMount,
- },
- {
- description: "is nil",
- hasProcMount: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasProcMount, oldPod := oldPodInfo.hasProcMount, oldPodInfo.pod()
- newPodHasProcMount, newPod := newPodInfo.hasProcMount, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.ProcMountType, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasProcMount:
- // new pod should not be changed if the feature is enabled, or if the old pod had ProcMount
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasProcMount:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have ProcMount
- if procMountInUse(&newPod.Spec) {
- t.Errorf("new pod had ProcMount: %#v", &newPod.Spec)
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropPodPriority(t *testing.T) {
- podPriority := int32(1000)
- podWithoutPriority := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Priority: nil,
- PriorityClassName: "",
- },
- }
- }
- podWithPriority := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Priority: &podPriority,
- PriorityClassName: "",
- },
- }
- }
- podWithPriorityClassOnly := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Priority: nil,
- PriorityClassName: "HighPriorityClass",
- },
- }
- }
- podWithBothPriorityFields := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Priority: &podPriority,
- PriorityClassName: "HighPriorityClass",
- },
- }
- }
- podInfo := []struct {
- description string
- hasPodPriority bool
- pod func() *api.Pod
- }{
- {
- description: "pod With no PodPriority fields set",
- hasPodPriority: false,
- pod: podWithoutPriority,
- },
- {
- description: "feature disabled and pod With PodPriority field set but class name not set",
- hasPodPriority: true,
- pod: podWithPriority,
- },
- {
- description: "feature disabled and pod With PodPriority ClassName field set but PortPriority not set",
- hasPodPriority: true,
- pod: podWithPriorityClassOnly,
- },
- {
- description: "feature disabled and pod With both PodPriority ClassName and PodPriority fields set",
- hasPodPriority: true,
- pod: podWithBothPriorityFields,
- },
- {
- description: "is nil",
- hasPodPriority: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasPodPriority, oldPod := oldPodInfo.hasPodPriority, oldPodInfo.pod()
- newPodHasPodPriority, newPod := newPodInfo.hasPodPriority, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodPriority, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasPodPriority:
- // new pod should not be changed if the feature is enabled, or if the old pod had PodPriority
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasPodPriority:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have PodPriority
- if !reflect.DeepEqual(newPod, podWithoutPriority()) {
- t.Errorf("new pod had PodPriority: %v", diff.ObjectReflectDiff(newPod, podWithoutPriority()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropEmptyDirSizeLimit(t *testing.T) {
- sizeLimit := resource.MustParse("1Gi")
- podWithEmptyDirSizeLimit := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Volumes: []api.Volume{
- {
- Name: "a",
- VolumeSource: api.VolumeSource{
- EmptyDir: &api.EmptyDirVolumeSource{
- Medium: "memory",
- SizeLimit: &sizeLimit,
- },
- },
- },
- },
- },
- }
- }
- podWithoutEmptyDirSizeLimit := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Volumes: []api.Volume{
- {
- Name: "a",
- VolumeSource: api.VolumeSource{
- EmptyDir: &api.EmptyDirVolumeSource{
- Medium: "memory",
- },
- },
- },
- },
- },
- }
- }
- podInfo := []struct {
- description string
- hasEmptyDirSizeLimit bool
- pod func() *api.Pod
- }{
- {
- description: "has EmptyDir Size Limit",
- hasEmptyDirSizeLimit: true,
- pod: podWithEmptyDirSizeLimit,
- },
- {
- description: "does not have EmptyDir Size Limit",
- hasEmptyDirSizeLimit: false,
- pod: podWithoutEmptyDirSizeLimit,
- },
- {
- description: "is nil",
- hasEmptyDirSizeLimit: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasEmptyDirSizeLimit, oldPod := oldPodInfo.hasEmptyDirSizeLimit, oldPodInfo.pod()
- newPodHasEmptyDirSizeLimit, newPod := newPodInfo.hasEmptyDirSizeLimit, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.LocalStorageCapacityIsolation, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasEmptyDirSizeLimit:
- // new pod should not be changed if the feature is enabled, or if the old pod had EmptyDir SizeLimit
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasEmptyDirSizeLimit:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have EmptyDir SizeLimit
- if !reflect.DeepEqual(newPod, podWithoutEmptyDirSizeLimit()) {
- t.Errorf("new pod had EmptyDir SizeLimit: %v", diff.ObjectReflectDiff(newPod, podWithoutEmptyDirSizeLimit()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropPodShareProcessNamespace(t *testing.T) {
- podWithShareProcessNamespace := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- SecurityContext: &api.PodSecurityContext{
- ShareProcessNamespace: &[]bool{true}[0],
- },
- },
- }
- }
- podWithoutShareProcessNamespace := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- SecurityContext: &api.PodSecurityContext{},
- },
- }
- }
- podWithoutSecurityContext := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{},
- }
- }
- podInfo := []struct {
- description string
- hasShareProcessNamespace bool
- pod func() *api.Pod
- }{
- {
- description: "has ShareProcessNamespace",
- hasShareProcessNamespace: true,
- pod: podWithShareProcessNamespace,
- },
- {
- description: "does not have ShareProcessNamespace",
- hasShareProcessNamespace: false,
- pod: podWithoutShareProcessNamespace,
- },
- {
- description: "does not have SecurityContext",
- hasShareProcessNamespace: false,
- pod: podWithoutSecurityContext,
- },
- {
- description: "is nil",
- hasShareProcessNamespace: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasShareProcessNamespace, oldPod := oldPodInfo.hasShareProcessNamespace, oldPodInfo.pod()
- newPodHasShareProcessNamespace, newPod := newPodInfo.hasShareProcessNamespace, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.PodShareProcessNamespace, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasShareProcessNamespace:
- // new pod should not be changed if the feature is enabled, or if the old pod had ShareProcessNamespace set
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasShareProcessNamespace:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have ShareProcessNamespace
- if !reflect.DeepEqual(newPod, podWithoutShareProcessNamespace()) {
- t.Errorf("new pod had ShareProcessNamespace: %v", diff.ObjectReflectDiff(newPod, podWithoutShareProcessNamespace()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropAppArmor(t *testing.T) {
- podWithAppArmor := func() *api.Pod {
- return &api.Pod{
- ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"a": "1", apparmor.ContainerAnnotationKeyPrefix + "foo": "default"}},
- Spec: api.PodSpec{},
- }
- }
- podWithoutAppArmor := func() *api.Pod {
- return &api.Pod{
- ObjectMeta: metav1.ObjectMeta{Annotations: map[string]string{"a": "1"}},
- Spec: api.PodSpec{},
- }
- }
- podInfo := []struct {
- description string
- hasAppArmor bool
- pod func() *api.Pod
- }{
- {
- description: "has AppArmor",
- hasAppArmor: true,
- pod: podWithAppArmor,
- },
- {
- description: "does not have AppArmor",
- hasAppArmor: false,
- pod: podWithoutAppArmor,
- },
- {
- description: "is nil",
- hasAppArmor: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasAppArmor, oldPod := oldPodInfo.hasAppArmor, oldPodInfo.pod()
- newPodHasAppArmor, newPod := newPodInfo.hasAppArmor, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.AppArmor, enabled)()
- DropDisabledPodFields(newPod, oldPod)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasAppArmor:
- // new pod should not be changed if the feature is enabled, or if the old pod had AppArmor
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasAppArmor:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have AppArmor
- if !reflect.DeepEqual(newPod, podWithoutAppArmor()) {
- t.Errorf("new pod had EmptyDir SizeLimit: %v", diff.ObjectReflectDiff(newPod, podWithoutAppArmor()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropTokenRequestProjection(t *testing.T) {
- podWithoutTRProjection := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Volumes: []api.Volume{{
- VolumeSource: api.VolumeSource{
- Projected: &api.ProjectedVolumeSource{
- Sources: []api.VolumeProjection{{
- ServiceAccountToken: nil,
- }},
- }}},
- },
- },
- }
- }
- podWithoutProjectedVolumeSource := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Volumes: []api.Volume{
- {VolumeSource: api.VolumeSource{
- ConfigMap: &api.ConfigMapVolumeSource{},
- }},
- },
- },
- }
- }
- podWithTRProjection := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- Volumes: []api.Volume{{
- VolumeSource: api.VolumeSource{
- Projected: &api.ProjectedVolumeSource{
- Sources: []api.VolumeProjection{{
- ServiceAccountToken: &api.ServiceAccountTokenProjection{
- Audience: "api",
- ExpirationSeconds: 3600,
- Path: "token",
- }},
- }},
- },
- },
- },
- }}
- }
- podInfo := []struct {
- description string
- hasTRProjection bool
- pod func() *api.Pod
- }{
- {
- description: "has TokenRequestProjection",
- hasTRProjection: true,
- pod: podWithTRProjection,
- },
- {
- description: "does not have TokenRequestProjection",
- hasTRProjection: false,
- pod: podWithoutTRProjection,
- },
- {
- description: "does not have ProjectedVolumeSource",
- hasTRProjection: false,
- pod: podWithoutProjectedVolumeSource,
- },
- {
- description: "is nil",
- hasTRProjection: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodhasTRProjection, oldPod := oldPodInfo.hasTRProjection, oldPodInfo.pod()
- newPodhasTRProjection, newPod := newPodInfo.hasTRProjection, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.TokenRequestProjection, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodhasTRProjection:
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodhasTRProjection:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("%v", oldPod)
- t.Errorf("%v", newPod)
- t.Errorf("new pod was not changed")
- }
- if !reflect.DeepEqual(newPod, podWithoutTRProjection()) {
- t.Errorf("new pod had Tokenrequestprojection: %v", diff.ObjectReflectDiff(newPod, podWithoutTRProjection()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropRunAsGroup(t *testing.T) {
- group := func() *int64 {
- testGroup := int64(1000)
- return &testGroup
- }
- defaultProcMount := api.DefaultProcMount
- defaultSecurityContext := func() *api.SecurityContext {
- return &api.SecurityContext{ProcMount: &defaultProcMount}
- }
- securityContextWithRunAsGroup := func() *api.SecurityContext {
- return &api.SecurityContext{ProcMount: &defaultProcMount, RunAsGroup: group()}
- }
- podWithoutRunAsGroup := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- SecurityContext: &api.PodSecurityContext{},
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: defaultSecurityContext()}},
- InitContainers: []api.Container{{Name: "initContainer1", Image: "testimage", SecurityContext: defaultSecurityContext()}},
- },
- }
- }
- podWithRunAsGroupInPod := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- SecurityContext: &api.PodSecurityContext{RunAsGroup: group()},
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: defaultSecurityContext()}},
- InitContainers: []api.Container{{Name: "initContainer1", Image: "testimage", SecurityContext: defaultSecurityContext()}},
- },
- }
- }
- podWithRunAsGroupInContainers := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- SecurityContext: &api.PodSecurityContext{},
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: securityContextWithRunAsGroup()}},
- InitContainers: []api.Container{{Name: "initContainer1", Image: "testimage", SecurityContext: defaultSecurityContext()}},
- },
- }
- }
- podWithRunAsGroupInInitContainers := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- SecurityContext: &api.PodSecurityContext{},
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: defaultSecurityContext()}},
- InitContainers: []api.Container{{Name: "initContainer1", Image: "testimage", SecurityContext: securityContextWithRunAsGroup()}},
- },
- }
- }
- podInfo := []struct {
- description string
- hasRunAsGroup bool
- pod func() *api.Pod
- }{
- {
- description: "have RunAsGroup in Pod",
- hasRunAsGroup: true,
- pod: podWithRunAsGroupInPod,
- },
- {
- description: "have RunAsGroup in Container",
- hasRunAsGroup: true,
- pod: podWithRunAsGroupInContainers,
- },
- {
- description: "have RunAsGroup in InitContainer",
- hasRunAsGroup: true,
- pod: podWithRunAsGroupInInitContainers,
- },
- {
- description: "does not have RunAsGroup",
- hasRunAsGroup: false,
- pod: podWithoutRunAsGroup,
- },
- {
- description: "is nil",
- hasRunAsGroup: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasRunAsGroup, oldPod := oldPodInfo.hasRunAsGroup, oldPodInfo.pod()
- newPodHasRunAsGroup, newPod := newPodInfo.hasRunAsGroup, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.RunAsGroup, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasRunAsGroup:
- // new pod should not be changed if the feature is enabled, or if the old pod had RunAsGroup
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasRunAsGroup:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("%v", oldPod)
- t.Errorf("%v", newPod)
- t.Errorf("new pod was not changed")
- }
- // new pod should not have RunAsGroup
- if !reflect.DeepEqual(newPod, podWithoutRunAsGroup()) {
- t.Errorf("new pod had RunAsGroup: %v", diff.ObjectReflectDiff(newPod, podWithoutRunAsGroup()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropGMSAFields(t *testing.T) {
- defaultContainerSecurityContextFactory := func() *api.SecurityContext {
- defaultProcMount := api.DefaultProcMount
- return &api.SecurityContext{ProcMount: &defaultProcMount}
- }
- podWithoutWindowsOptionsFactory := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- SecurityContext: &api.PodSecurityContext{},
- Containers: []api.Container{{Name: "container1", Image: "testimage", SecurityContext: defaultContainerSecurityContextFactory()}},
- InitContainers: []api.Container{{Name: "initContainer1", Image: "testimage", SecurityContext: defaultContainerSecurityContextFactory()}},
- },
- }
- }
- type podFactoryInfo struct {
- description string
- hasGMSAField bool
- // this factory should generate the input pod whose spec will be fed to dropDisabledFields
- podFactory func() *api.Pod
- // this factory should generate the expected pod after the GMSA fields have been dropped
- // we can't just use podWithoutWindowsOptionsFactory as is for this, since in some cases
- // we'll be left with a WindowsSecurityContextOptions struct with no GMSA field set, as opposed
- // to a nil pointer in the pod generated by podWithoutWindowsOptionsFactory
- // if this field is not set, it will default to the podFactory
- strippedPodFactory func() *api.Pod
- }
- podFactoryInfos := []podFactoryInfo{
- {
- description: "does not have any GMSA field set",
- hasGMSAField: false,
- podFactory: podWithoutWindowsOptionsFactory,
- },
- {
- description: "has a pod-level WindowsSecurityContextOptions struct with no GMSA field set",
- hasGMSAField: false,
- podFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.SecurityContext.WindowsOptions = &api.WindowsSecurityContextOptions{}
- return pod
- },
- },
- {
- description: "has a WindowsSecurityContextOptions struct with no GMSA field set on a container",
- hasGMSAField: false,
- podFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.Containers[0].SecurityContext.WindowsOptions = &api.WindowsSecurityContextOptions{}
- return pod
- },
- },
- {
- description: "has a WindowsSecurityContextOptions struct with no GMSA field set on an init container",
- hasGMSAField: false,
- podFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.InitContainers[0].SecurityContext.WindowsOptions = &api.WindowsSecurityContextOptions{}
- return pod
- },
- },
- {
- description: "is nil",
- hasGMSAField: false,
- podFactory: func() *api.Pod { return nil },
- },
- }
- toPtr := func(s string) *string {
- return &s
- }
- addGMSACredentialSpecName := func(windowsOptions *api.WindowsSecurityContextOptions) {
- windowsOptions.GMSACredentialSpecName = toPtr("dummy-gmsa-cred-spec-name")
- }
- addGMSACredentialSpec := func(windowsOptions *api.WindowsSecurityContextOptions) {
- windowsOptions.GMSACredentialSpec = toPtr("dummy-gmsa-cred-spec-contents")
- }
- addBothGMSAFields := func(windowsOptions *api.WindowsSecurityContextOptions) {
- addGMSACredentialSpecName(windowsOptions)
- addGMSACredentialSpec(windowsOptions)
- }
- for fieldName, windowsOptionsTransformingFunc := range map[string]func(*api.WindowsSecurityContextOptions){
- "GMSACredentialSpecName field": addGMSACredentialSpecName,
- "GMSACredentialSpec field": addGMSACredentialSpec,
- "both GMSA fields": addBothGMSAFields,
- } {
- // yes, these variables are indeed needed for the closure to work
- // properly, please do NOT remove them
- name := fieldName
- transformingFunc := windowsOptionsTransformingFunc
- windowsOptionsWithGMSAFieldFactory := func() *api.WindowsSecurityContextOptions {
- windowsOptions := &api.WindowsSecurityContextOptions{}
- transformingFunc(windowsOptions)
- return windowsOptions
- }
- podFactoryInfos = append(podFactoryInfos,
- podFactoryInfo{
- description: fmt.Sprintf("has %s in Pod", name),
- hasGMSAField: true,
- podFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.SecurityContext.WindowsOptions = windowsOptionsWithGMSAFieldFactory()
- return pod
- },
- strippedPodFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.SecurityContext.WindowsOptions = &api.WindowsSecurityContextOptions{}
- return pod
- },
- },
- podFactoryInfo{
- description: fmt.Sprintf("has %s in Container", name),
- hasGMSAField: true,
- podFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.Containers[0].SecurityContext.WindowsOptions = windowsOptionsWithGMSAFieldFactory()
- return pod
- },
- strippedPodFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.Containers[0].SecurityContext.WindowsOptions = &api.WindowsSecurityContextOptions{}
- return pod
- },
- },
- podFactoryInfo{
- description: fmt.Sprintf("has %s in InitContainer", name),
- hasGMSAField: true,
- podFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.InitContainers[0].SecurityContext.WindowsOptions = windowsOptionsWithGMSAFieldFactory()
- return pod
- },
- strippedPodFactory: func() *api.Pod {
- pod := podWithoutWindowsOptionsFactory()
- pod.Spec.InitContainers[0].SecurityContext.WindowsOptions = &api.WindowsSecurityContextOptions{}
- return pod
- },
- })
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodFactoryInfo := range podFactoryInfos {
- for _, newPodFactoryInfo := range podFactoryInfos {
- newPodHasGMSAField, newPod := newPodFactoryInfo.hasGMSAField, newPodFactoryInfo.podFactory()
- if newPod == nil {
- continue
- }
- oldPodHasGMSAField, oldPod := oldPodFactoryInfo.hasGMSAField, oldPodFactoryInfo.podFactory()
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %s, new pod %s", enabled, oldPodFactoryInfo.description, newPodFactoryInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.WindowsGMSA, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodFactoryInfo.podFactory()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodFactoryInfo.podFactory()))
- }
- switch {
- case enabled || oldPodHasGMSAField:
- // new pod should not be changed if the feature is enabled, or if the old pod had any GMSA field set
- if !reflect.DeepEqual(newPod, newPodFactoryInfo.podFactory()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodFactoryInfo.podFactory()))
- }
- case newPodHasGMSAField:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodFactoryInfo.podFactory()) {
- t.Errorf("%v", oldPod)
- t.Errorf("%v", newPod)
- t.Errorf("new pod was not changed")
- }
- // new pod should not have any GMSA field set
- var expectedStrippedPod *api.Pod
- if newPodFactoryInfo.strippedPodFactory == nil {
- expectedStrippedPod = newPodFactoryInfo.podFactory()
- } else {
- expectedStrippedPod = newPodFactoryInfo.strippedPodFactory()
- }
- if !reflect.DeepEqual(newPod, expectedStrippedPod) {
- t.Errorf("new pod had some GMSA field set: %v", diff.ObjectReflectDiff(newPod, expectedStrippedPod))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodFactoryInfo.podFactory()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodFactoryInfo.podFactory()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropPodSysctls(t *testing.T) {
- podWithSysctls := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- SecurityContext: &api.PodSecurityContext{
- Sysctls: []api.Sysctl{{Name: "test", Value: "value"}},
- },
- },
- }
- }
- podWithoutSysctls := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- SecurityContext: &api.PodSecurityContext{},
- },
- }
- }
- podWithoutSecurityContext := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{},
- }
- }
- podInfo := []struct {
- description string
- hasSysctls bool
- pod func() *api.Pod
- }{
- {
- description: "has Sysctls",
- hasSysctls: true,
- pod: podWithSysctls,
- },
- {
- description: "does not have Sysctls",
- hasSysctls: false,
- pod: podWithoutSysctls,
- },
- {
- description: "does not have SecurityContext",
- hasSysctls: false,
- pod: podWithoutSecurityContext,
- },
- {
- description: "is nil",
- hasSysctls: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasSysctls, oldPod := oldPodInfo.hasSysctls, oldPodInfo.pod()
- newPodHasSysctls, newPod := newPodInfo.hasSysctls, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.Sysctls, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasSysctls:
- // new pod should not be changed if the feature is enabled, or if the old pod had Sysctls set
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasSysctls:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have Sysctls
- if !reflect.DeepEqual(newPod, podWithoutSysctls()) {
- t.Errorf("new pod had Sysctls: %v", diff.ObjectReflectDiff(newPod, podWithoutSysctls()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
- func TestDropSubPathExpr(t *testing.T) {
- podWithSubpaths := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPathExpr: "foo"}, {Name: "a", SubPathExpr: "foo2"}, {Name: "a", SubPathExpr: "foo3"}}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPathExpr: "foo"}, {Name: "a", SubPathExpr: "foo2"}}}},
- Volumes: []api.Volume{{Name: "a", VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/dev/xvdc"}}}},
- },
- }
- }
- podWithoutSubpaths := func() *api.Pod {
- return &api.Pod{
- Spec: api.PodSpec{
- RestartPolicy: api.RestartPolicyNever,
- Containers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPathExpr: ""}, {Name: "a", SubPathExpr: ""}, {Name: "a", SubPathExpr: ""}}}},
- InitContainers: []api.Container{{Name: "container1", Image: "testimage", VolumeMounts: []api.VolumeMount{{Name: "a", SubPathExpr: ""}, {Name: "a", SubPathExpr: ""}}}},
- Volumes: []api.Volume{{Name: "a", VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/dev/xvdc"}}}},
- },
- }
- }
- podInfo := []struct {
- description string
- hasSubpaths bool
- pod func() *api.Pod
- }{
- {
- description: "has subpaths",
- hasSubpaths: true,
- pod: podWithSubpaths,
- },
- {
- description: "does not have subpaths",
- hasSubpaths: false,
- pod: podWithoutSubpaths,
- },
- {
- description: "is nil",
- hasSubpaths: false,
- pod: func() *api.Pod { return nil },
- },
- }
- for _, enabled := range []bool{true, false} {
- for _, oldPodInfo := range podInfo {
- for _, newPodInfo := range podInfo {
- oldPodHasSubpaths, oldPod := oldPodInfo.hasSubpaths, oldPodInfo.pod()
- newPodHasSubpaths, newPod := newPodInfo.hasSubpaths, newPodInfo.pod()
- if newPod == nil {
- continue
- }
- t.Run(fmt.Sprintf("feature enabled=%v, old pod %v, new pod %v", enabled, oldPodInfo.description, newPodInfo.description), func(t *testing.T) {
- defer featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.VolumeSubpathEnvExpansion, enabled)()
- var oldPodSpec *api.PodSpec
- if oldPod != nil {
- oldPodSpec = &oldPod.Spec
- }
- dropDisabledFields(&newPod.Spec, nil, oldPodSpec, nil)
- // old pod should never be changed
- if !reflect.DeepEqual(oldPod, oldPodInfo.pod()) {
- t.Errorf("old pod changed: %v", diff.ObjectReflectDiff(oldPod, oldPodInfo.pod()))
- }
- switch {
- case enabled || oldPodHasSubpaths:
- // new pod should not be changed if the feature is enabled, or if the old pod had subpaths
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- case newPodHasSubpaths:
- // new pod should be changed
- if reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod was not changed")
- }
- // new pod should not have subpaths
- if !reflect.DeepEqual(newPod, podWithoutSubpaths()) {
- t.Errorf("new pod had subpaths: %v", diff.ObjectReflectDiff(newPod, podWithoutSubpaths()))
- }
- default:
- // new pod should not need to be changed
- if !reflect.DeepEqual(newPod, newPodInfo.pod()) {
- t.Errorf("new pod changed: %v", diff.ObjectReflectDiff(newPod, newPodInfo.pod()))
- }
- }
- })
- }
- }
- }
- }
|