Skip to content

Commit

Permalink
Add support for podSecurityContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ekarlso committed Oct 29, 2021
1 parent 1b702c7 commit 921b83b
Show file tree
Hide file tree
Showing 10 changed files with 1,359 additions and 1,142 deletions.
2 changes: 2 additions & 0 deletions api/collector/v1alpha1/opentelemetrycollector_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ type OpenTelemetryCollectorSpec struct {
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
SecurityContext *v1.SecurityContext `json:"securityContext,omitempty"`

PodSecurityContext *v1.PodSecurityContext `json:"podSecurityContext,omitempty"`

// HostNetwork indicates if the pod should run in the host networking namespace.
// +optional
// +operator-sdk:gen-csv:customresourcedefinitions.specDescriptors=true
Expand Down
5 changes: 5 additions & 0 deletions api/collector/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

665 changes: 664 additions & 1 deletion bundle/manifests/opentelemetry.io_opentelemetrycollectors.yaml

Large diffs are not rendered by default.

1,743 changes: 602 additions & 1,141 deletions config/crd/bases/opentelemetry.io_opentelemetrycollectors.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/collector/daemonset.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,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,
SecurityContext: otelcol.Spec.PodSecurityContext,
},
},
},
Expand Down
28 changes: 28 additions & 0 deletions pkg/collector/daemonset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"testing"

"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/open-telemetry/opentelemetry-operator/api/collector/v1alpha1"
Expand Down Expand Up @@ -93,3 +94,30 @@ func TestDaemonsetPodAnnotations(t *testing.T) {
assert.Equal(t, "my-instance-collector", ds.Name)
assert.Equal(t, testPodAnnotationValues, ds.Spec.Template.Annotations)
}

func TestDaemonstPodSecurityContext(t *testing.T) {
runAsNonRoot := true
runAsUser := int64(1337)
runasGroup := int64(1338)

otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
PodSecurityContext: &v1.PodSecurityContext{
RunAsNonRoot: &runAsNonRoot,
RunAsUser: &runAsUser,
RunAsGroup: &runasGroup,
},
},
}

cfg := config.New()

d := DaemonSet(cfg, logger, otelcol)

assert.Equal(t, &runAsNonRoot, d.Spec.Template.Spec.SecurityContext.RunAsNonRoot)
assert.Equal(t, &runAsUser, d.Spec.Template.Spec.SecurityContext.RunAsUser)
assert.Equal(t, &runasGroup, d.Spec.Template.Spec.SecurityContext.RunAsGroup)
}
1 change: 1 addition & 0 deletions pkg/collector/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func Deployment(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTele
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
Tolerations: otelcol.Spec.Tolerations,
SecurityContext: otelcol.Spec.PodSecurityContext,
},
},
},
Expand Down
27 changes: 27 additions & 0 deletions pkg/collector/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,30 @@ func TestDeploymentPodAnnotations(t *testing.T) {
assert.Equal(t, "my-instance-collector", d.Name)
assert.Equal(t, testPodAnnotationValues, d.Spec.Template.Annotations)
}

func TestDeploymenttPodSecurityContext(t *testing.T) {
runAsNonRoot := true
runAsUser := int64(1337)
runasGroup := int64(1338)

otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
PodSecurityContext: &v1.PodSecurityContext{
RunAsNonRoot: &runAsNonRoot,
RunAsUser: &runAsUser,
RunAsGroup: &runasGroup,
},
},
}

cfg := config.New()

d := Deployment(cfg, logger, otelcol)

assert.Equal(t, &runAsNonRoot, d.Spec.Template.Spec.SecurityContext.RunAsNonRoot)
assert.Equal(t, &runAsUser, d.Spec.Template.Spec.SecurityContext.RunAsUser)
assert.Equal(t, &runasGroup, d.Spec.Template.Spec.SecurityContext.RunAsGroup)
}
1 change: 1 addition & 0 deletions pkg/collector/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ func StatefulSet(cfg config.Config, logger logr.Logger, otelcol v1alpha1.OpenTel
Containers: []corev1.Container{Container(cfg, logger, otelcol)},
Volumes: Volumes(cfg, otelcol),
Tolerations: otelcol.Spec.Tolerations,
SecurityContext: otelcol.Spec.PodSecurityContext,
},
},
Replicas: otelcol.Spec.Replicas,
Expand Down
28 changes: 28 additions & 0 deletions pkg/collector/statefulset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"github.com/stretchr/testify/assert"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

Expand Down Expand Up @@ -144,3 +145,30 @@ func TestStatefulSetPodAnnotations(t *testing.T) {
assert.Equal(t, "my-instance-collector", ss.Name)
assert.Equal(t, testPodAnnotationValues, ss.Spec.Template.Annotations)
}

func TestStatefulSetPodSecurityContext(t *testing.T) {
runAsNonRoot := true
runAsUser := int64(1337)
runasGroup := int64(1338)

otelcol := v1alpha1.OpenTelemetryCollector{
ObjectMeta: metav1.ObjectMeta{
Name: "my-instance",
},
Spec: v1alpha1.OpenTelemetryCollectorSpec{
PodSecurityContext: &v1.PodSecurityContext{
RunAsNonRoot: &runAsNonRoot,
RunAsUser: &runAsUser,
RunAsGroup: &runasGroup,
},
},
}

cfg := config.New()

d := StatefulSet(cfg, logger, otelcol)

assert.Equal(t, &runAsNonRoot, d.Spec.Template.Spec.SecurityContext.RunAsNonRoot)
assert.Equal(t, &runAsUser, d.Spec.Template.Spec.SecurityContext.RunAsUser)
assert.Equal(t, &runasGroup, d.Spec.Template.Spec.SecurityContext.RunAsGroup)
}

0 comments on commit 921b83b

Please sign in to comment.