mirror of https://github.com/k3s-io/k3s
Merge pull request #67810 from yue9944882/refactor/externalize-podpreset
Propagate externalization to podpreset admission controllerpull/58/head
commit
2042125a51
|
@ -12,15 +12,16 @@ go_test(
|
||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/apis/core:go_default_library",
|
"//pkg/apis/core:go_default_library",
|
||||||
"//pkg/apis/settings:go_default_library",
|
|
||||||
"//pkg/client/clientset_generated/internalclientset/fake:go_default_library",
|
|
||||||
"//pkg/client/informers/informers_generated/internalversion:go_default_library",
|
|
||||||
"//pkg/client/listers/settings/internalversion:go_default_library",
|
|
||||||
"//pkg/controller:go_default_library",
|
"//pkg/controller:go_default_library",
|
||||||
|
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||||
|
"//staging/src/k8s.io/api/settings/v1alpha1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||||
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/authentication/user:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/kubernetes/fake:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/listers/settings/v1alpha1:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -30,16 +31,17 @@ go_library(
|
||||||
importpath = "k8s.io/kubernetes/plugin/pkg/admission/podpreset",
|
importpath = "k8s.io/kubernetes/plugin/pkg/admission/podpreset",
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/apis/core:go_default_library",
|
"//pkg/apis/core:go_default_library",
|
||||||
"//pkg/apis/settings:go_default_library",
|
"//pkg/apis/core/v1:go_default_library",
|
||||||
"//pkg/client/clientset_generated/internalclientset:go_default_library",
|
"//staging/src/k8s.io/api/settings/v1alpha1:go_default_library",
|
||||||
"//pkg/client/informers/informers_generated/internalversion:go_default_library",
|
|
||||||
"//pkg/client/listers/settings/internalversion:go_default_library",
|
|
||||||
"//pkg/kubeapiserver/admission:go_default_library",
|
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/api/errors:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/labels:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/errors:go_default_library",
|
||||||
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
"//staging/src/k8s.io/apiserver/pkg/admission:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apiserver/pkg/admission/initializer:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/informers:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/kubernetes:go_default_library",
|
||||||
|
"//staging/src/k8s.io/client-go/listers/settings/v1alpha1:go_default_library",
|
||||||
"//vendor/github.com/golang/glog:go_default_library",
|
"//vendor/github.com/golang/glog:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
|
@ -24,17 +24,18 @@ import (
|
||||||
|
|
||||||
"github.com/golang/glog"
|
"github.com/golang/glog"
|
||||||
|
|
||||||
|
settingsv1alpha1 "k8s.io/api/settings/v1alpha1"
|
||||||
"k8s.io/apimachinery/pkg/api/errors"
|
"k8s.io/apimachinery/pkg/api/errors"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/labels"
|
"k8s.io/apimachinery/pkg/labels"
|
||||||
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
utilerrors "k8s.io/apimachinery/pkg/util/errors"
|
||||||
"k8s.io/apiserver/pkg/admission"
|
"k8s.io/apiserver/pkg/admission"
|
||||||
|
genericadmissioninitializer "k8s.io/apiserver/pkg/admission/initializer"
|
||||||
|
"k8s.io/client-go/informers"
|
||||||
|
"k8s.io/client-go/kubernetes"
|
||||||
|
settingsv1alpha1listers "k8s.io/client-go/listers/settings/v1alpha1"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/apis/settings"
|
apiscorev1 "k8s.io/kubernetes/pkg/apis/core/v1"
|
||||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset"
|
|
||||||
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
|
|
||||||
settingslisters "k8s.io/kubernetes/pkg/client/listers/settings/internalversion"
|
|
||||||
kubeapiserveradmission "k8s.io/kubernetes/pkg/kubeapiserver/admission"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -52,14 +53,14 @@ func Register(plugins *admission.Plugins) {
|
||||||
// podPresetPlugin is an implementation of admission.Interface.
|
// podPresetPlugin is an implementation of admission.Interface.
|
||||||
type podPresetPlugin struct {
|
type podPresetPlugin struct {
|
||||||
*admission.Handler
|
*admission.Handler
|
||||||
client internalclientset.Interface
|
client kubernetes.Interface
|
||||||
|
|
||||||
lister settingslisters.PodPresetLister
|
lister settingsv1alpha1listers.PodPresetLister
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ admission.MutationInterface = &podPresetPlugin{}
|
var _ admission.MutationInterface = &podPresetPlugin{}
|
||||||
var _ = kubeapiserveradmission.WantsInternalKubeInformerFactory(&podPresetPlugin{})
|
var _ = genericadmissioninitializer.WantsExternalKubeInformerFactory(&podPresetPlugin{})
|
||||||
var _ = kubeapiserveradmission.WantsInternalKubeClientSet(&podPresetPlugin{})
|
var _ = genericadmissioninitializer.WantsExternalKubeClientSet(&podPresetPlugin{})
|
||||||
|
|
||||||
// NewPlugin creates a new pod preset admission plugin.
|
// NewPlugin creates a new pod preset admission plugin.
|
||||||
func NewPlugin() *podPresetPlugin {
|
func NewPlugin() *podPresetPlugin {
|
||||||
|
@ -78,12 +79,12 @@ func (plugin *podPresetPlugin) ValidateInitialization() error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *podPresetPlugin) SetInternalKubeClientSet(client internalclientset.Interface) {
|
func (a *podPresetPlugin) SetExternalKubeClientSet(client kubernetes.Interface) {
|
||||||
a.client = client
|
a.client = client
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *podPresetPlugin) SetInternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
func (a *podPresetPlugin) SetExternalKubeInformerFactory(f informers.SharedInformerFactory) {
|
||||||
podPresetInformer := f.Settings().InternalVersion().PodPresets()
|
podPresetInformer := f.Settings().V1alpha1().PodPresets()
|
||||||
a.lister = podPresetInformer.Lister()
|
a.lister = podPresetInformer.Lister()
|
||||||
a.SetReadyFunc(podPresetInformer.Informer().HasSynced)
|
a.SetReadyFunc(podPresetInformer.Informer().HasSynced)
|
||||||
}
|
}
|
||||||
|
@ -149,8 +150,8 @@ func (c *podPresetPlugin) Admit(a admission.Attributes) error {
|
||||||
}
|
}
|
||||||
|
|
||||||
// filterPodPresets returns list of PodPresets which match given Pod.
|
// filterPodPresets returns list of PodPresets which match given Pod.
|
||||||
func filterPodPresets(list []*settings.PodPreset, pod *api.Pod) ([]*settings.PodPreset, error) {
|
func filterPodPresets(list []*settingsv1alpha1.PodPreset, pod *api.Pod) ([]*settingsv1alpha1.PodPreset, error) {
|
||||||
var matchingPPs []*settings.PodPreset
|
var matchingPPs []*settingsv1alpha1.PodPreset
|
||||||
|
|
||||||
for _, pp := range list {
|
for _, pp := range list {
|
||||||
selector, err := metav1.LabelSelectorAsSelector(&pp.Spec.Selector)
|
selector, err := metav1.LabelSelectorAsSelector(&pp.Spec.Selector)
|
||||||
|
@ -170,7 +171,7 @@ func filterPodPresets(list []*settings.PodPreset, pod *api.Pod) ([]*settings.Pod
|
||||||
|
|
||||||
// safeToApplyPodPresetsOnPod determines if there is any conflict in information
|
// safeToApplyPodPresetsOnPod determines if there is any conflict in information
|
||||||
// injected by given PodPresets in the Pod.
|
// injected by given PodPresets in the Pod.
|
||||||
func safeToApplyPodPresetsOnPod(pod *api.Pod, podPresets []*settings.PodPreset) error {
|
func safeToApplyPodPresetsOnPod(pod *api.Pod, podPresets []*settingsv1alpha1.PodPreset) error {
|
||||||
var errs []error
|
var errs []error
|
||||||
|
|
||||||
// volumes attribute is defined at the Pod level, so determine if volumes
|
// volumes attribute is defined at the Pod level, so determine if volumes
|
||||||
|
@ -188,7 +189,7 @@ func safeToApplyPodPresetsOnPod(pod *api.Pod, podPresets []*settings.PodPreset)
|
||||||
|
|
||||||
// safeToApplyPodPresetsOnContainer determines if there is any conflict in
|
// safeToApplyPodPresetsOnContainer determines if there is any conflict in
|
||||||
// information injected by given PodPresets in the given container.
|
// information injected by given PodPresets in the given container.
|
||||||
func safeToApplyPodPresetsOnContainer(ctr *api.Container, podPresets []*settings.PodPreset) error {
|
func safeToApplyPodPresetsOnContainer(ctr *api.Container, podPresets []*settingsv1alpha1.PodPreset) error {
|
||||||
var errs []error
|
var errs []error
|
||||||
// check if it is safe to merge env vars and volume mounts from given podpresets and
|
// check if it is safe to merge env vars and volume mounts from given podpresets and
|
||||||
// container's existing env vars.
|
// container's existing env vars.
|
||||||
|
@ -204,7 +205,7 @@ func safeToApplyPodPresetsOnContainer(ctr *api.Container, podPresets []*settings
|
||||||
|
|
||||||
// mergeEnv merges a list of env vars with the env vars injected by given list podPresets.
|
// mergeEnv merges a list of env vars with the env vars injected by given list podPresets.
|
||||||
// It returns an error if it detects any conflict during the merge.
|
// It returns an error if it detects any conflict during the merge.
|
||||||
func mergeEnv(envVars []api.EnvVar, podPresets []*settings.PodPreset) ([]api.EnvVar, error) {
|
func mergeEnv(envVars []api.EnvVar, podPresets []*settingsv1alpha1.PodPreset) ([]api.EnvVar, error) {
|
||||||
origEnv := map[string]api.EnvVar{}
|
origEnv := map[string]api.EnvVar{}
|
||||||
for _, v := range envVars {
|
for _, v := range envVars {
|
||||||
origEnv[v.Name] = v
|
origEnv[v.Name] = v
|
||||||
|
@ -217,16 +218,21 @@ func mergeEnv(envVars []api.EnvVar, podPresets []*settings.PodPreset) ([]api.Env
|
||||||
|
|
||||||
for _, pp := range podPresets {
|
for _, pp := range podPresets {
|
||||||
for _, v := range pp.Spec.Env {
|
for _, v := range pp.Spec.Env {
|
||||||
|
internalEnv := api.EnvVar{}
|
||||||
|
if err := apiscorev1.Convert_v1_EnvVar_To_core_EnvVar(&v, &internalEnv, nil); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
found, ok := origEnv[v.Name]
|
found, ok := origEnv[v.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// if we don't already have it append it and continue
|
// if we don't already have it append it and continue
|
||||||
origEnv[v.Name] = v
|
origEnv[v.Name] = internalEnv
|
||||||
mergedEnv = append(mergedEnv, v)
|
mergedEnv = append(mergedEnv, internalEnv)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure they are identical or throw an error
|
// make sure they are identical or throw an error
|
||||||
if !reflect.DeepEqual(found, v) {
|
if !reflect.DeepEqual(found, internalEnv) {
|
||||||
errs = append(errs, fmt.Errorf("merging env for %s has a conflict on %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.Name, v, found))
|
errs = append(errs, fmt.Errorf("merging env for %s has a conflict on %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.Name, v, found))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -240,12 +246,19 @@ func mergeEnv(envVars []api.EnvVar, podPresets []*settings.PodPreset) ([]api.Env
|
||||||
return mergedEnv, err
|
return mergedEnv, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func mergeEnvFrom(envSources []api.EnvFromSource, podPresets []*settings.PodPreset) ([]api.EnvFromSource, error) {
|
func mergeEnvFrom(envSources []api.EnvFromSource, podPresets []*settingsv1alpha1.PodPreset) ([]api.EnvFromSource, error) {
|
||||||
var mergedEnvFrom []api.EnvFromSource
|
var mergedEnvFrom []api.EnvFromSource
|
||||||
|
|
||||||
mergedEnvFrom = append(mergedEnvFrom, envSources...)
|
mergedEnvFrom = append(mergedEnvFrom, envSources...)
|
||||||
for _, pp := range podPresets {
|
for _, pp := range podPresets {
|
||||||
mergedEnvFrom = append(mergedEnvFrom, pp.Spec.EnvFrom...)
|
for _, envFromSource := range pp.Spec.EnvFrom {
|
||||||
|
internalEnvFrom := api.EnvFromSource{}
|
||||||
|
if err := apiscorev1.Convert_v1_EnvFromSource_To_core_EnvFromSource(&envFromSource, &internalEnvFrom, nil); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
mergedEnvFrom = append(mergedEnvFrom, internalEnvFrom)
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return mergedEnvFrom, nil
|
return mergedEnvFrom, nil
|
||||||
|
@ -253,7 +266,7 @@ func mergeEnvFrom(envSources []api.EnvFromSource, podPresets []*settings.PodPres
|
||||||
|
|
||||||
// mergeVolumeMounts merges given list of VolumeMounts with the volumeMounts
|
// mergeVolumeMounts merges given list of VolumeMounts with the volumeMounts
|
||||||
// injected by given podPresets. It returns an error if it detects any conflict during the merge.
|
// injected by given podPresets. It returns an error if it detects any conflict during the merge.
|
||||||
func mergeVolumeMounts(volumeMounts []api.VolumeMount, podPresets []*settings.PodPreset) ([]api.VolumeMount, error) {
|
func mergeVolumeMounts(volumeMounts []api.VolumeMount, podPresets []*settingsv1alpha1.PodPreset) ([]api.VolumeMount, error) {
|
||||||
|
|
||||||
origVolumeMounts := map[string]api.VolumeMount{}
|
origVolumeMounts := map[string]api.VolumeMount{}
|
||||||
volumeMountsByPath := map[string]api.VolumeMount{}
|
volumeMountsByPath := map[string]api.VolumeMount{}
|
||||||
|
@ -269,15 +282,19 @@ func mergeVolumeMounts(volumeMounts []api.VolumeMount, podPresets []*settings.Po
|
||||||
|
|
||||||
for _, pp := range podPresets {
|
for _, pp := range podPresets {
|
||||||
for _, v := range pp.Spec.VolumeMounts {
|
for _, v := range pp.Spec.VolumeMounts {
|
||||||
|
internalVolumeMount := api.VolumeMount{}
|
||||||
|
if err := apiscorev1.Convert_v1_VolumeMount_To_core_VolumeMount(&v, &internalVolumeMount, nil); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
found, ok := origVolumeMounts[v.Name]
|
found, ok := origVolumeMounts[v.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// if we don't already have it append it and continue
|
// if we don't already have it append it and continue
|
||||||
origVolumeMounts[v.Name] = v
|
origVolumeMounts[v.Name] = internalVolumeMount
|
||||||
mergedVolumeMounts = append(mergedVolumeMounts, v)
|
mergedVolumeMounts = append(mergedVolumeMounts, internalVolumeMount)
|
||||||
} else {
|
} else {
|
||||||
// make sure they are identical or throw an error
|
// make sure they are identical or throw an error
|
||||||
// shall we throw an error for identical volumeMounts ?
|
// shall we throw an error for identical volumeMounts ?
|
||||||
if !reflect.DeepEqual(found, v) {
|
if !reflect.DeepEqual(found, internalVolumeMount) {
|
||||||
errs = append(errs, fmt.Errorf("merging volume mounts for %s has a conflict on %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.Name, v, found))
|
errs = append(errs, fmt.Errorf("merging volume mounts for %s has a conflict on %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.Name, v, found))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -285,10 +302,10 @@ func mergeVolumeMounts(volumeMounts []api.VolumeMount, podPresets []*settings.Po
|
||||||
found, ok = volumeMountsByPath[v.MountPath]
|
found, ok = volumeMountsByPath[v.MountPath]
|
||||||
if !ok {
|
if !ok {
|
||||||
// if we don't already have it append it and continue
|
// if we don't already have it append it and continue
|
||||||
volumeMountsByPath[v.MountPath] = v
|
volumeMountsByPath[v.MountPath] = internalVolumeMount
|
||||||
} else {
|
} else {
|
||||||
// make sure they are identical or throw an error
|
// make sure they are identical or throw an error
|
||||||
if !reflect.DeepEqual(found, v) {
|
if !reflect.DeepEqual(found, internalVolumeMount) {
|
||||||
errs = append(errs, fmt.Errorf("merging volume mounts for %s has a conflict on mount path %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.MountPath, v, found))
|
errs = append(errs, fmt.Errorf("merging volume mounts for %s has a conflict on mount path %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.MountPath, v, found))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -305,7 +322,7 @@ func mergeVolumeMounts(volumeMounts []api.VolumeMount, podPresets []*settings.Po
|
||||||
|
|
||||||
// mergeVolumes merges given list of Volumes with the volumes injected by given
|
// mergeVolumes merges given list of Volumes with the volumes injected by given
|
||||||
// podPresets. It returns an error if it detects any conflict during the merge.
|
// podPresets. It returns an error if it detects any conflict during the merge.
|
||||||
func mergeVolumes(volumes []api.Volume, podPresets []*settings.PodPreset) ([]api.Volume, error) {
|
func mergeVolumes(volumes []api.Volume, podPresets []*settingsv1alpha1.PodPreset) ([]api.Volume, error) {
|
||||||
origVolumes := map[string]api.Volume{}
|
origVolumes := map[string]api.Volume{}
|
||||||
for _, v := range volumes {
|
for _, v := range volumes {
|
||||||
origVolumes[v.Name] = v
|
origVolumes[v.Name] = v
|
||||||
|
@ -318,16 +335,20 @@ func mergeVolumes(volumes []api.Volume, podPresets []*settings.PodPreset) ([]api
|
||||||
|
|
||||||
for _, pp := range podPresets {
|
for _, pp := range podPresets {
|
||||||
for _, v := range pp.Spec.Volumes {
|
for _, v := range pp.Spec.Volumes {
|
||||||
|
internalVolume := api.Volume{}
|
||||||
|
if err := apiscorev1.Convert_v1_Volume_To_core_Volume(&v, &internalVolume, nil); err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
found, ok := origVolumes[v.Name]
|
found, ok := origVolumes[v.Name]
|
||||||
if !ok {
|
if !ok {
|
||||||
// if we don't already have it append it and continue
|
// if we don't already have it append it and continue
|
||||||
origVolumes[v.Name] = v
|
origVolumes[v.Name] = internalVolume
|
||||||
mergedVolumes = append(mergedVolumes, v)
|
mergedVolumes = append(mergedVolumes, internalVolume)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
// make sure they are identical or throw an error
|
// make sure they are identical or throw an error
|
||||||
if !reflect.DeepEqual(found, v) {
|
if !reflect.DeepEqual(found, internalVolume) {
|
||||||
errs = append(errs, fmt.Errorf("merging volumes for %s has a conflict on %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.Name, v, found))
|
errs = append(errs, fmt.Errorf("merging volumes for %s has a conflict on %s: \n%#v\ndoes not match\n%#v\n in container", pp.GetName(), v.Name, v, found))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -348,7 +369,7 @@ func mergeVolumes(volumes []api.Volume, podPresets []*settings.PodPreset) ([]api
|
||||||
// applyPodPresetsOnPod updates the PodSpec with merged information from all the
|
// applyPodPresetsOnPod updates the PodSpec with merged information from all the
|
||||||
// applicable PodPresets. It ignores the errors of merge functions because merge
|
// applicable PodPresets. It ignores the errors of merge functions because merge
|
||||||
// errors have already been checked in safeToApplyPodPresetsOnPod function.
|
// errors have already been checked in safeToApplyPodPresetsOnPod function.
|
||||||
func applyPodPresetsOnPod(pod *api.Pod, podPresets []*settings.PodPreset) {
|
func applyPodPresetsOnPod(pod *api.Pod, podPresets []*settingsv1alpha1.PodPreset) {
|
||||||
if len(podPresets) == 0 {
|
if len(podPresets) == 0 {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
@ -374,7 +395,7 @@ func applyPodPresetsOnPod(pod *api.Pod, podPresets []*settings.PodPreset) {
|
||||||
// applyPodPresetsOnContainer injects envVars, VolumeMounts and envFrom from
|
// applyPodPresetsOnContainer injects envVars, VolumeMounts and envFrom from
|
||||||
// given podPresets in to the given container. It ignores conflict errors
|
// given podPresets in to the given container. It ignores conflict errors
|
||||||
// because it assumes those have been checked already by the caller.
|
// because it assumes those have been checked already by the caller.
|
||||||
func applyPodPresetsOnContainer(ctr *api.Container, podPresets []*settings.PodPreset) {
|
func applyPodPresetsOnContainer(ctr *api.Container, podPresets []*settingsv1alpha1.PodPreset) {
|
||||||
envVars, _ := mergeEnv(ctr.Env, podPresets)
|
envVars, _ := mergeEnv(ctr.Env, podPresets)
|
||||||
ctr.Env = envVars
|
ctr.Env = envVars
|
||||||
|
|
||||||
|
|
|
@ -20,44 +20,45 @@ import (
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
corev1 "k8s.io/api/core/v1"
|
||||||
|
settingsv1alpha1 "k8s.io/api/settings/v1alpha1"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/runtime"
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
kadmission "k8s.io/apiserver/pkg/admission"
|
kadmission "k8s.io/apiserver/pkg/admission"
|
||||||
"k8s.io/apiserver/pkg/authentication/user"
|
"k8s.io/apiserver/pkg/authentication/user"
|
||||||
|
"k8s.io/client-go/informers"
|
||||||
|
"k8s.io/client-go/kubernetes/fake"
|
||||||
|
settingsv1alpha1listers "k8s.io/client-go/listers/settings/v1alpha1"
|
||||||
api "k8s.io/kubernetes/pkg/apis/core"
|
api "k8s.io/kubernetes/pkg/apis/core"
|
||||||
"k8s.io/kubernetes/pkg/apis/settings"
|
|
||||||
"k8s.io/kubernetes/pkg/client/clientset_generated/internalclientset/fake"
|
|
||||||
informers "k8s.io/kubernetes/pkg/client/informers/informers_generated/internalversion"
|
|
||||||
settingslisters "k8s.io/kubernetes/pkg/client/listers/settings/internalversion"
|
|
||||||
"k8s.io/kubernetes/pkg/controller"
|
"k8s.io/kubernetes/pkg/controller"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestMergeEnv(t *testing.T) {
|
func TestMergeEnv(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
orig []api.EnvVar
|
orig []api.EnvVar
|
||||||
mod []api.EnvVar
|
mod []corev1.EnvVar
|
||||||
result []api.EnvVar
|
result []api.EnvVar
|
||||||
shouldFail bool
|
shouldFail bool
|
||||||
}{
|
}{
|
||||||
"empty original": {
|
"empty original": {
|
||||||
mod: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
mod: []corev1.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
||||||
result: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
result: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
||||||
shouldFail: false,
|
shouldFail: false,
|
||||||
},
|
},
|
||||||
"good merge": {
|
"good merge": {
|
||||||
orig: []api.EnvVar{{Name: "abcd", Value: "value2"}, {Name: "hello", Value: "value3"}},
|
orig: []api.EnvVar{{Name: "abcd", Value: "value2"}, {Name: "hello", Value: "value3"}},
|
||||||
mod: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
mod: []corev1.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
||||||
result: []api.EnvVar{{Name: "abcd", Value: "value2"}, {Name: "hello", Value: "value3"}, {Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
result: []api.EnvVar{{Name: "abcd", Value: "value2"}, {Name: "hello", Value: "value3"}, {Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
||||||
shouldFail: false,
|
shouldFail: false,
|
||||||
},
|
},
|
||||||
"conflict": {
|
"conflict": {
|
||||||
orig: []api.EnvVar{{Name: "abc", Value: "value3"}},
|
orig: []api.EnvVar{{Name: "abc", Value: "value3"}},
|
||||||
mod: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
mod: []corev1.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
||||||
shouldFail: true,
|
shouldFail: true,
|
||||||
},
|
},
|
||||||
"one is exact same": {
|
"one is exact same": {
|
||||||
orig: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "hello", Value: "value3"}},
|
orig: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "hello", Value: "value3"}},
|
||||||
mod: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
mod: []corev1.EnvVar{{Name: "abc", Value: "value2"}, {Name: "ABC", Value: "value3"}},
|
||||||
result: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "hello", Value: "value3"}, {Name: "ABC", Value: "value3"}},
|
result: []api.EnvVar{{Name: "abc", Value: "value2"}, {Name: "hello", Value: "value3"}, {Name: "ABC", Value: "value3"}},
|
||||||
shouldFail: false,
|
shouldFail: false,
|
||||||
},
|
},
|
||||||
|
@ -66,7 +67,7 @@ func TestMergeEnv(t *testing.T) {
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
result, err := mergeEnv(
|
result, err := mergeEnv(
|
||||||
test.orig,
|
test.orig,
|
||||||
[]*settings.PodPreset{{Spec: settings.PodPresetSpec{Env: test.mod}}},
|
[]*settingsv1alpha1.PodPreset{{Spec: settingsv1alpha1.PodPresetSpec{Env: test.mod}}},
|
||||||
)
|
)
|
||||||
if test.shouldFail && err == nil {
|
if test.shouldFail && err == nil {
|
||||||
t.Fatalf("expected test %q to fail but got nil", name)
|
t.Fatalf("expected test %q to fail but got nil", name)
|
||||||
|
@ -83,21 +84,21 @@ func TestMergeEnv(t *testing.T) {
|
||||||
func TestMergeEnvFrom(t *testing.T) {
|
func TestMergeEnvFrom(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
orig []api.EnvFromSource
|
orig []api.EnvFromSource
|
||||||
mod []api.EnvFromSource
|
mod []corev1.EnvFromSource
|
||||||
result []api.EnvFromSource
|
result []api.EnvFromSource
|
||||||
shouldFail bool
|
shouldFail bool
|
||||||
}{
|
}{
|
||||||
"empty original": {
|
"empty original": {
|
||||||
mod: []api.EnvFromSource{
|
mod: []corev1.EnvFromSource{
|
||||||
{
|
{
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Prefix: "pre_",
|
Prefix: "pre_",
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -124,16 +125,16 @@ func TestMergeEnvFrom(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
mod: []api.EnvFromSource{
|
mod: []corev1.EnvFromSource{
|
||||||
{
|
{
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Prefix: "pre_",
|
Prefix: "pre_",
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -162,7 +163,7 @@ func TestMergeEnvFrom(t *testing.T) {
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
result, err := mergeEnvFrom(
|
result, err := mergeEnvFrom(
|
||||||
test.orig,
|
test.orig,
|
||||||
[]*settings.PodPreset{{Spec: settings.PodPresetSpec{EnvFrom: test.mod}}},
|
[]*settingsv1alpha1.PodPreset{{Spec: settingsv1alpha1.PodPresetSpec{EnvFrom: test.mod}}},
|
||||||
)
|
)
|
||||||
if test.shouldFail && err == nil {
|
if test.shouldFail && err == nil {
|
||||||
t.Fatalf("expected test %q to fail but got nil", name)
|
t.Fatalf("expected test %q to fail but got nil", name)
|
||||||
|
@ -179,12 +180,12 @@ func TestMergeEnvFrom(t *testing.T) {
|
||||||
func TestMergeVolumeMounts(t *testing.T) {
|
func TestMergeVolumeMounts(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
orig []api.VolumeMount
|
orig []api.VolumeMount
|
||||||
mod []api.VolumeMount
|
mod []corev1.VolumeMount
|
||||||
result []api.VolumeMount
|
result []api.VolumeMount
|
||||||
shouldFail bool
|
shouldFail bool
|
||||||
}{
|
}{
|
||||||
"empty original": {
|
"empty original": {
|
||||||
mod: []api.VolumeMount{
|
mod: []corev1.VolumeMount{
|
||||||
{
|
{
|
||||||
Name: "simply-mounted-volume",
|
Name: "simply-mounted-volume",
|
||||||
MountPath: "/opt/",
|
MountPath: "/opt/",
|
||||||
|
@ -199,7 +200,7 @@ func TestMergeVolumeMounts(t *testing.T) {
|
||||||
shouldFail: false,
|
shouldFail: false,
|
||||||
},
|
},
|
||||||
"good merge": {
|
"good merge": {
|
||||||
mod: []api.VolumeMount{
|
mod: []corev1.VolumeMount{
|
||||||
{
|
{
|
||||||
Name: "simply-mounted-volume",
|
Name: "simply-mounted-volume",
|
||||||
MountPath: "/opt/",
|
MountPath: "/opt/",
|
||||||
|
@ -224,7 +225,7 @@ func TestMergeVolumeMounts(t *testing.T) {
|
||||||
shouldFail: false,
|
shouldFail: false,
|
||||||
},
|
},
|
||||||
"conflict": {
|
"conflict": {
|
||||||
mod: []api.VolumeMount{
|
mod: []corev1.VolumeMount{
|
||||||
{
|
{
|
||||||
Name: "simply-mounted-volume",
|
Name: "simply-mounted-volume",
|
||||||
MountPath: "/opt/",
|
MountPath: "/opt/",
|
||||||
|
@ -243,7 +244,7 @@ func TestMergeVolumeMounts(t *testing.T) {
|
||||||
shouldFail: true,
|
shouldFail: true,
|
||||||
},
|
},
|
||||||
"conflict on mount path": {
|
"conflict on mount path": {
|
||||||
mod: []api.VolumeMount{
|
mod: []corev1.VolumeMount{
|
||||||
{
|
{
|
||||||
Name: "simply-mounted-volume",
|
Name: "simply-mounted-volume",
|
||||||
MountPath: "/opt/",
|
MountPath: "/opt/",
|
||||||
|
@ -262,7 +263,7 @@ func TestMergeVolumeMounts(t *testing.T) {
|
||||||
shouldFail: true,
|
shouldFail: true,
|
||||||
},
|
},
|
||||||
"one is exact same": {
|
"one is exact same": {
|
||||||
mod: []api.VolumeMount{
|
mod: []corev1.VolumeMount{
|
||||||
{
|
{
|
||||||
Name: "simply-mounted-volume",
|
Name: "simply-mounted-volume",
|
||||||
MountPath: "/opt/",
|
MountPath: "/opt/",
|
||||||
|
@ -295,7 +296,7 @@ func TestMergeVolumeMounts(t *testing.T) {
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
result, err := mergeVolumeMounts(
|
result, err := mergeVolumeMounts(
|
||||||
test.orig,
|
test.orig,
|
||||||
[]*settings.PodPreset{{Spec: settings.PodPresetSpec{VolumeMounts: test.mod}}},
|
[]*settingsv1alpha1.PodPreset{{Spec: settingsv1alpha1.PodPresetSpec{VolumeMounts: test.mod}}},
|
||||||
)
|
)
|
||||||
if test.shouldFail && err == nil {
|
if test.shouldFail && err == nil {
|
||||||
t.Fatalf("expected test %q to fail but got nil", name)
|
t.Fatalf("expected test %q to fail but got nil", name)
|
||||||
|
@ -312,14 +313,14 @@ func TestMergeVolumeMounts(t *testing.T) {
|
||||||
func TestMergeVolumes(t *testing.T) {
|
func TestMergeVolumes(t *testing.T) {
|
||||||
tests := map[string]struct {
|
tests := map[string]struct {
|
||||||
orig []api.Volume
|
orig []api.Volume
|
||||||
mod []api.Volume
|
mod []corev1.Volume
|
||||||
result []api.Volume
|
result []api.Volume
|
||||||
shouldFail bool
|
shouldFail bool
|
||||||
}{
|
}{
|
||||||
"empty original": {
|
"empty original": {
|
||||||
mod: []api.Volume{
|
mod: []corev1.Volume{
|
||||||
{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
{Name: "vol2", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol2", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
result: []api.Volume{
|
result: []api.Volume{
|
||||||
{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
|
@ -332,9 +333,9 @@ func TestMergeVolumes(t *testing.T) {
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
{Name: "vol4", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol4", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
mod: []api.Volume{
|
mod: []corev1.Volume{
|
||||||
{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
{Name: "vol2", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol2", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
result: []api.Volume{
|
result: []api.Volume{
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
|
@ -349,9 +350,9 @@ func TestMergeVolumes(t *testing.T) {
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
{Name: "vol4", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol4", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
mod: []api.Volume{
|
mod: []corev1.Volume{
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{HostPath: &api.HostPathVolumeSource{Path: "/etc/apparmor.d"}}},
|
{Name: "vol3", VolumeSource: corev1.VolumeSource{HostPath: &corev1.HostPathVolumeSource{Path: "/etc/apparmor.d"}}},
|
||||||
{Name: "vol2", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol2", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
shouldFail: true,
|
shouldFail: true,
|
||||||
},
|
},
|
||||||
|
@ -360,9 +361,9 @@ func TestMergeVolumes(t *testing.T) {
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
{Name: "vol4", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol4", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
mod: []api.Volume{
|
mod: []corev1.Volume{
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol3", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
{Name: "vol2", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol2", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}},
|
||||||
},
|
},
|
||||||
result: []api.Volume{
|
result: []api.Volume{
|
||||||
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
{Name: "vol3", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}},
|
||||||
|
@ -376,7 +377,7 @@ func TestMergeVolumes(t *testing.T) {
|
||||||
for name, test := range tests {
|
for name, test := range tests {
|
||||||
result, err := mergeVolumes(
|
result, err := mergeVolumes(
|
||||||
test.orig,
|
test.orig,
|
||||||
[]*settings.PodPreset{{Spec: settings.PodPresetSpec{Volumes: test.mod}}},
|
[]*settingsv1alpha1.PodPreset{{Spec: settingsv1alpha1.PodPresetSpec{Volumes: test.mod}}},
|
||||||
)
|
)
|
||||||
if test.shouldFail && err == nil {
|
if test.shouldFail && err == nil {
|
||||||
t.Fatalf("expected test %q to fail but got nil", name)
|
t.Fatalf("expected test %q to fail but got nil", name)
|
||||||
|
@ -392,7 +393,7 @@ func TestMergeVolumes(t *testing.T) {
|
||||||
|
|
||||||
// NewTestAdmission provides an admission plugin with test implementations of internal structs. It uses
|
// NewTestAdmission provides an admission plugin with test implementations of internal structs. It uses
|
||||||
// an authorizer that always returns true.
|
// an authorizer that always returns true.
|
||||||
func NewTestAdmission(lister settingslisters.PodPresetLister, objects ...runtime.Object) kadmission.MutationInterface {
|
func NewTestAdmission(lister settingsv1alpha1listers.PodPresetLister, objects ...runtime.Object) kadmission.MutationInterface {
|
||||||
// Build a test client that the admission plugin can use to look up the service account missing from its cache
|
// Build a test client that the admission plugin can use to look up the service account missing from its cache
|
||||||
client := fake.NewSimpleClientset(objects...)
|
client := fake.NewSimpleClientset(objects...)
|
||||||
|
|
||||||
|
@ -424,12 +425,12 @@ func TestAdmitConflictWithDifferentNamespaceShouldDoNothing(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "othernamespace",
|
Namespace: "othernamespace",
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -439,7 +440,7 @@ func TestAdmitConflictWithDifferentNamespaceShouldDoNothing(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Env: []api.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -470,12 +471,12 @@ func TestAdmitConflictWithNonMatchingLabelsShouldNotError(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "namespace",
|
Namespace: "namespace",
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -485,7 +486,7 @@ func TestAdmitConflictWithNonMatchingLabelsShouldNotError(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Env: []api.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -517,12 +518,12 @@ func TestAdmitConflictShouldNotModifyPod(t *testing.T) {
|
||||||
}
|
}
|
||||||
origPod := *pod
|
origPod := *pod
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "namespace",
|
Namespace: "namespace",
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -532,7 +533,7 @@ func TestAdmitConflictShouldNotModifyPod(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Env: []api.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abc", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -566,12 +567,12 @@ func TestAdmit(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "namespace",
|
Namespace: "namespace",
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -581,18 +582,18 @@ func TestAdmit(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Volumes: []api.Volume{{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}},
|
Volumes: []corev1.Volume{{Name: "vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}},
|
||||||
Env: []api.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
EnvFrom: []api.EnvFromSource{
|
EnvFrom: []corev1.EnvFromSource{
|
||||||
{
|
{
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Prefix: "pre_",
|
Prefix: "pre_",
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -626,12 +627,12 @@ func TestAdmitMirrorPod(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "namespace",
|
Namespace: "namespace",
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -641,18 +642,18 @@ func TestAdmitMirrorPod(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Volumes: []api.Volume{{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}},
|
Volumes: []corev1.Volume{{Name: "vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}},
|
||||||
Env: []api.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
EnvFrom: []api.EnvFromSource{
|
EnvFrom: []corev1.EnvFromSource{
|
||||||
{
|
{
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Prefix: "pre_",
|
Prefix: "pre_",
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -696,12 +697,12 @@ func TestExclusionNoAdmit(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "namespace",
|
Namespace: "namespace",
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -711,18 +712,18 @@ func TestExclusionNoAdmit(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Volumes: []api.Volume{{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}},
|
Volumes: []corev1.Volume{{Name: "vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}},
|
||||||
Env: []api.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
EnvFrom: []api.EnvFromSource{
|
EnvFrom: []corev1.EnvFromSource{
|
||||||
{
|
{
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Prefix: "pre_",
|
Prefix: "pre_",
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -760,12 +761,12 @@ func TestAdmitEmptyPodNamespace(t *testing.T) {
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
pip := &settings.PodPreset{
|
pip := &settingsv1alpha1.PodPreset{
|
||||||
ObjectMeta: metav1.ObjectMeta{
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
Name: "hello",
|
Name: "hello",
|
||||||
Namespace: "different", // (pod will be submitted to namespace 'namespace')
|
Namespace: "different", // (pod will be submitted to namespace 'namespace')
|
||||||
},
|
},
|
||||||
Spec: settings.PodPresetSpec{
|
Spec: settingsv1alpha1.PodPresetSpec{
|
||||||
Selector: metav1.LabelSelector{
|
Selector: metav1.LabelSelector{
|
||||||
MatchExpressions: []metav1.LabelSelectorRequirement{
|
MatchExpressions: []metav1.LabelSelectorRequirement{
|
||||||
{
|
{
|
||||||
|
@ -775,18 +776,18 @@ func TestAdmitEmptyPodNamespace(t *testing.T) {
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
Volumes: []api.Volume{{Name: "vol", VolumeSource: api.VolumeSource{EmptyDir: &api.EmptyDirVolumeSource{}}}},
|
Volumes: []corev1.Volume{{Name: "vol", VolumeSource: corev1.VolumeSource{EmptyDir: &corev1.EmptyDirVolumeSource{}}}},
|
||||||
Env: []api.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
Env: []corev1.EnvVar{{Name: "abcd", Value: "value"}, {Name: "ABC", Value: "value"}},
|
||||||
EnvFrom: []api.EnvFromSource{
|
EnvFrom: []corev1.EnvFromSource{
|
||||||
{
|
{
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
Prefix: "pre_",
|
Prefix: "pre_",
|
||||||
ConfigMapRef: &api.ConfigMapEnvSource{
|
ConfigMapRef: &corev1.ConfigMapEnvSource{
|
||||||
LocalObjectReference: api.LocalObjectReference{Name: "abc"},
|
LocalObjectReference: corev1.LocalObjectReference{Name: "abc"},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
@ -804,11 +805,11 @@ func TestAdmitEmptyPodNamespace(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func admitPod(pod *api.Pod, pip *settings.PodPreset) error {
|
func admitPod(pod *api.Pod, pip *settingsv1alpha1.PodPreset) error {
|
||||||
informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc())
|
informerFactory := informers.NewSharedInformerFactory(nil, controller.NoResyncPeriodFunc())
|
||||||
store := informerFactory.Settings().InternalVersion().PodPresets().Informer().GetStore()
|
store := informerFactory.Settings().V1alpha1().PodPresets().Informer().GetStore()
|
||||||
store.Add(pip)
|
store.Add(pip)
|
||||||
plugin := NewTestAdmission(informerFactory.Settings().InternalVersion().PodPresets().Lister())
|
plugin := NewTestAdmission(informerFactory.Settings().V1alpha1().PodPresets().Lister())
|
||||||
attrs := kadmission.NewAttributesRecord(
|
attrs := kadmission.NewAttributesRecord(
|
||||||
pod,
|
pod,
|
||||||
nil,
|
nil,
|
||||||
|
|
Loading…
Reference in New Issue