Skip to content

Commit 7dd3594

Browse files
committed
Support for converting specific labels to env
1 parent 60cccb1 commit 7dd3594

File tree

9 files changed

+78
-36
lines changed

9 files changed

+78
-36
lines changed

deploy/all-cr.yaml

+7-5
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ data:
300300
initializeTimeout: ${CONFIG_POLICY_INITIALIZE_TIMEOUT:10000}
301301
service:
302302
localFirst: ${CONFIG_LOCAL_FIRST:true}
303-
localFirstMode: ${CONFIG_LOCAL_FIRST_MODE:CLUSTER}
303+
localFirstMode: ${CONFIG_LOCAL_FIRST_MODE:CELL}
304304
serviceGroups: ${CONFIG_SERVICE_GROUPS:}
305305
serviceGroupOpen: ${CONFIG_SERVICE_GROUP_OPEN:true}
306306
responseException: ${CONFIG_RESPONSE_EXCEPTION:true}
@@ -423,12 +423,12 @@ data:
423423
configcenter:
424424
type: nacos
425425
address: ${CONFIG_CENTER_ADDRESS:}
426-
username: ${CONFIG_CENTER_USERNAME:${CONFIG_USERNAME:guest}}
427-
password: ${CONFIG_CENTER_PASSWORD:${CONFIG_PASSWORD:guest}}
426+
username: ${CONFIG_USERNAME:guest}
427+
password: ${CONFIG_PASSWORD:guest}
428428
names: ${CONFIG_CENTER_NAMES:}
429429
name:
430430
namespace: ${CONFIG_CENTER_NAMESPACE:public}
431-
name: ${CONFIG_CENTER_NAME:${APPLICATION_NAME}}
431+
name: ${CONFIG_CENTER_NAME}
432432
profile: ${CONFIG_CENTER_PROFILE:DEFAULT_GROUP}
433433
counter:
434434
gateway: true
@@ -443,7 +443,7 @@ data:
443443
# Define the version, environment variables, and other information of joylive-injector injecting joylive-agent
444444
agent:
445445
image: ghcr.m.daocloud.io/jd-opensource/joylive-agent
446-
version: v1.4.0
446+
version: v1.6.0
447447
env:
448448
JAVA_TOOL_OPTIONS: -javaagent:/joylive/live.jar
449449
logback.xml: |
@@ -702,6 +702,8 @@ spec:
702702
value: joylive-injector-config
703703
- name: JOYLIVE_NAMESPACE
704704
value: joylive
705+
- name: JOYLIVE_MATCH_ENV_LABELS
706+
value: x-live, x-service
705707
name: joylive-injector
706708
image: ghcr.m.daocloud.io/jd-opensource/joylive-injector:v1.2.1
707709
imagePullPolicy: Always

deploy/joylive-injector/config/config.yaml

+3-3
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,12 @@ agent:
292292
configcenter:
293293
type: nacos
294294
address: ${CONFIG_CENTER_ADDRESS:}
295-
username: ${CONFIG_CENTER_USERNAME:${CONFIG_USERNAME:guest}}
296-
password: ${CONFIG_CENTER_PASSWORD:${CONFIG_PASSWORD:guest}}
295+
username: ${CONFIG_USERNAME:guest}
296+
password: ${CONFIG_PASSWORD:guest}
297297
names: ${CONFIG_CENTER_NAMES:}
298298
name:
299299
namespace: ${CONFIG_CENTER_NAMESPACE:public}
300-
name: ${CONFIG_CENTER_NAME:${APPLICATION_NAME}}
300+
name: ${CONFIG_CENTER_NAME}
301301
profile: ${CONFIG_CENTER_PROFILE:DEFAULT_GROUP}
302302
counter:
303303
gateway: true

deploy/joylive-injector/templates/deployment.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ spec:
3232
value: {{ .Values.configMapName }}
3333
- name: JOYLIVE_NAMESPACE
3434
value: {{ .Values.namespace }}
35+
- name: JOYLIVE_MATCH_ENV_LABELS
36+
value: {{ .Values.matchEnvLabels }}
3537
name: joylive-injector
3638
image: {{ .Values.image.repository }}:{{ .Values.image.tag }}
3739
imagePullPolicy: {{ .Values.image.pullPolicy }}

deploy/joylive-injector/values.yaml

