@@ -22,6 +22,7 @@ import (
22
22
23
23
"github.com/go-logr/logr"
24
24
corev1 "k8s.io/api/core/v1"
25
+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25
26
"k8s.io/apimachinery/pkg/types"
26
27
"k8s.io/client-go/tools/record"
27
28
"sigs.k8s.io/controller-runtime/pkg/client"
@@ -46,7 +47,7 @@ type instPodMutator struct {
46
47
47
48
type instrumentationWithContainers struct {
48
49
Instrumentation * v1alpha1.Instrumentation
49
- Containers string
50
+ Containers [] string
50
51
AdditionalAnnotations map [string ]string
51
52
}
52
53
@@ -61,38 +62,6 @@ type languageInstrumentations struct {
61
62
Sdk instrumentationWithContainers
62
63
}
63
64
64
- // Check if single instrumentation is configured for Pod and return which is configured.
65
- func (langInsts languageInstrumentations ) isSingleInstrumentationEnabled () bool {
66
- count := 0
67
-
68
- if langInsts .Java .Instrumentation != nil {
69
- count ++
70
- }
71
- if langInsts .NodeJS .Instrumentation != nil {
72
- count ++
73
- }
74
- if langInsts .Python .Instrumentation != nil {
75
- count ++
76
- }
77
- if langInsts .DotNet .Instrumentation != nil {
78
- count ++
79
- }
80
- if langInsts .ApacheHttpd .Instrumentation != nil {
81
- count ++
82
- }
83
- if langInsts .Nginx .Instrumentation != nil {
84
- count ++
85
- }
86
- if langInsts .Go .Instrumentation != nil {
87
- count ++
88
- }
89
- if langInsts .Sdk .Instrumentation != nil {
90
- count ++
91
- }
92
-
93
- return count == 1
94
- }
95
-
96
65
// Check if specific containers are provided for configured instrumentation.
97
66
func (langInsts languageInstrumentations ) areContainerNamesConfiguredForMultipleInstrumentations () (bool , error ) {
98
67
var instrWithoutContainers int
@@ -103,42 +72,42 @@ func (langInsts languageInstrumentations) areContainerNamesConfiguredForMultiple
103
72
if langInsts .Java .Instrumentation != nil {
104
73
instrWithContainers += isInstrWithContainers (langInsts .Java )
105
74
instrWithoutContainers += isInstrWithoutContainers (langInsts .Java )
106
- allContainers = append (allContainers , langInsts .Java .Containers )
75
+ allContainers = append (allContainers , langInsts .Java .Containers ... )
107
76
}
108
77
if langInsts .NodeJS .Instrumentation != nil {
109
78
instrWithContainers += isInstrWithContainers (langInsts .NodeJS )
110
79
instrWithoutContainers += isInstrWithoutContainers (langInsts .NodeJS )
111
- allContainers = append (allContainers , langInsts .NodeJS .Containers )
80
+ allContainers = append (allContainers , langInsts .NodeJS .Containers ... )
112
81
}
113
82
if langInsts .Python .Instrumentation != nil {
114
83
instrWithContainers += isInstrWithContainers (langInsts .Python )
115
84
instrWithoutContainers += isInstrWithoutContainers (langInsts .Python )
116
- allContainers = append (allContainers , langInsts .Python .Containers )
85
+ allContainers = append (allContainers , langInsts .Python .Containers ... )
117
86
}
118
87
if langInsts .DotNet .Instrumentation != nil {
119
88
instrWithContainers += isInstrWithContainers (langInsts .DotNet )
120
89
instrWithoutContainers += isInstrWithoutContainers (langInsts .DotNet )
121
- allContainers = append (allContainers , langInsts .DotNet .Containers )
90
+ allContainers = append (allContainers , langInsts .DotNet .Containers ... )
122
91
}
123
92
if langInsts .ApacheHttpd .Instrumentation != nil {
124
93
instrWithContainers += isInstrWithContainers (langInsts .ApacheHttpd )
125
94
instrWithoutContainers += isInstrWithoutContainers (langInsts .ApacheHttpd )
126
- allContainers = append (allContainers , langInsts .ApacheHttpd .Containers )
95
+ allContainers = append (allContainers , langInsts .ApacheHttpd .Containers ... )
127
96
}
128
97
if langInsts .Nginx .Instrumentation != nil {
129
98
instrWithContainers += isInstrWithContainers (langInsts .Nginx )
130
99
instrWithoutContainers += isInstrWithoutContainers (langInsts .Nginx )
131
- allContainers = append (allContainers , langInsts .Nginx .Containers )
100
+ allContainers = append (allContainers , langInsts .Nginx .Containers ... )
132
101
}
133
102
if langInsts .Go .Instrumentation != nil {
134
103
instrWithContainers += isInstrWithContainers (langInsts .Go )
135
104
instrWithoutContainers += isInstrWithoutContainers (langInsts .Go )
136
- allContainers = append (allContainers , langInsts .Go .Containers )
105
+ allContainers = append (allContainers , langInsts .Go .Containers ... )
137
106
}
138
107
if langInsts .Sdk .Instrumentation != nil {
139
108
instrWithContainers += isInstrWithContainers (langInsts .Sdk )
140
109
instrWithoutContainers += isInstrWithoutContainers (langInsts .Sdk )
141
- allContainers = append (allContainers , langInsts .Sdk .Containers )
110
+ allContainers = append (allContainers , langInsts .Sdk .Containers ... )
142
111
}
143
112
144
113
// Look for duplicated containers.
@@ -165,10 +134,23 @@ func (langInsts languageInstrumentations) areContainerNamesConfiguredForMultiple
165
134
}
166
135
167
136
// Set containers for configured instrumentation.
168
- func (langInsts * languageInstrumentations ) setInstrumentationLanguageContainers (containers string ) {
137
+ func (langInsts * languageInstrumentations ) setCommonInstrumentedContainers (ns corev1.Namespace , pod corev1.Pod ) error {
138
+ containersAnnotation := annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectContainerName )
139
+ if err := isValidContainersAnnotation (containersAnnotation ); err != nil {
140
+ return err
141
+ }
142
+
143
+ var containers []string
144
+ if containersAnnotation == "" {
145
+ return nil
146
+ } else {
147
+ containers = strings .Split (containersAnnotation , "," )
148
+ }
149
+
169
150
if langInsts .Java .Instrumentation != nil {
170
151
langInsts .Java .Containers = containers
171
152
}
153
+
172
154
if langInsts .NodeJS .Instrumentation != nil {
173
155
langInsts .NodeJS .Containers = containers
174
156
}
@@ -190,6 +172,35 @@ func (langInsts *languageInstrumentations) setInstrumentationLanguageContainers(
190
172
if langInsts .Sdk .Instrumentation != nil {
191
173
langInsts .Sdk .Containers = containers
192
174
}
175
+ return nil
176
+ }
177
+
178
+ func (langInsts * languageInstrumentations ) setLanguageSpecificContainers (ns metav1.ObjectMeta , pod metav1.ObjectMeta ) error {
179
+ if err := configureLanguageContainers (& langInsts .Java , annotationInjectJavaContainersName , ns , pod ); err != nil {
180
+ return err
181
+ }
182
+ if err := configureLanguageContainers (& langInsts .NodeJS , annotationInjectNodeJSContainersName , ns , pod ); err != nil {
183
+ return err
184
+ }
185
+ if err := configureLanguageContainers (& langInsts .Python , annotationInjectPythonContainersName , ns , pod ); err != nil {
186
+ return err
187
+ }
188
+ if err := configureLanguageContainers (& langInsts .DotNet , annotationInjectDotnetContainersName , ns , pod ); err != nil {
189
+ return err
190
+ }
191
+ if err := configureLanguageContainers (& langInsts .Go , annotationInjectGoContainersName , ns , pod ); err != nil {
192
+ return err
193
+ }
194
+ if err := configureLanguageContainers (& langInsts .ApacheHttpd , annotationInjectNginxContainersName , ns , pod ); err != nil {
195
+ return err
196
+ }
197
+ if err := configureLanguageContainers (& langInsts .Nginx , annotationInjectJavaContainersName , ns , pod ); err != nil {
198
+ return err
199
+ }
200
+ if err := configureLanguageContainers (& langInsts .Sdk , annotationInjectSdkContainersName , ns , pod ); err != nil {
201
+ return err
202
+ }
203
+ return nil
193
204
}
194
205
195
206
var _ podmutation.PodMutator = (* instPodMutator )(nil )
@@ -329,36 +340,24 @@ func (pm *instPodMutator) Mutate(ctx context.Context, ns corev1.Namespace, pod c
329
340
return pod , nil
330
341
}
331
342
343
+ err = insts .setCommonInstrumentedContainers (ns , pod )
344
+ if err != nil {
345
+ return pod , err
346
+ }
347
+
332
348
// We retrieve the annotation for podname
333
349
if pm .config .EnableMultiInstrumentation () {
334
- // We use annotations specific for instrumentation language
335
- insts .Java .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectJavaContainersName )
336
- insts .NodeJS .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectNodeJSContainersName )
337
- insts .Python .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectPythonContainersName )
338
- insts .DotNet .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectDotnetContainersName )
339
- insts .Go .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectGoContainersName )
340
- insts .ApacheHttpd .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectApacheHttpdContainersName )
341
- insts .Nginx .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectNginxContainersName )
342
- insts .Sdk .Containers = annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectSdkContainersName )
350
+ err = insts .setLanguageSpecificContainers (ns .ObjectMeta , pod .ObjectMeta )
351
+ if err != nil {
352
+ return pod , err
353
+ }
343
354
344
355
// We check if provided annotations and instrumentations are valid
345
356
ok , msg := insts .areContainerNamesConfiguredForMultipleInstrumentations ()
346
357
if ! ok {
347
358
logger .V (1 ).Error (msg , "skipping instrumentation injection" )
348
359
return pod , nil
349
360
}
350
- } else {
351
- // We use general annotation for container names
352
- // only when multi instrumentation is disabled
353
- singleInstrEnabled := insts .isSingleInstrumentationEnabled ()
354
- if singleInstrEnabled {
355
- generalContainerNames := annotationValue (ns .ObjectMeta , pod .ObjectMeta , annotationInjectContainerName )
356
- insts .setInstrumentationLanguageContainers (generalContainerNames )
357
- } else {
358
- logger .V (1 ).Error (fmt .Errorf ("multiple injection annotations present" ), "skipping instrumentation injection" )
359
- return pod , nil
360
- }
361
-
362
361
}
363
362
364
363
// once it's been determined that instrumentation is desired, none exists yet, and we know which instance it should talk to,
0 commit comments