Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Match pod dnsPolicy to hostNetwork config #691

Merged
merged 4 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found this section on that page:

Note: "Default" is not the default DNS policy. If dnsPolicy is not explicitly specified, then "ClusterFirst" is used.

if otelcol.Spec.HostNetwork {
dnsPolicy = corev1.DNSClusterFirstWithHostNet
}
return dnsPolicy
}