@@ -20,13 +20,13 @@ import (
20
20
"context"
21
21
"fmt"
22
22
23
+ operatorv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
24
+ securityv1 "github.com/openshift/api/security/v1"
23
25
corev1 "k8s.io/api/core/v1"
24
26
"k8s.io/apimachinery/pkg/api/errors"
25
27
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
26
28
"k8s.io/apimachinery/pkg/types"
27
29
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
28
-
29
- operatorv1alpha1 "github.com/ocp-power-automation/rsct-operator/api/v1alpha1"
30
30
)
31
31
32
32
// ensureRSCTServiceAccount ensures that the RSCT service account exists.
@@ -54,7 +54,7 @@ func (r *RSCTReconciler) ensureRSCTServiceAccount(ctx context.Context, rsct *ope
54
54
return true , current , nil
55
55
}
56
56
57
- // currentRSCTServiceAccount gets the current RSCT service account resource.
57
+ // currentRSCTServiceAccount gets the current RSCT service account resource and ensures it has privileged SCC .
58
58
func (r * RSCTReconciler ) currentRSCTServiceAccount (ctx context.Context , nsName types.NamespacedName ) (bool , * corev1.ServiceAccount , error ) {
59
59
sa := & corev1.ServiceAccount {}
60
60
if err := r .Client .Get (ctx , nsName , sa ); err != nil {
@@ -63,9 +63,41 @@ func (r *RSCTReconciler) currentRSCTServiceAccount(ctx context.Context, nsName t
63
63
}
64
64
return false , nil , err
65
65
}
66
+
67
+ // Try to get the privileged SCC; if not found, skip SCC logic (not OpenShift)
68
+ scc := & securityv1.SecurityContextConstraints {}
69
+ err := r .Client .Get (ctx , types.NamespacedName {Name : "privileged" }, scc )
70
+ if err != nil {
71
+ // If SCC resource type not found, skip (not OpenShift)
72
+ if errors .IsNotFound (err ) || isNoMatchError (err ) {
73
+ return true , sa , nil
74
+ }
75
+ return true , sa , fmt .Errorf ("error getting privileged SCC: %w" , err )
76
+ }
77
+
78
+ saUser := fmt .Sprintf ("system:serviceaccount:%s:%s" , nsName .Namespace , nsName .Name )
79
+ found := false
80
+ for _ , user := range scc .Users {
81
+ if user == saUser {
82
+ found = true
83
+ break
84
+ }
85
+ }
86
+ if ! found {
87
+ scc .Users = append (scc .Users , saUser )
88
+ if err := r .Client .Update (ctx , scc ); err != nil {
89
+ return true , sa , fmt .Errorf ("failed to update privileged SCC: %w" , err )
90
+ }
91
+ }
92
+
66
93
return true , sa , nil
67
94
}
68
95
96
+ // isNoMatchError checks if the error is a NoMatchError (resource not registered, i.e., not OpenShift)
97
+ func isNoMatchError (err error ) bool {
98
+ return err != nil && (err .Error () == "no matches for kind \" SecurityContextConstraints\" in group \" security.openshift.io\" " )
99
+ }
100
+
69
101
// desiredRSCTServiceAccount returns the desired serivce account resource.
70
102
func desiredRSCTServiceAccount (nsName types.NamespacedName ) * corev1.ServiceAccount {
71
103
return & corev1.ServiceAccount {
0 commit comments