diff --git a/cmd/clusterctl/cmd/config_providers.go b/cmd/clusterctl/cmd/config_providers.go index f0566ca6fb8a..a5ab3952e267 100644 --- a/cmd/clusterctl/cmd/config_providers.go +++ b/cmd/clusterctl/cmd/config_providers.go @@ -72,7 +72,7 @@ var configProvidersCmd = &cobra.Command{ if len(args) == 0 { return runGetRepositories() } - return runGetRepository(args[0], cpo.targetNamespace, cpo.watchingNamespace, cpo.output) + return runGetComponents(args[0], cpo.targetNamespace, cpo.watchingNamespace, cpo.output) }, } @@ -105,6 +105,48 @@ func runGetRepositories() error { return nil } -func runGetRepository(providerName, targetNamespace, watchingNamespace, output string) error { +func runGetComponents(providerName, targetNamespace, watchingNamespace, output string) error { + c, err := client.New(cfgFile) + if err != nil { + return err + } + + components, err := c.GetProviderComponents(providerName, targetNamespace, watchingNamespace) + if err != nil { + return err + } + + if output == "yaml" { + return componentsYAMLOutput(components) + } + return componentsDefaultOutput(components) +} + +func componentsYAMLOutput(c client.Components) error { + yaml, err := c.Yaml() + if err != nil { + return err + } + + fmt.Println(string(yaml)) + return err +} + +func componentsDefaultOutput(c client.Components) error { + fmt.Printf("Name: %s\n", c.Name()) + fmt.Printf("Type: %s\n", c.Type()) + fmt.Printf("URL: %s\n", c.URL()) + fmt.Printf("Version: %s\n", c.Version()) + fmt.Printf("TargetNamespace: %s\n", c.TargetNamespace()) + fmt.Printf("WatchingNamespace: %s\n", c.WatchingNamespace()) + if len(c.Variables()) > 0 { + fmt.Println("Variables:") + fmt.Println(" Name") + fmt.Println(" ----") + for _, v := range c.Variables() { + fmt.Printf(" %s\n", v) + } + } + return nil } diff --git a/cmd/clusterctl/pkg/client/client.go b/cmd/clusterctl/pkg/client/client.go index 0882141f73b5..676bd4aec968 100644 --- a/cmd/clusterctl/pkg/client/client.go +++ b/cmd/clusterctl/pkg/client/client.go @@ -38,6 +38,9 @@ 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, targetNamespace, watchingNamespace. + GetProviderComponents(provider, targetNameSpace, watchingNamespace string) (Components, error) + // Init initializes a management cluster by adding the requested list of providers. Init(options InitOptions) ([]Components, bool, error) } diff --git a/cmd/clusterctl/pkg/client/client_test.go b/cmd/clusterctl/pkg/client/client_test.go index d8ad22e42779..363a4091a4f3 100644 --- a/cmd/clusterctl/pkg/client/client_test.go +++ b/cmd/clusterctl/pkg/client/client_test.go @@ -66,6 +66,10 @@ func (f fakeClient) GetProvidersConfig() ([]Provider, error) { return f.internalClient.GetProvidersConfig() } +func (f fakeClient) GetProviderComponents(provider, targetNameSpace, watchingNamespace string) (Components, error) { + return f.internalClient.GetProviderComponents(provider, targetNameSpace, watchingNamespace) +} + func (f fakeClient) Init(options InitOptions) ([]Components, bool, error) { return f.internalClient.Init(options) } diff --git a/cmd/clusterctl/pkg/client/config.go b/cmd/clusterctl/pkg/client/config.go index 7ddd4f113af7..f2722c301a45 100644 --- a/cmd/clusterctl/pkg/client/config.go +++ b/cmd/clusterctl/pkg/client/config.go @@ -30,3 +30,12 @@ func (c *clusterctlClient) GetProvidersConfig() ([]Provider, error) { return rr, nil } + +func (c *clusterctlClient) GetProviderComponents(provider, targetNameSpace, watchingNamespace string) (Components, error) { + components, err := c.getComponentsByName(provider, targetNameSpace, watchingNamespace) + if err != nil { + return nil, err + } + + return components, nil +} diff --git a/cmd/clusterctl/pkg/client/config_test.go b/cmd/clusterctl/pkg/client/config_test.go index 90cffe0d5208..737020806311 100644 --- a/cmd/clusterctl/pkg/client/config_test.go +++ b/cmd/clusterctl/pkg/client/config_test.go @@ -17,6 +17,7 @@ limitations under the License. package client import ( + "fmt" "testing" clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" @@ -69,12 +70,14 @@ func Test_clusterctlClient_GetProvidersConfig(t *testing.T) { t.Run(tt.name, func(t *testing.T) { got, err := tt.field.client.GetProvidersConfig() if (err != nil) != tt.wantErr { - t.Errorf("GetProvidersConfig() error = %v, wantErr %v", err, tt.wantErr) + t.Fatalf("error = %v, wantErr %v", err, tt.wantErr) + } + if err != nil { return } if len(got) != len(tt.wantProviders) { - t.Errorf("Init() got = %v items, want %v items", len(got), len(tt.wantProviders)) + t.Errorf("got = %v items, want %v items", len(got), len(tt.wantProviders)) return } @@ -82,9 +85,80 @@ func Test_clusterctlClient_GetProvidersConfig(t *testing.T) { w := tt.wantProviders[i] if g.Name() != w { - t.Errorf("GetProvidersConfig(), Item[%d].Name() got = %v, want = %v ", i, g.Name(), w) + t.Errorf("Item[%d].Name() got = %v, want = %v ", i, g.Name(), w) } } }) } } + +func Test_clusterctlClient_GetProviderComponents(t *testing.T) { + config1 := newFakeConfig(). + WithProvider(capiProviderConfig) + + repository1 := newFakeRepository(capiProviderConfig, config1.Variables()). + WithPaths("root", "components.yaml"). + WithDefaultVersion("v1.0.0"). + WithFile("v1.0.0", "components.yaml", componentsYAML("ns1")) + + client := newFakeClient(config1). + WithRepository(repository1) + + type args struct { + provider string + targetNameSpace string + watchingNamespace string + } + type want struct { + provider config.Provider + version string + } + tests := []struct { + name string + args args + want want + wantErr bool + }{ + { + name: "Pass", + args: args{ + provider: capiProviderConfig.Name(), + targetNameSpace: "ns2", + watchingNamespace: "", + }, + want: want{ + provider: capiProviderConfig, + version: "v1.0.0", + }, + wantErr: false, + }, + { + name: "Fail", + args: args{ + provider: fmt.Sprintf("%s:v0.2.0", capiProviderConfig.Name()), + targetNameSpace: "ns2", + watchingNamespace: "", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got, err := client.GetProviderComponents(tt.args.provider, tt.args.targetNameSpace, tt.args.watchingNamespace) + if tt.wantErr != (err != nil) { + t.Fatalf("error = %v, wantErr = %v", err, tt.wantErr) + } + if tt.wantErr { + return + } + + if got.Name() != tt.want.provider.Name() { + t.Errorf("got.Name() = %v, want = %v ", got.Name(), tt.want.provider.Name()) + } + + if got.Version() != tt.want.version { + t.Errorf("got.Version() = %v, want = %v ", got.Version(), tt.want.version) + } + }) + } +}