-
Notifications
You must be signed in to change notification settings - Fork 36
/
operator_uninstall.go
70 lines (58 loc) · 1.92 KB
/
operator_uninstall.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
package action
import (
"context"
"fmt"
"strings"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
olmv1 "github.com/operator-framework/operator-controller/api/v1alpha1"
"github.com/operator-framework/kubectl-operator/pkg/action"
)
type OperatorUninstall struct {
config *action.Configuration
Package string
Logf func(string, ...interface{})
}
func NewOperatorUninstall(cfg *action.Configuration) *OperatorUninstall {
return &OperatorUninstall{
config: cfg,
Logf: func(string, ...interface{}) {},
}
}
func (u *OperatorUninstall) Run(ctx context.Context) error {
opKey := types.NamespacedName{Name: u.Package}
op := &olmv1.ClusterExtension{}
op.SetName(opKey.Name)
op.SetGroupVersionKind(olmv1.GroupVersion.WithKind("Operator"))
lowerKind := strings.ToLower(op.GetObjectKind().GroupVersionKind().Kind)
if err := u.config.Client.Delete(ctx, op); err != nil && !apierrors.IsNotFound(err) {
return fmt.Errorf("delete %s %q: %v", lowerKind, op.GetName(), err)
}
return waitForDeletion(ctx, u.config.Client, op)
}
func objectKeyForObject(obj client.Object) types.NamespacedName {
return types.NamespacedName{
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}
}
func waitForDeletion(ctx context.Context, cl client.Client, objs ...client.Object) error {
for _, obj := range objs {
obj := obj
lowerKind := strings.ToLower(obj.GetObjectKind().GroupVersionKind().Kind)
key := objectKeyForObject(obj)
if err := wait.PollUntilContextCancel(ctx, pollTimeout, true, func(conditionCtx context.Context) (bool, error) {
if err := cl.Get(conditionCtx, key, obj); apierrors.IsNotFound(err) {
return true, nil
} else if err != nil {
return false, err
}
return false, nil
}); err != nil {
return fmt.Errorf("wait for %s %q deleted: %v", lowerKind, key.Name, err)
}
}
return nil
}