github.com/operator-framework/operator-lifecycle-manager@v0.30.0/pkg/lib/operatorclient/configmap.go (about) 1 package operatorclient 2 3 import ( 4 "context" 5 "fmt" 6 7 corev1 "k8s.io/api/core/v1" 8 apierrors "k8s.io/apimachinery/pkg/api/errors" 9 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "k8s.io/apimachinery/pkg/types" 11 "k8s.io/klog" 12 ) 13 14 // CreateConfigMap creates the ConfigMap. 15 func (c *Client) CreateConfigMap(ig *corev1.ConfigMap) (*corev1.ConfigMap, error) { 16 createdCM, err := c.CoreV1().ConfigMaps(ig.GetNamespace()).Create(context.TODO(), ig, metav1.CreateOptions{}) 17 if apierrors.IsAlreadyExists(err) { 18 return c.UpdateConfigMap(ig) 19 } 20 return createdCM, err 21 } 22 23 // GetConfigMap returns the existing ConfigMap. 24 func (c *Client) GetConfigMap(namespace, name string) (*corev1.ConfigMap, error) { 25 return c.CoreV1().ConfigMaps(namespace).Get(context.TODO(), name, metav1.GetOptions{}) 26 } 27 28 // DeleteConfigMap deletes the ConfigMap. 29 func (c *Client) DeleteConfigMap(namespace, name string, options *metav1.DeleteOptions) error { 30 return c.CoreV1().ConfigMaps(namespace).Delete(context.TODO(), name, *options) 31 } 32 33 // UpdateConfigMap will update the given ConfigMap resource. 34 func (c *Client) UpdateConfigMap(configmap *corev1.ConfigMap) (*corev1.ConfigMap, error) { 35 klog.V(4).Infof("[UPDATE ConfigMap]: %s", configmap.GetName()) 36 oldSa, err := c.GetConfigMap(configmap.GetNamespace(), configmap.GetName()) 37 if err != nil { 38 return nil, err 39 } 40 patchBytes, err := createPatch(oldSa, configmap) 41 if err != nil { 42 return nil, fmt.Errorf("error creating patch for ConfigMap: %v", err) 43 } 44 return c.CoreV1().ConfigMaps(configmap.GetNamespace()).Patch(context.TODO(), configmap.GetName(), types.StrategicMergePatchType, patchBytes, metav1.PatchOptions{}) 45 }