sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/repository/components_client.go (about) 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 "context" 21 22 "github.com/pkg/errors" 23 24 "sigs.k8s.io/cluster-api/cmd/clusterctl/client/config" 25 yaml "sigs.k8s.io/cluster-api/cmd/clusterctl/client/yamlprocessor" 26 logf "sigs.k8s.io/cluster-api/cmd/clusterctl/log" 27 ) 28 29 // ComponentsClient has methods to work with yaml file for generating provider components. 30 // Assets are yaml files to be used for deploying a provider into a management cluster. 31 type ComponentsClient interface { 32 Raw(ctx context.Context, options ComponentsOptions) ([]byte, error) 33 Get(ctx context.Context, options ComponentsOptions) (Components, error) 34 } 35 36 // componentsClient implements ComponentsClient. 37 type componentsClient struct { 38 provider config.Provider 39 repository Repository 40 configClient config.Client 41 processor yaml.Processor 42 } 43 44 // ensure componentsClient implements ComponentsClient. 45 var _ ComponentsClient = &componentsClient{} 46 47 // newComponentsClient returns a componentsClient. 48 func newComponentsClient(provider config.Provider, repository Repository, configClient config.Client) *componentsClient { 49 return &componentsClient{ 50 provider: provider, 51 repository: repository, 52 configClient: configClient, 53 processor: yaml.NewSimpleProcessor(), 54 } 55 } 56 57 // Raw returns the components from a repository. 58 func (f *componentsClient) Raw(ctx context.Context, options ComponentsOptions) ([]byte, error) { 59 return f.getRawBytes(ctx, &options) 60 } 61 62 // Get returns the components from a repository. 63 func (f *componentsClient) Get(ctx context.Context, options ComponentsOptions) (Components, error) { 64 file, err := f.getRawBytes(ctx, &options) 65 if err != nil { 66 return nil, err 67 } 68 return NewComponents(ComponentsInput{f.provider, f.configClient, f.processor, file, options}) 69 } 70 71 func (f *componentsClient) getRawBytes(ctx context.Context, options *ComponentsOptions) ([]byte, error) { 72 log := logf.Log 73 74 // If the request does not target a specific version, read from the default repository version that is derived from the repository URL, e.g. latest. 75 if options.Version == "" { 76 options.Version = f.repository.DefaultVersion() 77 } 78 79 // Retrieve the path where the path is stored 80 path := f.repository.ComponentsPath() 81 82 // Read the component YAML, reading the local override file if it exists, otherwise read from the provider repository 83 file, err := getLocalOverride(&newOverrideInput{ 84 configVariablesClient: f.configClient.Variables(), 85 provider: f.provider, 86 version: options.Version, 87 filePath: path, 88 }) 89 if err != nil { 90 return nil, err 91 } 92 93 if file == nil { 94 log.V(5).Info("Fetching", "File", path, "Provider", f.provider.Name(), "Type", f.provider.Type(), "Version", options.Version) 95 file, err = f.repository.GetFile(ctx, options.Version, path) 96 if err != nil { 97 return nil, errors.Wrapf(err, "failed to read %q from provider's repository %q", path, f.provider.ManifestLabel()) 98 } 99 } else { 100 log.Info("Using", "Override", path, "Provider", f.provider.ManifestLabel(), "Version", options.Version) 101 } 102 return file, nil 103 }