Skip to content

Commit

Permalink
chore: optimize the configmap update code
Browse files Browse the repository at this point in the history
  • Loading branch information
mojtaba-esk committed Sep 25, 2024
1 parent 6572017 commit 9012c69
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 28 deletions.
44 changes: 16 additions & 28 deletions pkg/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8s

import (
"context"
"errors"

v1 "k8s.io/api/core/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -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)
}

Expand All @@ -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)
}

Expand All @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions pkg/k8s/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 9012c69

Please sign in to comment.