-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathinstallplan.go
58 lines (51 loc) · 1.81 KB
/
installplan.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
package test
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
operatorsv1alpha1 "github.com/operator-framework/api/pkg/operators/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func WaitForInstallPlan(ctx *Context, namespace string, csvName, olmSource string, timeout time.Duration) (*operatorsv1alpha1.InstallPlan, error) {
var plan *operatorsv1alpha1.InstallPlan
if waitErr := wait.PollUntilContextTimeout(context.Background(), Interval, timeout, true, func(_ context.Context) (bool, error) {
installPlans, err := ctx.Clients.OLM.OperatorsV1alpha1().InstallPlans(namespace).List(context.Background(), metav1.ListOptions{})
if err != nil {
return false, err
}
for _, installPlan := range installPlans.Items {
if installsCSVFromSource(installPlan, csvName, olmSource) {
plan = &installPlan
return true, nil
}
}
return false, nil
}); waitErr != nil {
return plan, fmt.Errorf("installplan for CSV %s and OLM source %s not found: %w", csvName, olmSource, waitErr)
}
return plan, nil
}
func installsCSVFromSource(installPlan operatorsv1alpha1.InstallPlan, csvName, olmSource string) bool {
if len(installPlan.Status.BundleLookups) == 0 ||
installPlan.Status.BundleLookups[0].CatalogSourceRef == nil ||
installPlan.Status.BundleLookups[0].CatalogSourceRef.Name != olmSource {
return false
}
for _, name := range installPlan.Spec.ClusterServiceVersionNames {
if name == csvName {
return true
}
}
return false
}
func ApproveInstallPlan(ctx *Context, name string) error {
patch := []byte(`{"spec":{"approved":true}}`)
_, err := ctx.Clients.OLM.OperatorsV1alpha1().InstallPlans(OperatorsNamespace).
Patch(context.Background(), name, types.MergePatchType, patch, metav1.PatchOptions{})
if err != nil {
return err
}
return nil
}