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

Inject NSM-envs into NSM-containers #18

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 15 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,18 @@ func (s *admissionWebhookServer) Review(in *admissionv1.AdmissionRequest) *admis
annotation := annotations[s.config.Annotation]

if annotation != "" {
envs := s.config.GetOrResolveEnvs()
// Get NSM environments
for i := range spec.Containers {
for _, e := range spec.Containers[i].Env {
if strings.HasPrefix(e.Name, "NSM_") {
envs = append(envs, e)
}
}
}
bytes, err := json.Marshal([]jsonpatch.JsonPatchOperation{
s.createInitContainerPatch(p, annotation, spec.InitContainers),
s.createContainerPatch(p, annotation, spec.Containers),
s.createInitContainerPatch(p, annotation, spec.InitContainers, envs),
s.createContainerPatch(p, annotation, spec.Containers, envs),
s.createVolumesPatch(p, spec.Volumes),
})
if err != nil {
Expand Down Expand Up @@ -164,11 +173,11 @@ func (s *admissionWebhookServer) createVolumesPatch(p string, volumes []corev1.V
return jsonpatch.NewOperation("add", path.Join(p, "volumes"), volumes)
}

func (s *admissionWebhookServer) createInitContainerPatch(p, v string, initContainers []corev1.Container) jsonpatch.JsonPatchOperation {
func (s *admissionWebhookServer) createInitContainerPatch(p, v string, initContainers []corev1.Container, envs []corev1.EnvVar) jsonpatch.JsonPatchOperation {
for _, img := range s.config.InitContainerImages {
initContainers = append(initContainers, corev1.Container{
Name: nameOf(img),
Env: append(s.config.GetOrResolveEnvs(), corev1.EnvVar{Name: s.config.NSURLEnvName, Value: v}),
Env: append(envs, corev1.EnvVar{Name: s.config.NSURLEnvName, Value: v}),
Image: img,
ImagePullPolicy: corev1.PullIfNotPresent,
})
Expand All @@ -177,11 +186,11 @@ func (s *admissionWebhookServer) createInitContainerPatch(p, v string, initConta
return jsonpatch.NewOperation("add", path.Join(p, "initContainers"), initContainers)
}

func (s *admissionWebhookServer) createContainerPatch(p, v string, containers []corev1.Container) jsonpatch.JsonPatchOperation {
func (s *admissionWebhookServer) createContainerPatch(p, v string, containers []corev1.Container, envs []corev1.EnvVar) jsonpatch.JsonPatchOperation {
for _, img := range s.config.ContainerImages {
containers = append(containers, corev1.Container{
Name: nameOf(img),
Env: append(s.config.GetOrResolveEnvs(), corev1.EnvVar{Name: s.config.NSURLEnvName, Value: v}),
Env: append(envs, corev1.EnvVar{Name: s.config.NSURLEnvName, Value: v}),
Image: img,
ImagePullPolicy: corev1.PullIfNotPresent,
})
Expand Down