Skip to content

Commit bc6d19c

Browse files
committed
Clean up and add more tests
1 parent 51254f5 commit bc6d19c

File tree

4 files changed

+154
-21
lines changed

4 files changed

+154
-21
lines changed

Diff for: charts/naiserator/Feature.yaml

+5
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ values:
177177
naiserator.observability.otel.enabled:
178178
computed:
179179
template: '{{ne .Kind "onprem"}}'
180+
naiserator.observability.auto-instrumentation.enabled:
181+
config:
182+
type: bool
183+
ignoreKind:
184+
- onprem
180185
naiserator.observability.logging.destinations:
181186
computed:
182187
# Find all logging_*_default_flow environment variables and use for configuration of available log destinations

Diff for: pkg/resourcecreator/observability/observability.go

+36-18
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ func otelAutoInstrumentAnnotations(source Source, otel config.Otel) map[string]s
8686
}
8787
}
8888

89-
func labelsFromCollectorConfig(collector config.OtelCollector) map[string]string {
89+
func netpolLabelsFromCollectorConfig(collector config.OtelCollector) map[string]string {
9090
labels := collector.Labels
9191
labelMap := make(map[string]string, len(labels))
9292
for _, label := range labels {
@@ -99,7 +99,29 @@ func labelsFromCollectorConfig(collector config.OtelCollector) map[string]string
9999
return labelMap
100100
}
101101

102-
func tracingNetpol(source Source, otel config.Otel) (*networkingv1.NetworkPolicy, error) {
102+
func logLabels(obs *nais_io_v1.Observability, cfg config.Logging) (map[string]string, error) {
103+
if !obs.Logging.Enabled {
104+
return map[string]string{logLabelDefault: "false"}, nil
105+
}
106+
107+
labels := map[string]string{}
108+
109+
if len(obs.Logging.Destinations) > 0 {
110+
labels[logLabelDefault] = "false"
111+
112+
for _, destination := range obs.Logging.Destinations {
113+
if !slices.Contains(cfg.Destinations, destination.ID) {
114+
return nil, fmt.Errorf("logging destination %q does not exist in cluster", destination.ID)
115+
}
116+
117+
labels[logLabelPrefix+destination.ID] = "true"
118+
}
119+
}
120+
121+
return labels, nil
122+
}
123+
124+
func otelNetpol(source Source, otel config.Otel) (*networkingv1.NetworkPolicy, error) {
103125
name, err := namegen.ShortName(source.GetName()+"-"+"tracing", validation.DNS1035LabelMaxLength)
104126
if err != nil {
105127
return nil, err
@@ -109,7 +131,7 @@ func tracingNetpol(source Source, otel config.Otel) (*networkingv1.NetworkPolicy
109131
objectMeta.Name = name
110132

111133
protocolTCP := corev1.ProtocolTCP
112-
collectorLabels := labelsFromCollectorConfig(otel.Collector)
134+
collectorLabels := netpolLabelsFromCollectorConfig(otel.Collector)
113135

114136
return &networkingv1.NetworkPolicy{
115137
ObjectMeta: objectMeta,
@@ -166,10 +188,14 @@ func Create(source Source, ast *resource.Ast, config Config) error {
166188

167189
if (obs.Tracing != nil && obs.Tracing.Enabled) || (obs.AutoInstrumentation != nil && obs.AutoInstrumentation.Enabled) {
168190
if !cfg.Otel.Enabled {
169-
return fmt.Errorf("tracing and auto-instrumentation are not supported in this cluster")
191+
return fmt.Errorf("opentelemetry is not supported for this cluster")
170192
}
171193

172-
netpol, err := tracingNetpol(source, cfg.Otel)
194+
if !cfg.Otel.AutoInstrumentation.Enabled && obs.AutoInstrumentation != nil && obs.AutoInstrumentation.Enabled {
195+
return fmt.Errorf("auto-instrumentation is not supported for this cluster")
196+
}
197+
198+
netpol, err := otelNetpol(source, cfg.Otel)
173199
if err != nil {
174200
return err
175201
}
@@ -185,21 +211,13 @@ func Create(source Source, ast *resource.Ast, config Config) error {
185211
}
186212

187213
if obs.Logging != nil {
188-
if !obs.Logging.Enabled {
189-
ast.Labels[logLabelDefault] = "false"
190-
return nil
214+
logLabels, err := logLabels(obs, cfg.Logging)
215+
if err != nil {
216+
return err
191217
}
192218

193-
if len(obs.Logging.Destinations) > 0 {
194-
ast.Labels[logLabelDefault] = "false"
195-
196-
for _, destination := range obs.Logging.Destinations {
197-
if !slices.Contains(cfg.Logging.Destinations, destination.ID) {
198-
return fmt.Errorf("logging destination %q does not exist in cluster", destination.ID)
199-
}
200-
201-
ast.Labels[logLabelPrefix+destination.ID] = "true"
202-
}
219+
for k, v := range logLabels {
220+
ast.Labels[k] = v
203221
}
204222
}
205223

Diff for: pkg/resourcecreator/observability/observability_test.go

+112-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package observability
22

33
import (
4+
"fmt"
45
"testing"
56

67
nais_io_v1 "github.com/nais/liberator/pkg/apis/nais.io/v1"
@@ -141,7 +142,7 @@ func TestLabelsFromCollectorConfig(t *testing.T) {
141142

142143
for _, tc := range testCases {
143144
t.Run(tc.name, func(t *testing.T) {
144-
actualLabels := labelsFromCollectorConfig(tc.collector)
145+
actualLabels := netpolLabelsFromCollectorConfig(tc.collector)
145146
assert.Equal(t, tc.expectedLabels, actualLabels)
146147
})
147148
}
@@ -225,7 +226,116 @@ func TestTracingNetpol(t *testing.T) {
225226
},
226227
}
227228

228-
actualNetworkPolicy, err := tracingNetpol(&app, otel)
229+
actualNetworkPolicy, err := otelNetpol(&app, otel)
229230
assert.NoError(t, err)
230231
assert.Equal(t, expectedNetworkPolicy, actualNetworkPolicy)
231232
}
233+
func TestLogLabels(t *testing.T) {
234+
tests := []struct {
235+
name string
236+
obs *nais_io_v1.Observability
237+
cfg config.Logging
238+
expectedLabels map[string]string
239+
expectedError error
240+
}{
241+
{
242+
name: "Enabled with multiple destinations",
243+
obs: &nais_io_v1.Observability{
244+
Logging: &nais_io_v1.Logging{
245+
Enabled: true,
246+
Destinations: []nais_io_v1.LogDestination{
247+
{
248+
ID: "destination1",
249+
},
250+
{
251+
ID: "destination2",
252+
},
253+
},
254+
},
255+
},
256+
cfg: config.Logging{
257+
Destinations: []string{"destination1", "destination2"},
258+
},
259+
expectedLabels: map[string]string{
260+
"logs.nais.io/flow-default": "false",
261+
"logs.nais.io/flow-destination1": "true",
262+
"logs.nais.io/flow-destination2": "true",
263+
},
264+
expectedError: nil,
265+
},
266+
{
267+
name: "Disabled",
268+
obs: &nais_io_v1.Observability{
269+
Logging: &nais_io_v1.Logging{
270+
Enabled: false,
271+
},
272+
},
273+
cfg: config.Logging{
274+
Destinations: []string{"destination1", "destination2"},
275+
},
276+
expectedLabels: map[string]string{
277+
"logs.nais.io/flow-default": "false",
278+
},
279+
expectedError: nil,
280+
},
281+
{
282+
name: "Invalid destination",
283+
obs: &nais_io_v1.Observability{
284+
Logging: &nais_io_v1.Logging{
285+
Enabled: true,
286+
Destinations: []nais_io_v1.LogDestination{
287+
{
288+
ID: "destination1",
289+
},
290+
},
291+
},
292+
},
293+
cfg: config.Logging{
294+
Destinations: []string{"destination2"},
295+
},
296+
expectedLabels: nil,
297+
expectedError: fmt.Errorf("logging destination %q does not exist in cluster", "destination1"),
298+
},
299+
}
300+
301+
for _, tt := range tests {
302+
t.Run(tt.name, func(t *testing.T) {
303+
actualLabels, err := logLabels(tt.obs, tt.cfg)
304+
305+
assert.Equal(t, tt.expectedError, err)
306+
assert.Equal(t, tt.expectedLabels, actualLabels)
307+
})
308+
}
309+
}
310+
311+
func TestOtelAutoInstrumentAnnotations(t *testing.T) {
312+
app := nais_io_v1alpha1.Application{
313+
ObjectMeta: metav1.ObjectMeta{
314+
Name: "my-app",
315+
Namespace: "my-namespace",
316+
},
317+
Spec: nais_io_v1alpha1.ApplicationSpec{
318+
Observability: &nais_io_v1.Observability{
319+
AutoInstrumentation: &nais_io_v1.AutoInstrumentation{
320+
Enabled: true,
321+
Runtime: "java",
322+
},
323+
},
324+
},
325+
}
326+
327+
otel := config.Otel{
328+
AutoInstrumentation: config.AutoInstrumentation{
329+
AppConfig: "system-namespace/my-config",
330+
},
331+
}
332+
333+
expectedAnnotations := map[string]string{
334+
"instrumentation.opentelemetry.io/inject-java": "system-namespace/my-config",
335+
"instrumentation.opentelemetry.io/container-names": "my-app",
336+
}
337+
338+
actualAnnotations := otelAutoInstrumentAnnotations(&app, otel)
339+
340+
assert.Equal(t, expectedAnnotations, actualAnnotations)
341+
}

Diff for: pkg/resourcecreator/testdata/observability_tracing_enabled_error.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ input:
2121
tracing:
2222
enabled: true
2323

24-
error: "tracing and auto-instrumentation are not supported in this cluster"
24+
error: "opentelemetry is not supported for this cluster"

0 commit comments

Comments
 (0)