|
| 1 | +// Copyright © 2026 Kube logging authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package fluentbit |
| 16 | + |
| 17 | +import ( |
| 18 | + "testing" |
| 19 | + |
| 20 | + "github.com/stretchr/testify/require" |
| 21 | + corev1 "k8s.io/api/core/v1" |
| 22 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 23 | + "k8s.io/apimachinery/pkg/runtime" |
| 24 | + |
| 25 | + "github.com/kube-logging/logging-operator/pkg/sdk/logging/api/v1beta1" |
| 26 | +) |
| 27 | + |
| 28 | +func TestMetricsServicesIPFamilies(t *testing.T) { |
| 29 | + metricsEnabled := true |
| 30 | + tests := []struct { |
| 31 | + name string |
| 32 | + enabledIPv6 bool |
| 33 | + }{ |
| 34 | + {name: "single-stack"}, |
| 35 | + {name: "dual-stack", enabledIPv6: true}, |
| 36 | + } |
| 37 | + |
| 38 | + for _, test := range tests { |
| 39 | + t.Run(test.name, func(t *testing.T) { |
| 40 | + logging := &v1beta1.Logging{ObjectMeta: metav1.ObjectMeta{Name: "test"}} |
| 41 | + r := &Reconciler{ |
| 42 | + Logging: logging, |
| 43 | + fluentbitSpec: &v1beta1.FluentbitSpec{ |
| 44 | + EnabledIPv6: test.enabledIPv6, |
| 45 | + Metrics: &v1beta1.Metrics{ |
| 46 | + Enabled: &metricsEnabled, |
| 47 | + Port: 2020, |
| 48 | + }, |
| 49 | + BufferVolumeMetrics: &v1beta1.Metrics{ |
| 50 | + Enabled: &metricsEnabled, |
| 51 | + Port: 9200, |
| 52 | + }, |
| 53 | + }, |
| 54 | + nameProvider: NewLegacyFluentbitNameProvider(logging), |
| 55 | + } |
| 56 | + |
| 57 | + metricsService, _, err := r.serviceMetrics() |
| 58 | + require.NoError(t, err) |
| 59 | + bufferMetricsService, _, err := r.serviceBufferMetrics() |
| 60 | + require.NoError(t, err) |
| 61 | + |
| 62 | + assertIPFamilies(t, metricsService, test.enabledIPv6) |
| 63 | + assertIPFamilies(t, bufferMetricsService, test.enabledIPv6) |
| 64 | + }) |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +func assertIPFamilies(t *testing.T, object runtime.Object, enabledIPv6 bool) { |
| 69 | + t.Helper() |
| 70 | + |
| 71 | + service, ok := object.(*corev1.Service) |
| 72 | + require.True(t, ok) |
| 73 | + |
| 74 | + if !enabledIPv6 { |
| 75 | + require.Nil(t, service.Spec.IPFamilyPolicy) |
| 76 | + require.Nil(t, service.Spec.IPFamilies) |
| 77 | + return |
| 78 | + } |
| 79 | + |
| 80 | + require.NotNil(t, service.Spec.IPFamilyPolicy) |
| 81 | + require.Equal(t, corev1.IPFamilyPolicyPreferDualStack, *service.Spec.IPFamilyPolicy) |
| 82 | + require.Equal(t, []corev1.IPFamily{corev1.IPv4Protocol, corev1.IPv6Protocol}, service.Spec.IPFamilies) |
| 83 | +} |
0 commit comments