sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/config/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 config
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  )
    24  
    25  // Client is used to interact with the clusterctl configurations.
    26  // Clusterctl v2 handles the following configurations:
    27  // 1. The cert manager configuration (URL of the repository)
    28  // 2. The configuration of the providers (name, type and URL of the provider repository)
    29  // 3. Variables used when installing providers/creating clusters. Variables can be read from the environment or from the config file
    30  // 4. The configuration about image overrides.
    31  type Client interface {
    32  	// CertManager provide access to the cert-manager configurations.
    33  	CertManager() CertManagerClient
    34  
    35  	// Providers provide access to provider configurations.
    36  	Providers() ProvidersClient
    37  
    38  	// Variables provide access to environment variables and/or variables defined in the clusterctl configuration file.
    39  	Variables() VariablesClient
    40  
    41  	// ImageMeta provide access to image meta configurations.
    42  	ImageMeta() ImageMetaClient
    43  }
    44  
    45  // configClient implements Client.
    46  type configClient struct {
    47  	reader Reader
    48  }
    49  
    50  // ensure configClient implements Client.
    51  var _ Client = &configClient{}
    52  
    53  func (c *configClient) CertManager() CertManagerClient {
    54  	return newCertManagerClient(c.reader)
    55  }
    56  
    57  func (c *configClient) Providers() ProvidersClient {
    58  	return newProvidersClient(c.reader)
    59  }
    60  
    61  func (c *configClient) Variables() VariablesClient {
    62  	return newVariablesClient(c.reader)
    63  }
    64  
    65  func (c *configClient) ImageMeta() ImageMetaClient {
    66  	return newImageMetaClient(c.reader)
    67  }
    68  
    69  // Option is a configuration option supplied to New.
    70  type Option func(*configClient)
    71  
    72  // InjectReader allows to override the default configuration reader used by clusterctl.
    73  func InjectReader(reader Reader) Option {
    74  	return func(c *configClient) {
    75  		c.reader = reader
    76  	}
    77  }
    78  
    79  // New returns a Client for interacting with the clusterctl configuration.
    80  func New(ctx context.Context, path string, options ...Option) (Client, error) {
    81  	return newConfigClient(ctx, path, options...)
    82  }
    83  
    84  func newConfigClient(ctx context.Context, path string, options ...Option) (*configClient, error) {
    85  	client := &configClient{}
    86  	for _, o := range options {
    87  		o(client)
    88  	}
    89  
    90  	// if there is an injected reader, use it, otherwise use a default one
    91  	var err error
    92  	if client.reader == nil {
    93  		if client.reader, err = newViperReader(); err != nil {
    94  			return nil, errors.Wrap(err, "failed to create the configuration reader")
    95  		}
    96  		if err = client.reader.Init(ctx, path); err != nil {
    97  			return nil, errors.Wrap(err, "failed to initialize the configuration reader")
    98  		}
    99  	}
   100  
   101  	return client, nil
   102  }
   103  
   104  // Reader define the behaviours of a configuration reader.
   105  type Reader interface {
   106  	// Init allows to initialize the configuration reader.
   107  	Init(ctx context.Context, path string) error
   108  
   109  	// Get returns a configuration value of type string.
   110  	// In case the configuration value does not exists, it returns an error.
   111  	Get(key string) (string, error)
   112  
   113  	// Set allows to set an explicit override for a config value.
   114  	// e.g. It is used to set an override from a flag value over environment/config file variables.
   115  	Set(key, value string)
   116  
   117  	// UnmarshalKey reads a configuration value and unmarshals it into the provided value object.
   118  	UnmarshalKey(key string, value interface{}) error
   119  }