Skip to content

Commit

Permalink
handle error when namespace already exsist and ignore it
Browse files Browse the repository at this point in the history
Signed-off-by: hungran <26101787+hungran@users.noreply.github.com>
  • Loading branch information
hungran committed Jul 21, 2023
1 parent 994be6a commit d69e425
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
12 changes: 8 additions & 4 deletions pkg/app/piped/platformprovider/kubernetes/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@ func (a *applier) ApplyManifest(ctx context.Context, manifest Manifest) error {
if a.initErr != nil {
return a.initErr
}

if a.input.AutoCreateNamespace == true {
if err := a.kubectl.CreateNamespace(
err := a.kubectl.CreateNamespace(
ctx,
a.platformProvider.KubeConfigPath,
a.getNamespaceToRun(manifest.Key),
); err != nil {
)
if err != nil && !errors.Is(err, errReplaceAlreadyExists) {
return err
}

Check warning on line 75 in pkg/app/piped/platformprovider/kubernetes/applier.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/kubernetes/applier.go#L67-L75

Added lines #L67 - L75 were not covered by tests
}
Expand All @@ -89,12 +91,14 @@ func (a *applier) CreateManifest(ctx context.Context, manifest Manifest) error {
if a.initErr != nil {
return a.initErr
}

if a.input.AutoCreateNamespace == true {
if err := a.kubectl.CreateNamespace(
err := a.kubectl.CreateNamespace(
ctx,
a.platformProvider.KubeConfigPath,
a.getNamespaceToRun(manifest.Key),
); err != nil {
)
if err != nil && !errors.Is(err, errReplaceAlreadyExists) {
return err
}

Check warning on line 103 in pkg/app/piped/platformprovider/kubernetes/applier.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/piped/platformprovider/kubernetes/applier.go#L95-L103

Added lines #L95 - L103 were not covered by tests
}
Expand Down
13 changes: 6 additions & 7 deletions pkg/app/piped/platformprovider/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ import (
)

var (
errorReplaceNotFound = errors.New("specified resource is not found")
errorNotFoundLiteral = "Error from server (NotFound)"
errorReplaceNotFound = errors.New("specified resource is not found")
errorNotFoundLiteral = "Error from server (NotFound)"
errReplaceAlreadyExists = errors.New("resource already exists")
errAlreadyExistsLiteral = "Error from server (AlreadyExists)"
)

type Kubectl struct {
Expand Down Expand Up @@ -234,11 +236,8 @@ func (c *Kubectl) CreateNamespace(ctx context.Context, kubeconfig, namespace str
cmd := exec.CommandContext(ctx, c.execPath, args...)
out, err := cmd.CombinedOutput()

if strings.Contains(string(out), "(AlreadyExists)") {
_, err := fmt.Printf("Namespace already exists: %s", string(out))
if err != nil {
return err
}
if strings.Contains(string(out), errAlreadyExistsLiteral) {
return errReplaceAlreadyExists
}
if err != nil {
return fmt.Errorf("failed to create namespace: %s, %v", string(out), err)
Expand Down

0 comments on commit d69e425

Please sign in to comment.