+4-2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ containerArgs:
2424
- --cert=/etc/kubernetes/ssl/dac.pem
2525
- --key=/etc/kubernetes/ssl/dac-key.pem
2626

27+
matchEnvLabels: x-live, x-service
28+
2729
imagePullSecrets: [ ]
2830
nameOverride: ""
2931
fullnameOverride: ""
@@ -35,7 +37,7 @@ serviceAccount:
3537
agent:
3638
image:
3739
repository: ghcr.m.daocloud.io/jd-opensource/joylive-agent
38-
tag: v1.4.0
40+
tag: v1.6.0
3941
sync:
4042
liveSpace:
4143
type: file
@@ -61,7 +63,7 @@ agent:
6163
configcenter: false
6264
governance:
6365
service:
64-
localFirstMode: CLUSTER
66+
localFirstMode: CELL
6567
location:
6668
cloud:
6769
region:
12 Bytes
Binary file not shown.

main.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,16 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/jd-opensource/joylive-injector/pkg/log"
8-
"go.uber.org/zap"
97
"net/http"
108
"os"
119
"os/signal"
1210
"runtime"
1311
"sync"
1412
"syscall"
1513

14+
"github.com/jd-opensource/joylive-injector/pkg/log"
15+
"go.uber.org/zap"
16+
1617
"github.com/jd-opensource/joylive-injector/pkg/admission"
1718
_ "github.com/jd-opensource/joylive-injector/pkg/mutation"
1819

