Skip to content

Commit

Permalink
expose labels Application Creation Option to cli (#271)
Browse files Browse the repository at this point in the history
* expose labels as flag
* get commit author data from token
* added labels docs
* format `config.json` and `config_dir.json` file for simpler reading
  • Loading branch information
ATGardner authored Apr 7, 2022
1 parent 0b05d00 commit ca1b7a6
Show file tree
Hide file tree
Showing 28 changed files with 595 additions and 475 deletions.
107 changes: 53 additions & 54 deletions build/ci.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
version: '1.0'
mode: parallel

stages:
- Prepare
- Test
- Build
- Push Dev
stages:
- Prepare
- Test
- Build
- Push Dev

steps:

main_clone:
stage: Prepare
title: clone repository
Expand All @@ -22,66 +21,66 @@ steps:
title: prepare-env & download modules
image: quay.io/codefresh/golang-ci-helper:latest
commands:
- cf_export GO111MODULE=on
- cf_export GOCACHE=/codefresh/volume/gocache # change gopath to codefresh shared volume
- cf_export GOPATH=/codefresh/volume/gopath
- cf_export PATH=$PATH:/codefresh/volume/gopath/bin
- cf_export GO111MODULE=on
- cf_export GOCACHE=/codefresh/volume/gocache # change gopath to codefresh shared volume
- cf_export GOPATH=/codefresh/volume/gopath
- cf_export PATH=$PATH:/codefresh/volume/gopath/bin
when:
steps:
- name: main_clone
on:
- success
- name: main_clone
on:
- success

download_modules:
<<: *deps
stage: Prepare
title: download modules
commands:
- go mod download -x
- make clean
- go mod download -x
- make clean
when:
steps:
- name: prepare_env_vars
on:
- success
- name: prepare_env_vars
on:
- success

lint:
<<: *deps
stage: Test
title: lint
commands:
- make lint
- make check-worktree
- make lint
- make check-worktree
when:
steps:
- name: download_modules
on:
- success
- name: download_modules
on:
- success

codegen:
<<: *deps
stage: Test
title: check codegen
commands:
- make codegen
- make check-worktree
- make codegen
- make check-worktree
when:
steps:
- name: download_modules
on:
- success
- name: lint
on:
- success

test:
<<: *deps
stage: Test
title: run tests
commands:
- make test
- make test
when:
steps:
- name: lint
on:
- success
- name: codegen
on:
- success

codecov-report:
stage: Test
Expand All @@ -91,10 +90,10 @@ steps:
codecov_integration: ${{CODECOV_INTEGRATION}}
when:
steps:
- name: test
on:
- success
- name: test
on:
- success

build:
stage: Build
title: build docker image
Expand All @@ -105,44 +104,44 @@ steps:
disable_push: true
when:
steps:
- name: main_clone
on:
- success
- name: main_clone
on:
- success

scan-image:
stage: Build
title: scan docker image
image: quay.io/codefresh/snyk-cli:latest
shell: bash
environment:
- SNYK_TOKEN=${{SNYK_TOKEN}}
- LOCAL_IMAGE_REF=${{IMAGE_NAME}}:${{CF_BRANCH_TAG_NORMALIZED_LOWER_CASE}}
- SNYK_TOKEN=${{SNYK_TOKEN}}
- LOCAL_IMAGE_REF=${{IMAGE_NAME}}:${{CF_BRANCH_TAG_NORMALIZED_LOWER_CASE}}
commands:
- |
snyk test --severity-threshold=${{SNYK_SEVERITY_THRESHOLD}} || fail=1
snyk container test --severity-threshold=${{SNYK_SEVERITY_THRESHOLD}} --file=Dockerfile ${LOCAL_IMAGE_REF}
if [ "$fail" == "1" ]; then exit 1; fi
- |
snyk test --severity-threshold=${{SNYK_SEVERITY_THRESHOLD}} || fail=1
snyk container test --severity-threshold=${{SNYK_SEVERITY_THRESHOLD}} --file=Dockerfile ${LOCAL_IMAGE_REF}
if [ "$fail" == "1" ]; then exit 1; fi
when:
steps:
- name: build
on:
- success
- name: build
on:
- success

push_dev:
stage: Push Dev
title: push with dev tags
type: push
candidate: ${{build}}
tags:
- dev-${{CF_BRANCH_TAG_NORMALIZED}}
- dev-${{CF_BRANCH_TAG_NORMALIZED}}
when:
steps:
- name: build
on:
- success
- name: build
on:
- success
branch:
ignore: [ "main" ]
ignore: ['main']
scale:
push_quay_dev:
title: push to quay
registry: "${{REGISTRY_INTEGRATION_QUAY}}"
registry: '${{REGISTRY_INTEGRATION_QUAY}}'
1 change: 1 addition & 0 deletions cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error {

if opts.AppsCloneOpts.Repo != "" {
if opts.AppsCloneOpts.Auth.Password == "" {
opts.AppsCloneOpts.Auth.Username = opts.CloneOpts.Auth.Username
opts.AppsCloneOpts.Auth.Password = opts.CloneOpts.Auth.Password
}

Expand Down
18 changes: 9 additions & 9 deletions cmd/commands/app_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ import (
gitmocks "github.com/argoproj-labs/argocd-autopilot/pkg/git/mocks"
kubemocks "github.com/argoproj-labs/argocd-autopilot/pkg/kube/mocks"
"github.com/argoproj-labs/argocd-autopilot/pkg/store"
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/golang/mock/gomock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/go-git/go-billy/v5/memfs"
billyUtils "github.com/go-git/go-billy/v5/util"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestRunAppCreate(t *testing.T) {
Expand Down Expand Up @@ -473,7 +473,7 @@ func TestRunAppDelete(t *testing.T) {
Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app")))
},
},
Expand Down Expand Up @@ -528,7 +528,7 @@ func TestRunAppDelete(t *testing.T) {
Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.True(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app", store.Default.OverlaysDir)))
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app", store.Default.OverlaysDir, "project")))
},
Expand All @@ -547,7 +547,7 @@ func TestRunAppDelete(t *testing.T) {
Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app")))
},
},
Expand All @@ -566,7 +566,7 @@ func TestRunAppDelete(t *testing.T) {
Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.True(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app")))
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app", "project")))
},
Expand All @@ -585,7 +585,7 @@ func TestRunAppDelete(t *testing.T) {
Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app")))
},
},
Expand All @@ -604,7 +604,7 @@ func TestRunAppDelete(t *testing.T) {
Return("", fmt.Errorf("some error"))
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app")))
},
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ import (
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
gitmocks "github.com/argoproj-labs/argocd-autopilot/pkg/git/mocks"
"github.com/argoproj-labs/argocd-autopilot/pkg/store"
"github.com/golang/mock/gomock"

"github.com/go-git/go-billy/v5/memfs"
billyUtils "github.com/go-git/go-billy/v5/util"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
)

Expand Down
23 changes: 16 additions & 7 deletions cmd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type (
ProjectCreateOptions struct {
CloneOpts *git.CloneOptions
ProjectName string
DestKubeServer string
DestKubeContext string
DryRun bool
AddCmd argocd.AddClusterCmd
Expand Down Expand Up @@ -80,9 +81,11 @@ func NewProjectCommand() *cobra.Command {

func NewProjectCreateCommand() *cobra.Command {
var (
kubeServer string
kubeContext string
dryRun bool
addCmd argocd.AddClusterCmd
labels map[string]string
cloneOpts *git.CloneOptions
)

Expand Down Expand Up @@ -114,15 +117,19 @@ func NewProjectCreateCommand() *cobra.Command {
return RunProjectCreate(ctx, &ProjectCreateOptions{
CloneOpts: cloneOpts,
ProjectName: args[0],
DestKubeServer: kubeServer,
DestKubeContext: kubeContext,
DryRun: dryRun,
AddCmd: addCmd,
Labels: labels,
})
},
}

cmd.Flags().StringVar(&kubeContext, "dest-kube-context", "", "The default destination kubernetes context for applications in this project")
cmd.Flags().StringVar(&kubeServer, "dest-server", "", "The default destination kubernetes server for applications in this project")
cmd.Flags().StringVar(&kubeContext, "dest-kube-context", "", "The default destination kubernetes context for applications in this project (will be ignored if --dest-kube-server is supplied)")
cmd.Flags().BoolVar(&dryRun, "dry-run", false, "If true, print manifests instead of applying them to the cluster (nothing will be commited to git)")
cmd.Flags().StringToStringVar(&labels, "labels", nil, "Optional labels that will be set on the Application resource. (e.g. \"app.kubernetes.io/managed-by={{ placeholder }}\"")

cloneOpts = git.AddFlags(cmd, &git.AddFlagsOptions{
FS: memfs.New(),
Expand Down Expand Up @@ -157,11 +164,13 @@ func RunProjectCreate(ctx context.Context, opts *ProjectCreateOptions) error {

log.G(ctx).Debug("repository is ok")

destServer := store.Default.DestServer
if opts.DestKubeContext != "" {
destServer, err = util.KubeContextToServer(opts.DestKubeContext)
if err != nil {
return err
if opts.DestKubeServer == "" {
opts.DestKubeServer = store.Default.DestServer
if opts.DestKubeContext != "" {
opts.DestKubeServer, err = util.KubeContextToServer(opts.DestKubeContext)
if err != nil {
return err
}
}
}

Expand All @@ -171,7 +180,7 @@ func RunProjectCreate(ctx context.Context, opts *ProjectCreateOptions) error {
RepoURL: opts.CloneOpts.URL(),
Revision: opts.CloneOpts.Revision(),
InstallationPath: opts.CloneOpts.Path(),
DefaultDestServer: destServer,
DefaultDestServer: opts.DestKubeServer,
DefaultDestContext: opts.DestKubeContext,
Labels: opts.Labels,
})
Expand Down
10 changes: 5 additions & 5 deletions cmd/commands/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func TestRunProjectCreate(t *testing.T) {
getInstallationNamespace: func(_ fs.FS) (string, error) {
return "namespace", nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
exists := repofs.ExistsOrDie("projects/project.yaml")
assert.True(t, exists)
},
Expand Down Expand Up @@ -559,7 +559,7 @@ func TestRunProjectDelete(t *testing.T) {
}).Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app1")))
},
},
Expand All @@ -576,7 +576,7 @@ func TestRunProjectDelete(t *testing.T) {
}).Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.True(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app1", store.Default.OverlaysDir)))
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app1", store.Default.OverlaysDir, "project")))
},
Expand All @@ -593,7 +593,7 @@ func TestRunProjectDelete(t *testing.T) {
}).Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app1")))
},
},
Expand All @@ -614,7 +614,7 @@ func TestRunProjectDelete(t *testing.T) {
}).Return("revision", nil)
return mockRepo, fs.Create(memfs), nil
},
assertFn: func(t *testing.T, repo git.Repository, repofs fs.FS) {
assertFn: func(t *testing.T, _ git.Repository, repofs fs.FS) {
assert.True(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app1", store.Default.OverlaysDir)))
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app1", store.Default.OverlaysDir, "project")))
assert.False(t, repofs.ExistsOrDie(filepath.Join(store.Default.AppsDir, "app2")))
Expand Down
Loading

0 comments on commit ca1b7a6

Please sign in to comment.