diff --git a/api/v1/discoveryconfig_types.go b/api/v1/discoveryconfig_types.go index 39c45114..4f54a8f2 100644 --- a/api/v1/discoveryconfig_types.go +++ b/api/v1/discoveryconfig_types.go @@ -46,6 +46,8 @@ type DiscoveryConfigSpec struct { // Sets restrictions on what kind of clusters to discover // +optional Filters Filter `json:"filters,omitempty"` + + ImportAllRosaAsManaged bool `json:"importAllRosaAsManaged"` } // DiscoveryConfigStatus defines the observed state of DiscoveryConfig diff --git a/api/v1/zz_generated.deepcopy.go b/api/v1/zz_generated.deepcopy.go index 60968ec0..d971a374 100644 --- a/api/v1/zz_generated.deepcopy.go +++ b/api/v1/zz_generated.deepcopy.go @@ -87,14 +87,14 @@ func (in *DiscoveredClusterList) DeepCopyObject() runtime.Object { // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *DiscoveredClusterSpec) DeepCopyInto(out *DiscoveredClusterSpec) { *out = *in - if in.CreationTimestamp != nil { - in, out := &in.CreationTimestamp, &out.CreationTimestamp - *out = (*in).DeepCopy() - } if in.ActivityTimestamp != nil { in, out := &in.ActivityTimestamp, &out.ActivityTimestamp *out = (*in).DeepCopy() } + if in.CreationTimestamp != nil { + in, out := &in.CreationTimestamp, &out.CreationTimestamp + *out = (*in).DeepCopy() + } out.Credential = in.Credential } diff --git a/bundle/manifests/discovery.open-cluster-management.io_discoveryconfigs.yaml b/bundle/manifests/discovery.open-cluster-management.io_discoveryconfigs.yaml index a102c86e..0d1601fe 100644 --- a/bundle/manifests/discovery.open-cluster-management.io_discoveryconfigs.yaml +++ b/bundle/manifests/discovery.open-cluster-management.io_discoveryconfigs.yaml @@ -56,6 +56,12 @@ spec: type: string type: array type: object + importAllRosaAsManaged: + default: false + description: | + 'ImportAllRosaAsManaged when enabled, this setting allows automatic import of all + Red Hat Openshift Service on AWS (ROSA) clusters within the specified context.' + type: boolean required: - credential type: object diff --git a/config/crd/bases/discovery.open-cluster-management.io_discoveryconfigs.yaml b/config/crd/bases/discovery.open-cluster-management.io_discoveryconfigs.yaml index 605258ab..0d597981 100644 --- a/config/crd/bases/discovery.open-cluster-management.io_discoveryconfigs.yaml +++ b/config/crd/bases/discovery.open-cluster-management.io_discoveryconfigs.yaml @@ -57,6 +57,12 @@ spec: type: string type: array type: object + importAllRosaAsManaged: + default: false + description: | + 'ImportAllRosaAsManaged when enabled, this setting allows automatic import of all + Red Hat Openshift Service on AWS (ROSA) clusters within the specified context.' + type: boolean required: - credential type: object diff --git a/config/samples/discovery_v1_discoveryconfig.yaml b/config/samples/discovery_v1_discoveryconfig.yaml index d6fe396d..721b43b2 100644 --- a/config/samples/discovery_v1_discoveryconfig.yaml +++ b/config/samples/discovery_v1_discoveryconfig.yaml @@ -6,3 +6,4 @@ spec: credential: ocm-api-token filters: lastActive: 7 + importAllRosaAsManaged: false diff --git a/controllers/discoveredcluster_controller_test.go b/controllers/discoveredcluster_controller_test.go index 0f29c802..891f1794 100644 --- a/controllers/discoveredcluster_controller_test.go +++ b/controllers/discoveredcluster_controller_test.go @@ -61,7 +61,8 @@ func Test_DiscoveredCluster_Reconciler_Reconcile(t *testing.T) { Namespace: "discovery", }, Spec: discovery.DiscoveryConfigSpec{ - Credential: "fake-admin", + Credential: "fake-admin", + ImportAllRosaAsManaged: false, }, }, ns: &corev1.Namespace{ @@ -343,7 +344,8 @@ func Test_Reconciler_EnsureAutoImportSecret(t *testing.T) { Namespace: "discovery", }, Spec: discovery.DiscoveryConfigSpec{ - Credential: "admin", + Credential: "admin", + ImportAllRosaAsManaged: false, }, }, dc: &discovery.DiscoveredCluster{ diff --git a/controllers/discoveryconfig_controller.go b/controllers/discoveryconfig_controller.go index a17af401..02879d2d 100644 --- a/controllers/discoveryconfig_controller.go +++ b/controllers/discoveryconfig_controller.go @@ -149,8 +149,10 @@ func (r *DiscoveryConfigReconciler) updateDiscoveredClusters(ctx context.Context discovered, err := []discovery.DiscoveredCluster{}, nil if val, ok := os.LookupEnv("UNIT_TEST"); ok && val == "true" { discovered, err = mockDiscoveredCluster() + } else { - discovered, err = ocm.DiscoverClusters(userToken, baseURL, baseAuthURL, filters) + discovered, err = ocm.DiscoverClusters(userToken, baseURL, baseAuthURL, filters, + config.Spec.ImportAllRosaAsManaged) } if err != nil { diff --git a/controllers/discoveryconfig_controller_test.go b/controllers/discoveryconfig_controller_test.go index 767f9b63..dbd282bf 100644 --- a/controllers/discoveryconfig_controller_test.go +++ b/controllers/discoveryconfig_controller_test.go @@ -105,8 +105,9 @@ var _ = Describe("Discoveryconfig controller", func() { Namespace: TestDiscoveryNamespace, }, Spec: discovery.DiscoveryConfigSpec{ - Credential: TestSecretName, - Filters: discovery.Filter{LastActive: 7}, + Credential: TestSecretName, + Filters: discovery.Filter{LastActive: 7}, + ImportAllRosaAsManaged: false, }, })).Should(Succeed()) }) @@ -194,8 +195,9 @@ var _ = Describe("Discoveryconfig controller", func() { Namespace: "invalid", }, Spec: discovery.DiscoveryConfigSpec{ - Credential: TestSecretName, - Filters: discovery.Filter{LastActive: 7}, + Credential: TestSecretName, + Filters: discovery.Filter{LastActive: 7}, + ImportAllRosaAsManaged: false, }, })).Should(Succeed()) }) diff --git a/controllers/managedcluster_controller_test.go b/controllers/managedcluster_controller_test.go index fce7e008..52ad176b 100644 --- a/controllers/managedcluster_controller_test.go +++ b/controllers/managedcluster_controller_test.go @@ -99,8 +99,9 @@ var _ = Describe("ManagedCluster controller", func() { Namespace: TestManagedNamespace, }, Spec: discovery.DiscoveryConfigSpec{ - Credential: TestManagedSecretName, - Filters: discovery.Filter{LastActive: 7}, + Credential: TestManagedSecretName, + Filters: discovery.Filter{LastActive: 7}, + ImportAllRosaAsManaged: false, }, })).Should(Succeed()) }) diff --git a/pkg/ocm/ocm.go b/pkg/ocm/ocm.go index c345d759..af062c99 100644 --- a/pkg/ocm/ocm.go +++ b/pkg/ocm/ocm.go @@ -15,7 +15,8 @@ import ( // DiscoverClusters returns a list of DiscoveredClusters found in both the accounts_mgmt and // accounts_mgmt apis with the given filters -func DiscoverClusters(token string, baseURL string, baseAuthURL string, filters discovery.Filter) ([]discovery.DiscoveredCluster, error) { +func DiscoverClusters(token string, baseURL string, baseAuthURL string, filters discovery.Filter, importAllRosa bool) ( + []discovery.DiscoveredCluster, error) { // Request ephemeral access token with user token. This will be used for OCM requests authRequest := auth.AuthRequest{ Token: token, @@ -41,7 +42,7 @@ func DiscoverClusters(token string, baseURL string, baseAuthURL string, filters var discoveredClusters []discovery.DiscoveredCluster for _, sub := range subscriptions { // Build a DiscoveredCluster object from the subscription information - if dc, valid := formatCluster(sub); valid { + if dc, valid := formatCluster(sub, importAllRosa); valid { discoveredClusters = append(discoveredClusters, dc) } } @@ -50,7 +51,7 @@ func DiscoverClusters(token string, baseURL string, baseAuthURL string, filters } // formatCluster converts a cluster from OCM form to DiscoveredCluster form, or returns false if it is not valid -func formatCluster(sub subscription.Subscription) (discovery.DiscoveredCluster, bool) { +func formatCluster(sub subscription.Subscription, importAllRosa bool) (discovery.DiscoveredCluster, bool) { discoveredCluster := discovery.DiscoveredCluster{} // TODO: consider refactoring to "filter" clusters ouside this function to retain function clarity if len(sub.Metrics) == 0 { @@ -62,6 +63,12 @@ func formatCluster(sub subscription.Subscription) (discovery.DiscoveredCluster, if sub.Status == "Reserved" { return discoveredCluster, false } + + shouldAutoImport := false + if computeType(sub) == "ROSA" && importAllRosa { + shouldAutoImport = true + } + discoveredCluster = discovery.DiscoveredCluster{ TypeMeta: metav1.TypeMeta{ APIVersion: "operator.open-cluster-management.io/v1", @@ -77,6 +84,7 @@ func formatCluster(sub subscription.Subscription) (discovery.DiscoveredCluster, Console: sub.ConsoleURL, CreationTimestamp: sub.CreatedAt, DisplayName: computeDisplayName(sub), + EnableAutoImport: shouldAutoImport, Name: sub.ExternalClusterID, OCPClusterID: sub.ExternalClusterID, OpenshiftVersion: sub.Metrics[0].OpenShiftVersion, diff --git a/pkg/ocm/ocm_test.go b/pkg/ocm/ocm_test.go index b18f7d22..77fc0dc9 100644 --- a/pkg/ocm/ocm_test.go +++ b/pkg/ocm/ocm_test.go @@ -112,7 +112,7 @@ func TestDiscoverClusters(t *testing.T) { // TODO: Running `getSubscriptionsFunc` should yield the subscriptions to test against, but we don't do this getSubscriptionsFunc = tt.subscriptionFunc - got, err := DiscoverClusters(tt.args.token, tt.args.baseURL, tt.args.baseAuthURL, tt.args.filters) + got, err := DiscoverClusters(tt.args.token, tt.args.baseURL, tt.args.baseAuthURL, tt.args.filters, false) if (err != nil) != tt.wantErr { t.Errorf("DiscoverClusters() error = %v, wantErr %v", err, tt.wantErr) return @@ -346,7 +346,7 @@ func TestFormatCLusterError(t *testing.T) { } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - if _, got := formatCluster(tt.sub); got != tt.want { + if _, got := formatCluster(tt.sub, false); got != tt.want { t.Errorf("formatCluster() = %v, want %v", got, tt.want) } }) diff --git a/test/e2e/discovery_test.go b/test/e2e/discovery_test.go index 5ef26217..1017b617 100644 --- a/test/e2e/discovery_test.go +++ b/test/e2e/discovery_test.go @@ -727,8 +727,9 @@ func defaultDiscoveryConfig() *discovery.DiscoveryConfig { Namespace: discoveryNamespace, }, Spec: discovery.DiscoveryConfigSpec{ - Credential: SecretName, - Filters: discovery.Filter{LastActive: 7}, + Credential: SecretName, + Filters: discovery.Filter{LastActive: 7}, + ImportAllRosaAsManaged: false, }, } } diff --git a/test/scale/scale.go b/test/scale/scale.go index 237b9d70..4fe9017a 100644 --- a/test/scale/scale.go +++ b/test/scale/scale.go @@ -142,8 +142,9 @@ func createConfig(ns string) error { Namespace: ns, }, Spec: discovery.DiscoveryConfigSpec{ - Credential: SecretName, - Filters: discovery.Filter{LastActive: 7}, + Credential: SecretName, + Filters: discovery.Filter{LastActive: 7}, + ImportAllRosaAsManaged: false, }, }