diff --git a/.chloggen/implement-dnsconfig.yaml b/.chloggen/implement-dnsconfig.yaml new file mode 100755 index 0000000000..534238afb7 --- /dev/null +++ b/.chloggen/implement-dnsconfig.yaml @@ -0,0 +1,16 @@ +# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix' +change_type: 'enhancement' + +# The name of the component, or a single word describing the area of concern, (e.g. collector, target allocator, auto-instrumentation, opamp, github action) +component: collector, target allocator, opamp + +# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`). +note: Enabling PodDnsConfig for OpenTelemetry Collector, TargetAllocator and OpAMPBridge. + +# One or more tracking issues related to the change +issues: [2658] + +# (Optional) One or more lines of additional information to render under the primary note. +# These lines will be padded with 2 spaces and then inserted directly into the document. +# Use pipe (|) for multiline entries. +subtext: \ No newline at end of file diff --git a/apis/v1alpha1/opampbridge_types.go b/apis/v1alpha1/opampbridge_types.go index 0479b10198..e8ee0e52e6 100644 --- a/apis/v1alpha1/opampbridge_types.go +++ b/apis/v1alpha1/opampbridge_types.go @@ -105,6 +105,8 @@ type OpAMPBridgeSpec struct { // https://kubernetes.io/docs/concepts/workloads/pods/pod-topology-spread-constraints/ // +optional TopologySpreadConstraints []v1.TopologySpreadConstraint `json:"topologySpreadConstraints,omitempty"` + // PodDNSConfig defines the DNS parameters of a pod in addition to those generated from DNSPolicy. + PodDNSConfig v1.PodDNSConfig `json:"podDnsConfig,omitempty"` } // OpAMPBridgeStatus defines the observed state of OpAMPBridge. diff --git a/apis/v1alpha1/zz_generated.deepcopy.go b/apis/v1alpha1/zz_generated.deepcopy.go index 3918521e75..086fafcfd2 100644 --- a/apis/v1alpha1/zz_generated.deepcopy.go +++ b/apis/v1alpha1/zz_generated.deepcopy.go @@ -680,6 +680,7 @@ func (in *OpAMPBridgeSpec) DeepCopyInto(out *OpAMPBridgeSpec) { (*in)[i].DeepCopyInto(&(*out)[i]) } } + in.PodDNSConfig.DeepCopyInto(&out.PodDNSConfig) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpAMPBridgeSpec. diff --git a/apis/v1beta1/common.go b/apis/v1beta1/common.go index e91b0e6de7..374f5a2a82 100644 --- a/apis/v1beta1/common.go +++ b/apis/v1beta1/common.go @@ -224,6 +224,8 @@ type OpenTelemetryCommonFields struct { // // +optional AdditionalContainers []v1.Container `json:"additionalContainers,omitempty"` + // PodDNSConfig defines the DNS parameters of a pod in addition to those generated from DNSPolicy. + PodDNSConfig v1.PodDNSConfig `json:"podDnsConfig,omitempty"` } type StatefulSetCommonFields struct { diff --git a/apis/v1beta1/zz_generated.deepcopy.go b/apis/v1beta1/zz_generated.deepcopy.go index e5fb8ef086..cff0c978e3 100644 --- a/apis/v1beta1/zz_generated.deepcopy.go +++ b/apis/v1beta1/zz_generated.deepcopy.go @@ -476,6 +476,7 @@ func (in *OpenTelemetryCommonFields) DeepCopyInto(out *OpenTelemetryCommonFields (*in)[i].DeepCopyInto(&(*out)[i]) } } + in.PodDNSConfig.DeepCopyInto(&out.PodDNSConfig) } // DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new OpenTelemetryCommonFields. diff --git a/bundle/manifests/opentelemetry.io_opampbridges.yaml b/bundle/manifests/opentelemetry.io_opampbridges.yaml index b9fdbf81db..6e7a42fd34 100644 --- a/bundle/manifests/opentelemetry.io_opampbridges.yaml +++ b/bundle/manifests/opentelemetry.io_opampbridges.yaml @@ -600,6 +600,29 @@ spec: additionalProperties: type: string type: object + podDnsConfig: + properties: + nameservers: + items: + type: string + type: array + x-kubernetes-list-type: atomic + options: + items: + properties: + name: + type: string + value: + type: string + type: object + type: array + x-kubernetes-list-type: atomic + searches: + items: + type: string + type: array + x-kubernetes-list-type: atomic + type: object podSecurityContext: properties: appArmorProfile: diff --git a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml index 8696e0ee60..af08b8e91c 100644 --- a/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml +++ b/bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml @@ -6928,6 +6928,29 @@ spec: - type: string x-kubernetes-int-or-string: true type: object + podDnsConfig: + properties: + nameservers: + items: + type: string + type: array + x-kubernetes-list-type: atomic + options: + items: + properties: + name: + type: string + value: + type: string + type: object + type: array + x-kubernetes-list-type: atomic + searches: + items: + type: string + type: array + x-kubernetes-list-type: atomic + type: object podSecurityContext: properties: appArmorProfile: diff --git a/config/crd/bases/opentelemetry.io_opampbridges.yaml b/config/crd/bases/opentelemetry.io_opampbridges.yaml index c554b2b101..181f2f2d1c 100644 --- a/config/crd/bases/opentelemetry.io_opampbridges.yaml +++ b/config/crd/bases/opentelemetry.io_opampbridges.yaml @@ -597,6 +597,29 @@ spec: additionalProperties: type: string type: object + podDnsConfig: + properties: + nameservers: + items: + type: string + type: array + x-kubernetes-list-type: atomic + options: + items: + properties: + name: + type: string + value: + type: string + type: object + type: array + x-kubernetes-list-type: atomic + searches: + items: + type: string + type: array + x-kubernetes-list-type: atomic + type: object podSecurityContext: properties: appArmorProfile: diff --git a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml index ac1c099f14..445cc211e5 100644 --- a/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml +++ b/config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml @@ -6914,6 +6914,29 @@ spec: - type: string x-kubernetes-int-or-string: true type: object + podDnsConfig: + properties: + nameservers: + items: + type: string + type: array + x-kubernetes-list-type: atomic + options: + items: + properties: + name: + type: string + value: + type: string + type: object + type: array + x-kubernetes-list-type: atomic + searches: + items: + type: string + type: array + x-kubernetes-list-type: atomic + type: object podSecurityContext: properties: appArmorProfile: diff --git a/config/crd/bases/opentelemetry.io_targetallocators.yaml b/config/crd/bases/opentelemetry.io_targetallocators.yaml index b25aa1a1fc..27e6c74678 100644 --- a/config/crd/bases/opentelemetry.io_targetallocators.yaml +++ b/config/crd/bases/opentelemetry.io_targetallocators.yaml @@ -2089,6 +2089,29 @@ spec: - type: string x-kubernetes-int-or-string: true type: object + podDnsConfig: + properties: + nameservers: + items: + type: string + type: array + x-kubernetes-list-type: atomic + options: + items: + properties: + name: + type: string + value: + type: string + type: object + type: array + x-kubernetes-list-type: atomic + searches: + items: + type: string + type: array + x-kubernetes-list-type: atomic + type: object podSecurityContext: properties: appArmorProfile: diff --git a/controllers/builder_test.go b/controllers/builder_test.go index 8870910990..12304494bc 100644 --- a/controllers/builder_test.go +++ b/controllers/builder_test.go @@ -221,6 +221,7 @@ service: }, ShareProcessNamespace: ptr.To(false), DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ServiceAccountName: "test-collector", }, }, @@ -471,6 +472,7 @@ service: }, ShareProcessNamespace: ptr.To(false), DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ServiceAccountName: "test-collector", }, }, @@ -757,6 +759,7 @@ service: }, ShareProcessNamespace: ptr.To(false), DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ServiceAccountName: "my-special-sa", }, }, @@ -1005,6 +1008,7 @@ func TestBuildAll_OpAMPBridge(t *testing.T) { }, }, DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ServiceAccountName: "test-opamp-bridge", }, }, @@ -1285,6 +1289,7 @@ service: }, ShareProcessNamespace: ptr.To(false), DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ServiceAccountName: "test-collector", }, }, @@ -1496,6 +1501,7 @@ prometheus_cr: }, }, DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ShareProcessNamespace: ptr.To(false), ServiceAccountName: "test-targetallocator", }, @@ -1679,6 +1685,7 @@ prometheus_cr: }, ShareProcessNamespace: ptr.To(false), DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ServiceAccountName: "test-collector", }, }, @@ -1890,6 +1897,7 @@ prometheus_cr: }, }, DNSPolicy: "ClusterFirst", + DNSConfig: &corev1.PodDNSConfig{}, ShareProcessNamespace: ptr.To(false), ServiceAccountName: "test-targetallocator", }, diff --git a/docs/api.md b/docs/api.md index 462ecb519b..ab8e1a0b02 100644 --- a/docs/api.md +++ b/docs/api.md @@ -4198,6 +4198,13 @@ typically used to set access tokens or other authorization headers.
OpAMPBridge pods.
false + + podDnsConfig + object + + PodDNSConfig defines the DNS parameters of a pod in addition to those generated from DNSPolicy.
+ + false podSecurityContext object @@ -6325,6 +6332,88 @@ TODO: Drop `kubebuilder:default` when controller-gen doesn't need it https://git +### OpAMPBridge.spec.podDnsConfig +[↩ Parent](#opampbridgespec) + + + +PodDNSConfig defines the DNS parameters of a pod in addition to those generated from DNSPolicy. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
nameservers[]string + A list of DNS name server IP addresses. +This will be appended to the base nameservers generated from DNSPolicy. +Duplicated nameservers will be removed.
+
false
options[]object + A list of DNS resolver options. +This will be merged with the base options generated from DNSPolicy. +Duplicated entries will be removed. Resolution options given in Options +will override those that appear in the base DNSPolicy.
+
false
searches[]string + A list of DNS search domains for host-name lookup. +This will be appended to the base search paths generated from DNSPolicy. +Duplicated search paths will be removed.
+
false
+ + +### OpAMPBridge.spec.podDnsConfig.options[index] +[↩ Parent](#opampbridgespecpoddnsconfig) + + + +PodDNSConfigOption defines DNS resolver options of a pod. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
namestring + Required.
+
false
valuestring +
+
false
+ + ### OpAMPBridge.spec.podSecurityContext [↩ Parent](#opampbridgespec) @@ -30913,6 +31002,13 @@ the generated pods.
for the generated workload.
false + + podDnsConfig + object + + PodDNSConfig defines the DNS parameters of a pod in addition to those generated from DNSPolicy.
+ + false podSecurityContext object @@ -40722,6 +40818,88 @@ evictions by specifying "100%".
+### OpenTelemetryCollector.spec.podDnsConfig +[↩ Parent](#opentelemetrycollectorspec-1) + + + +PodDNSConfig defines the DNS parameters of a pod in addition to those generated from DNSPolicy. + + + + + + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
nameservers[]string + A list of DNS name server IP addresses. +This will be appended to the base nameservers generated from DNSPolicy. +Duplicated nameservers will be removed.
+
false
options[]object + A list of DNS resolver options. +This will be merged with the base options generated from DNSPolicy. +Duplicated entries will be removed. Resolution options given in Options +will override those that appear in the base DNSPolicy.
+
false
searches[]string + A list of DNS search domains for host-name lookup. +This will be appended to the base search paths generated from DNSPolicy. +Duplicated search paths will be removed.
+
false
+ + +### OpenTelemetryCollector.spec.podDnsConfig.options[index] +[↩ Parent](#opentelemetrycollectorspecpoddnsconfig) + + + +PodDNSConfigOption defines DNS resolver options of a pod. + + + + + + + + + + + + + + + + + + + + + +
NameTypeDescriptionRequired
namestring + Required.
+
false
valuestring +
+
false
+ + ### OpenTelemetryCollector.spec.podSecurityContext [↩ Parent](#opentelemetrycollectorspec-1) diff --git a/internal/manifests/collector/daemonset.go b/internal/manifests/collector/daemonset.go index 4aaffa30b3..919c73151a 100644 --- a/internal/manifests/collector/daemonset.go +++ b/internal/manifests/collector/daemonset.go @@ -64,7 +64,8 @@ func DaemonSet(params manifests.Params) (*appsv1.DaemonSet, error) { NodeSelector: params.OtelCol.Spec.NodeSelector, HostNetwork: params.OtelCol.Spec.HostNetwork, ShareProcessNamespace: ¶ms.OtelCol.Spec.ShareProcessNamespace, - DNSPolicy: manifestutils.GetDNSPolicy(params.OtelCol.Spec.HostNetwork), + DNSPolicy: manifestutils.GetDNSPolicy(params.OtelCol.Spec.HostNetwork, params.OtelCol.Spec.PodDNSConfig), + DNSConfig: ¶ms.OtelCol.Spec.PodDNSConfig, SecurityContext: params.OtelCol.Spec.PodSecurityContext, PriorityClassName: params.OtelCol.Spec.PriorityClassName, Affinity: params.OtelCol.Spec.Affinity, diff --git a/internal/manifests/collector/daemonset_test.go b/internal/manifests/collector/daemonset_test.go index 778c3791f6..82e42c739c 100644 --- a/internal/manifests/collector/daemonset_test.go +++ b/internal/manifests/collector/daemonset_test.go @@ -591,3 +591,35 @@ func TestDaemonsetShareProcessNamespace(t *testing.T) { require.NoError(t, err) assert.True(t, *d2.Spec.Template.Spec.ShareProcessNamespace) } + +func TestDaemonSetDNSConfig(t *testing.T) { + // prepare + otelcol := v1beta1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "my-namespace", + }, + Spec: v1beta1.OpenTelemetryCollectorSpec{ + OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{ + PodDNSConfig: v1.PodDNSConfig{ + Nameservers: []string{"8.8.8.8"}, + Searches: []string{"my.dns.search.suffix"}, + }, + }, + }, + } + cfg := config.New() + + params := manifests.Params{ + Config: cfg, + OtelCol: otelcol, + Log: logger, + } + + // test + d, err := DaemonSet(params) + require.NoError(t, err) + assert.Equal(t, "my-instance-collector", d.Name) + assert.Equal(t, v1.DNSPolicy("None"), d.Spec.Template.Spec.DNSPolicy) + assert.Equal(t, d.Spec.Template.Spec.DNSConfig.Nameservers, []string{"8.8.8.8"}) +} diff --git a/internal/manifests/collector/deployment.go b/internal/manifests/collector/deployment.go index 3a7e72fa15..339d10c551 100644 --- a/internal/manifests/collector/deployment.go +++ b/internal/manifests/collector/deployment.go @@ -61,7 +61,8 @@ func Deployment(params manifests.Params) (*appsv1.Deployment, error) { InitContainers: params.OtelCol.Spec.InitContainers, Containers: append(params.OtelCol.Spec.AdditionalContainers, Container(params.Config, params.Log, params.OtelCol, true)), Volumes: Volumes(params.Config, params.OtelCol), - DNSPolicy: manifestutils.GetDNSPolicy(params.OtelCol.Spec.HostNetwork), + DNSPolicy: manifestutils.GetDNSPolicy(params.OtelCol.Spec.HostNetwork, params.OtelCol.Spec.PodDNSConfig), + DNSConfig: ¶ms.OtelCol.Spec.PodDNSConfig, HostNetwork: params.OtelCol.Spec.HostNetwork, ShareProcessNamespace: ¶ms.OtelCol.Spec.ShareProcessNamespace, Tolerations: params.OtelCol.Spec.Tolerations, diff --git a/internal/manifests/collector/deployment_test.go b/internal/manifests/collector/deployment_test.go index 0523a214b6..e4922913a6 100644 --- a/internal/manifests/collector/deployment_test.go +++ b/internal/manifests/collector/deployment_test.go @@ -705,3 +705,35 @@ func TestDeploymentShareProcessNamespace(t *testing.T) { require.NoError(t, err) assert.True(t, *d2.Spec.Template.Spec.ShareProcessNamespace) } + +func TestDeploymentDNSConfig(t *testing.T) { + // prepare + otelcol := v1beta1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "my-namespace", + }, + Spec: v1beta1.OpenTelemetryCollectorSpec{ + OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{ + PodDNSConfig: v1.PodDNSConfig{ + Nameservers: []string{"8.8.8.8"}, + Searches: []string{"my.dns.search.suffix"}, + }, + }, + }, + } + cfg := config.New() + + params := manifests.Params{ + Config: cfg, + OtelCol: otelcol, + Log: logger, + } + + // test + d, err := Deployment(params) + require.NoError(t, err) + assert.Equal(t, "my-instance-collector", d.Name) + assert.Equal(t, v1.DNSPolicy("None"), d.Spec.Template.Spec.DNSPolicy) + assert.Equal(t, d.Spec.Template.Spec.DNSConfig.Nameservers, []string{"8.8.8.8"}) +} diff --git a/internal/manifests/collector/statefulset.go b/internal/manifests/collector/statefulset.go index bd0ecae892..6b7e92ec05 100644 --- a/internal/manifests/collector/statefulset.go +++ b/internal/manifests/collector/statefulset.go @@ -61,7 +61,8 @@ func StatefulSet(params manifests.Params) (*appsv1.StatefulSet, error) { InitContainers: params.OtelCol.Spec.InitContainers, Containers: append(params.OtelCol.Spec.AdditionalContainers, Container(params.Config, params.Log, params.OtelCol, true)), Volumes: Volumes(params.Config, params.OtelCol), - DNSPolicy: manifestutils.GetDNSPolicy(params.OtelCol.Spec.HostNetwork), + DNSPolicy: manifestutils.GetDNSPolicy(params.OtelCol.Spec.HostNetwork, params.OtelCol.Spec.PodDNSConfig), + DNSConfig: ¶ms.OtelCol.Spec.PodDNSConfig, HostNetwork: params.OtelCol.Spec.HostNetwork, ShareProcessNamespace: ¶ms.OtelCol.Spec.ShareProcessNamespace, Tolerations: params.OtelCol.Spec.Tolerations, diff --git a/internal/manifests/collector/statefulset_test.go b/internal/manifests/collector/statefulset_test.go index b59cf167c0..69ad9b1649 100644 --- a/internal/manifests/collector/statefulset_test.go +++ b/internal/manifests/collector/statefulset_test.go @@ -670,3 +670,35 @@ func TestStatefulSetShareProcessNamespace(t *testing.T) { require.NoError(t, err) assert.True(t, *d2.Spec.Template.Spec.ShareProcessNamespace) } + +func TestStatefulSetDNSConfig(t *testing.T) { + // prepare + otelcol := v1beta1.OpenTelemetryCollector{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "my-namespace", + }, + Spec: v1beta1.OpenTelemetryCollectorSpec{ + OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{ + PodDNSConfig: v1.PodDNSConfig{ + Nameservers: []string{"8.8.8.8"}, + Searches: []string{"my.dns.search.suffix"}, + }, + }, + }, + } + cfg := config.New() + + params := manifests.Params{ + Config: cfg, + OtelCol: otelcol, + Log: logger, + } + + // test + d, err := StatefulSet(params) + require.NoError(t, err) + assert.Equal(t, "my-instance-collector", d.Name) + assert.Equal(t, v1.DNSPolicy("None"), d.Spec.Template.Spec.DNSPolicy) + assert.Equal(t, d.Spec.Template.Spec.DNSConfig.Nameservers, []string{"8.8.8.8"}) +} diff --git a/internal/manifests/manifestutils/dns.go b/internal/manifests/manifestutils/dns.go index b9b038f315..bdf75c78e5 100644 --- a/internal/manifests/manifestutils/dns.go +++ b/internal/manifests/manifestutils/dns.go @@ -15,14 +15,22 @@ package manifestutils import ( + "errors" + corev1 "k8s.io/api/core/v1" ) +var ErrorDNSPolicy = errors.New("when a dnsPolicy is set to None, the dnsConfig field has to be specified") + // Get the Pod DNS Policy depending on whether we're using a host network. -func GetDNSPolicy(hostNetwork bool) corev1.DNSPolicy { +func GetDNSPolicy(hostNetwork bool, dnsConfig corev1.PodDNSConfig) corev1.DNSPolicy { dnsPolicy := corev1.DNSClusterFirst if hostNetwork { dnsPolicy = corev1.DNSClusterFirstWithHostNet } + // If local DNS configuration is set, takes precedence of hostNetwork. + if dnsConfig.Nameservers != nil { + dnsPolicy = corev1.DNSNone + } return dnsPolicy } diff --git a/internal/manifests/opampbridge/deployment.go b/internal/manifests/opampbridge/deployment.go index 9b146c83eb..02c0e5c221 100644 --- a/internal/manifests/opampbridge/deployment.go +++ b/internal/manifests/opampbridge/deployment.go @@ -55,7 +55,8 @@ func Deployment(params manifests.Params) *appsv1.Deployment { ServiceAccountName: ServiceAccountName(params.OpAMPBridge), Containers: []corev1.Container{Container(params.Config, params.Log, params.OpAMPBridge)}, Volumes: Volumes(params.Config, params.OpAMPBridge), - DNSPolicy: getDNSPolicy(params.OpAMPBridge), + DNSPolicy: manifestutils.GetDNSPolicy(params.OpAMPBridge.Spec.HostNetwork, params.OpAMPBridge.Spec.PodDNSConfig), + DNSConfig: ¶ms.OpAMPBridge.Spec.PodDNSConfig, HostNetwork: params.OpAMPBridge.Spec.HostNetwork, Tolerations: params.OpAMPBridge.Spec.Tolerations, NodeSelector: params.OpAMPBridge.Spec.NodeSelector, diff --git a/internal/manifests/opampbridge/deployment_test.go b/internal/manifests/opampbridge/deployment_test.go index 77af7843b0..35b6e20b40 100644 --- a/internal/manifests/opampbridge/deployment_test.go +++ b/internal/manifests/opampbridge/deployment_test.go @@ -453,3 +453,33 @@ func TestDeploymentTopologySpreadConstraints(t *testing.T) { assert.NotEmpty(t, d2.Spec.Template.Spec.TopologySpreadConstraints) assert.Equal(t, testTopologySpreadConstraintValue, d2.Spec.Template.Spec.TopologySpreadConstraints) } + +func TestDeploymentDNSConfig(t *testing.T) { + // prepare + opAmpBridge := v1alpha1.OpAMPBridge{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "my-namespace", + }, + Spec: v1alpha1.OpAMPBridgeSpec{ + PodDNSConfig: v1.PodDNSConfig{ + Nameservers: []string{"8.8.8.8"}, + Searches: []string{"my.dns.search.suffix"}, + }, + }, + } + + cfg := config.New() + + params := manifests.Params{ + Config: cfg, + OpAMPBridge: opAmpBridge, + Log: logger, + } + + // test + d := Deployment(params) + assert.Equal(t, "my-instance-opamp-bridge", d.Name) + assert.Equal(t, v1.DNSPolicy("None"), d.Spec.Template.Spec.DNSPolicy) + assert.Equal(t, d.Spec.Template.Spec.DNSConfig.Nameservers, []string{"8.8.8.8"}) +} diff --git a/internal/manifests/opampbridge/utils.go b/internal/manifests/opampbridge/utils.go deleted file mode 100644 index 2400e0101a..0000000000 --- a/internal/manifests/opampbridge/utils.go +++ /dev/null @@ -1,29 +0,0 @@ -// Copyright The OpenTelemetry Authors -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -package opampbridge - -import ( - corev1 "k8s.io/api/core/v1" - - "github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1" -) - -func getDNSPolicy(opampBridge v1alpha1.OpAMPBridge) corev1.DNSPolicy { - dnsPolicy := corev1.DNSClusterFirst - if opampBridge.Spec.HostNetwork { - dnsPolicy = corev1.DNSClusterFirstWithHostNet - } - return dnsPolicy -} diff --git a/internal/manifests/targetallocator/deployment.go b/internal/manifests/targetallocator/deployment.go index 97ac6ae8c6..ab8c57c340 100644 --- a/internal/manifests/targetallocator/deployment.go +++ b/internal/manifests/targetallocator/deployment.go @@ -56,7 +56,8 @@ func Deployment(params Params) (*appsv1.Deployment, error) { InitContainers: params.TargetAllocator.Spec.InitContainers, Containers: append(params.TargetAllocator.Spec.AdditionalContainers, Container(params.Config, params.Log, params.TargetAllocator)), Volumes: Volumes(params.Config, params.TargetAllocator), - DNSPolicy: manifestutils.GetDNSPolicy(params.TargetAllocator.Spec.HostNetwork), + DNSPolicy: manifestutils.GetDNSPolicy(params.TargetAllocator.Spec.HostNetwork, params.TargetAllocator.Spec.PodDNSConfig), + DNSConfig: ¶ms.TargetAllocator.Spec.PodDNSConfig, HostNetwork: params.TargetAllocator.Spec.HostNetwork, ShareProcessNamespace: ¶ms.TargetAllocator.Spec.ShareProcessNamespace, Tolerations: params.TargetAllocator.Spec.Tolerations, diff --git a/internal/manifests/targetallocator/deployment_test.go b/internal/manifests/targetallocator/deployment_test.go index 49a85e9323..cc0d59d906 100644 --- a/internal/manifests/targetallocator/deployment_test.go +++ b/internal/manifests/targetallocator/deployment_test.go @@ -532,3 +532,36 @@ func TestDeploymentTerminationGracePeriodSeconds(t *testing.T) { require.NoError(t, err) assert.Equal(t, gracePeriod, *d2.Spec.Template.Spec.TerminationGracePeriodSeconds) } + +func TestDeploymentDNSConfig(t *testing.T) { + // Test default + otelcol := collectorInstance() + // prepare + targetAllocator := v1alpha1.TargetAllocator{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-instance", + Namespace: "my-namespace", + }, + Spec: v1alpha1.TargetAllocatorSpec{ + OpenTelemetryCommonFields: v1beta1.OpenTelemetryCommonFields{ + PodDNSConfig: v1.PodDNSConfig{ + Nameservers: []string{"8.8.8.8"}, + Searches: []string{"my.dns.search.suffix"}, + }, + }, + }, + } + params := Params{ + Collector: otelcol, + TargetAllocator: targetAllocator, + Config: config.New(), + Log: logger, + } + + // test + d, err := Deployment(params) + require.NoError(t, err) + assert.Equal(t, "my-instance-targetallocator", d.Name) + assert.Equal(t, v1.DNSPolicy("None"), d.Spec.Template.Spec.DNSPolicy) + assert.Equal(t, d.Spec.Template.Spec.DNSConfig.Nameservers, []string{"8.8.8.8"}) +} diff --git a/tests/e2e/smoke-dns-config/00-assert.yaml b/tests/e2e/smoke-dns-config/00-assert.yaml new file mode 100644 index 0000000000..b51caf645e --- /dev/null +++ b/tests/e2e/smoke-dns-config/00-assert.yaml @@ -0,0 +1,14 @@ +apiVersion: apps/v1 +kind: Deployment +metadata: + name: poddnsconfig-collector +spec: + template: + spec: + dnsConfig: + nameservers: + - 8.8.8.8 + searches: + - my.dns.search.suffix +status: + readyReplicas: 1 diff --git a/tests/e2e/smoke-dns-config/00-install.yaml b/tests/e2e/smoke-dns-config/00-install.yaml new file mode 100644 index 0000000000..724ac558a6 --- /dev/null +++ b/tests/e2e/smoke-dns-config/00-install.yaml @@ -0,0 +1,28 @@ +apiVersion: opentelemetry.io/v1beta1 +kind: OpenTelemetryCollector +metadata: + name: poddnsconfig +spec: + mode: "deployment" + podDnsConfig: + nameservers: + - 8.8.8.8 + searches: + - my.dns.search.suffix + config: + receivers: + otlp: + protocols: + grpc: + http: + processors: + batch: + timeout: 10s + exporters: + logging: + service: + pipelines: + traces: + receivers: [otlp] + processors: [batch] + exporters: [logging] diff --git a/tests/e2e/smoke-dns-config/chainsaw-test.yaml b/tests/e2e/smoke-dns-config/chainsaw-test.yaml new file mode 100644 index 0000000000..036896d055 --- /dev/null +++ b/tests/e2e/smoke-dns-config/chainsaw-test.yaml @@ -0,0 +1,13 @@ +apiVersion: chainsaw.kyverno.io/v1alpha1 +kind: Test +metadata: + creationTimestamp: null + name: smoke-pod-dns-config +spec: + steps: + - name: step-00 + try: + - apply: + file: 00-install.yaml + - assert: + file: 00-assert.yaml