@@ -82,13 +82,57 @@ func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.Admiss
8282 },
8383 }, nil
8484 }
85- if len (config .ControlPlaneUrl ) == 0 {
85+
86+ target := deploy .DeepCopy ()
87+ added := false
88+ // Check if the x-live-enabled label exists in deploy's spec.template.metadata.labels; if not, add it.
89+ if _ , ok := deploy .Spec .Template .Labels [config .WebHookMatchKey ]; ! ok {
90+ target .Spec .Template .Labels [config .WebHookMatchKey ] = config .WebHookMatchValue
91+ added = true
92+ }
93+
94+ if len (config .ControlPlaneUrl ) != 0 {
8695 return & admissionv1.AdmissionResponse {
8796 UID : request .UID ,
8897 Allowed : true ,
8998 }, nil
9099 } else {
91- return AddApplicationEnvironments (request , deploy )
100+ err := AddApplicationEnvironments (deploy , target , added )
101+ if err != nil {
102+ errMsg := fmt .Sprintf ("[mutation] /injection-deploy: failed to get application environments: %v" , err )
103+ return & admissionv1.AdmissionResponse {
104+ Allowed : false ,
105+ Result : & metav1.Status {
106+ Code : http .StatusInternalServerError ,
107+ Message : errMsg ,
108+ },
109+ }, err
110+ }
111+ }
112+ if ! added {
113+ log .Infof ("[mutation] /injection-deploy: no envs to add to deployment %s/%s" , deploy .Name , deploy .Namespace )
114+ return & admissionv1.AdmissionResponse {
115+ UID : request .UID ,
116+ Allowed : true ,
117+ }, nil
118+ } else {
119+ patchStr , err := createDeployPatch (target , & deploy )
120+ if err != nil {
121+ log .Errorf ("[mutation] /injection-deploy: failed to create patch: %v" , err )
122+ return & admissionv1.AdmissionResponse {
123+ UID : request .UID ,
124+ Allowed : true ,
125+ }, nil
126+ }
127+ return & admissionv1.AdmissionResponse {
128+ UID : request .UID ,
129+ Allowed : true ,
130+ Patch : patchStr ,
131+ PatchType : func () * admissionv1.PatchType {
132+ pt := admissionv1 .PatchTypeJSONPatch
133+ return & pt
134+ }(),
135+ }, nil
92136 }
93137 default :
94138 return & admissionv1.AdmissionResponse {
@@ -98,22 +142,12 @@ func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.Admiss
98142 }
99143}
100144
101- func AddApplicationEnvironments (request * apiv1. AdmissionRequest , deploy appsv1.Deployment ) ( * apiv1. AdmissionResponse , error ) {
145+ func AddApplicationEnvironments (source appsv1. Deployment , target * appsv1.Deployment , added bool ) error {
102146 // Get the application environment variables
103- envs , err := resource .GetApplicationEnvironments (deploy .GetLabels ())
147+ envs , err := resource .GetApplicationEnvironments (source .GetLabels ())
104148 if err != nil {
105- errMsg := fmt .Sprintf ("[mutation] /injection-deploy: failed to get application environments: %v" , err )
106- log .Error (errMsg )
107- return & admissionv1.AdmissionResponse {
108- Allowed : false ,
109- Result : & metav1.Status {
110- Code : http .StatusInternalServerError ,
111- Message : errMsg ,
112- },
113- }, err
149+ return err
114150 }
115- target := deploy .DeepCopy ()
116- added := false
117151 for k , v := range envs {
118152 // Check if the environment variable already exists
119153 exists := false
@@ -132,32 +166,8 @@ func AddApplicationEnvironments(request *apiv1.AdmissionRequest, deploy appsv1.D
132166 added = true
133167 }
134168 log .Infof ("[mutation] /injection-deploy: add envs to deployment %s/%s, envs: %v, deploy's envs: %v" ,
135- deploy .Name , deploy .Namespace , envs , target .Spec .Template .Spec .Containers [0 ].Env )
136- if added {
137- patchStr , err := createDeployPatch (target , & deploy )
138- if err != nil {
139- log .Errorf ("[mutation] /injection-deploy: failed to create patch: %v" , err )
140- return & admissionv1.AdmissionResponse {
141- UID : request .UID ,
142- Allowed : true ,
143- }, nil
144- }
145- return & admissionv1.AdmissionResponse {
146- UID : request .UID ,
147- Allowed : true ,
148- Patch : patchStr ,
149- PatchType : func () * admissionv1.PatchType {
150- pt := admissionv1 .PatchTypeJSONPatch
151- return & pt
152- }(),
153- }, nil
154- } else {
155- log .Infof ("[mutation] /injection-deploy: no envs to add to deployment %s/%s" , deploy .Name , deploy .Namespace )
156- return & admissionv1.AdmissionResponse {
157- UID : request .UID ,
158- Allowed : true ,
159- }, nil
160- }
169+ source .Name , source .Namespace , envs , target .Spec .Template .Spec .Containers [0 ].Env )
170+ return nil
161171}
162172
163173func createOrUpdateConfigMap (deploy * appsv1.Deployment ) error {
0 commit comments