@@ -20,13 +20,14 @@ import (
2020 "context"
2121 "fmt"
2222
23+ operatorv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
24+ securityv1 "github.com/openshift/api/security/v1"
2325 corev1 "k8s.io/api/core/v1"
2426 "k8s.io/apimachinery/pkg/api/errors"
2527 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
2628 "k8s.io/apimachinery/pkg/types"
29+ "sigs.k8s.io/controller-runtime/pkg/client"
2730 "sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
28-
29- operatorv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
3031)
3132
3233// ensureRSCTServiceAccount ensures that the RSCT service account exists.
@@ -54,7 +55,7 @@ func (r *RSCTReconciler) ensureRSCTServiceAccount(ctx context.Context, rsct *ope
5455 return true , current , nil
5556}
5657
57- // currentRSCTServiceAccount gets the current RSCT service account resource.
58+ // currentRSCTServiceAccount gets the current RSCT service account resource and ensures it has privileged SCC .
5859func (r * RSCTReconciler ) currentRSCTServiceAccount (ctx context.Context , nsName types.NamespacedName ) (bool , * corev1.ServiceAccount , error ) {
5960 sa := & corev1.ServiceAccount {}
6061 if err := r .Client .Get (ctx , nsName , sa ); err != nil {
@@ -63,9 +64,35 @@ func (r *RSCTReconciler) currentRSCTServiceAccount(ctx context.Context, nsName t
6364 }
6465 return false , nil , err
6566 }
67+
68+ scc := & securityv1.SecurityContextConstraints {}
69+ if err := r .Client .Get (ctx , types.NamespacedName {Name : "privileged" }, scc ); err != nil {
70+ return true , sa , fmt .Errorf ("error getting privileged SCC: %w" , err )
71+ }
72+
73+ saUser := fmt .Sprintf ("system:serviceaccount:%s:%s" , nsName .Namespace , nsName .Name )
74+
75+ if ! contains (scc .Users , saUser ) {
76+ patch := client .MergeFrom (scc .DeepCopy ())
77+ scc .Users = append (scc .Users , saUser )
78+
79+ if err := r .Client .Patch (ctx , scc , patch ); err != nil {
80+ return true , sa , fmt .Errorf ("failed to patch privileged SCC: %w" , err )
81+ }
82+ }
83+
6684 return true , sa , nil
6785}
6886
87+ func contains (list []string , s string ) bool {
88+ for _ , v := range list {
89+ if v == s {
90+ return true
91+ }
92+ }
93+ return false
94+ }
95+
6996// desiredRSCTServiceAccount returns the desired serivce account resource.
7097func desiredRSCTServiceAccount (nsName types.NamespacedName ) * corev1.ServiceAccount {
7198 return & corev1.ServiceAccount {
0 commit comments