Skip to content

Commit 925f578

Browse files
OADP-552: Add privileged pod security labels to operator namespace (#763)
* add privileged pod security labels to operator ns * handle empty operator ns case * fix function name typo
1 parent a024873 commit 925f578

File tree

3 files changed

+69
-0
lines changed

3 files changed

+69
-0
lines changed

bundle/manifests/oadp-operator.clusterserviceversion.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,17 @@ spec:
591591
- patch
592592
- update
593593
- watch
594+
- apiGroups:
595+
- ""
596+
resources:
597+
- namespaces
598+
verbs:
599+
- list
600+
- get
601+
- create
602+
- patch
603+
- update
604+
- watch
594605
- apiGroups:
595606
- apps
596607
resources:

config/rbac/role.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,17 @@ rules:
170170
- patch
171171
- update
172172
- watch
173+
- apiGroups:
174+
- ""
175+
resources:
176+
- namespaces
177+
verbs:
178+
- list
179+
- get
180+
- create
181+
- patch
182+
- update
183+
- watch
173184
- apiGroups:
174185
- apps
175186
resources:

main.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,12 @@ limitations under the License.
1717
package main
1818

1919
import (
20+
"context"
2021
"flag"
2122
"fmt"
2223
monitor "github.com/prometheus-operator/prometheus-operator/pkg/apis/monitoring/v1"
24+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
"k8s.io/client-go/kubernetes"
2326
"os"
2427

2528
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -78,6 +81,13 @@ func main() {
7881
"the manager will watch and manage resources in all namespaces")
7982
}
8083

84+
// setting privileged pod security labels to operator ns
85+
err = addPodSecurityPrivilegedLabels(watchNamespace)
86+
if err != nil {
87+
setupLog.Error(err, "error setting privileged pod security labels to operator namespace")
88+
os.Exit(1)
89+
}
90+
8191
mgr, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{
8292
Scheme: scheme,
8393
MetricsBindAddress: metricsAddr,
@@ -171,3 +181,40 @@ func getWatchNamespace() (string, error) {
171181
}
172182
return ns, nil
173183
}
184+
185+
// setting privileged pod security labels to OADP operator namespace
186+
func addPodSecurityPrivilegedLabels(watchNamespaceName string) error {
187+
setupLog.Info("patching operator namespace with PSA labels")
188+
189+
if len(watchNamespaceName) == 0 {
190+
return fmt.Errorf("cannot add privileged pod security labels, watchNamespaceName is empty")
191+
}
192+
193+
kubeconf := ctrl.GetConfigOrDie()
194+
clientset, err := kubernetes.NewForConfig(kubeconf)
195+
if err != nil {
196+
setupLog.Error(err, "problem getting client")
197+
return err
198+
}
199+
200+
operatorNamespace, err := clientset.CoreV1().Namespaces().Get(context.TODO(), watchNamespaceName, metav1.GetOptions{})
201+
if err != nil {
202+
setupLog.Error(err, "problem getting operator namespace")
203+
return err
204+
}
205+
206+
privilegedLabels := map[string]string{
207+
"pod-security.kubernetes.io/enforce": "privileged",
208+
"pod-security.kubernetes.io/audit": "privileged",
209+
"pod-security.kubernetes.io/warn": "privileged",
210+
}
211+
212+
operatorNamespace.SetLabels(privilegedLabels)
213+
214+
_, err = clientset.CoreV1().Namespaces().Update(context.TODO(), operatorNamespace, metav1.UpdateOptions{})
215+
if err != nil {
216+
setupLog.Error(err, "problem patching operator namespace for privileged pod security labels")
217+
return err
218+
}
219+
return nil
220+
}

0 commit comments

Comments
 (0)