sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/internal/test/fake_reader.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 test
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/pkg/errors"
    23  	"sigs.k8s.io/yaml"
    24  
    25  	clusterctlv1 "sigs.k8s.io/cluster-api/cmd/clusterctl/api/v1alpha3"
    26  )
    27  
    28  // FakeReader provider a reader implementation backed by a map.
    29  type FakeReader struct {
    30  	initialized bool
    31  	variables   map[string]string
    32  	providers   []configProvider
    33  	certManager configCertManager
    34  	imageMetas  map[string]imageMeta
    35  }
    36  
    37  // configProvider is a mirror of config.Provider, re-implemented here in order to
    38  // avoid circular dependencies between pkg/client/config and pkg/internal/test.
    39  type configProvider struct {
    40  	Name string                    `json:"name,omitempty"`
    41  	URL  string                    `json:"url,omitempty"`
    42  	Type clusterctlv1.ProviderType `json:"type,omitempty"`
    43  }
    44  
    45  // configCertManager is a mirror of config.CertManager, re-implemented here in order to
    46  // avoid circular dependencies between pkg/client/config and pkg/internal/test.
    47  type configCertManager struct {
    48  	URL     string `json:"url,omitempty"`
    49  	Version string `json:"version,omitempty"`
    50  	Timeout string `json:"timeout,omitempty"`
    51  }
    52  
    53  // imageMeta is a mirror of config.imageMeta, re-implemented here in order to
    54  // avoid circular dependencies between pkg/client/config and pkg/internal/test.
    55  type imageMeta struct {
    56  	Repository string `json:"repository,omitempty"`
    57  	Tag        string `json:"tag,omitempty"`
    58  }
    59  
    60  func (f *FakeReader) Init(_ context.Context, _ string) error {
    61  	f.initialized = true
    62  	return nil
    63  }
    64  
    65  func (f *FakeReader) Get(key string) (string, error) {
    66  	if val, ok := f.variables[key]; ok {
    67  		return val, nil
    68  	}
    69  	return "", errors.Errorf("value for variable %q is not set", key)
    70  }
    71  
    72  func (f *FakeReader) Set(key, value string) {
    73  	f.variables[key] = value
    74  }
    75  
    76  func (f *FakeReader) UnmarshalKey(key string, rawval interface{}) error {
    77  	data, err := f.Get(key)
    78  	if err != nil {
    79  		return nil //nolint:nilerr // We expect to not error if the key is not present
    80  	}
    81  	return yaml.Unmarshal([]byte(data), rawval)
    82  }
    83  
    84  func NewFakeReader() *FakeReader {
    85  	return &FakeReader{
    86  		variables:  map[string]string{},
    87  		imageMetas: map[string]imageMeta{},
    88  	}
    89  }
    90  
    91  func (f *FakeReader) WithVar(key, value string) *FakeReader {
    92  	f.variables[key] = value
    93  	return f
    94  }
    95  
    96  func (f *FakeReader) WithProvider(name string, ttype clusterctlv1.ProviderType, url string) *FakeReader {
    97  	f.providers = append(f.providers, configProvider{
    98  		Name: name,
    99  		URL:  url,
   100  		Type: ttype,
   101  	})
   102  
   103  	yaml, _ := yaml.Marshal(f.providers)
   104  	f.variables["providers"] = string(yaml)
   105  
   106  	return f
   107  }
   108  
   109  func (f *FakeReader) WithCertManager(url, version, timeout string) *FakeReader {
   110  	f.certManager = configCertManager{
   111  		URL:     url,
   112  		Version: version,
   113  		Timeout: timeout,
   114  	}
   115  
   116  	yaml, _ := yaml.Marshal(f.certManager)
   117  	f.variables["cert-manager"] = string(yaml)
   118  
   119  	return f
   120  }
   121  
   122  func (f *FakeReader) WithImageMeta(component, repository, tag string) *FakeReader {
   123  	f.imageMetas[component] = imageMeta{
   124  		Repository: repository,
   125  		Tag:        tag,
   126  	}
   127  
   128  	yaml, _ := yaml.Marshal(f.imageMetas)
   129  	f.variables["images"] = string(yaml)
   130  
   131  	return f
   132  }