Skip to content

Commit

Permalink
Merge pull request #1768 from Prateeknandle/job-rbac-1
Browse files Browse the repository at this point in the history
fix(core):patch apparmor annotations for cronjobs and updating rbac rules
  • Loading branch information
DelusionalOptimist authored Jun 12, 2024
2 parents b9ea20c + 0a8837f commit 322dd5a
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 3 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
3 changes: 1 addition & 2 deletions 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 Expand Up @@ -546,7 +546,6 @@ func GetKubeArmorControllerDeployment(namespace string) *appsv1.Deployment {
Labels: KubeArmorControllerLabels,
},
Spec: corev1.PodSpec{
PriorityClassName: "system-node-critical",
ServiceAccountName: KubeArmorControllerServiceAccountName,
Volumes: []corev1.Volume{
KubeArmorControllerCertVolume,
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 @@ -136,6 +136,10 @@ rules:
- cronjobs
verbs:
- get
- patch
- list
- watch
- update
- apiGroups:
- security.kubearmor.com
resources:
Expand Down
11 changes: 11 additions & 0 deletions deployments/operator/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,17 @@ rules:
- list
- watch
- update
- apiGroups:
- batch
resources:
- jobs
- 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 322dd5a

Please sign in to comment.