|
| 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 | + "github.com/pkg/errors" |
| 21 | + "k8s.io/apimachinery/pkg/util/version" |
| 22 | + |
| 23 | + clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1" |
| 24 | + clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3" |
| 25 | +) |
| 26 | + |
| 27 | +func (c *clusterctlClient) GenerateProvider(provider string, providerType clusterctlv1.ProviderType, options ComponentsOptions) (Components, error) { |
| 28 | + providerName, providerVersion, err := parseProviderName(provider) |
| 29 | + if err != nil { |
| 30 | + return nil, err |
| 31 | + } |
| 32 | + |
| 33 | + configRepository, err := c.configClient.Providers().Get(providerName, providerType) |
| 34 | + if err != nil { |
| 35 | + return nil, err |
| 36 | + } |
| 37 | + |
| 38 | + providerRepositoryClient, err := c.repositoryClientFactory(RepositoryClientFactoryInput{Provider: configRepository}) |
| 39 | + if err != nil { |
| 40 | + return nil, err |
| 41 | + } |
| 42 | + |
| 43 | + if providerVersion == "" { |
| 44 | + providerVersion = providerRepositoryClient.DefaultVersion() |
| 45 | + } |
| 46 | + |
| 47 | + latestMetadata, err := providerRepositoryClient.Metadata(providerVersion).Get() |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + |
| 52 | + currentVersion, err := version.ParseSemantic(providerVersion) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + |
| 57 | + releaseSeries := latestMetadata.GetReleaseSeriesForVersion(currentVersion) |
| 58 | + if releaseSeries == nil { |
| 59 | + return nil, errors.Errorf("invalid provider metadata: version %s for the provider %s does not match any release series", providerVersion, providerName) |
| 60 | + } |
| 61 | + |
| 62 | + if releaseSeries.Contract != clusterv1.GroupVersion.Version { |
| 63 | + 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) |
| 64 | + } |
| 65 | + |
| 66 | + return c.GetProviderComponents(provider, providerType, options) |
| 67 | +} |
0 commit comments