|
| 1 | +/* |
| 2 | +Copyright 2019 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 repository |
| 18 | + |
| 19 | +import ( |
| 20 | + "net/url" |
| 21 | + |
| 22 | + "github.com/pkg/errors" |
| 23 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/client/config" |
| 24 | + "sigs.k8s.io/cluster-api/cmd/clusterctl/pkg/internal/test" |
| 25 | +) |
| 26 | + |
| 27 | +// Client is used to interact with provider repositories. |
| 28 | +// Provider repository are expected to contain two types of YAML files: |
| 29 | +// - YAML files defining the provider components (CRD, Controller, RBAC etc.) |
| 30 | +// - YAML files defining the cluster templates (Cluster, Machines) |
| 31 | +type Client interface { |
| 32 | + config.Provider |
| 33 | + |
| 34 | + // Components provide access to YAML file for creating provider components. |
| 35 | + Components() ComponentsClient |
| 36 | + |
| 37 | + // Templates provide access to YAML file for generating workload cluster templates. |
| 38 | + // Please note that templates are expected to exist for the infrastructure providers only. |
| 39 | + Templates(version string) TemplatesClient |
| 40 | +} |
| 41 | + |
| 42 | +// repositoryClient implements Client. |
| 43 | +type repositoryClient struct { |
| 44 | + config.Provider |
| 45 | + configVariablesClient config.VariablesClient |
| 46 | + repository Repository |
| 47 | +} |
| 48 | + |
| 49 | +// ensure repositoryClient implements Client. |
| 50 | +var _ Client = &repositoryClient{} |
| 51 | + |
| 52 | +func (c *repositoryClient) Components() ComponentsClient { |
| 53 | + return newComponentsClient(c.Provider, c.repository, c.configVariablesClient) |
| 54 | +} |
| 55 | + |
| 56 | +func (c *repositoryClient) Templates(version string) TemplatesClient { |
| 57 | + return newTemplatesClient(c.Provider, version, c.repository, c.configVariablesClient) |
| 58 | +} |
| 59 | + |
| 60 | +// NewOptions carries the options supported by New |
| 61 | +type NewOptions struct { |
| 62 | + injectRepository Repository |
| 63 | +} |
| 64 | + |
| 65 | +// Option is a configuration option supplied to New |
| 66 | +type Option func(*NewOptions) |
| 67 | + |
| 68 | +// InjectRepository allows to override the repository implementation to use; |
| 69 | +// by default, the repository implementation to use is created according to the |
| 70 | +// repository URL. |
| 71 | +func InjectRepository(repository Repository) Option { |
| 72 | + return func(c *NewOptions) { |
| 73 | + c.injectRepository = repository |
| 74 | + } |
| 75 | +} |
| 76 | + |
| 77 | +// New returns a Client. |
| 78 | +func New(provider config.Provider, configVariablesClient config.VariablesClient, options Options) (Client, error) { |
| 79 | + return newRepositoryClient(provider, configVariablesClient, options) |
| 80 | +} |
| 81 | + |
| 82 | +func newRepositoryClient(provider config.Provider, configVariablesClient config.VariablesClient, options Options) (*repositoryClient, error) { |
| 83 | + repository := options.InjectRepository |
| 84 | + if repository == nil { |
| 85 | + r, err := repositoryFactory(provider, configVariablesClient) |
| 86 | + if err != nil { |
| 87 | + return nil, errors.Wrapf(err, "failed to get repository client for %q", provider.Name()) |
| 88 | + } |
| 89 | + repository = r |
| 90 | + } |
| 91 | + |
| 92 | + return &repositoryClient{ |
| 93 | + Provider: provider, |
| 94 | + repository: repository, |
| 95 | + configVariablesClient: configVariablesClient, |
| 96 | + }, nil |
| 97 | +} |
| 98 | + |
| 99 | +// Options allow to set Client options |
| 100 | +type Options struct { |
| 101 | + InjectRepository Repository |
| 102 | +} |
| 103 | + |
| 104 | +// Repository defines the behavior of a repository implementation. |
| 105 | +// clusterctl is designed to support different repository types; each repository implementation should consider |
| 106 | +// the following capabilities and provide best support considering the underlying technology. |
| 107 | +// 1. Versions awareness: repositories are expected to be aware of the provider version they are hosting, and |
| 108 | +// possibly to host more than one version. |
| 109 | +// 2. Kustomize awareness: even if it is recommended that provider expose “pre-compiled” YAML files, we want |
| 110 | +// to allow usage of clusterctl reading from the “raw” /config directory generated by kubebuilder (developer friendly) |
| 111 | +type Repository interface { |
| 112 | + // DefaultVersion returns the default provider version returned by a repository. |
| 113 | + // In case the repository URL points to latest, this method returns the current latest version; in other cases |
| 114 | + // it returns the version of the provider hosted in the repository. |
| 115 | + DefaultVersion() string |
| 116 | + |
| 117 | + // RootPath returns the path inside the repository where the YAML file for creating provider components and |
| 118 | + // the YAML file for generating workload cluster templates are stored. |
| 119 | + // This value is derived from the repository URL; all the paths returned by this interface should be relative to this path. |
| 120 | + RootPath() string |
| 121 | + |
| 122 | + // ComponentsPath return the path (a folder name or file name) of the YAML file for creating provider components. |
| 123 | + // This value is derived from the repository URL. |
| 124 | + ComponentsPath() string |
| 125 | + |
| 126 | + // KustomizeDir returns the path to a folder containing the kustomization.yaml for creating the YAML file provider components. |
| 127 | + // This value is derived from the repository URL, and it is used only when the YAML for provider components is spread |
| 128 | + // across nested folders inside the repository like e.g. the /config folder generated by kubebuilder. |
| 129 | + KustomizeDir() string |
| 130 | + |
| 131 | + // GetFiles returns files for a given provider version. |
| 132 | + // If file is a path, the entire content of the path is returned. |
| 133 | + GetFiles(version string, path string) (map[string][]byte, error) |
| 134 | +} |
| 135 | + |
| 136 | +var _ Repository = &test.FakeRepository{} |
| 137 | + |
| 138 | +//repositoryFactory returns the repository implementation corresponding to the provider URL. |
| 139 | +func repositoryFactory(providerConfig config.Provider, configVariablesClient config.VariablesClient) (Repository, error) { //nolint |
| 140 | + // parse the repository url |
| 141 | + rURL, err := url.Parse(providerConfig.URL()) |
| 142 | + if err != nil { |
| 143 | + return nil, errors.Errorf("failed to parse repository url %q", providerConfig.URL()) |
| 144 | + } |
| 145 | + |
| 146 | + // if the url is a github repository |
| 147 | + //TODO: implement in a follow up PR |
| 148 | + |
| 149 | + // if the url is a local repository |
| 150 | + //TODO: implement in a follow up PR |
| 151 | + |
| 152 | + return nil, errors.Errorf("invalid provider url. there are no provider implementation for %q schema", rURL.Scheme) |
| 153 | +} |
0 commit comments