Skip to content

Commit 4f627f3

Browse files
committed
Allow Controller SA to modify SCC
1 parent de45f32 commit 4f627f3

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

cmd/main.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
// to ensure that exec-entrypoint and run can make use of them.
2626
_ "k8s.io/client-go/plugin/pkg/client/auth"
2727

28+
securityv1 "github.com/openshift/api/security/v1"
2829
"k8s.io/apimachinery/pkg/runtime"
2930
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
3031
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
@@ -49,6 +50,7 @@ func init() {
4950
utilruntime.Must(clientgoscheme.AddToScheme(scheme))
5051

5152
utilruntime.Must(rsctv1alpha1.AddToScheme(scheme))
53+
utilruntime.Must(securityv1.AddToScheme(scheme))
5254
//+kubebuilder:scaffold:scheme
5355
}
5456

internal/controller/service_account.go

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
5859
func (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.
7097
func desiredRSCTServiceAccount(nsName types.NamespacedName) *corev1.ServiceAccount {
7198
return &corev1.ServiceAccount{

0 commit comments

Comments
 (0)