Skip to content

Commit

Permalink
patch apparmor annotations for job & cronjobs and updating rbac rules
Browse files Browse the repository at this point in the history
Signed-off-by: Prateek Nandle <[email protected]>
  • Loading branch information
Prateeknandle committed May 27, 2024
1 parent 0eaec66 commit eaa5f0e
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
15 changes: 14 additions & 1 deletion KubeArmor/core/k8sHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,10 @@ func (kh *K8sHandler) PatchResourceWithAppArmorAnnotations(namespaceName, deploy
}

spec := `{"spec":{"template":{"metadata":{"annotations":{"kubearmor-policy":"enabled",`
if kind == "CronJob" {
spec = `{"spec":{"jobTemplate":{"spec":{"template":{"metadata":{"annotations":{"kubearmor-policy":"enabled",`
}

count := len(appArmorAnnotations)

for k, v := range appArmorAnnotations {
Expand All @@ -246,7 +250,11 @@ func (kh *K8sHandler) PatchResourceWithAppArmorAnnotations(namespaceName, deploy
count--
}

spec = spec + `}}}}}`
if kind == "CronJob" {
spec = spec + `}}}}}}}`
} else {
spec = spec + `}}}}}`
}

if kind == "StatefulSet" {
_, err := kh.K8sClient.AppsV1().StatefulSets(namespaceName).Patch(context.Background(), deploymentName, types.StrategicMergePatchType, []byte(spec), metav1.PatchOptions{})
Expand Down Expand Up @@ -292,6 +300,11 @@ func (kh *K8sHandler) PatchResourceWithAppArmorAnnotations(namespaceName, deploy
if err != nil {
return err
}
} else if kind == "CronJob" {
_, err := kh.K8sClient.BatchV1().CronJobs(namespaceName).Patch(context.Background(), deploymentName, types.StrategicMergePatchType, []byte(spec), metav1.PatchOptions{})
if err != nil {
return err
}
} else if kind == "Pod" {
// this condition wont be triggered, handled by controller
return nil
Expand Down
2 changes: 1 addition & 1 deletion deployments/get/objects.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GetClusterRole() *rbacv1.ClusterRole {
{
APIGroups: []string{"batch"},
Resources: []string{"jobs", "cronjobs"},
Verbs: []string{"get"},
Verbs: []string{"get", "patch", "list", "watch", "update"},
},
{
APIGroups: []string{"security.kubearmor.com"},
Expand Down
4 changes: 4 additions & 0 deletions deployments/helm/KubeArmor/templates/RBAC/roles.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ rules:
- cronjobs
verbs:
- get
- patch
- list
- watch
- update
- apiGroups:
- security.kubearmor.com
resources:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ rules:
- cronjobs
verbs:
- get
- patch
- list
- watch
- update
- apiGroups:
- security.kubearmor.com
resources:
Expand Down
4 changes: 4 additions & 0 deletions pkg/KubeArmorOperator/config/rbac/clusterrole.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ rules:
- cronjobs
verbs:
- get
- patch
- list
- watch
- update
- apiGroups:
- security.kubearmor.com
resources:
Expand Down

0 comments on commit eaa5f0e

Please sign in to comment.