sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/generate_provider.go (about)

     1  /*
     2  Copyright 2022 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  	"k8s.io/apimachinery/pkg/util/version"
    24  
    25  	clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
    26  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    27  )
    28  
    29  func (c *clusterctlClient) GenerateProvider(ctx context.Context, provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) {
    30  	providerName, providerVersion, err := parseProviderName(provider)
    31  	if err != nil {
    32  		return nil, err
    33  	}
    34  
    35  	configRepository, err := c.configClient.Providers().Get(providerName, providerType)
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	providerRepositoryClient, err := c.repositoryClientFactory(ctx, RepositoryClientFactoryInput{Provider: configRepository})
    41  	if err != nil {
    42  		return nil, err
    43  	}
    44  
    45  	if providerVersion == "" {
    46  		providerVersion = providerRepositoryClient.DefaultVersion()
    47  	}
    48  
    49  	latestMetadata, err := providerRepositoryClient.Metadata(providerVersion).Get(ctx)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  
    54  	currentVersion, err := version.ParseSemantic(providerVersion)
    55  	if err != nil {
    56  		return nil, err
    57  	}
    58  
    59  	releaseSeries := latestMetadata.GetReleaseSeriesForVersion(currentVersion)
    60  	if releaseSeries == nil {
    61  		return nil, errors.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", providerVersion, providerName)
    62  	}
    63  
    64  	if releaseSeries.Contract != clusterv1.GroupVersion.Version {
    65  		return nil, errors.Errorf("current version of clusterctl is only compatible with %s providers, detected %s for provider %s", clusterv1.GroupVersion.Version, releaseSeries.Contract, providerName)
    66  	}
    67  
    68  	return c.GetProviderComponents(ctx, provider, providerType, options)
    69  }