Skip to content

Commit

Permalink
chore: moves operator/subscriptions operations to pkg/cluster (openda…
Browse files Browse the repository at this point in the history
…tahub-io#1027)

They do not belong to pkg/deploy as they are about reading/writing cluster resources rather than deploying resources.
  • Loading branch information
bartoszmajsak authored May 30, 2024
1 parent f4e66f0 commit d90983b
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 69 deletions.
2 changes: 1 addition & 1 deletion components/codeflare/codeflare.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func (c *CodeFlare) ReconcileComponent(ctx context.Context, cli client.Client, l
// Both ODH and RHOAI should have the same operator name
dependentOperator := CodeflareOperator

if found, err := deploy.OperatorExists(cli, dependentOperator); err != nil {
if found, err := cluster.OperatorExists(cli, dependentOperator); err != nil {
return fmt.Errorf("operator exists throws error %w", err)
} else if found {
return fmt.Errorf("operator %s is found. Please uninstall the operator before enabling %s component",
Expand Down
4 changes: 2 additions & 2 deletions components/kserve/servicemesh_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature/servicemesh"
)
Expand All @@ -32,7 +32,7 @@ func (k *Kserve) removeServiceMeshConfigurations(cli client.Client, dscispec *ds

func (k *Kserve) defineServiceMeshFeatures(cli client.Client) feature.FeaturesProvider {
return func(handler *feature.FeaturesHandler) error {
authorinoInstalled, err := deploy.ClusterSubscriptionExists(cli, "authorino-operator")
authorinoInstalled, err := cluster.SubscriptionExists(cli, "authorino-operator")
if err != nil {
return fmt.Errorf("failed to list subscriptions %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions controllers/dscinitialization/servicemesh_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

dsciv1 "github.com/opendatahub-io/opendatahub-operator/v2/apis/dscinitialization/v1"
"github.com/opendatahub-io/opendatahub-operator/v2/controllers/status"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/feature/servicemesh"
)
Expand Down Expand Up @@ -86,7 +86,7 @@ func (r *DSCInitializationReconciler) serviceMeshCapability(instance *dsciv1.DSC
}

func (r *DSCInitializationReconciler) authorizationCapability(instance *dsciv1.DSCInitialization, condition *conditionsv1.Condition) (*feature.HandlerWithReporter[*dsciv1.DSCInitialization], error) { //nolint:lll // Reason: generics are long
authorinoInstalled, err := deploy.ClusterSubscriptionExists(r.Client, "authorino-operator")
authorinoInstalled, err := cluster.SubscriptionExists(r.Client, "authorino-operator")
if err != nil {
return nil, fmt.Errorf("failed to list subscriptions %w", err)
}
Expand Down
69 changes: 69 additions & 0 deletions pkg/cluster/operator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cluster

import (
"context"
"fmt"
"strings"

"github.com/operator-framework/api/pkg/operators/v1alpha1"
v2 "github.com/operator-framework/api/pkg/operators/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
)

// GetSubscription checks if a Subscription for the operator exists in the given namespace.
// if exists, return object; otherwise, return error.
func GetSubscription(cli client.Client, namespace string, name string) (*v1alpha1.Subscription, error) {
sub := &v1alpha1.Subscription{}
if err := cli.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, sub); err != nil {
// real error or 'not found' both return here
return nil, err
}
return sub, nil
}

func SubscriptionExists(cli client.Client, name string) (bool, error) {
subscriptionList := &v1alpha1.SubscriptionList{}
if err := cli.List(context.TODO(), subscriptionList); err != nil {
return false, err
}

for _, sub := range subscriptionList.Items {
if sub.Name == name {
return true, nil
}
}
return false, nil
}

// DeleteExistingSubscription deletes given Subscription if it exists
// Do not error if the Subscription does not exist.
func DeleteExistingSubscription(cli client.Client, operatorNs string, subsName string) error {
sub, err := GetSubscription(cli, operatorNs, subsName)
if err != nil {
return client.IgnoreNotFound(err)
}

if err := cli.Delete(context.TODO(), sub); client.IgnoreNotFound(err) != nil {
return fmt.Errorf("error deleting subscription %s: %w", sub.Name, err)
}

return nil
}

// OperatorExists checks if an Operator with 'operatorPrefix' is installed.
// Return true if found it, false if not.
// if we need to check exact version of the operator installed, can append vX.Y.Z later.
func OperatorExists(cli client.Client, operatorPrefix string) (bool, error) {
opConditionList := &v2.OperatorConditionList{}
err := cli.List(context.TODO(), opConditionList)
if err != nil {
return false, err
}
for _, opCondition := range opConditionList.Items {
if strings.HasPrefix(opCondition.Name, operatorPrefix) {
return true, nil
}
}

return false, nil
}
34 changes: 0 additions & 34 deletions pkg/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ import (
"path/filepath"
"strings"

ofapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
ofapiv2 "github.com/operator-framework/api/pkg/operators/v2"
"golang.org/x/exp/maps"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -355,38 +353,6 @@ func removeResourcesFromDeployment(u *unstructured.Unstructured) error {
return nil
}

func ClusterSubscriptionExists(cli client.Client, name string) (bool, error) {
subscriptionList := &ofapiv1alpha1.SubscriptionList{}
if err := cli.List(context.TODO(), subscriptionList); err != nil {
return false, err
}

for _, sub := range subscriptionList.Items {
if sub.Name == name {
return true, nil
}
}
return false, nil
}

// OperatorExists checks if an Operator with 'operatorPrefix' is installed.
// Return true if found it, false if not.
// if we need to check exact version of the operator installed, can append vX.Y.Z later.
func OperatorExists(cli client.Client, operatorPrefix string) (bool, error) {
opConditionList := &ofapiv2.OperatorConditionList{}
err := cli.List(context.TODO(), opConditionList)
if err != nil {
return false, err
}
for _, opCondition := range opConditionList.Items {
if strings.HasPrefix(opCondition.Name, operatorPrefix) {
return true, nil
}
}

return false, nil
}

func getResource(ctx context.Context, cli client.Client, obj *unstructured.Unstructured) (*unstructured.Unstructured, error) {
found := &unstructured.Unstructured{}
// Setting gvk is required to do Get request
Expand Down
4 changes: 2 additions & 2 deletions pkg/feature/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/opendatahub-io/opendatahub-operator/v2/pkg/deploy"
"github.com/opendatahub-io/opendatahub-operator/v2/pkg/cluster"
)

const (
Expand Down Expand Up @@ -41,7 +41,7 @@ func (e *MissingOperatorError) Error() string {

func EnsureOperatorIsInstalled(operatorName string) Action {
return func(f *Feature) error {
if found, err := deploy.ClusterSubscriptionExists(f.Client, operatorName); !found || err != nil {
if found, err := cluster.SubscriptionExists(f.Client, operatorName); !found || err != nil {
return fmt.Errorf(
"failed to find the pre-requisite operator subscription %q, please ensure operator is installed. %w",
operatorName,
Expand Down
29 changes: 1 addition & 28 deletions pkg/upgrade/uninstallation.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/hashicorp/go-multierror"
ofapiv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
corev1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -81,7 +80,7 @@ func OperatorUninstall(ctx context.Context, cli client.Client) error {
subsName = "rhods-operator"
}
if platform != cluster.ManagedRhods {
if err := DeleteExistingSubscription(cli, operatorNs, subsName); err != nil {
if err := cluster.DeleteExistingSubscription(cli, operatorNs, subsName); err != nil {
return err
}
}
Expand Down Expand Up @@ -162,29 +161,3 @@ func removeCSV(ctx context.Context, c client.Client) error {
fmt.Printf("No clusterserviceversion for the operator found.\n")
return nil
}

// DeleteExistingSubscription deletes given Subscription if it exists
// Do not error if the Subscription does not exist.
func DeleteExistingSubscription(cli client.Client, operatorNs string, subsName string) error {
sub, err := getSubscription(cli, operatorNs, subsName)
if err != nil {
return client.IgnoreNotFound(err)
}

if err := cli.Delete(context.TODO(), sub); client.IgnoreNotFound(err) != nil {
return fmt.Errorf("error deleting subscription %s: %w", sub.Name, err)
}

return nil
}

// GetSubscription checks if a Subscription for the operator exists in the given namespace.
// if exist, return object; otherwise, return error.
func getSubscription(cli client.Client, namespace string, name string) (*ofapiv1alpha1.Subscription, error) {
sub := &ofapiv1alpha1.Subscription{}
if err := cli.Get(context.TODO(), client.ObjectKey{Namespace: namespace, Name: name}, sub); err != nil {
// real error or 'not found' both return here
return nil, err
}
return sub, nil
}

0 comments on commit d90983b

Please sign in to comment.