@@ -17,9 +17,12 @@ limitations under the License.
17
17
package main
18
18
19
19
import (
20
+ "context"
20
21
"flag"
21
22
"fmt"
22
23
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"
23
26
"os"
24
27
25
28
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
@@ -78,6 +81,13 @@ func main() {
78
81
"the manager will watch and manage resources in all namespaces" )
79
82
}
80
83
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
+
81
91
mgr , err := ctrl .NewManager (ctrl .GetConfigOrDie (), ctrl.Options {
82
92
Scheme : scheme ,
83
93
MetricsBindAddress : metricsAddr ,
@@ -171,3 +181,40 @@ func getWatchNamespace() (string, error) {
171
181
}
172
182
return ns , nil
173
183
}
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