sigs.k8s.io/cluster-api@v1.7.1/cmd/clusterctl/client/config/reader_memory.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  	"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  // MemoryReader provides a reader implementation backed by a map.
    29  // This is to be used by the operator to place config from a secret
    30  // and the ProviderSpec.Fetchconfig.
    31  type MemoryReader struct {
    32  	variables map[string]string
    33  	providers []configProvider
    34  }
    35  
    36  var _ Reader = &MemoryReader{}
    37  
    38  // NewMemoryReader return a new MemoryReader.
    39  func NewMemoryReader() *MemoryReader {
    40  	return &MemoryReader{
    41  		variables: map[string]string{},
    42  		providers: []configProvider{},
    43  	}
    44  }
    45  
    46  // Init initialize the reader.
    47  func (f *MemoryReader) Init(_ context.Context, _ string) error {
    48  	data, err := yaml.Marshal(f.providers)
    49  	if err != nil {
    50  		return err
    51  	}
    52  	f.variables["providers"] = string(data)
    53  
    54  	// images is not used by the operator, but it is read by the clusterctrl
    55  	// code, so we need a correct empty "images".
    56  	data, err = yaml.Marshal(map[string]imageMeta{})
    57  	if err != nil {
    58  		return err
    59  	}
    60  	f.variables["images"] = string(data)
    61  	return nil
    62  }
    63  
    64  // Get gets a value for the given key.
    65  func (f *MemoryReader) 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  // Set sets a value for the given key.
    73  func (f *MemoryReader) Set(key, value string) {
    74  	f.variables[key] = value
    75  }
    76  
    77  // UnmarshalKey gets a value for the given key, then unmarshal it.
    78  func (f *MemoryReader) UnmarshalKey(key string, rawval interface{}) error {
    79  	data, err := f.Get(key)
    80  	if err != nil {
    81  		return nil //nolint:nilerr // We expect to not error if the key is not present
    82  	}
    83  	return yaml.Unmarshal([]byte(data), rawval)
    84  }
    85  
    86  // AddProvider adds the given provider to the "providers" map entry and returns any errors.
    87  func (f *MemoryReader) AddProvider(name string, ttype clusterctlv1.ProviderType, url string) (*MemoryReader, error) {
    88  	f.providers = append(f.providers, configProvider{
    89  		Name: name,
    90  		URL:  url,
    91  		Type: ttype,
    92  	})
    93  
    94  	yaml, err := yaml.Marshal(f.providers)
    95  	if err != nil {
    96  		return f, err
    97  	}
    98  	f.variables["providers"] = string(yaml)
    99  
   100  	return f, nil
   101  }