Skip to content

Commit 503427e

Browse files
committed
Add JOYLIVE_MATCH_KEY and JOYLIVE_MATCH_VALUE environment variables to deployment
1 parent 952f69f commit 503427e

File tree

5 files changed

+71
-47
lines changed

5 files changed

+71
-47
lines changed

deploy/all-cr.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,6 +751,10 @@ spec:
751751
value:
752752
- name: JOYLIVE_CLUSTER_ID
753753
value:
754+
- name: JOYLIVE_MATCH_KEY
755+
value: x-live-enabled
756+
- name: JOYLIVE_MATCH_VALUE
757+
value: "true"
754758
name: joylive-injector
755759
image: ghcr.m.daocloud.io/jd-opensource/joylive-injector:v1.2.2
756760
imagePullPolicy: Always

deploy/joylive-injector/templates/deployment.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ spec:
3838
value: {{ .Values.controlPlaneUrl }}
3939
- name: JOYLIVE_CLUSTER_ID
4040
value: {{ .Values.clusterId }}
41+
- name: JOYLIVE_MATCH_KEY
42+
value: {{ .Values.matchLabels.matchKey}}
43+
- name: JOYLIVE_MATCH_VALUE
44+
value: {{ .Values.matchLabels.matchValue }}
4145
name: joylive-injector
4246
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
4347
imagePullPolicy: {{ .Values.image.pullPolicy }}
31 Bytes
Binary file not shown.

pkg/config/config.go

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,19 @@ const (
2828
JdapServiceSpaceLabel = "jmsf.jd.com/service-space"
2929
ApplicationLabel = "jmsf.jd.com/application"
3030
JdapApplicationLabel = "app.jdap.io/name"
31+
WebHookMatchKeyEnv = "JOYLIVE_MATCH_KEY"
32+
WebHookMatchValueEnv = "JOYLIVE_MATCH_VALUE"
3133
)
3234

3335
var (
34-
Cert string
35-
Key string
36-
Addr string
37-
MatchLabels string
38-
ControlPlaneUrl string
39-
ClusterId string
36+
Cert string
37+
Key string
38+
Addr string
39+
MatchLabels string
40+
ControlPlaneUrl string
41+
ClusterId string
42+
WebHookMatchKey string
43+
WebHookMatchValue string
4044
)
4145

4246
// injection_deploy config
@@ -59,6 +63,8 @@ func init() {
5963
ControlPlaneUrl = os.Getenv(ControlPlaneUrlEnvName)
6064
// Initialize the default cluster id from environment variables
6165
ClusterId = os.Getenv(ClusterIdEnvName)
66+
WebHookMatchKey = os.Getenv(WebHookMatchKeyEnv)
67+
WebHookMatchValue = os.Getenv(WebHookMatchValueEnv)
6268
}
6369

6470
func GetNamespace() string {

pkg/mutation/mutation_deploy.go

Lines changed: 51 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -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

163173
func createOrUpdateConfigMap(deploy *appsv1.Deployment) error {

0 commit comments

Comments
 (0)