Skip to content

Commit

Permalink
fix: ensure pod security label on namespace (#774)
Browse files Browse the repository at this point in the history
* fix: ensure pod security label on namespace

Signed-off-by: saumeya <[email protected]>

* fix:

Signed-off-by: saumeya <[email protected]>

* review comments

Signed-off-by: saumeya <[email protected]>

---------

Signed-off-by: saumeya <[email protected]>
  • Loading branch information
saumeya authored Sep 4, 2024
1 parent 45c82e3 commit b50e9e1
Showing 1 changed file with 39 additions and 0 deletions.
39 changes: 39 additions & 0 deletions controllers/gitopsservice_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,14 @@ func (r *ReconcileGitopsService) Reconcile(ctx context.Context, request reconcil
} else {
return reconcile.Result{}, err
}
} else {
needUpdate, updateNameSpace := ensurePodSecurityLabels(namespaceRef)
if needUpdate {
err = r.Client.Update(context.TODO(), updateNameSpace)
if err != nil {
return reconcile.Result{}, err
}
}
}

gitopsserviceNamespacedName := types.NamespacedName{
Expand Down Expand Up @@ -369,6 +377,15 @@ func (r *ReconcileGitopsService) reconcileDefaultArgoCDInstance(instance *pipeli
return reconcile.Result{}, err
}
}

needUpdate, updateNameSpace := ensurePodSecurityLabels(argocdNS)
if needUpdate {
err = r.Client.Update(context.TODO(), updateNameSpace)
if err != nil {
return reconcile.Result{}, err
}
}

}

// Set GitopsService instance as the owner and controller
Expand Down Expand Up @@ -920,3 +937,25 @@ func policyRuleForBackendServiceClusterRole() []rbacv1.PolicyRule {
},
}
}

func ensurePodSecurityLabels(namespace *corev1.Namespace) (bool, *corev1.Namespace) {

pssLabels := map[string]string{
"pod-security.kubernetes.io/enforce": "restricted",
"pod-security.kubernetes.io/enforce-version": "v1.29",
"pod-security.kubernetes.io/audit": "restricted",
"pod-security.kubernetes.io/audit-version": "latest",
"pod-security.kubernetes.io/warn": "restricted",
"pod-security.kubernetes.io/warn-version": "latest",
}

changed := false
for pssKey, pssVal := range pssLabels {
if nsVal, exists := namespace.Labels[pssKey]; !exists || nsVal != pssVal {
namespace.Labels[pssKey] = pssVal
changed = true
}

}
return changed, namespace
}

0 comments on commit b50e9e1

Please sign in to comment.