Skip to content

Commit

Permalink
added labels to project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ATGardner committed Aug 1, 2021
1 parent 1eed10b commit 04af9c6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 7 deletions.
20 changes: 16 additions & 4 deletions cmd/commands/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type (
DestKubeContext string
DryRun bool
AddCmd argocd.AddClusterCmd
Labels map[string]string
}

ProjectDeleteOptions struct {
Expand All @@ -55,6 +56,7 @@ type (
RepoURL string
Revision string
InstallationPath string
Labels map[string]string
}
)

Expand Down Expand Up @@ -171,6 +173,7 @@ func RunProjectCreate(ctx context.Context, opts *ProjectCreateOptions) error {
InstallationPath: opts.CloneOpts.Path(),
DefaultDestServer: destServer,
DefaultDestContext: opts.DestKubeContext,
Labels: opts.Labels,
})
if err != nil {
return fmt.Errorf("failed to generate project resources: %w", err)
Expand Down Expand Up @@ -280,10 +283,7 @@ func generateProjectManifests(o *GenerateProjectOptions) (projectYAML, appSetYAM
destNamespace: "{{ destNamespace }}",
prune: true,
preserveResourcesOnDeletion: false,
appLabels: map[string]string{
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
"app.kubernetes.io/name": "{{ appName }}",
},
appLabels: getDefaultAppLabels(o.Labels),
generators: []appset.ApplicationSetGenerator{
{
Git: &appset.GitGenerator{
Expand Down Expand Up @@ -315,6 +315,18 @@ func generateProjectManifests(o *GenerateProjectOptions) (projectYAML, appSetYAM
return
}

func getDefaultAppLabels(labels map[string]string) map[string]string {
res := map[string]string{
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
store.Default.LabelKeyAppName: "{{ appName }}",
}
for k, v := range labels {
res[k] = v
}

return res
}

func NewProjectListCommand(cloneOpts *git.CloneOptions) *cobra.Command {
cmd := &cobra.Command{
Use: "list ",
Expand Down
64 changes: 61 additions & 3 deletions cmd/commands/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@ import (
"strings"
"testing"

appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
"github.com/argoproj-labs/argocd-autopilot/pkg/application"
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
fsmocks "github.com/argoproj-labs/argocd-autopilot/pkg/fs/mocks"
"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/argoproj-labs/argocd-autopilot/pkg/util"

appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/ghodss/yaml"
"github.com/go-git/go-billy/v5/memfs"
Expand Down Expand Up @@ -157,7 +156,7 @@ func TestRunProjectCreate(t *testing.T) {
}
}

func Test_generateProject(t *testing.T) {
func Test_generateProjectManifests(t *testing.T) {
tests := map[string]struct {
o *GenerateProjectOptions
wantName string
Expand All @@ -168,6 +167,7 @@ func Test_generateProject(t *testing.T) {
wantDefaultDestServer string
wantProject string
wantContextName string
wantLabels map[string]string
}{
"should generate project and appset with correct values": {
o: &GenerateProjectOptions{
Expand All @@ -178,6 +178,9 @@ func Test_generateProject(t *testing.T) {
RepoURL: "repoUrl",
Revision: "revision",
InstallationPath: "some/path",
Labels: map[string]string{
"some-key": "some-value",
},
},
wantName: "name",
wantNamespace: "namespace",
Expand All @@ -186,6 +189,11 @@ func Test_generateProject(t *testing.T) {
wantRevision: "revision",
wantDefaultDestServer: "defaultDestServer",
wantContextName: "some-context-name",
wantLabels: map[string]string{
"some-key": "some-value",
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
store.Default.LabelKeyAppName: "{{ appName }}",
},
},
}
for ttname, tt := range tests {
Expand Down Expand Up @@ -647,3 +655,53 @@ func TestRunProjectDelete(t *testing.T) {
})
}
}

func Test_getDefaultAppLabels(t *testing.T) {
tests := map[string]struct {
labels map[string]string
want map[string]string
}{
"Should return the default map when sending nil": {
labels: nil,
want: map[string]string{
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
store.Default.LabelKeyAppName: "{{ name }}",
},
},
"Should contain any additional labels sent": {
labels: map[string]string{
"something": "or the other",
},
want: map[string]string{
"something": "or the other",
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
store.Default.LabelKeyAppName: "{{ name }}",
},
},
"Should overwrite the default managed by": {
labels: map[string]string{
store.Default.LabelKeyAppManagedBy: "someone else",
},
want: map[string]string{
store.Default.LabelKeyAppManagedBy: "someone else",
store.Default.LabelKeyAppName: "{{ name }}",
},
},
"Should overwrite the default app name": {
labels: map[string]string{
store.Default.LabelKeyAppName: "another name",
},
want: map[string]string{
store.Default.LabelKeyAppManagedBy: store.Default.LabelValueManagedBy,
store.Default.LabelKeyAppName: "another name",
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
if got := getDefaultAppLabels(tt.labels); !reflect.DeepEqual(got, tt.want) {
t.Errorf("getDefaultAppLabels() = %v, want %v", got, tt.want)
}
})
}
}
2 changes: 2 additions & 0 deletions pkg/store/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ var Default = struct {
DummyName string
DestServerAnnotation string
GitUsername string
LabelKeyAppName string
LabelKeyAppManagedBy string
LabelValueManagedBy string
OverlaysDir string
Expand All @@ -66,6 +67,7 @@ var Default = struct {
DestServerAnnotation: "argocd-autopilot.argoproj-labs.io/default-dest-server",
DummyName: "DUMMY",
GitUsername: "username",
LabelKeyAppName: "app.kubernetes.io/name",
LabelKeyAppManagedBy: "app.kubernetes.io/managed-by",
LabelValueManagedBy: "argocd-autopilot",
OverlaysDir: "overlays",
Expand Down

0 comments on commit 04af9c6

Please sign in to comment.