Skip to content

OCPBUGS-23744: Wait for required RBAC before creating packageserver CSV #708

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions cmd/package-server-manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import (
"k8s.io/apimachinery/pkg/fields"
_ "k8s.io/client-go/plugin/pkg/client/auth"

corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -80,6 +83,9 @@ func run(cmd *cobra.Command, args []string) error {
le := leaderelection.GetLeaderElectionConfig(setupLog, restConfig, !disableLeaderElection)

packageserverCSVFields := fields.Set{"metadata.name": name}
serviceaccountFields := fields.Set{"metadata.name": "olm-operator-serviceaccount"}
clusterroleFields := fields.Set{"metadata.name": "system:controller:operator-lifecycle-manager"}
clusterrolebindingFields := fields.Set{"metadata.name": "olm-operator-binding-openshift-operator-lifecycle-manager"}
mgr, err := ctrl.NewManager(restConfig, manager.Options{
Scheme: setupScheme(),
Metrics: metricsserver.Options{BindAddress: metricsAddr},
Expand All @@ -100,6 +106,15 @@ func run(cmd *cobra.Command, args []string) error {
&olmv1alpha1.ClusterServiceVersion{}: {
Field: packageserverCSVFields.AsSelector(),
},
&corev1.ServiceAccount{}: {
Field: serviceaccountFields.AsSelector(),
},
&rbacv1.ClusterRole{}: {
Field: clusterroleFields.AsSelector(),
},
&rbacv1.ClusterRoleBinding{}: {
Field: clusterrolebindingFields.AsSelector(),
},
},
},
})
Expand Down
21 changes: 21 additions & 0 deletions pkg/package-server-manager/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ import (
"github.com/openshift/operator-framework-olm/pkg/manifests"
olmv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"

corev1 "k8s.io/api/core/v1"
rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

Expand Down Expand Up @@ -67,6 +69,10 @@ func (r *PackageServerCSVReconciler) Reconcile(ctx context.Context, req ctrl.Req
log.Info("handling current request", "request", req.String())
defer log.Info("finished request reconciliation")

if err := ensureRBAC(r.Client, ctx, r.Namespace, log); err != nil {
return ctrl.Result{}, err
}

var infra configv1.Infrastructure
if err := r.Client.Get(ctx, types.NamespacedName{Name: infrastructureName}, &infra); err != nil {
return ctrl.Result{}, err
Expand Down Expand Up @@ -102,6 +108,21 @@ func (r *PackageServerCSVReconciler) Reconcile(ctx context.Context, req ctrl.Req
return ctrl.Result{}, nil
}

func ensureRBAC(client client.Client, ctx context.Context, namespace string, log logr.Logger) error {
log.Info("checking to see if required RBAC exists")
if err := client.Get(ctx, types.NamespacedName{Name: "olm-operator-serviceaccount", Namespace: namespace}, &corev1.ServiceAccount{}); err != nil {
return fmt.Errorf("could not get service account:%v", err)
}
if err := client.Get(ctx, types.NamespacedName{Name: "system:controller:operator-lifecycle-manager"}, &rbacv1.ClusterRole{}); err != nil {
return fmt.Errorf("could not get ClusterRole:% v", err)
}
if err := client.Get(ctx, types.NamespacedName{Name: "olm-operator-binding-openshift-operator-lifecycle-manager"}, &rbacv1.ClusterRoleBinding{}); err != nil {
return fmt.Errorf("could not get ClusterRoleBinding: %v", err)
}
log.Info("confimed required RBAC exists")
return nil
}

func reconcileCSV(log logr.Logger, image string, interval string, csv *olmv1alpha1.ClusterServiceVersion, highAvailabilityMode bool) error {
if csv.ObjectMeta.CreationTimestamp.IsZero() {
log.Info("attempting to create the packageserver csv")
Expand Down