Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP][ACM-11050] Added DiscoveryConfig spec importAllRosaAsManaged field #236

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api/v1/discoveryconfig_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions config/samples/discovery_v1_discoveryconfig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ spec:
credential: ocm-api-token
filters:
lastActive: 7
importAllRosaAsManaged: false
6 changes: 4 additions & 2 deletions controllers/discoveredcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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{
Expand Down
4 changes: 3 additions & 1 deletion controllers/discoveryconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 6 additions & 4 deletions controllers/discoveryconfig_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down Expand Up @@ -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())
})
Expand Down
5 changes: 3 additions & 2 deletions controllers/managedcluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
Expand Down
14 changes: 11 additions & 3 deletions pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)
}
}
Expand All @@ -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 {
Expand All @@ -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",
Expand All @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pkg/ocm/ocm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
})
Expand Down
5 changes: 3 additions & 2 deletions test/e2e/discovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}
}
Expand Down
5 changes: 3 additions & 2 deletions test/scale/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
}

Expand Down
Loading