Skip to content

Commit 4e58f62

Browse files
committed
fix: ensure pod security label on namespace
Signed-off-by: saumeya <[email protected]>
1 parent a671295 commit 4e58f62

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

controllers/gitopsservice_controller.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,14 @@ func (r *ReconcileGitopsService) Reconcile(ctx context.Context, request reconcil
230230
} else {
231231
return reconcile.Result{}, err
232232
}
233+
} else {
234+
needUpdate, updateNameSpace := ensurePodSecurityLabels(argocdNS)
235+
if needUpdate {
236+
err = r.Client.Update(context.TODO(), updateNameSpace)
237+
if err != nil {
238+
return reconcile.Result{}, err
239+
}
240+
}
233241
}
234242

235243
gitopsserviceNamespacedName := types.NamespacedName{
@@ -369,6 +377,15 @@ func (r *ReconcileGitopsService) reconcileDefaultArgoCDInstance(instance *pipeli
369377
return reconcile.Result{}, err
370378
}
371379
}
380+
381+
needUpdate, updateNameSpace := ensurePodSecurityLabels(argocdNS)
382+
if needUpdate {
383+
err = r.Client.Update(context.TODO(), updateNameSpace)
384+
if err != nil {
385+
return reconcile.Result{}, err
386+
}
387+
}
388+
372389
}
373390

374391
// Set GitopsService instance as the owner and controller
@@ -920,3 +937,20 @@ func policyRuleForBackendServiceClusterRole() []rbacv1.PolicyRule {
920937
},
921938
}
922939
}
940+
941+
func ensurePodSecurityLabels(namespace *corev1.Namespace) (bool, *corev1.Namespace) {
942+
for key := range namespace.Labels {
943+
if strings.HasPrefix(key, "pod-security") {
944+
return false, namespace
945+
}
946+
}
947+
948+
namespace.Labels["pod-security.kubernetes.io/enforce"] = "restricted"
949+
namespace.Labels["pod-security.kubernetes.io/enforce-version"] = "v1.29"
950+
namespace.Labels["pod-security.kubernetes.io/audit"] = "restricted"
951+
namespace.Labels["pod-security.kubernetes.io/audit-version"] = "latest"
952+
namespace.Labels["pod-security.kubernetes.io/warn"] = "restricted"
953+
namespace.Labels["pod-security.kubernetes.io/warn-version"] = "latest"
954+
955+
return true, namespace
956+
}

0 commit comments

Comments
 (0)