@@ -3,10 +3,11 @@ package mutation
3
3
import (
4
4
"encoding/json"
5
5
"fmt"
6
- "go.uber.org/zap"
7
6
"net/http"
8
7
"strings"
9
8
9
+ "go.uber.org/zap"
10
+
10
11
"github.com/jd-opensource/joylive-injector/pkg/admission"
11
12
"github.com/jd-opensource/joylive-injector/pkg/config"
12
13
"github.com/jd-opensource/joylive-injector/pkg/log"
@@ -110,31 +111,60 @@ func injectionPod(request *admissionv1.AdmissionRequest) (*admissionv1.Admission
110
111
}
111
112
112
113
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
+ }
118
127
}
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
+ }
125
149
}
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
+ },
132
160
}
133
161
}
134
- return envs
162
+
163
+ log .Debugf ("[mutation] /injection-pod: no labels matched with prefixes %v" , validPrefixes )
164
+ return nil
135
165
}
136
166
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 {
138
168
initContainers := targetPod .Spec .InitContainers
139
169
for _ , container := range initContainers {
140
170
if container .Name == config .InitContainerName {
0 commit comments