@@ -97,7 +98,7 @@ func init() {
9798
rootCmd.PersistentFlags().StringVarP(&config.Addr, "listen", "l", ":443", "Admission Controller listen address")
9899
rootCmd.PersistentFlags().StringVar(&config.Cert, "cert", "", "Admission Controller TLS cert")
99100
rootCmd.PersistentFlags().StringVar(&config.Key, "key", "", "Admission Controller TLS cert key")
100-
rootCmd.PersistentFlags().StringVar(&config.MatchLabel, "match-label", "x-live", "Match label")
101+
rootCmd.PersistentFlags().StringVar(&config.MatchLabels, "match-label", config.MatchLabels, "Match label")
101102

102103
// admission config
103104
rootCmd.PersistentFlags().StringVar(&config.InitContainerName, "init-container-name", "joylive-init-container", "Init container name")

pkg/config/config.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package config
22

33
import (
4+
"os"
5+
46
v1 "github.com/jd-opensource/joylive-injector/client-go/apis/injector/v1"
57
"github.com/jd-opensource/joylive-injector/pkg/log"
68
"github.com/jd-opensource/joylive-injector/pkg/resource"
79
"go.uber.org/zap"
8-
"os"
910
)
1011

1112
const (
@@ -20,6 +21,7 @@ const (
2021
InitContainerArgs = "-c, cp -r /joylive/* /agent && chmod -R 777 /agent"
2122
ConfigMapEnvName = "JOYLIVE_CONFIGMAP_NAME"
2223
NamespaceEnvName = "JOYLIVE_NAMESPACE"
24+
MatchLabelsEnvName = "JOYLIVE_MATCH_ENV_LABELS"
2325
DefaultNamespace = "joylive"
2426
AgentVersionLabel = "x-live-version"
2527
LiveSpaceIdLabel = "x-live-space-id"
@@ -32,7 +34,7 @@ var (
3234
Key string
3335
Addr string
3436
ConfigMountSubPath string
35-
MatchLabel string
37+
MatchLabels string
3638
)
3739

3840
// injection_deploy config
@@ -70,6 +72,9 @@ func init() {
7072
if err != nil {
7173
log.Fatal("init agentVersion error", zap.Error(err))
7274
}
75+
76+
// Initialize the default matchLabels from environment variables
77+
MatchLabels = os.Getenv(MatchLabelsEnvName)
7378
}
7479

7580
func GetNamespace() string {

pkg/mutation/mutation_deploy.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func init() {
3131
func injectionDeploy(request *admissionv1.AdmissionRequest) (*admissionv1.AdmissionResponse, error) {
3232
switch request.Kind.Kind {
3333
case "Deployment":
34-
log.Debugf("[mutation] ----- /injection-deploy: received request: %v,the operition is %s ", request.Resource, request.Operation)
34+
log.Debugf("[mutation] /injection-deploy: received request: %v,the operition is %s ", request.Resource, request.Operation)
3535
if request.Operation == apiv1.Operation("DELETE") {
3636
log.Debugf("[mutation] /injection-deploy: received delete request name is : %s, namespace is %s ", request.Name, request.Namespace)
3737
err := deleteConfigMap(request.Name, request.Namespace)

pkg/mutation/mutation_pod.go

+50-20
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ package mutation
33
import (
44
"encoding/json"
55
"fmt"
6-
"go.uber.org/zap"
76
"net/http"
87
"strings"
98

9+
"go.uber.org/zap"
10+
1011
"github.com/jd-opensource/joylive-injector/pkg/admission"
1112
"github.com/jd-opensource/joylive-injector/pkg/config"
1213
"github.com/jd-opensource/joylive-injector/pkg/log"
@@ -110,31 +111,60 @@ func injectionPod(request *admissionv1.AdmissionRequest) (*admissionv1.Admission
110111
}
111112

112113
func makePodEnvs(pod *corev1.Pod) []corev1.EnvVar {
113-
anoEnv := pod.Annotations
114-
envs := make([]corev1.EnvVar, 0)
115-
if _, ok := anoEnv[config.MatchLabel]; !ok {
116-
log.Warnf("[mutation] /injection-pod: the annotations do not have %s", config.MatchLabel)
117-
return envs
114+
metaPairs := make([]string, 0)
115+
rawPrefixes := config.MatchLabels
116+
117+
// Split multiple prefix configurations
118+
prefixes := strings.Split(rawPrefixes, ",")
119+
validPrefixes := make([]string, 0, len(prefixes))
120+
121+
// Clean up and validate prefixes
122+
for _, p := range prefixes {
123+
trimmed := strings.TrimSpace(p)
124+
if trimmed != "" {
125+
validPrefixes = append(validPrefixes, trimmed)
126+
}
118127
}
119-
log.Debugf("[mutation] /injection-pod: the annotations is %s", anoEnv[config.MatchLabel])
120-
anoEnvMap := make(map[string]string)
121-
err := json.Unmarshal([]byte(anoEnv[config.MatchLabel]), &anoEnvMap)
122-
if err != nil {
123-
log.Errorf("[mutation] /injection-pod: failed to unmarshal annotations: %v", err)
124-
return envs
128+
129+
if len(validPrefixes) == 0 {
130+
log.Warnf("[mutation] /injection-pod: no valid prefix configured in MatchLabels")
131+
return nil
132+
}
133+
134+
// Iterate through all labels, matching multiple prefixes
135+
for labelKey, labelValue := range pod.Labels {
136+
if labelValue == "" {
137+
continue
138+
}
139+
140+
for _, prefix := range validPrefixes {
141+
if strings.HasPrefix(labelKey, prefix) {
142+
trimmedKey := strings.TrimPrefix(labelKey, prefix)
143+
if trimmedKey != "" {
144+
metaPairs = append(metaPairs, fmt.Sprintf("%s=%s", trimmedKey, labelValue))
145+
}
146+
break
147+
}
148+
}
125149
}
126-
for k, v := range anoEnvMap {
127-
if k != "" && v != "" {
128-
envs = append(envs, corev1.EnvVar{
129-
Name: k,
130-
Value: v,
131-
})
150+
151+
if len(metaPairs) > 0 {
152+
metaValue := strings.Join(metaPairs, ";") + ";"
153+
log.Debugf("[mutation] /injection-pod: APPLICATION_SERVICE_META=%s (prefixes: %v)",
154+
metaValue, validPrefixes)
155+
return []corev1.EnvVar{
156+
{
157+
Name: "APPLICATION_SERVICE_META",
158+
Value: metaValue,
159+
},
132160
}
133161
}
134-
return envs
162+
163+
log.Debugf("[mutation] /injection-pod: no labels matched with prefixes %v", validPrefixes)
164+
return nil
135165
}
136166

137-
func addPodInitContainer(targetPod *corev1.Pod, envs []corev1.EnvVar, deploymentName string) []corev1.Container {
167+
func addPodInitContainer(targetPod *corev1.Pod, _ []corev1.EnvVar, deploymentName string) []corev1.Container {
138168
initContainers := targetPod.Spec.InitContainers
139169
for _, container := range initContainers {
140170
if container.Name == config.InitContainerName {

0 commit comments

Comments
 (0)