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

✨clusterctl: add the config provider <provider-name> command #2048

Merged
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
46 changes: 44 additions & 2 deletions cmd/clusterctl/cmd/config_providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
}

Expand Down Expand Up @@ -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
}
3 changes: 3 additions & 0 deletions cmd/clusterctl/pkg/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/clusterctl/pkg/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 9 additions & 0 deletions cmd/clusterctl/pkg/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
80 changes: 77 additions & 3 deletions cmd/clusterctl/pkg/client/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package client

import (
"fmt"
"testing"

clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
Expand Down Expand Up @@ -69,22 +70,95 @@ 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
}

for i, g := range got {
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)
}
})
}
}