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

     1  /*
     2  Copyright 2021 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  // ClusterClassClient has methods to work with cluster class templates hosted on a provider repository.
    30  // Templates are yaml files to be used for creating a guest cluster.
    31  type ClusterClassClient interface {
    32  	Get(ctx context.Context, name, targetNamespace string, skipTemplateProcess bool) (Template, error)
    33  }
    34  
    35  type clusterClassClient struct {
    36  	version               string
    37  	provider              config.Provider
    38  	repository            Repository
    39  	configVariablesClient config.VariablesClient
    40  	processor             yaml.Processor
    41  }
    42  
    43  // ClusterClassClientInput is an input struct for newClusterClassClient.
    44  type ClusterClassClientInput struct {
    45  	version               string
    46  	provider              config.Provider
    47  	repository            Repository
    48  	configVariablesClient config.VariablesClient
    49  	processor             yaml.Processor
    50  }
    51  
    52  func newClusterClassClient(input ClusterClassClientInput) *clusterClassClient {
    53  	return &clusterClassClient{
    54  		version:               input.version,
    55  		provider:              input.provider,
    56  		repository:            input.repository,
    57  		configVariablesClient: input.configVariablesClient,
    58  		processor:             input.processor,
    59  	}
    60  }
    61  
    62  func (cc *clusterClassClient) Get(ctx context.Context, name, targetNamespace string, skipTemplateProcess bool) (Template, error) {
    63  	log := logf.Log
    64  
    65  	if targetNamespace == "" {
    66  		return nil, errors.New("invalid arguments: please provide a targetNamespace")
    67  	}
    68  
    69  	version := cc.version
    70  	filename := cc.processor.GetClusterClassTemplateName(version, name)
    71  
    72  	// read the component YAML, reading the local override file if it exists, otherwise read from the provider repository
    73  	rawArtifact, err := getLocalOverride(&newOverrideInput{
    74  		configVariablesClient: cc.configVariablesClient,
    75  		provider:              cc.provider,
    76  		version:               version,
    77  		filePath:              filename,
    78  	})
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	if rawArtifact == nil {
    84  		log.V(5).Info("Fetching", "File", filename, "Provider", cc.provider.Name(), "Type", cc.provider.Type(), "Version", version)
    85  		rawArtifact, err = cc.repository.GetFile(ctx, version, filename)
    86  		if err != nil {
    87  			return nil, errors.Wrapf(err, "failed to read %q from provider's repository %q", filename, cc.provider.ManifestLabel())
    88  		}
    89  	} else {
    90  		log.V(1).Info("Using", "Override", filename, "Provider", cc.provider.ManifestLabel(), "Version", version)
    91  	}
    92  
    93  	return NewTemplate(TemplateInput{
    94  		rawArtifact,
    95  		cc.configVariablesClient,
    96  		cc.processor,
    97  		targetNamespace,
    98  		skipTemplateProcess,
    99  	})
   100  }