From d3a2e9462ac966ddb83d7a8e07015026b8f244ad Mon Sep 17 00:00:00 2001 From: fabriziopandini Date: Tue, 25 May 2021 14:23:47 +0200 Subject: [PATCH] Remove clusterctl --watching-namespace --- cmd/clusterctl/client/client.go | 2 +- cmd/clusterctl/client/cluster/upgrader.go | 5 +- cmd/clusterctl/client/common.go | 4 +- cmd/clusterctl/client/config.go | 7 +- cmd/clusterctl/client/config_test.go | 23 +-- cmd/clusterctl/client/init.go | 23 +-- cmd/clusterctl/client/init_test.go | 134 ++++++--------- .../client/repository/components.go | 161 ++---------------- .../repository/components_client_test.go | 130 +++++--------- .../client/repository/components_test.go | 149 ---------------- cmd/clusterctl/client/upgrade_test.go | 2 +- cmd/clusterctl/cmd/config_provider.go | 9 +- cmd/clusterctl/cmd/generate_provider.go | 6 +- cmd/clusterctl/cmd/init.go | 7 - cmd/clusterctl/cmd/util.go | 1 - 15 files changed, 143 insertions(+), 520 deletions(-) diff --git a/cmd/clusterctl/client/client.go b/cmd/clusterctl/client/client.go index 0731b64191a8..488667cd6f5d 100644 --- a/cmd/clusterctl/client/client.go +++ b/cmd/clusterctl/client/client.go @@ -30,7 +30,7 @@ type Client interface { // GetProvidersConfig returns the list of providers configured for this instance of clusterctl. GetProvidersConfig() ([]Provider, error) - // GetProviderComponents returns the provider components for a given provider with options including targetNamespace, watchingNamespace. + // GetProviderComponents returns the provider components for a given provider with options including targetNamespace. GetProviderComponents(provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) // Init initializes a management cluster by adding the requested list of providers. diff --git a/cmd/clusterctl/client/cluster/upgrader.go b/cmd/clusterctl/client/cluster/upgrader.go index ea85f1478815..7c5bdcc06124 100644 --- a/cmd/clusterctl/client/cluster/upgrader.go +++ b/cmd/clusterctl/client/cluster/upgrader.go @@ -343,9 +343,8 @@ func (u *providerUpgrader) getUpgradeComponents(provider UpgradeItem) (repositor } options := repository.ComponentsOptions{ - Version: provider.NextVersion, - TargetNamespace: provider.Namespace, - WatchingNamespace: provider.WatchedNamespace, + Version: provider.NextVersion, + TargetNamespace: provider.Namespace, } components, err := providerRepository.Components().Get(options) if err != nil { diff --git a/cmd/clusterctl/client/common.go b/cmd/clusterctl/client/common.go index 8239ca122062..9924e788fea3 100644 --- a/cmd/clusterctl/client/common.go +++ b/cmd/clusterctl/client/common.go @@ -26,7 +26,7 @@ import ( ) // getComponentsByName is a utility method that returns components -// for a given provider with options including targetNamespace, and watchingNamespace. +// for a given provider with options including targetNamespace. func (c *clusterctlClient) getComponentsByName(provider string, providerType clusterctlv1.ProviderType, options repository.ComponentsOptions) (repository.Components, error) { // Parse the abbreviated syntax for name[:version] name, version, err := parseProviderName(provider) @@ -43,7 +43,7 @@ func (c *clusterctlClient) getComponentsByName(provider string, providerType clu // Get a client for the provider repository and read the provider components; // during the process, provider components will be processed performing variable substitution, customization of target - // and watching namespace etc. + // namespace etc. // Currently we are not supporting custom yaml processors for the provider // components. So we revert to using the default SimpleYamlProcessor. repositoryClientFactory, err := c.repositoryClientFactory(RepositoryClientFactoryInput{Provider: providerConfig}) diff --git a/cmd/clusterctl/client/config.go b/cmd/clusterctl/client/config.go index c6347f8045cc..7b0c4b7b2b13 100644 --- a/cmd/clusterctl/client/config.go +++ b/cmd/clusterctl/client/config.go @@ -48,10 +48,9 @@ func (c *clusterctlClient) GetProvidersConfig() ([]Provider, error) { func (c *clusterctlClient) GetProviderComponents(provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) { // ComponentsOptions is an alias for repository.ComponentsOptions; this makes the conversion inputOptions := repository.ComponentsOptions{ - Version: options.Version, - TargetNamespace: options.TargetNamespace, - WatchingNamespace: options.WatchingNamespace, - SkipVariables: options.SkipVariables, + Version: options.Version, + TargetNamespace: options.TargetNamespace, + SkipVariables: options.SkipVariables, } components, err := c.getComponentsByName(provider, providerType, inputOptions) if err != nil { diff --git a/cmd/clusterctl/client/config_test.go b/cmd/clusterctl/client/config_test.go index 78baf0c325d7..f03c084f3e9e 100644 --- a/cmd/clusterctl/client/config_test.go +++ b/cmd/clusterctl/client/config_test.go @@ -137,9 +137,8 @@ func Test_clusterctlClient_GetProviderComponents(t *testing.T) { WithRepository(repository1) type args struct { - provider string - targetNameSpace string - watchingNamespace string + provider string + targetNameSpace string } type want struct { provider config.Provider @@ -154,9 +153,8 @@ func Test_clusterctlClient_GetProviderComponents(t *testing.T) { { name: "Pass", args: args{ - provider: capiProviderConfig.Name(), - targetNameSpace: "ns2", - watchingNamespace: "", + provider: capiProviderConfig.Name(), + targetNameSpace: "ns2", }, want: want{ provider: capiProviderConfig, @@ -167,9 +165,8 @@ func Test_clusterctlClient_GetProviderComponents(t *testing.T) { { name: "Fail", args: args{ - provider: fmt.Sprintf("%s:v0.2.0", capiProviderConfig.Name()), - targetNameSpace: "ns2", - watchingNamespace: "", + provider: fmt.Sprintf("%s:v0.2.0", capiProviderConfig.Name()), + targetNameSpace: "ns2", }, wantErr: true, }, @@ -179,8 +176,7 @@ func Test_clusterctlClient_GetProviderComponents(t *testing.T) { g := NewWithT(t) options := ComponentsOptions{ - TargetNamespace: tt.args.targetNameSpace, - WatchingNamespace: tt.args.watchingNamespace, + TargetNamespace: tt.args.targetNameSpace, } got, err := client.GetProviderComponents(tt.args.provider, capiProviderConfig.Type(), options) if tt.wantErr { @@ -224,9 +220,8 @@ func Test_getComponentsByName_withEmptyVariables(t *testing.T) { WithCluster(cluster1) options := ComponentsOptions{ - TargetNamespace: "ns1", - WatchingNamespace: "", - SkipVariables: true, + TargetNamespace: "ns1", + SkipVariables: true, } components, err := client.GetProviderComponents(repository1Config.Name(), repository1Config.Type(), options) g.Expect(err).NotTo(HaveOccurred()) diff --git a/cmd/clusterctl/client/init.go b/cmd/clusterctl/client/init.go index 6b09237de21f..9fc08c1f7722 100644 --- a/cmd/clusterctl/client/init.go +++ b/cmd/clusterctl/client/init.go @@ -54,10 +54,6 @@ type InitOptions struct { // will be installed in a provider's default namespace. TargetNamespace string - // WatchingNamespace defines the namespace the providers should watch to reconcile Cluster API objects. - // If unspecified, the providers watches for Cluster API objects across all namespaces. - WatchingNamespace string - // LogUsageInstructions instructs the init command to print the usage instructions in case of first run. LogUsageInstructions bool @@ -193,10 +189,9 @@ func (c *clusterctlClient) setupInstaller(cluster cluster.Client, options InitOp installer := cluster.ProviderInstaller() addOptions := addToInstallerOptions{ - installer: installer, - targetNamespace: options.TargetNamespace, - watchingNamespace: options.WatchingNamespace, - skipVariables: options.skipVariables, + installer: installer, + targetNamespace: options.TargetNamespace, + skipVariables: options.skipVariables, } if options.CoreProvider != "" { @@ -245,10 +240,9 @@ func (c *clusterctlClient) addDefaultProviders(cluster cluster.Client, options * } type addToInstallerOptions struct { - installer cluster.ProviderInstaller - targetNamespace string - watchingNamespace string - skipVariables bool + installer cluster.ProviderInstaller + targetNamespace string + skipVariables bool } // addToInstaller adds the components to the install queue and checks that the actual provider type match the target group. @@ -262,9 +256,8 @@ func (c *clusterctlClient) addToInstaller(options addToInstallerOptions, provide continue } componentsOptions := repository.ComponentsOptions{ - TargetNamespace: options.targetNamespace, - WatchingNamespace: options.watchingNamespace, - SkipVariables: options.skipVariables, + TargetNamespace: options.targetNamespace, + SkipVariables: options.skipVariables, } components, err := c.getComponentsByName(provider, providerType, componentsOptions) if err != nil { diff --git a/cmd/clusterctl/client/init_test.go b/cmd/clusterctl/client/init_test.go index 41ff5c8da242..d434342dd1b4 100644 --- a/cmd/clusterctl/client/init_test.go +++ b/cmd/clusterctl/client/init_test.go @@ -215,13 +215,11 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider []string infrastructureProvider []string targetNameSpace string - watchingNamespace string } type want struct { - provider Provider - version string - targetNamespace string - watchingNamespace string + provider Provider + version string + targetNamespace string } tests := []struct { @@ -256,32 +254,27 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: nil, // with an empty cluster, a control plane provider should be added automatically infrastructureProvider: []string{"infra"}, targetNameSpace: "", - watchingNamespace: "", }, want: []want{ { - provider: capiProviderConfig, - version: "v1.0.0", - targetNamespace: "ns1", - watchingNamespace: "", + provider: capiProviderConfig, + version: "v1.0.0", + targetNamespace: "ns1", }, { - provider: bootstrapProviderConfig, - version: "v2.0.0", - targetNamespace: "ns2", - watchingNamespace: "", + provider: bootstrapProviderConfig, + version: "v2.0.0", + targetNamespace: "ns2", }, { - provider: controlPlaneProviderConfig, - version: "v2.0.0", - targetNamespace: "ns3", - watchingNamespace: "", + provider: controlPlaneProviderConfig, + version: "v2.0.0", + targetNamespace: "ns3", }, { - provider: infraProviderConfig, - version: "v3.0.0", - targetNamespace: "ns4", - watchingNamespace: "", + provider: infraProviderConfig, + version: "v3.0.0", + targetNamespace: "ns4", }, }, wantErr: false, @@ -298,20 +291,17 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: []string{"-"}, // opt-out from the automatic control plane provider installation infrastructureProvider: []string{"infra"}, targetNameSpace: "", - watchingNamespace: "", }, want: []want{ { - provider: capiProviderConfig, - version: "v1.0.0", - targetNamespace: "ns1", - watchingNamespace: "", + provider: capiProviderConfig, + version: "v1.0.0", + targetNamespace: "ns1", }, { - provider: infraProviderConfig, - version: "v3.0.0", - targetNamespace: "ns4", - watchingNamespace: "", + provider: infraProviderConfig, + version: "v3.0.0", + targetNamespace: "ns4", }, }, wantErr: false, @@ -328,32 +318,27 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: []string{fmt.Sprintf("%s:v2.1.0", config.KubeadmControlPlaneProviderName)}, infrastructureProvider: []string{"infra:v3.1.0"}, targetNameSpace: "", - watchingNamespace: "", }, want: []want{ { - provider: capiProviderConfig, - version: "v1.1.0", - targetNamespace: "ns1", - watchingNamespace: "", + provider: capiProviderConfig, + version: "v1.1.0", + targetNamespace: "ns1", }, { - provider: bootstrapProviderConfig, - version: "v2.1.0", - targetNamespace: "ns2", - watchingNamespace: "", + provider: bootstrapProviderConfig, + version: "v2.1.0", + targetNamespace: "ns2", }, { - provider: controlPlaneProviderConfig, - version: "v2.1.0", - targetNamespace: "ns3", - watchingNamespace: "", + provider: controlPlaneProviderConfig, + version: "v2.1.0", + targetNamespace: "ns3", }, { - provider: infraProviderConfig, - version: "v3.1.0", - targetNamespace: "ns4", - watchingNamespace: "", + provider: infraProviderConfig, + version: "v3.1.0", + targetNamespace: "ns4", }, }, wantErr: false, @@ -369,32 +354,27 @@ func Test_clusterctlClient_Init(t *testing.T) { bootstrapProvider: []string{config.KubeadmBootstrapProviderName}, infrastructureProvider: []string{"infra"}, targetNameSpace: "nsx", - watchingNamespace: "", }, want: []want{ { - provider: capiProviderConfig, - version: "v1.0.0", - targetNamespace: "nsx", - watchingNamespace: "", + provider: capiProviderConfig, + version: "v1.0.0", + targetNamespace: "nsx", }, { - provider: bootstrapProviderConfig, - version: "v2.0.0", - targetNamespace: "nsx", - watchingNamespace: "", + provider: bootstrapProviderConfig, + version: "v2.0.0", + targetNamespace: "nsx", }, { - provider: controlPlaneProviderConfig, - version: "v2.0.0", - targetNamespace: "nsx", - watchingNamespace: "", + provider: controlPlaneProviderConfig, + version: "v2.0.0", + targetNamespace: "nsx", }, { - provider: infraProviderConfig, - version: "v3.0.0", - targetNamespace: "nsx", - watchingNamespace: "", + provider: infraProviderConfig, + version: "v3.0.0", + targetNamespace: "nsx", }, }, wantErr: false, @@ -410,20 +390,17 @@ func Test_clusterctlClient_Init(t *testing.T) { bootstrapProvider: []string{config.KubeadmBootstrapProviderName}, infrastructureProvider: []string{"infra"}, targetNameSpace: "", - watchingNamespace: "", }, want: []want{ { - provider: bootstrapProviderConfig, - version: "v2.0.0", - targetNamespace: "ns2", - watchingNamespace: "", + provider: bootstrapProviderConfig, + version: "v2.0.0", + targetNamespace: "ns2", }, { - provider: infraProviderConfig, - version: "v3.0.0", - targetNamespace: "ns4", - watchingNamespace: "", + provider: infraProviderConfig, + version: "v3.0.0", + targetNamespace: "ns4", }, }, wantErr: false, @@ -439,7 +416,6 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: nil, infrastructureProvider: nil, targetNameSpace: "", - watchingNamespace: "", }, want: nil, wantErr: true, @@ -455,7 +431,6 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: nil, infrastructureProvider: nil, targetNameSpace: "", - watchingNamespace: "", }, want: nil, wantErr: true, @@ -471,7 +446,6 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: nil, infrastructureProvider: nil, targetNameSpace: "", - watchingNamespace: "", }, want: nil, wantErr: true, @@ -487,7 +461,6 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: []string{"infra"}, // wrong infrastructureProvider: nil, targetNameSpace: "", - watchingNamespace: "", }, want: nil, wantErr: true, @@ -503,7 +476,6 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: nil, infrastructureProvider: []string{config.KubeadmBootstrapProviderName}, // wrong targetNameSpace: "", - watchingNamespace: "", }, want: nil, wantErr: true, @@ -520,7 +492,6 @@ func Test_clusterctlClient_Init(t *testing.T) { controlPlaneProvider: []string{fmt.Sprintf("%s:v0.9.0", config.KubeadmControlPlaneProviderName)}, infrastructureProvider: []string{"infra:v0.9.0"}, targetNameSpace: "", - watchingNamespace: "", }, wantErr: true, }, @@ -535,7 +506,6 @@ func Test_clusterctlClient_Init(t *testing.T) { bootstrapProvider: []string{fmt.Sprintf("%s:v0.9.0", config.KubeadmBootstrapProviderName)}, infrastructureProvider: []string{"infra:v0.9.0"}, targetNameSpace: "", - watchingNamespace: "", }, wantErr: true, }, @@ -556,7 +526,6 @@ func Test_clusterctlClient_Init(t *testing.T) { ControlPlaneProviders: tt.args.controlPlaneProvider, InfrastructureProviders: tt.args.infrastructureProvider, TargetNamespace: tt.args.targetNameSpace, - WatchingNamespace: tt.args.watchingNamespace, }) if tt.wantErr { g.Expect(err).To(HaveOccurred()) @@ -571,7 +540,6 @@ func Test_clusterctlClient_Init(t *testing.T) { g.Expect(gItem.Type()).To(Equal(w.provider.Type())) g.Expect(gItem.Version()).To(Equal(w.version)) g.Expect(gItem.TargetNamespace()).To(Equal(w.targetNamespace)) - g.Expect(gItem.WatchingNamespace()).To(Equal(w.watchingNamespace)) } }) } diff --git a/cmd/clusterctl/client/repository/components.go b/cmd/clusterctl/client/repository/components.go index cdcb12f41207..db193627f943 100644 --- a/cmd/clusterctl/client/repository/components.go +++ b/cmd/clusterctl/client/repository/components.go @@ -18,10 +18,8 @@ package repository import ( "fmt" - "strings" "github.com/pkg/errors" - appsv1 "k8s.io/api/apps/v1" rbacv1 "k8s.io/api/rbac/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" @@ -82,11 +80,6 @@ type Components interface { // during the creation of the Components object. TargetNamespace() string - // WatchingNamespace defines the namespace where the provider controller is is watching (empty means all namespaces). - // By default this value is derived by the component YAML, but it is possible to override it - // during the creation of the Components object. - WatchingNamespace() string - // InventoryObject returns the clusterctl inventory object representing the provider that will be // generated by this components. InventoryObject() clusterctlv1.Provider @@ -104,13 +97,12 @@ type Components interface { // components implement Components. type components struct { config.Provider - version string - variables []string - images []string - targetNamespace string - watchingNamespace string - instanceObjs []unstructured.Unstructured - sharedObjs []unstructured.Unstructured + version string + variables []string + images []string + targetNamespace string + instanceObjs []unstructured.Unstructured + sharedObjs []unstructured.Unstructured } // ensure components implement Components. @@ -132,10 +124,6 @@ func (c *components) TargetNamespace() string { return c.targetNamespace } -func (c *components) WatchingNamespace() string { - return c.watchingNamespace -} - func (c *components) InventoryObject() clusterctlv1.Provider { labels := getCommonLabels(c.Provider) labels[clusterctlv1.ClusterctlCoreLabelName] = "inventory" @@ -150,10 +138,9 @@ func (c *components) InventoryObject() clusterctlv1.Provider { Name: c.ManifestLabel(), Labels: labels, }, - ProviderName: c.Name(), - Type: string(c.Type()), - Version: c.version, - WatchedNamespace: c.watchingNamespace, + ProviderName: c.Name(), + Type: string(c.Type()), + Version: c.version, } } @@ -176,9 +163,8 @@ func (c *components) Yaml() ([]byte, error) { // ComponentsOptions represents specific inputs that are passed in to // clusterctl library. These are user specified inputs. type ComponentsOptions struct { - Version string - TargetNamespace string - WatchingNamespace string + Version string + TargetNamespace string // Allows for skipping variable replacement in the component YAML SkipVariables bool } @@ -277,21 +263,6 @@ func NewComponents(input ComponentsInput) (Components, error) { return nil, errors.Wrap(err, "failed to fix ClusterRoleBinding names") } - // inspect the list of objects for the default watching namespace - // the default watching namespace is the namespace the controller is set for watching in the component yaml read from the repository, if any - defaultWatchingNamespace, err := inspectWatchNamespace(instanceObjs) - if err != nil { - return nil, errors.Wrap(err, "failed to detect default watching namespace") - } - - // if the requested watchingNamespace is different from the defaultWatchingNamespace, fix it - if defaultWatchingNamespace != input.Options.WatchingNamespace { - instanceObjs, err = fixWatchNamespace(instanceObjs, input.Options.WatchingNamespace) - if err != nil { - return nil, errors.Wrap(err, "failed to set watching namespace") - } - } - // Add common labels to both the obj groups. instanceObjs = addCommonLabels(instanceObjs, input.Provider) sharedObjs = addCommonLabels(sharedObjs, input.Provider) @@ -302,14 +273,13 @@ func NewComponents(input ComponentsInput) (Components, error) { sharedObjs = fixSharedLabels(sharedObjs) return &components{ - Provider: input.Provider, - version: input.Options.Version, - variables: variables, - images: images, - targetNamespace: input.Options.TargetNamespace, - watchingNamespace: input.Options.WatchingNamespace, - instanceObjs: instanceObjs, - sharedObjs: sharedObjs, + Provider: input.Provider, + version: input.Options.Version, + variables: variables, + images: images, + targetNamespace: input.Options.TargetNamespace, + instanceObjs: instanceObjs, + sharedObjs: sharedObjs, }, nil } @@ -480,101 +450,6 @@ func fixRBAC(objs []unstructured.Unstructured, targetNamespace string) ([]unstru return objs, nil } -// inspectWatchNamespace inspects the list of components objects for the default watching namespace -// the default watching namespace is the namespace the controller is set for watching in the component yaml read from the repository, if any. -func inspectWatchNamespace(objs []unstructured.Unstructured) (string, error) { - namespace := "" - // look for resources of kind Deployment - for i := range objs { - o := objs[i] - if o.GetKind() != deploymentKind { - continue - } - - // Convert Unstructured into a typed object - d := &appsv1.Deployment{} - if err := scheme.Scheme.Convert(&o, d, nil); err != nil { - return "", err - } - - // look for a container with name "manager" - for _, c := range d.Spec.Template.Spec.Containers { - if c.Name != controllerContainerName { - continue - } - - // look for the --namespace command arg - for _, a := range c.Args { - if strings.HasPrefix(a, namespaceArgPrefix) { - n := strings.TrimPrefix(a, namespaceArgPrefix) - if namespace != "" && n != namespace { - return "", errors.New("Invalid manifest. All the controllers should watch have the same --namespace command arg in the provider components yaml") - } - namespace = n - } - } - } - } - - return namespace, nil -} - -func fixWatchNamespace(objs []unstructured.Unstructured, watchingNamespace string) ([]unstructured.Unstructured, error) { - // look for resources of kind Deployment - for i := range objs { - o := objs[i] - if o.GetKind() != deploymentKind { - continue - } - - // Convert Unstructured into a typed object - d := &appsv1.Deployment{} - if err := scheme.Scheme.Convert(&o, d, nil); err != nil { - return nil, err - } - - // look for a container with name "manager" - for j, c := range d.Spec.Template.Spec.Containers { - if c.Name == controllerContainerName { - // look for the --namespace command arg - found := false - for k, a := range c.Args { - // if it exist - if strings.HasPrefix(a, namespaceArgPrefix) { - found = true - - // replace the command arg with the desired value or delete the arg if the controller should watch for objects in all the namespaces - if watchingNamespace != "" { - c.Args[k] = fmt.Sprintf("%s%s", namespaceArgPrefix, watchingNamespace) - continue - } - c.Args = remove(c.Args, k) - } - } - - // If it doesn't exist, and the controller should watch for objects in a specific namespace, set the command arg. - if !found && watchingNamespace != "" { - c.Args = append(c.Args, fmt.Sprintf("%s%s", namespaceArgPrefix, watchingNamespace)) - } - } - - d.Spec.Template.Spec.Containers[j] = c - } - - // Convert Deployment back to Unstructured - if err := scheme.Scheme.Convert(d, &o, nil); err != nil { - return nil, err - } - objs[i] = o - } - return objs, nil -} - -func remove(slice []string, i int) []string { - copy(slice[i:], slice[i+1:]) - return slice[:len(slice)-1] -} - // addCommonLabels ensures all the provider components have a consistent set of labels. func addCommonLabels(objs []unstructured.Unstructured, provider config.Provider) []unstructured.Unstructured { for _, o := range objs { diff --git a/cmd/clusterctl/client/repository/components_client_test.go b/cmd/clusterctl/client/repository/components_client_test.go index 2e542d4f56f7..1b83ab9dfa87 100644 --- a/cmd/clusterctl/client/repository/components_client_test.go +++ b/cmd/clusterctl/client/repository/components_client_test.go @@ -75,17 +75,15 @@ func Test_componentsClient_Get(t *testing.T) { processor yaml.Processor } type args struct { - version string - targetNamespace string - watchingNamespace string - skipVariables bool + version string + targetNamespace string + skipVariables bool } type want struct { - provider config.Provider - version string - targetNamespace string - watchingNamespace string - variables []string + provider config.Provider + version string + targetNamespace string + variables []string } tests := []struct { name string @@ -104,16 +102,14 @@ func Test_componentsClient_Get(t *testing.T) { WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(namespaceYaml, controllerYaml, configMapYaml)), }, args: args{ - version: "v1.0.0", - targetNamespace: "", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "", }, want: want{ - provider: p1, - version: "v1.0.0", // version detected - targetNamespace: namespaceName, // default targetNamespace detected - watchingNamespace: "", - variables: []string{variableName}, // variable detected + provider: p1, + version: "v1.0.0", // version detected + targetNamespace: namespaceName, // default targetNamespace detected + variables: []string{variableName}, // variable detected }, wantErr: false, }, @@ -127,17 +123,15 @@ func Test_componentsClient_Get(t *testing.T) { WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(namespaceYaml, controllerYaml, configMapYaml)), }, args: args{ - version: "v1.0.0", - targetNamespace: "", - watchingNamespace: "", - skipVariables: true, + version: "v1.0.0", + targetNamespace: "", + skipVariables: true, }, want: want{ - provider: p1, - version: "v1.0.0", // version detected - targetNamespace: namespaceName, // default targetNamespace detected - watchingNamespace: "", - variables: []string{variableName}, // variable detected + provider: p1, + version: "v1.0.0", // version detected + targetNamespace: namespaceName, // default targetNamespace detected + variables: []string{variableName}, // variable detected }, wantErr: false, }, @@ -151,39 +145,14 @@ func Test_componentsClient_Get(t *testing.T) { WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(namespaceYaml, controllerYaml, configMapYaml)), }, args: args{ - version: "v1.0.0", - targetNamespace: "ns2", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "ns2", }, want: want{ - provider: p1, - version: "v1.0.0", // version detected - targetNamespace: "ns2", // targetNamespace overrides default targetNamespace - watchingNamespace: "", - variables: []string{variableName}, // variable detected - }, - wantErr: false, - }, - { - name: "watchingNamespace overrides default watchingNamespace", - fields: fields{ - provider: p1, - repository: test.NewFakeRepository(). - WithPaths("root", "components.yaml"). - WithDefaultVersion("v1.0.0"). - WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(namespaceYaml, controllerYaml, configMapYaml)), - }, - args: args{ - version: "v1.0.0", - targetNamespace: "", - watchingNamespace: "ns2", - }, - want: want{ - provider: p1, - version: "v1.0.0", // version detected - targetNamespace: namespaceName, // default targetNamespace detected - watchingNamespace: "ns2", // watchingNamespace overrides default watchingNamespace - variables: []string{variableName}, // variable detected + provider: p1, + version: "v1.0.0", // version detected + targetNamespace: "ns2", // targetNamespace overrides default targetNamespace + variables: []string{variableName}, // variable detected }, wantErr: false, }, @@ -196,9 +165,8 @@ func Test_componentsClient_Get(t *testing.T) { WithDefaultVersion("v1.0.0"), }, args: args{ - version: "v1.0.0", - targetNamespace: "", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "", }, wantErr: true, }, @@ -212,9 +180,8 @@ func Test_componentsClient_Get(t *testing.T) { WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(controllerYaml, configMapYaml)), }, args: args{ - version: "v1.0.0", - targetNamespace: "", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "", }, wantErr: true, }, @@ -228,16 +195,14 @@ func Test_componentsClient_Get(t *testing.T) { WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(controllerYaml, configMapYaml)), }, args: args{ - version: "v1.0.0", - targetNamespace: "ns2", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "ns2", }, want: want{ - provider: p1, - version: "v1.0.0", // version detected - targetNamespace: "ns2", // target targetNamespace applied - watchingNamespace: "", - variables: []string{variableName}, // variable detected + provider: p1, + version: "v1.0.0", // version detected + targetNamespace: "ns2", // target targetNamespace applied + variables: []string{variableName}, // variable detected }, wantErr: false, }, @@ -251,9 +216,8 @@ func Test_componentsClient_Get(t *testing.T) { WithFile("v1.0.0", "components.yaml", utilyaml.JoinYaml(controllerYaml, configMapYaml)), }, args: args{ - version: "v2.0.0", - targetNamespace: "", - watchingNamespace: "", + version: "v2.0.0", + targetNamespace: "", }, wantErr: true, }, @@ -268,9 +232,8 @@ func Test_componentsClient_Get(t *testing.T) { processor: test.NewFakeProcessor().WithGetVariablesErr(errors.New("cannot get vars")), }, args: args{ - version: "v1.0.0", - targetNamespace: "default", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "default", }, wantErr: true, }, @@ -286,9 +249,8 @@ func Test_componentsClient_Get(t *testing.T) { processor: test.NewFakeProcessor().WithProcessErr(errors.New("cannot process")), }, args: args{ - version: "v1.0.0", - targetNamespace: "default", - watchingNamespace: "", + version: "v1.0.0", + targetNamespace: "default", }, wantErr: true, }, @@ -298,10 +260,9 @@ func Test_componentsClient_Get(t *testing.T) { gs := NewWithT(t) options := ComponentsOptions{ - Version: tt.args.version, - TargetNamespace: tt.args.targetNamespace, - WatchingNamespace: tt.args.watchingNamespace, - SkipVariables: tt.args.skipVariables, + Version: tt.args.version, + TargetNamespace: tt.args.targetNamespace, + SkipVariables: tt.args.skipVariables, } f := newComponentsClient(tt.fields.provider, tt.fields.repository, configClient) if tt.fields.processor != nil { @@ -318,7 +279,6 @@ func Test_componentsClient_Get(t *testing.T) { gs.Expect(got.Type()).To(Equal(tt.want.provider.Type())) gs.Expect(got.Version()).To(Equal(tt.want.version)) gs.Expect(got.TargetNamespace()).To(Equal(tt.want.targetNamespace)) - gs.Expect(got.WatchingNamespace()).To(Equal(tt.want.watchingNamespace)) gs.Expect(got.Variables()).To(Equal(tt.want.variables)) yaml, err := got.Yaml() diff --git a/cmd/clusterctl/client/repository/components_test.go b/cmd/clusterctl/client/repository/components_test.go index 91f7e17a69c9..3427beb31d11 100644 --- a/cmd/clusterctl/client/repository/components_test.go +++ b/cmd/clusterctl/client/repository/components_test.go @@ -17,7 +17,6 @@ limitations under the License. package repository import ( - "fmt" "testing" . "github.com/onsi/gomega" @@ -631,154 +630,6 @@ func Test_fixRBAC(t *testing.T) { } } -func fakeDeployment(watchNamespace string) unstructured.Unstructured { - args := []string{} - if watchNamespace != "" { - args = append(args, fmt.Sprintf("%s%s", namespaceArgPrefix, watchNamespace)) - } - return unstructured.Unstructured{ - Object: map[string]interface{}{ - "apiVersion": "apps/v1", - "kind": deploymentKind, - "spec": map[string]interface{}{ - "template": map[string]interface{}{ - "spec": map[string]interface{}{ - "containers": []map[string]interface{}{ - { - "name": controllerContainerName, - "args": args, - }, - }, - }, - }, - }, - }, - } -} - -func Test_inspectWatchNamespace(t *testing.T) { - type args struct { - objs []unstructured.Unstructured - } - tests := []struct { - name string - args args - want string - wantErr bool - }{ - { - name: "get watchingNamespace if exists", - args: args{ - objs: []unstructured.Unstructured{ - fakeDeployment("foo"), - }, - }, - want: "foo", - }, - { - name: "get watchingNamespace if exists more than once, but it is consistent", - args: args{ - objs: []unstructured.Unstructured{ - fakeDeployment("foo"), - fakeDeployment("foo"), - }, - }, - want: "foo", - }, - { - name: "return empty if there is no watchingNamespace", - args: args{ - objs: []unstructured.Unstructured{}, - }, - want: "", - }, - { - name: "fails if inconsistent watchingNamespace", - args: args{ - objs: []unstructured.Unstructured{ - fakeDeployment("foo"), - fakeDeployment("bar"), - }, - }, - want: "", - wantErr: true, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - g := NewWithT(t) - - got, err := inspectWatchNamespace(tt.args.objs) - if tt.wantErr { - g.Expect(err).To(HaveOccurred()) - return - } - g.Expect(err).NotTo(HaveOccurred()) - - g.Expect(got).To(Equal(tt.want)) - }) - } -} - -func Test_fixWatchNamespace(t *testing.T) { - type args struct { - objs []unstructured.Unstructured - watchingNamespace string - } - tests := []struct { - name string - args args - wantErr bool - }{ - { - name: "fix if existing", - args: args{ - objs: []unstructured.Unstructured{ - fakeDeployment("foo"), - }, - watchingNamespace: "bar", - }, - wantErr: false, - }, - { - name: "set if not existing", - args: args{ - objs: []unstructured.Unstructured{ - fakeDeployment(""), - }, - watchingNamespace: "bar", - }, - wantErr: false, - }, - { - name: "unset if existing", - args: args{ - objs: []unstructured.Unstructured{ - fakeDeployment("foo"), - }, - watchingNamespace: "", - }, - wantErr: false, - }, - } - for _, tt := range tests { - t.Run(tt.name, func(t *testing.T) { - g := NewWithT(t) - - got, err := fixWatchNamespace(tt.args.objs, tt.args.watchingNamespace) - if tt.wantErr { - g.Expect(err).To(HaveOccurred()) - return - } - g.Expect(err).NotTo(HaveOccurred()) - - wgot, err := inspectWatchNamespace(got) - g.Expect(err).NotTo(HaveOccurred()) - g.Expect(wgot).To(Equal(tt.args.watchingNamespace)) - }) - } -} - func Test_addCommonLabels(t *testing.T) { type args struct { objs []unstructured.Unstructured diff --git a/cmd/clusterctl/client/upgrade_test.go b/cmd/clusterctl/client/upgrade_test.go index 26dbdb86eaa4..8a4a9f403f61 100644 --- a/cmd/clusterctl/client/upgrade_test.go +++ b/cmd/clusterctl/client/upgrade_test.go @@ -370,7 +370,7 @@ func fakeProvider(name string, providerType clusterctlv1.ProviderType, version, ProviderName: name, Type: string(providerType), Version: version, - WatchedNamespace: "watchingNS", + WatchedNamespace: "", } } diff --git a/cmd/clusterctl/cmd/config_provider.go b/cmd/clusterctl/cmd/config_provider.go index e3de481f6638..695eeb9ccf3d 100644 --- a/cmd/clusterctl/cmd/config_provider.go +++ b/cmd/clusterctl/cmd/config_provider.go @@ -52,7 +52,6 @@ type configProvidersOptions struct { infrastructureProvider string output string targetNamespace string - watchingNamespace string } var cpo = &configProvidersOptions{} @@ -101,8 +100,6 @@ func init() { fmt.Sprintf("Output format. Valid values: %v.", ComponentsOutputs)) configProviderCmd.Flags().StringVar(&cpo.targetNamespace, "target-namespace", "", "The target namespace where the provider should be deployed. If unspecified, the components default namespace is used.") - configProviderCmd.Flags().StringVar(&cpo.watchingNamespace, "watching-namespace", "", - "Namespace the provider should watch when reconciling objects. If unspecified, all namespaces are watched.") configCmd.AddCommand(configProviderCmd) } @@ -145,9 +142,8 @@ func runGetComponents() error { } options := client.ComponentsOptions{ - TargetNamespace: cpo.targetNamespace, - WatchingNamespace: cpo.watchingNamespace, - SkipVariables: true, + TargetNamespace: cpo.targetNamespace, + SkipVariables: true, } components, err := c.GetProviderComponents(providerName, providerType, options) if err != nil { @@ -169,7 +165,6 @@ func printComponents(c client.Components, output string) error { fmt.Printf("Version: %s\n", c.Version()) fmt.Printf("File: %s\n", file) fmt.Printf("TargetNamespace: %s\n", c.TargetNamespace()) - fmt.Printf("WatchingNamespace: %s\n", c.WatchingNamespace()) if len(c.Variables()) > 0 { fmt.Println("Variables:") for _, v := range c.Variables() { diff --git a/cmd/clusterctl/cmd/generate_provider.go b/cmd/clusterctl/cmd/generate_provider.go index 4bade88eab01..3d74d37d9317 100644 --- a/cmd/clusterctl/cmd/generate_provider.go +++ b/cmd/clusterctl/cmd/generate_provider.go @@ -29,7 +29,6 @@ type generateProvidersOptions struct { controlPlaneProvider string infrastructureProvider string targetNamespace string - watchingNamespace string textOutput bool } @@ -79,8 +78,6 @@ func init() { "ControlPlane provider and version (e.g. kubeadm:v0.3.0)") generateProviderCmd.Flags().StringVar(&gpo.targetNamespace, "target-namespace", "", "The target namespace where the provider should be deployed. If unspecified, the components default namespace is used.") - generateProviderCmd.Flags().StringVar(&gpo.watchingNamespace, "watching-namespace", "", - "Namespace the provider should watch when reconciling objects. If unspecified, all namespaces are watched.") generateProviderCmd.Flags().BoolVar(&gpo.textOutput, "describe", false, "Generate configuration without variable substitution.") @@ -98,8 +95,7 @@ func runGenerateProviderComponents() error { } options := client.ComponentsOptions{ - TargetNamespace: gpo.targetNamespace, - WatchingNamespace: gpo.watchingNamespace, + TargetNamespace: gpo.targetNamespace, } components, err := c.GetProviderComponents(providerName, providerType, options) if err != nil { diff --git a/cmd/clusterctl/cmd/init.go b/cmd/clusterctl/cmd/init.go index 3cf521422fec..87e33bc9e506 100644 --- a/cmd/clusterctl/cmd/init.go +++ b/cmd/clusterctl/cmd/init.go @@ -31,7 +31,6 @@ type initOptions struct { controlPlaneProviders []string infrastructureProviders []string targetNamespace string - watchingNamespace string listImages bool } @@ -76,9 +75,6 @@ var initCmd = &cobra.Command{ # Initialize a management cluster with a custom target namespace for the provider resources. clusterctl init --infrastructure aws --target-namespace foo - # Initialize a management cluster with a custom watching namespace for the given provider. - clusterctl init --infrastructure aws --watching-namespace=foo - # Lists the container images required for initializing the management cluster. # # Note: This command is a dry-run; it won't perform any action other than printing to screen. @@ -104,8 +100,6 @@ func init() { "Control plane providers and versions (e.g. kubeadm:v0.3.0) to add to the management cluster. If unspecified, the Kubeadm control plane provider's latest release is used.") initCmd.Flags().StringVar(&initOpts.targetNamespace, "target-namespace", "", "The target namespace where the providers should be deployed. If unspecified, the provider components' default namespace is used.") - initCmd.Flags().StringVar(&initOpts.watchingNamespace, "watching-namespace", "", - "Namespace the providers should watch when reconciling objects. If unspecified, all namespaces are watched.") // TODO: Move this to a sub-command or similar, it shouldn't really be a flag. initCmd.Flags().BoolVar(&initOpts.listImages, "list-images", false, @@ -127,7 +121,6 @@ func runInit() error { ControlPlaneProviders: initOpts.controlPlaneProviders, InfrastructureProviders: initOpts.infrastructureProviders, TargetNamespace: initOpts.targetNamespace, - WatchingNamespace: initOpts.watchingNamespace, LogUsageInstructions: true, } diff --git a/cmd/clusterctl/cmd/util.go b/cmd/clusterctl/cmd/util.go index 36963ddfb2e8..8839aa27d6ea 100644 --- a/cmd/clusterctl/cmd/util.go +++ b/cmd/clusterctl/cmd/util.go @@ -65,7 +65,6 @@ func printComponentsAsText(c client.Components) error { fmt.Printf("Version: %s\n", c.Version()) fmt.Printf("File: %s\n", file) fmt.Printf("TargetNamespace: %s\n", c.TargetNamespace()) - fmt.Printf("WatchingNamespace: %s\n", c.WatchingNamespace()) if len(c.Variables()) > 0 { fmt.Println("Variables:") for _, v := range c.Variables() {