github.com/jenkins-x/jx/v2@v2.1.155/pkg/kube/jx_install_config.go (about)

     1  package kube
     2  
     3  import (
     4  	"github.com/pkg/errors"
     5  	v1 "k8s.io/api/core/v1"
     6  	"k8s.io/client-go/kubernetes"
     7  )
     8  
     9  // JXInstallConfig is the struct used to create the jx-install-config configmap
    10  type JXInstallConfig struct {
    11  	Server       string `structs:"server" yaml:"server" json:"server"`
    12  	CA           []byte `structs:"ca.crt" yaml:"ca.crt" json:"ca.crt"`
    13  	KubeProvider string `structs:"kubeProvider" yaml:"kubeProvider" json:"kubeProvider"`
    14  }
    15  
    16  // RememberRegion remembers cloud providers region in Kubernetes Config Map (jx-install-config), so jx can access this
    17  // information later. Usually executed when provisioning new Kubernetes cluster.
    18  //
    19  // If jx-install-config config map doesn't exist, it will be created. If region value is already saved, it will be
    20  // overridden by this function call.
    21  func RememberRegion(kubeClient kubernetes.Interface, namespace string, region string) error {
    22  	_, err := DefaultModifyConfigMap(kubeClient, namespace, ConfigMapNameJXInstallConfig, func(configMap *v1.ConfigMap) error {
    23  		configMap.Data[Region] = region
    24  		return nil
    25  	}, nil)
    26  	if err != nil {
    27  		return errors.Wrapf(err, "saving cloud region in ConfigMap %s", ConfigMapNameJXInstallConfig)
    28  	}
    29  	return nil
    30  }
    31  
    32  // RememberInstallValues remembers any non-blank installation values in the Kubernetes Config Map (jx-install-config), so jx can access this
    33  // information later. Usually executed when provisioning new Kubernetes cluster.
    34  //
    35  // If jx-install-config config map doesn't exist, it will be created. If region value is already saved, it will be
    36  // overridden by this function call.
    37  func RememberInstallValues(kubeClient kubernetes.Interface, namespace string, values map[string]string) error {
    38  	_, err := DefaultModifyConfigMap(kubeClient, namespace, ConfigMapNameJXInstallConfig, func(configMap *v1.ConfigMap) error {
    39  		if configMap.Data == nil {
    40  			configMap.Data = values
    41  			return nil
    42  		}
    43  		for k, v := range values {
    44  			if v != "" {
    45  				configMap.Data[k] = v
    46  			}
    47  		}
    48  		return nil
    49  	}, nil)
    50  	if err != nil {
    51  		return errors.Wrapf(err, "saving install values in in ConfigMap %s", ConfigMapNameJXInstallConfig)
    52  	}
    53  	return nil
    54  }
    55  
    56  // ReadInstallValues reads the installed configuration values from the installation namespace
    57  //
    58  // Empty map is returned if:
    59  // - jx-install-config config map doesn't exist
    60  // - kube client returns error on Config Map read operation
    61  //
    62  // Error is returned if:
    63  // - jx-install-config config map doesn't exist
    64  // - kube client returns error on Config Map read operation
    65  func ReadInstallValues(kubeClient kubernetes.Interface, namespace string) (map[string]string, error) {
    66  	data, err := GetConfigMapData(kubeClient, ConfigMapNameJXInstallConfig, namespace)
    67  	if data == nil {
    68  		data = map[string]string{}
    69  	}
    70  	if err != nil {
    71  		return data, err
    72  	}
    73  	return data, nil
    74  }
    75  
    76  // ReadRegion allows to read cloud region from Config Map (jx-install-config). Region value is usually written using
    77  // RememberRegion function.
    78  //
    79  // Empty string is returned if:
    80  // - region value doesn't exist
    81  // - has empty value
    82  // - jx-install-config config map doesn't exist
    83  // - kube client returns error on Config Map read operation
    84  //
    85  // Error is returned if:
    86  // - jx-install-config config map doesn't exist
    87  // - kube client returns error on Config Map read operation
    88  func ReadRegion(kubeClient kubernetes.Interface, namespace string) (string, error) {
    89  	data, err := ReadInstallValues(kubeClient, namespace)
    90  	if err != nil {
    91  		return "", err
    92  	}
    93  	return data[Region], nil
    94  }