Skip to content

Commit

Permalink
Add support for securityContext and serviceAccount (#456)
Browse files Browse the repository at this point in the history
Add support for securityContext and serviceAccount

Signed-off-by: mwieczorek <wieczorek-michal@wp.pl>
  • Loading branch information
mwieczorek authored and jpkrohling committed Jun 5, 2019
1 parent df59446 commit 4005a6b
Show file tree
Hide file tree
Showing 13 changed files with 137 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -514,6 +514,10 @@ The types of configuration supported include:

* link:https://kubernetes.io/docs/concepts/storage/volumes/[volumes] and volume mounts

* link:https://kubernetes.io/docs/tasks/configure-pod-container/configure-service-account/[serviceAccount] to run each component with separate identity

* link:https://kubernetes.io/docs/tasks/configure-pod-container/security-context/[securityContext] to define privileges of running components

[source,yaml]
----
apiVersion: jaegertracing.io/v1
Expand Down Expand Up @@ -563,6 +567,9 @@ spec:
operator: "Equal"
value: "value1"
effect: "NoExecute"
serviceAccount: nameOfServiceAccount
securityContext:
runAsUser: 1000
volumeMounts:
- name: config-vol
mountPath: /etc/config
Expand Down
21 changes: 21 additions & 0 deletions pkg/account/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package account

// Component represents type of jaeger component
type Component string

const (
// CollectorComponent represents the value for the Component type for Jaeger Collector
CollectorComponent Component = "collector"

// QueryComponent represents the value for the Component type for Jaeger Query
QueryComponent Component = "query"

// IngesterComponent represents the value for the Component type for Jaeger Ingester
IngesterComponent Component = "ingester"

// AllInOneComponent represents the value for the Component type for Jaeger All-In-One
AllInOneComponent Component = "all-in-one"

// AgentComponent epresents the value for the Component type for Jaeger Agent
AgentComponent Component = "agent"
)
28 changes: 22 additions & 6 deletions pkg/account/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package account

import (
"fmt"

corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/util"
)

// Get returns all the service accounts to be created for this Jaeger instance
Expand All @@ -22,11 +21,11 @@ func getMain(jaeger *v1.Jaeger) *corev1.ServiceAccount {
trueVar := true
return &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: JaegerServiceAccountFor(jaeger),
Name: JaegerServiceAccountFor(jaeger, ""),
Namespace: jaeger.Namespace,
Labels: map[string]string{
"app": "jaeger",
"app.kubernetes.io/name": JaegerServiceAccountFor(jaeger),
"app.kubernetes.io/name": JaegerServiceAccountFor(jaeger, ""),
"app.kubernetes.io/instance": jaeger.Name,
"app.kubernetes.io/component": "service-account",
"app.kubernetes.io/part-of": "jaeger",
Expand All @@ -46,6 +45,23 @@ func getMain(jaeger *v1.Jaeger) *corev1.ServiceAccount {
}

// JaegerServiceAccountFor prints service name for Jaeger instance
func JaegerServiceAccountFor(jaeger *v1.Jaeger) string {
return fmt.Sprintf("%s", jaeger.Name)
func JaegerServiceAccountFor(jaeger *v1.Jaeger, component Component) string {
sa := ""
switch component {
case CollectorComponent:
sa = util.Merge([]v1.JaegerCommonSpec{jaeger.Spec.Collector.JaegerCommonSpec, jaeger.Spec.JaegerCommonSpec}).ServiceAccount
case QueryComponent:
sa = util.Merge([]v1.JaegerCommonSpec{jaeger.Spec.Query.JaegerCommonSpec, jaeger.Spec.JaegerCommonSpec}).ServiceAccount
case IngesterComponent:
sa = util.Merge([]v1.JaegerCommonSpec{jaeger.Spec.Ingester.JaegerCommonSpec, jaeger.Spec.JaegerCommonSpec}).ServiceAccount
case AllInOneComponent:
sa = util.Merge([]v1.JaegerCommonSpec{jaeger.Spec.AllInOne.JaegerCommonSpec, jaeger.Spec.JaegerCommonSpec}).ServiceAccount
case AgentComponent:
sa = util.Merge([]v1.JaegerCommonSpec{jaeger.Spec.Agent.JaegerCommonSpec, jaeger.Spec.JaegerCommonSpec}).ServiceAccount
}

if sa == "" {
return jaeger.Name
}
return sa
}
13 changes: 12 additions & 1 deletion pkg/account/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,16 @@ func TestWithSecurityOAuthProxy(t *testing.T) {

func TestJaegerName(t *testing.T) {
jaeger := v1.NewJaeger("foo")
assert.Equal(t, "foo", JaegerServiceAccountFor(jaeger))
jaeger.Spec.ServiceAccount = "bar"
jaeger.Spec.Collector.ServiceAccount = "col-sa"
jaeger.Spec.Query.ServiceAccount = "query-sa"
jaeger.Spec.Agent.ServiceAccount = "agent-sa"
jaeger.Spec.AllInOne.ServiceAccount = "aio-sa"

assert.Equal(t, "foo", JaegerServiceAccountFor(jaeger, ""))
assert.Equal(t, "col-sa", JaegerServiceAccountFor(jaeger, CollectorComponent))
assert.Equal(t, "query-sa", JaegerServiceAccountFor(jaeger, QueryComponent))
assert.Equal(t, "aio-sa", JaegerServiceAccountFor(jaeger, AllInOneComponent))
assert.Equal(t, "agent-sa", JaegerServiceAccountFor(jaeger, AgentComponent))
assert.Equal(t, "bar", JaegerServiceAccountFor(jaeger, IngesterComponent))
}
16 changes: 9 additions & 7 deletions pkg/apis/jaegertracing/v1/jaeger_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,13 +86,15 @@ type Jaeger struct {
// JaegerCommonSpec defines the common elements used in multiple other spec structs
// +k8s:openapi-gen=true
type JaegerCommonSpec struct {
Volumes []v1.Volume `json:"volumes"`
VolumeMounts []v1.VolumeMount `json:"volumeMounts"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Resources v1.ResourceRequirements `json:"resources,omitempty"`
Affinity *v1.Affinity `json:"affinity,omitempty"`
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
Volumes []v1.Volume `json:"volumes"`
VolumeMounts []v1.VolumeMount `json:"volumeMounts"`
Annotations map[string]string `json:"annotations,omitempty"`
Labels map[string]string `json:"labels,omitempty"`
Resources v1.ResourceRequirements `json:"resources,omitempty"`
Affinity *v1.Affinity `json:"affinity,omitempty"`
Tolerations []v1.Toleration `json:"tolerations,omitempty"`
SecurityContext *v1.PodSecurityContext `json:"securityContext,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
}

// JaegerQuerySpec defines the options to be used when deploying the query
Expand Down
5 changes: 5 additions & 0 deletions pkg/apis/jaegertracing/v1/zz_generated.deepcopy.go

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

7 changes: 5 additions & 2 deletions pkg/deployment/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/jaegertracing/jaeger-operator/pkg/account"
"github.com/jaegertracing/jaeger-operator/pkg/apis/jaegertracing/v1"
"github.com/jaegertracing/jaeger-operator/pkg/service"
"github.com/jaegertracing/jaeger-operator/pkg/util"
Expand Down Expand Up @@ -147,8 +148,10 @@ func (a *Agent) Get() *appsv1.DaemonSet {
},
Resources: commonSpec.Resources,
}},
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
SecurityContext: commonSpec.SecurityContext,
ServiceAccountName: account.JaegerServiceAccountFor(a.jaeger, account.AgentComponent),
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/deployment/all-in-one.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,9 +175,10 @@ func (a *AllInOne) Get() *appsv1.Deployment {
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
ServiceAccountName: account.JaegerServiceAccountFor(a.jaeger),
ServiceAccountName: account.JaegerServiceAccountFor(a.jaeger, account.AllInOneComponent),
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
SecurityContext: commonSpec.SecurityContext,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/deployment/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,9 +169,10 @@ func (c *Collector) Get() *appsv1.Deployment {
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
ServiceAccountName: account.JaegerServiceAccountFor(c.jaeger),
ServiceAccountName: account.JaegerServiceAccountFor(c.jaeger, account.CollectorComponent),
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
SecurityContext: commonSpec.SecurityContext,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/deployment/ingester.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,9 +146,10 @@ func (i *Ingester) Get() *appsv1.Deployment {
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
ServiceAccountName: account.JaegerServiceAccountFor(i.jaeger),
ServiceAccountName: account.JaegerServiceAccountFor(i.jaeger, account.IngesterComponent),
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
SecurityContext: commonSpec.SecurityContext,
},
},
},
Expand Down
3 changes: 2 additions & 1 deletion pkg/deployment/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,10 @@ func (q *Query) Get() *appsv1.Deployment {
Resources: commonSpec.Resources,
}},
Volumes: commonSpec.Volumes,
ServiceAccountName: account.JaegerServiceAccountFor(q.jaeger),
ServiceAccountName: account.JaegerServiceAccountFor(q.jaeger, account.QueryComponent),
Affinity: commonSpec.Affinity,
Tolerations: commonSpec.Tolerations,
SecurityContext: commonSpec.SecurityContext,
},
},
},
Expand Down
26 changes: 19 additions & 7 deletions pkg/util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec {
resources := &corev1.ResourceRequirements{}
var affinity *corev1.Affinity
var tolerations []corev1.Toleration
var securityContext *corev1.PodSecurityContext
var serviceAccount string

for _, commonSpec := range commonSpecs {
// Merge annotations
Expand Down Expand Up @@ -79,16 +81,26 @@ func Merge(commonSpecs []v1.JaegerCommonSpec) *v1.JaegerCommonSpec {
}

tolerations = append(tolerations, commonSpec.Tolerations...)

if securityContext == nil {
securityContext = commonSpec.SecurityContext
}

if serviceAccount == "" {
serviceAccount = commonSpec.ServiceAccount
}
}

return &v1.JaegerCommonSpec{
Annotations: annotations,
Labels: labels,
VolumeMounts: removeDuplicatedVolumeMounts(volumeMounts),
Volumes: removeDuplicatedVolumes(volumes),
Resources: *resources,
Affinity: affinity,
Tolerations: tolerations,
Annotations: annotations,
Labels: labels,
VolumeMounts: removeDuplicatedVolumeMounts(volumeMounts),
Volumes: removeDuplicatedVolumes(volumes),
Resources: *resources,
Affinity: affinity,
Tolerations: tolerations,
SecurityContext: securityContext,
ServiceAccount: serviceAccount,
}
}

