diff --git a/pkg/k8s/configmap.go b/pkg/k8s/configmap.go index 3f0743d..6081d4f 100644 --- a/pkg/k8s/configmap.go +++ b/pkg/k8s/configmap.go @@ -2,6 +2,7 @@ package k8s import ( "context" + "errors" v1 "k8s.io/api/core/v1" apierrs "k8s.io/apimachinery/pkg/api/errors" @@ -37,27 +38,16 @@ func (c *Client) CreateConfigMap( return nil, ErrClientTerminated } - if err := validateConfigMapName(name); err != nil { + if err := validateConfigMap(name, labels, data); err != nil { return nil, err } - if err := validateLabels(labels); err != nil { - return nil, err - } - if err := validateConfigMapKeys(data); err != nil { - return nil, err - } - - exists, err := c.ConfigMapExists(ctx, name) - if err != nil { - return nil, err - } - if exists { - return nil, ErrConfigmapAlreadyExists.WithParams(name) - } cm := prepareConfigMap(c.namespace, name, labels, data) created, err := c.clientset.CoreV1().ConfigMaps(c.namespace).Create(ctx, cm, metav1.CreateOptions{}) if err != nil { + if !apierrs.IsAlreadyExists(err) { + return nil, ErrConfigmapAlreadyExists.WithParams(name).Wrap(err) + } return nil, ErrCreatingConfigmap.WithParams(name).Wrap(err) } @@ -72,19 +62,16 @@ func (c *Client) UpdateConfigMap( return nil, ErrClientTerminated } - if err := validateConfigMapName(name); err != nil { - return nil, err - } - if err := validateLabels(labels); err != nil { - return nil, err - } - if err := validateConfigMapKeys(data); err != nil { + if err := validateConfigMap(name, labels, data); err != nil { return nil, err } cm := prepareConfigMap(c.namespace, name, labels, data) updated, err := c.clientset.CoreV1().ConfigMaps(c.namespace).Update(ctx, cm, metav1.UpdateOptions{}) if err != nil { + if !apierrs.IsNotFound(err) { + return nil, ErrConfigmapDoesNotExist.WithParams(name).Wrap(err) + } return nil, ErrUpdatingConfigmap.WithParams(name).Wrap(err) } @@ -95,15 +82,16 @@ func (c *Client) CreateOrUpdateConfigMap( ctx context.Context, name string, labels, data map[string]string, ) (*v1.ConfigMap, error) { - exists, err := c.ConfigMapExists(ctx, name) - if err != nil { - return nil, err + updated, err := c.UpdateConfigMap(ctx, name, labels, data) + if err == nil { + return updated, nil } - if exists { - return c.UpdateConfigMap(ctx, name, labels, data) + + if errors.Is(err, ErrConfigmapDoesNotExist) { + return c.CreateConfigMap(ctx, name, labels, data) } - return c.CreateConfigMap(ctx, name, labels, data) + return nil, ErrUpdatingConfigmap.WithParams(name).Wrap(err) } func (c *Client) DeleteConfigMap(ctx context.Context, name string) error { diff --git a/pkg/k8s/validate.go b/pkg/k8s/validate.go index 3eecb73..ee1502e 100644 --- a/pkg/k8s/validate.go +++ b/pkg/k8s/validate.go @@ -276,3 +276,13 @@ func validatePorts(ports []int) error { } return nil } + +func validateConfigMap(name string, labels, data map[string]string) error { + if err := validateConfigMapName(name); err != nil { + return err + } + if err := validateLabels(labels); err != nil { + return err + } + return validateConfigMapKeys(data) +}