Skip to content

Commit

Permalink
Match pod dnsPolicy to hostNetwork config (#691)
Browse files Browse the repository at this point in the history
* feat: match pod dnspolicy to hostNetwork

* feat: add dnspolicy support to deployment and sts

* added tests for hostNetwork DNSpolicy

* use hostnetwork spec in deployment and statefulset
  • Loading branch information
gai6948 authored Feb 25, 2022
1 parent 2777905 commit 4dfffb8
Show file tree
Hide file tree
Showing 7 changed files with 100 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/collector/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem

annotations := Annotations(otelcol)
podAnnotations := PodAnnotations(otelcol)

return appsv1.DaemonSet{
ObjectMeta: metav1.ObjectMeta{
Name: naming.Collector(otelcol),
Expand All @@ -55,6 +54,7 @@ func DaemonSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTelem
Volumes: Volumes(cfg, otelcol),
Tolerations: otelcol.Spec.Tolerations,
HostNetwork: otelcol.Spec.HostNetwork,
DNSPolicy: getDnsPolicy(otelcol),
SecurityContext: otelcol.Spec.PodSecurityContext,
},
},
Expand Down
2 changes: 2 additions & 0 deletions pkg/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func TestDaemonsetHostNetwork(t *testing.T) {
Spec: v1alpha1.OpenTelemetryCollectorSpec{},
})
assert.False(t, d1.Spec.Template.Spec.HostNetwork)
assert.Equal(t, d1.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirst)

// verify custom
d2 := DaemonSet(config.New(), logger, v1alpha1.OpenTelemetryCollector{
Expand All @@ -75,6 +76,7 @@ func TestDaemonsetHostNetwork(t *testing.T) {
},
})
assert.True(t, d2.Spec.Template.Spec.HostNetwork)
assert.Equal(t, d2.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirstWithHostNet)
}

func TestDaemonsetPodAnnotations(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions pkg/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
ServiceAccountName: ServiceAccountName(otelcol),
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
DNSPolicy: getDnsPolicy(otelcol),
HostNetwork: otelcol.Spec.HostNetwork,
Tolerations: otelcol.Spec.Tolerations,
SecurityContext: otelcol.Spec.PodSecurityContext,
},
Expand Down
32 changes: 32 additions & 0 deletions pkg/collector/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,3 +119,35 @@ func TestDeploymenttPodSecurityContext(t *testing.T) {
assert.Equal(t, &runAsUser, d.Spec.Template.Spec.SecurityContext.RunAsUser)
assert.Equal(t, &runasGroup, d.Spec.Template.Spec.SecurityContext.RunAsGroup)
}

func TestDeploymentHostNetwork(t *testing.T) {
// Test default
otelcol_1 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
}

cfg := config.New()

d1 := Deployment(cfg, logger, otelcol_1)

assert.Equal(t, d1.Spec.Template.Spec.HostNetwork, false)
assert.Equal(t, d1.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirst)

// Test hostNetwork=true
otelcol_2 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-hostnetwork",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
HostNetwork: true,
},
}

cfg = config.New()

d2 := Deployment(cfg, logger, otelcol_2)
assert.Equal(t, d2.Spec.Template.Spec.HostNetwork, true)
assert.Equal(t, d2.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirstWithHostNet)
}
2 changes: 2 additions & 0 deletions pkg/collector/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ func StatefulSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTel
ServiceAccountName: ServiceAccountName(otelcol),
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
DNSPolicy: getDnsPolicy(otelcol),
HostNetwork: otelcol.Spec.HostNetwork,
Tolerations: otelcol.Spec.Tolerations,
SecurityContext: otelcol.Spec.PodSecurityContext,
},
Expand Down
32 changes: 32 additions & 0 deletions pkg/collector/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,35 @@ func TestStatefulSetPodSecurityContext(t *testing.T) {
assert.Equal(t, &runAsUser, d.Spec.Template.Spec.SecurityContext.RunAsUser)
assert.Equal(t, &runasGroup, d.Spec.Template.Spec.SecurityContext.RunAsGroup)
}

func TestStatefulSetHostNetwork(t *testing.T) {
// Test default
otelcol_1 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
}

cfg := config.New()

d1 := StatefulSet(cfg, logger, otelcol_1)

assert.Equal(t, d1.Spec.Template.Spec.HostNetwork, false)
assert.Equal(t, d1.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirst)

// Test hostNetwork=true
otelcol_2 := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance-hostnetwork",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
HostNetwork: true,
},
}

cfg = config.New()

d2 := StatefulSet(cfg, logger, otelcol_2)
assert.Equal(t, d2.Spec.Template.Spec.HostNetwork, true)
assert.Equal(t, d2.Spec.Template.Spec.DNSPolicy, v1.DNSClusterFirstWithHostNet)
}
29 changes: 29 additions & 0 deletions pkg/collector/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 collector

import (
corev1 "k8s.io/api/core/v1"

"github.com/open-telemetry/opentelemetry-operator/apis/v1alpha1"
)

func getDnsPolicy(otelcol v1alpha1.OpenTelemetryCollector) corev1.DNSPolicy {
dnsPolicy := corev1.DNSClusterFirst
if otelcol.Spec.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
}
return dnsPolicy
}

0 comments on commit 4dfffb8

Please sign in to comment.