|
| 1 | +package olm |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + coreV1 "k8s.io/api/core/v1" |
| 8 | + rbacV1 "k8s.io/api/rbac/v1" |
| 9 | + "k8s.io/apimachinery/pkg/api/meta" |
| 10 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 11 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 12 | + "k8s.io/apimachinery/pkg/util/json" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 14 | +) |
| 15 | + |
| 16 | +type clusterExtensionWrapper struct { |
| 17 | + *unstructured.Unstructured |
| 18 | +} |
| 19 | + |
| 20 | +func (e *clusterExtensionWrapper) Unwrap() client.Object { |
| 21 | + return e.Unstructured |
| 22 | +} |
| 23 | + |
| 24 | +func (e *clusterExtensionWrapper) IsReady(_ context.Context, _ client.Client) bool { |
| 25 | + return meta.IsStatusConditionTrue(conditions(e.Object), "Installed") |
| 26 | +} |
| 27 | + |
| 28 | +func (e *clusterExtensionWrapper) GetVersion(_ context.Context, _ client.Client) string { |
| 29 | + version, _, _ := unstructured.NestedString(e.Object, "status", "install", "bundle", "version") |
| 30 | + return version |
| 31 | +} |
| 32 | + |
| 33 | +type clusterCatalogSource struct { |
| 34 | + *unstructured.Unstructured |
| 35 | +} |
| 36 | + |
| 37 | +func (c *clusterCatalogSource) Unwrap() client.Object { |
| 38 | + return c.Unstructured |
| 39 | +} |
| 40 | + |
| 41 | +func (c *clusterCatalogSource) IsReady(_ context.Context, _ client.Client) bool { |
| 42 | + return meta.IsStatusConditionTrue(conditions(c.Object), "Serving") |
| 43 | +} |
| 44 | + |
| 45 | +func (c *clusterCatalogSource) UpdateSourceImage(s string) { |
| 46 | + _ = unstructured.SetNestedField(c.Object, s, "spec", "source", "image", "ref") |
| 47 | +} |
| 48 | + |
| 49 | +func OlmV1Installer(ctx context.Context, cli client.Client, catalogImage, ns, packageName string) (Extension, ExtensionSource, error) { |
| 50 | + sa := &coreV1.ServiceAccount{ |
| 51 | + ObjectMeta: metav1.ObjectMeta{ |
| 52 | + Name: fmt.Sprintf("%s-installer", packageName), |
| 53 | + Namespace: ns, |
| 54 | + }, |
| 55 | + } |
| 56 | + |
| 57 | + crb := &rbacV1.ClusterRoleBinding{ |
| 58 | + ObjectMeta: metav1.ObjectMeta{ |
| 59 | + Name: fmt.Sprintf("%s-installer-%s", packageName, ns), |
| 60 | + }, |
| 61 | + RoleRef: rbacV1.RoleRef{ |
| 62 | + APIGroup: coreV1.SchemeGroupVersion.Group, |
| 63 | + Kind: "ClusterRole", |
| 64 | + Name: "cluster-admin", |
| 65 | + }, |
| 66 | + Subjects: []rbacV1.Subject{{Kind: "ServiceAccount", Name: sa.Name, Namespace: ns}}, |
| 67 | + } |
| 68 | + |
| 69 | + selector := map[string]string{"catalog": "test"} |
| 70 | + |
| 71 | + cs := &clusterCatalogSource{ |
| 72 | + Unstructured: &unstructured.Unstructured{}, |
| 73 | + } |
| 74 | + cs.SetKind("ClusterCatalog") |
| 75 | + cs.SetAPIVersion("olm.operatorframework.io/v1") |
| 76 | + cs.SetName(fmt.Sprintf("%s-catalog", packageName)) |
| 77 | + cs.SetLabels(selector) |
| 78 | + if err := unstructured.SetNestedMap(cs.Object, map[string]interface{}{ |
| 79 | + "availabilityMode": "Available", |
| 80 | + "priority": int64(-300), |
| 81 | + "source": map[string]interface{}{ |
| 82 | + "type": "Image", |
| 83 | + "image": map[string]interface{}{ |
| 84 | + "ref": catalogImage, |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, "spec"); err != nil { |
| 88 | + return nil, nil, err |
| 89 | + } |
| 90 | + |
| 91 | + es := &clusterExtensionWrapper{ |
| 92 | + Unstructured: &unstructured.Unstructured{}, |
| 93 | + } |
| 94 | + es.SetKind("ClusterExtension") |
| 95 | + es.SetAPIVersion("olm.operatorframework.io/v1") |
| 96 | + es.SetName(packageName) |
| 97 | + |
| 98 | + if err := unstructured.SetNestedMap(es.Object, map[string]interface{}{ |
| 99 | + "namespace": ns, |
| 100 | + "serviceAccount": map[string]interface{}{"name": sa.Name}, |
| 101 | + "source": map[string]interface{}{ |
| 102 | + "sourceType": "Catalog", |
| 103 | + "catalog": map[string]interface{}{ |
| 104 | + "packageName": packageName, |
| 105 | + }, |
| 106 | + }, |
| 107 | + }, "spec"); err != nil { |
| 108 | + return nil, nil, err |
| 109 | + } |
| 110 | + if err := unstructured.SetNestedStringMap(cs.Object, selector, "spec", "source", "catalog", "selector", "matchLabels"); err != nil { |
| 111 | + return nil, nil, err |
| 112 | + } |
| 113 | + |
| 114 | + for _, obj := range []client.Object{sa, crb, es.Unwrap(), cs.Unwrap()} { |
| 115 | + if err := cli.Create(ctx, obj); err != nil { |
| 116 | + return nil, nil, err |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return es, cs, nil |
| 121 | +} |
| 122 | + |
| 123 | +func conditions(object map[string]interface{}) []metav1.Condition { |
| 124 | + var ( |
| 125 | + conditionsSlice []interface{} |
| 126 | + found bool |
| 127 | + err error |
| 128 | + ) |
| 129 | + if conditionsSlice, found, err = unstructured.NestedSlice(object, "status", "conditions"); !found || err != nil { |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + jsonBytes, err := json.Marshal(conditionsSlice) |
| 134 | + if err != nil { |
| 135 | + return nil |
| 136 | + } |
| 137 | + |
| 138 | + var typedConditions []metav1.Condition |
| 139 | + if err := json.Unmarshal(jsonBytes, &typedConditions); err != nil { |
| 140 | + return nil |
| 141 | + } |
| 142 | + return typedConditions |
| 143 | +} |
0 commit comments