|
| 1 | +package steps |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + |
| 9 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 10 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 11 | + "k8s.io/apimachinery/pkg/runtime" |
| 12 | + |
| 13 | + ocv1 "github.com/operator-framework/operator-controller/api/v1" |
| 14 | +) |
| 15 | + |
| 16 | +// A local cache index does not make spec.group selectable through kubectl. |
| 17 | +func listClusterObjectSetsInGroup(ctx context.Context, group string) ([]ocv1.ClusterObjectSet, error) { |
| 18 | + out, err := k8sClient(ctx, "get", "clusterobjectsets", "-o", "json") |
| 19 | + if err != nil { |
| 20 | + return nil, err |
| 21 | + } |
| 22 | + var list ocv1.ClusterObjectSetList |
| 23 | + if err := json.Unmarshal([]byte(out), &list); err != nil { |
| 24 | + return nil, err |
| 25 | + } |
| 26 | + var revisions []ocv1.ClusterObjectSet |
| 27 | + for _, rev := range list.Items { |
| 28 | + if rev.Spec.Group == group { |
| 29 | + revisions = append(revisions, rev) |
| 30 | + } |
| 31 | + } |
| 32 | + return revisions, nil |
| 33 | +} |
| 34 | + |
| 35 | +func ClusterObjectSetHasGroup(ctx context.Context, revisionName, group string) error { |
| 36 | + sc := scenarioCtx(ctx) |
| 37 | + revisionName = substituteScenarioVars(revisionName, sc) |
| 38 | + group = substituteScenarioVars(group, sc) |
| 39 | + waitFor(ctx, func() bool { |
| 40 | + obj, err := getResource("clusterobjectset", revisionName, "") |
| 41 | + if err != nil { |
| 42 | + return false |
| 43 | + } |
| 44 | + var cos ocv1.ClusterObjectSet |
| 45 | + return runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &cos) == nil && cos.Spec.Group == group |
| 46 | + }) |
| 47 | + return nil |
| 48 | +} |
| 49 | + |
| 50 | +// ClusterObjectSetObjectsUseSSAManager checks actual managed fields on every |
| 51 | +// inline or externally stored phase object, including their revision owner. |
| 52 | +func ClusterObjectSetObjectsUseSSAManager(ctx context.Context, revisionName, manager string) error { |
| 53 | + sc := scenarioCtx(ctx) |
| 54 | + revisionName = substituteScenarioVars(revisionName, sc) |
| 55 | + manager = substituteScenarioVars(manager, sc) |
| 56 | + waitFor(ctx, func() bool { |
| 57 | + obj, err := getResource("clusterobjectset", revisionName, "") |
| 58 | + if err != nil { |
| 59 | + return false |
| 60 | + } |
| 61 | + var cos ocv1.ClusterObjectSet |
| 62 | + if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.Object, &cos); err != nil { |
| 63 | + return false |
| 64 | + } |
| 65 | + count := 0 |
| 66 | + for _, phase := range cos.Spec.Phases { |
| 67 | + for _, phaseObj := range phase.Objects { |
| 68 | + desired := &phaseObj.Object |
| 69 | + if phaseObj.Ref.Name != "" { |
| 70 | + desired, err = resolveObjectRef(phaseObj.Ref) |
| 71 | + if err != nil { |
| 72 | + return false |
| 73 | + } |
| 74 | + } |
| 75 | + // kubectl's JSON/YAML printers omit managedFields by default. |
| 76 | + args := []string{"get", desired.GetKind(), desired.GetName(), "-o", "json", "--show-managed-fields"} |
| 77 | + if desired.GetNamespace() != "" { |
| 78 | + args = append(args, "-n", desired.GetNamespace()) |
| 79 | + } |
| 80 | + out, err := k8sClient(ctx, args...) |
| 81 | + if err != nil { |
| 82 | + return false |
| 83 | + } |
| 84 | + actual := &unstructured.Unstructured{} |
| 85 | + if err := json.Unmarshal([]byte(out), &actual.Object); err != nil { |
| 86 | + return false |
| 87 | + } |
| 88 | + owner := metav1.GetControllerOf(actual) |
| 89 | + if owner == nil || owner.Kind != ocv1.ClusterObjectSetKind || owner.UID != cos.UID { |
| 90 | + return false |
| 91 | + } |
| 92 | + found := false |
| 93 | + for _, fields := range actual.GetManagedFields() { |
| 94 | + if fields.Operation != metav1.ManagedFieldsOperationApply { |
| 95 | + continue |
| 96 | + } |
| 97 | + if strings.HasPrefix(fields.Manager, "cos-group/") && fields.Manager != manager { |
| 98 | + return false |
| 99 | + } |
| 100 | + if fields.Manager == manager { |
| 101 | + found = true |
| 102 | + } |
| 103 | + } |
| 104 | + if !found { |
| 105 | + return false |
| 106 | + } |
| 107 | + count++ |
| 108 | + } |
| 109 | + } |
| 110 | + return count > 0 |
| 111 | + }) |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func RememberClusterObjectSetSecrets(ctx context.Context, revisionName string) error { |
| 116 | + sc := scenarioCtx(ctx) |
| 117 | + revisionName = substituteScenarioVars(revisionName, sc) |
| 118 | + names, err := collectReferredSecretNames(ctx, revisionName) |
| 119 | + if err != nil { |
| 120 | + return err |
| 121 | + } |
| 122 | + for _, name := range names { |
| 123 | + sc.revisionSecrets = append(sc.revisionSecrets, resource{kind: "secret", name: name, namespace: olmNamespace}) |
| 124 | + } |
| 125 | + return nil |
| 126 | +} |
| 127 | + |
| 128 | +func RememberedRevisionSecretsRemoved(ctx context.Context) error { |
| 129 | + sc := scenarioCtx(ctx) |
| 130 | + if len(sc.revisionSecrets) == 0 { |
| 131 | + return fmt.Errorf("no revision secrets were remembered") |
| 132 | + } |
| 133 | + for _, secret := range sc.revisionSecrets { |
| 134 | + waitFor(ctx, func() bool { |
| 135 | + out, err := k8sClient(ctx, "get", secret.kind, secret.name, "-n", secret.namespace, "--ignore-not-found", "-o", "name") |
| 136 | + return err == nil && strings.TrimSpace(out) == "" |
| 137 | + }) |
| 138 | + } |
| 139 | + return nil |
| 140 | +} |
0 commit comments