sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/config/cert_manager_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 config
    18  
    19  import (
    20  	"os"
    21  	"time"
    22  
    23  	"github.com/drone/envsubst/v2"
    24  	"github.com/pkg/errors"
    25  )
    26  
    27  const (
    28  	// CertManagerConfigKey defines the name of the top level config key for cert-manager configuration.
    29  	CertManagerConfigKey = "cert-manager"
    30  
    31  	// CertManagerDefaultVersion defines the default cert-manager version to be used by clusterctl.
    32  	CertManagerDefaultVersion = "v1.14.4"
    33  
    34  	// CertManagerDefaultURL defines the default cert-manager repository url to be used by clusterctl.
    35  	// NOTE: At runtime CertManagerDefaultVersion may be replaced with the
    36  	// version defined by the user in the clusterctl configuration file.
    37  	CertManagerDefaultURL = "https://github.com/cert-manager/cert-manager/releases/" + CertManagerDefaultVersion + "/cert-manager.yaml"
    38  
    39  	// CertManagerDefaultTimeout defines the default cert-manager timeout to be used by clusterctl.
    40  	CertManagerDefaultTimeout = 10 * time.Minute
    41  )
    42  
    43  // CertManagerClient has methods to work with cert-manager configurations.
    44  type CertManagerClient interface {
    45  	// Get returns the cert-manager configuration.
    46  	Get() (CertManager, error)
    47  }
    48  
    49  // certManagerClient implements CertManagerClient.
    50  type certManagerClient struct {
    51  	reader Reader
    52  }
    53  
    54  // ensure certManagerClient implements CertManagerClient.
    55  var _ CertManagerClient = &certManagerClient{}
    56  
    57  func newCertManagerClient(reader Reader) *certManagerClient {
    58  	return &certManagerClient{
    59  		reader: reader,
    60  	}
    61  }
    62  
    63  // configCertManager mirrors config.CertManager interface and allows serialization of the corresponding info.
    64  type configCertManager struct {
    65  	URL     string `json:"url,omitempty"`
    66  	Version string `json:"version,omitempty"`
    67  	Timeout string `json:"timeout,omitempty"`
    68  }
    69  
    70  func (p *certManagerClient) Get() (CertManager, error) {
    71  	url := CertManagerDefaultURL
    72  	version := CertManagerDefaultVersion
    73  	timeout := CertManagerDefaultTimeout.String()
    74  
    75  	userCertManager := &configCertManager{}
    76  	if err := p.reader.UnmarshalKey(CertManagerConfigKey, &userCertManager); err != nil {
    77  		return nil, errors.Wrap(err, "failed to unmarshal certManager from the clusterctl configuration file")
    78  	}
    79  	if userCertManager.URL != "" {
    80  		url = userCertManager.URL
    81  	}
    82  
    83  	url, err := envsubst.Eval(url, os.Getenv)
    84  	if err != nil {
    85  		return nil, errors.Wrapf(err, "unable to evaluate url: %q", url)
    86  	}
    87  
    88  	if userCertManager.Version != "" {
    89  		version = userCertManager.Version
    90  	}
    91  	if userCertManager.Timeout != "" {
    92  		timeout = userCertManager.Timeout
    93  	}
    94  
    95  	return NewCertManager(url, version, timeout), nil
    96  }