Expand Down
29 changes: 29 additions & 0 deletions pkg/util/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,35 @@ func TestAffinityOverride(t *testing.T) {
assert.Nil(t, merged.Affinity.NodeAffinity)
}

func TestSecurityContextDefault(t *testing.T) {
generalSpec := v1.JaegerCommonSpec{}
specificSpec := v1.JaegerCommonSpec{}

merged := Merge([]v1.JaegerCommonSpec{specificSpec, generalSpec})

assert.Nil(t, merged.SecurityContext)
}

func TestSecurityContextOverride(t *testing.T) {
intVal := int64(1000)
generalSpec := v1.JaegerCommonSpec{
SecurityContext: &corev1.PodSecurityContext{
RunAsUser: &intVal,
},
}
specificSpec := v1.JaegerCommonSpec{
SecurityContext: &corev1.PodSecurityContext{
RunAsGroup: &intVal,
},
}

merged := Merge([]v1.JaegerCommonSpec{specificSpec, generalSpec})

assert.NotNil(t, merged.SecurityContext)
assert.NotNil(t, merged.SecurityContext.RunAsGroup)
assert.Nil(t, merged.SecurityContext.RunAsUser)
}

func TestMergeTolerations(t *testing.T) {
generalSpec := v1.JaegerCommonSpec{
Tolerations: []corev1.Toleration{{
Expand Down

0 comments on commit 4005a6b

Please sign in to comment.