Skip to content

Commit

Permalink
introduce version check validation for generate provider command
Browse files Browse the repository at this point in the history
Signed-off-by: tharun <[email protected]>
  • Loading branch information
tharun208 committed Jan 13, 2022
1 parent 1e13f44 commit e6cfe94
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions cmd/clusterctl/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ type Client interface {
// DescribeCluster returns the object tree representing the status of a Cluster API cluster.
DescribeCluster(options DescribeClusterOptions) (*tree.ObjectTree, error)

// GenerateProvider validates the input provider version is compatible with the version of clusterctl and returns the provider components.
GenerateProvider(providerName string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error)

// AlphaClient is an Interface for alpha features in clusterctl
AlphaClient
}
Expand Down
4 changes: 4 additions & 0 deletions cmd/clusterctl/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (f fakeClient) GetProviderComponents(provider string, providerType clusterc
return f.internalClient.GetProviderComponents(provider, providerType, options)
}

func (f fakeClient) GenerateProvider(provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) {
return f.internalClient.GenerateProvider(provider, providerType, options)
}

func (f fakeClient) GetClusterTemplate(options GetClusterTemplateOptions) (Template, error) {
return f.internalClient.GetClusterTemplate(options)
}
Expand Down
40 changes: 40 additions & 0 deletions cmd/clusterctl/client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/apimachinery/pkg/util/version"
"k8s.io/utils/pointer"

clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/cluster"
"sigs.k8s.io/cluster-api/cmd/clusterctl/client/repository"
Expand Down Expand Up @@ -54,6 +55,45 @@ func (c *clusterctlClient) GetProviderComponents(provider string, providerType c
return components, nil
}

func (c *clusterctlClient) GenerateProvider(providerName string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) {

providerName, providerVersion, err := parseProviderName(providerName)
if err != nil {
return nil, err
}

// Gets the provider configuration (that includes the location of the provider repository)
providerConfig, err := c.configClient.Providers().Get(providerName, providerType)
if err != nil {
return nil, err
}

// 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
// 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})
if err != nil {
return nil, err
}

latestMetadata, err := repositoryClientFactory.Metadata(providerVersion).Get()
if err != nil {
return nil, err
}
// Gets the contract for the current release.
currentVersion, err := version.ParseSemantic(providerVersion)
if err != nil {
return nil, errors.Wrapf(err, "failed to parse current version for the %s provider", providerName)
}
releaseSeries := latestMetadata.GetReleaseSeriesForVersion(currentVersion)
if releaseSeries.Contract != clusterv1.GroupVersion.Version {
return nil, errors.Errorf("current version of clusterctl is only compatible with %s providers, detected %s for provider %s", clusterv1.GroupVersion.Version, releaseSeries.Contract, providerVersion)
}
return c.GetProviderComponents(providerName, providerType, options)
}

// ReaderSourceOptions define the options to be used when reading a template
// from an arbitrary reader.
type ReaderSourceOptions struct {
Expand Down
2 changes: 1 addition & 1 deletion cmd/clusterctl/cmd/generate_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ func runGenerateProviderComponents() error {
SkipTemplateProcess: gpo.raw,
}

components, err := c.GetProviderComponents(providerName, providerType, options)
components, err := c.GenerateProvider(providerName, providerType, options)
if err != nil {
return err
}
Expand Down

0 comments on commit e6cfe94

Please sign in to comment.