Skip to content

Commit b3594ca

Browse files
committed
Ensure labels are initialized in deployment and pod mutations
1 parent dc7d102 commit b3594ca

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

pkg/mutation/mutation_deploy.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.Admiss
8787
target := deploy.DeepCopy()
8888
added := false
8989

90+
if deploy.Labels == nil {
91+
deploy.Labels = make(map[string]string)
92+
}
93+
94+
if deploy.Spec.Template.Labels == nil {
95+
deploy.Spec.Template.Labels = make(map[string]string)
96+
}
97+
98+
if target.Labels == nil {
99+
target.Labels = make(map[string]string)
100+
}
101+
102+
if target.Spec.Template.Labels == nil {
103+
target.Spec.Template.Labels = make(map[string]string)
104+
}
105+
90106
if enhanceType, ok := deploy.Labels[config.EnhanceTypeLabel]; ok && enhanceType == config.EnhanceTypeSidecar {
91107
log.Infof("[mutation] /injection-deploy: add label %s to deployment %s/%s", config.SidecarEnhanceLabel, deploy.Name, deploy.Namespace)
92108
target.Spec.Template.Labels[config.SidecarEnhanceLabel] = "true"

pkg/mutation/mutation_pod.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ func injectionPod(request *admissionv1.AdmissionRequest) (*admissionv1.Admission
6666

6767
envs := makePodEnvs(&pod)
6868
targetPod := pod.DeepCopy()
69+
70+
if pod.Labels == nil {
71+
pod.Labels = make(map[string]string)
72+
}
73+
74+
if targetPod.Labels == nil {
75+
targetPod.Labels = make(map[string]string)
76+
}
77+
6978
rs := resource.GetResource()
7079
deploymentName, err := rs.GetDeploymentName(&pod, request.Namespace)
7180
if err != nil {

pkg/resource/control.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,11 @@ func GetApplicationEnvironments(labels map[string]string) (map[string]string, er
3030
envMaps := make(map[string]string)
3131
if len(serviceSpace) != 0 && len(application) != 0 {
3232
url := fmt.Sprintf(
33-
"%s/ns/%s/application/%s/environments",
34-
config.ControlPlaneUrl, serviceSpace, application,
33+
"%s/ns/%s/application/%s/environments", config.ControlPlaneUrl, serviceSpace, application,
3534
)
3635
resp, err := http.Get(url)
3736
if err != nil {
38-
return nil, err
37+
return nil, nil
3938
}
4039
defer resp.Body.Close()
4140

0 commit comments

Comments
 (0)