forked from opendatahub-io/opendatahub-operator
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: moves operator/subscriptions operations to pkg/cluster (openda…
…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
1 parent
f4e66f0
commit d90983b
Showing
7 changed files
with
77 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters