Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ignored mirror pods in PodPreset admission plugin #45958

Merged
merged 1 commit into from
May 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions plugin/pkg/admission/podpreset/admission.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ func (c *podPresetPlugin) Admit(a admission.Attributes) error {
if !ok {
return errors.NewBadRequest("Resource was marked with kind Pod but was unable to be converted")
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the logic looks good

if _, isMirrorPod := pod.Annotations[api.MirrorPodAnnotationKey]; isMirrorPod {
return nil
}

list, err := c.lister.PodPresets(pod.GetNamespace()).List(labels.Everything())
if err != nil {
return fmt.Errorf("listing pod presets failed: %v", err)
Expand Down
67 changes: 67 additions & 0 deletions plugin/pkg/admission/podpreset/admission_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,73 @@ func TestAdmit(t *testing.T) {
}
}

func TestAdmitMirrorPod(t *testing.T) {
containerName := "container"

mirrorPod := &api.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "mypod",
Namespace: "namespace",
Labels: map[string]string{
"security": "S2",
},
Annotations: map[string]string{api.MirrorPodAnnotationKey: "mirror"},
},
Spec: api.PodSpec{
Containers: []api.Container{
{
Name: containerName,
},
},
},
}

pip := &settings.PodPreset{
ObjectMeta: v1.ObjectMeta{
Name: "hello",
Namespace: "namespace",
},
Spec: settings.PodPresetSpec{
Selector: v1.LabelSelector{
MatchExpressions: []v1.LabelSelectorRequirement{
{
Key: "security",
Operator: v1.LabelSelectorOpIn,
Values: []string{"S2"},
},
},
},
Volumes: []api.Volume{{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}},
Env: []api.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
EnvFrom: []api.EnvFromSource{
{
ConfigMapRef: &api.ConfigMapEnvSource{
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
},
},
{
Prefix: "pre_",
ConfigMapRef: &api.ConfigMapEnvSource{
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
},
},
},
},
}

if err := admitPod(mirrorPod, pip); err != nil {
t.Fatal(err)
}

container := mirrorPod.Spec.Containers[0]
if len(mirrorPod.Spec.Volumes) != 0 ||
len(container.VolumeMounts) != 0 ||
len(container.Env) != 0 ||
len(container.EnvFrom) != 0 {
t.Fatalf("mirror pod is updated by PodPreset admission:\n\tVolumes got %d, expected 0\n\tVolumeMounts go %d, expected 0\n\tEnv got, %d expected 0\n\tEnvFrom got %d, expected 0", len(mirrorPod.Spec.Volumes), len(container.VolumeMounts), len(container.Env), len(container.EnvFrom))
}
}

func admitPod(pod *api.Pod, pip *settings.PodPreset) error {
informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc())
store := informerFactory.Settings().InternalVersion().PodPresets().Informer().GetStore()
Expand Down