Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

delete git auth secret when app creation fails & do not create git auth secret if it already exists #70

Merged
merged 2 commits into from
Dec 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.21'
- run: git config --global url.https://[email protected]/.insteadOf https://github.com/
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
with:
version: v1.50.1
version: v1.54
args: --timeout=10m
test:
name: test
Expand All @@ -29,7 +29,7 @@ jobs:
- uses: actions/checkout@v3
- uses: actions/setup-go@v3
with:
go-version: 1.19
go-version: '1.21'
- run: git config --global url.https://[email protected]/.insteadOf https://github.com/
- name: Get dependencies
run: go get -v -t -d ./...
Expand Down
28 changes: 26 additions & 2 deletions create/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ninech/nctl/api/util"
"github.com/ninech/nctl/internal/format"
"github.com/ninech/nctl/logs"
kerrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
"k8s.io/utils/pointer"
Expand Down Expand Up @@ -102,10 +103,26 @@ func (app *applicationCmd) Run(ctx context.Context, client *api.Client) error {
if err := auth.Valid(); err != nil {
return fmt.Errorf("the credentials are given but they are empty: %w", err)
}
// for git auth we create a separate secret and then reference it in the app.

secret := auth.Secret(newApp)
// for git auth we create a separate secret and then reference it in the app.
if err := client.Create(ctx, secret); err != nil {
return fmt.Errorf("unable to create git auth secret: %w", err)
if kerrors.IsAlreadyExists(err) {
// only update the secret if it is managed by nctl in the first place
if v, exists := newApp.Annotations[util.ManagedByAnnotation]; exists && v == util.NctlName {
fmt.Println("updating git auth credentials")
if err := client.Get(ctx, client.Name(secret.Name), secret); err != nil {
return err
}

auth.UpdateSecret(secret)
if err := client.Update(ctx, secret); err != nil {
ctrox marked this conversation as resolved.
Show resolved Hide resolved
return err
}
}
} else {
return fmt.Errorf("unable to create git auth secret: %w", err)
}
}

newApp.Spec.ForProvider.Git.Auth = &apps.GitAuth{
Expand All @@ -120,6 +137,13 @@ func (app *applicationCmd) Run(ctx context.Context, client *api.Client) error {
defer cancel()

if err := c.createResource(appWaitCtx); err != nil {
if auth.Enabled() {
secret := auth.Secret(newApp)
if gitErr := client.Delete(ctx, secret); err != nil {
return errors.Join(err, fmt.Errorf("unable to delete git auth secret: %w", gitErr))
}
}

return err
}

Expand Down
5 changes: 2 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
module github.com/ninech/nctl

go 1.20
go 1.21

require (
github.com/alecthomas/kong v0.7.0
github.com/crossplane/crossplane-runtime v0.15.1
github.com/docker/docker v20.10.23+incompatible
github.com/fatih/color v1.14.1
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48
github.com/gobuffalo/flect v0.2.3
github.com/goccy/go-yaml v1.11.0
github.com/golang-jwt/jwt v3.2.2+incompatible
Expand All @@ -17,6 +16,7 @@ require (
github.com/hashicorp/go-multierror v1.1.1
github.com/int128/kubelogin v1.25.3
github.com/lucasepe/codename v0.2.0
github.com/mattn/go-isatty v0.0.19
github.com/moby/moby v24.0.1+incompatible
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6
github.com/ninech/apis v0.0.0-20230905134643-5501080a23f9
Expand Down Expand Up @@ -119,7 +119,6 @@ require (
github.com/julienschmidt/httprouter v1.3.0 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.4 // indirect
github.com/miekg/dns v1.1.50 // indirect
Expand Down
Loading