Skip to content

Commit

Permalink
Allow the use of annotations to extend pod, support securityContext
Browse files Browse the repository at this point in the history
  • Loading branch information
davidqhr committed Aug 24, 2020
1 parent 4afa454 commit 80a1686
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
42 changes: 42 additions & 0 deletions controller/controllers/component_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"strconv"
"strings"

corev1alpha1 "github.com/kalmhq/kalm/controller/api/v1alpha1"
Expand Down Expand Up @@ -280,6 +281,46 @@ func (r *ComponentReconcilerTask) GetAnnotations() map[string]string {
return res
}

func GetPodSecurityContextFromAnnotation(annotations map[string]string) *coreV1.PodSecurityContext {
securityContext := new(coreV1.PodSecurityContext)
annotationFound := false

for k, v := range annotations {
if !strings.HasPrefix(k, "core.kalm.dev/podExt/securityContext") {
continue
}

annotationFound = true

rest := strings.TrimPrefix(k, "core.kalm.dev/podExt/securityContext")

switch rest {
case "runAsGroup":
n, err := strconv.ParseInt(v, 0, 64)

if err != nil {
continue
}

securityContext.RunAsGroup = &n
case "runAsUser":
n, err := strconv.ParseInt(v, 0, 64)

if err != nil {
continue
}

securityContext.RunAsUser = &n
}
}

if !annotationFound {
return nil
}

return securityContext
}

func (r *ComponentReconcilerTask) FixComponentDefaultValues() (err error) {
if r.component == nil {
return nil
Expand Down Expand Up @@ -981,6 +1022,7 @@ func (r *ComponentReconcilerTask) GetPodTemplateWithoutVols() (template *coreV1.
LivenessProbe: r.FixProbe(component.Spec.LivenessProbe),
},
},
SecurityContext: GetPodSecurityContextFromAnnotation(annotations),
},
}

Expand Down
4 changes: 3 additions & 1 deletion controller/controllers/logsystem_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -409,7 +409,9 @@ func (r *LogSystemReconcilerTask) ReconcilePLGMonolithicPromtail() error {
},
Spec: corev1alpha1.ComponentSpec{
Annotations: map[string]string{
"sidecar.istio.io/inject": "false",
"sidecar.istio.io/inject": "false",
"core.kalm.dev/podExt/securityContext/runAsGroup": "0",
"core.kalm.dev/podExt/securityContext/runAsUser": "0",
},
Image: promtailImage,
WorkloadType: corev1alpha1.WorkloadTypeDaemonSet,
Expand Down

0 comments on commit 80a1686

Please sign in to comment.