github.com/olli-ai/jx/v2@v2.0.400-0.20210921045218-14731b4dd448/pkg/kube/configmap.go (about) 1 package kube 2 3 import ( 4 "fmt" 5 "sort" 6 "strings" 7 8 "github.com/olli-ai/jx/v2/pkg/util" 9 "github.com/pkg/errors" 10 v1 "k8s.io/api/core/v1" 11 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 12 "k8s.io/client-go/kubernetes" 13 ) 14 15 // GetConfigMapData gets config map data 16 func GetConfigMapData(client kubernetes.Interface, name, ns string) (map[string]string, error) { 17 answer := map[string]string{} 18 cm, err := GetConfigMap(client, ns, name) 19 if err != nil { 20 return answer, fmt.Errorf("failed to get configmap %s in namespace %s, %v", name, ns, err) 21 } 22 if cm.Data == nil { 23 return answer, nil 24 } 25 return cm.Data, nil 26 } 27 28 // GetConfigMap gets a named ConfigMap 29 func GetConfigMap(client kubernetes.Interface, ns string, name string) (*v1.ConfigMap, error) { 30 return client.CoreV1().ConfigMaps(ns).Get(name, metav1.GetOptions{}) 31 } 32 33 // GetCurrentDomain gets the current domain 34 func GetCurrentDomain(client kubernetes.Interface, ns string) (string, error) { 35 data, err := GetConfigMapData(client, ConfigMapIngressConfig, ns) 36 if err != nil { 37 data, err = GetConfigMapData(client, ConfigMapExposecontroller, ns) 38 if err != nil { 39 return "", errors.Wrapf(err, "Failed to find ConfigMap in namespace %s for names %s and %s", ns, ConfigMapExposecontroller, ConfigMapIngressConfig) 40 } 41 } 42 return ExtractDomainValue(data) 43 } 44 45 // ExtractDomainValue returns the domain value 46 func ExtractDomainValue(data map[string]string) (string, error) { 47 answer := data["domain"] 48 if answer != "" { 49 return answer, nil 50 } 51 yaml := data["config.yml"] 52 values := strings.Split(yaml, "\n") 53 for _, line := range values { 54 pair := strings.Split(line, ":") 55 if pair[0] == "domain" { 56 return strings.TrimSpace(pair[1]), nil 57 } 58 } 59 return "", fmt.Errorf("failed to find a domain in %s configmap", ConfigMapExposecontroller) 60 } 61 62 // SaveAsConfigMap to the specified namespace as a config map. 63 func SaveAsConfigMap(c kubernetes.Interface, configMapName string, ns string, obj interface{}) (*v1.ConfigMap, error) { 64 config := util.ToStringMapStringFromStruct(obj) 65 66 cm, err := GetConfigMap(c, ns, configMapName) 67 68 if err != nil { 69 cm := &v1.ConfigMap{ 70 Data: config, 71 ObjectMeta: metav1.ObjectMeta{ 72 Name: configMapName, 73 }, 74 } 75 _, err := c.CoreV1().ConfigMaps(ns).Create(cm) 76 if err != nil { 77 return &v1.ConfigMap{}, err 78 } 79 return cm, nil 80 } 81 82 // replace configmap values if it already exists 83 cm.Data = config 84 _, err = c.CoreV1().ConfigMaps(ns).Update(cm) 85 if err != nil { 86 return &v1.ConfigMap{}, err 87 } 88 return cm, nil 89 } 90 91 // GetConfigMaps returns a map of the ConfigMaps along with a sorted list of names 92 func GetConfigMaps(kubeClient kubernetes.Interface, ns string) (map[string]*v1.ConfigMap, []string, error) { 93 m := map[string]*v1.ConfigMap{} 94 95 names := []string{} 96 resourceList, err := kubeClient.CoreV1().ConfigMaps(ns).List(metav1.ListOptions{}) 97 if err != nil { 98 return m, names, err 99 } 100 for _, resource := range resourceList.Items { 101 n := resource.Name 102 copy := resource 103 m[n] = © 104 if n != "" { 105 names = append(names, n) 106 } 107 } 108 sort.Strings(names) 109 return m, names, nil 110 } 111 112 // DefaultModifyConfigMap default implementation of a function to modify 113 func DefaultModifyConfigMap(kubeClient kubernetes.Interface, ns string, name string, fn func(env *v1.ConfigMap) error, defaultConfigMap *v1.ConfigMap) (*v1.ConfigMap, error) { 114 create := false 115 configMap, err := GetConfigMap(kubeClient, ns, name) 116 if err != nil { 117 create = true 118 initialConfigMap := v1.ConfigMap{ 119 ObjectMeta: metav1.ObjectMeta{ 120 Name: name, 121 Labels: map[string]string{}, 122 Annotations: map[string]string{}, 123 }, 124 Data: map[string]string{}, 125 } 126 if defaultConfigMap != nil { 127 initialConfigMap = *defaultConfigMap 128 } 129 configMap = &initialConfigMap 130 } 131 err = fn(configMap) 132 if err != nil { 133 return configMap, err 134 } 135 configMapInterface := kubeClient.CoreV1().ConfigMaps(ns) 136 if create { 137 _, err = configMapInterface.Create(configMap) 138 if err != nil { 139 return configMap, errors.Wrapf(err, "Failed to create ConfigMap %s in namespace %s", name, ns) 140 } 141 return configMap, err 142 } 143 _, err = configMapInterface.Update(configMap) 144 if err != nil { 145 return configMap, errors.Wrapf(err, "Failed to update ConfigMap %s in namespace %s", name, ns) 146 } 147 return configMap, nil 148 }