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

Fix namespace collision #79

Merged
merged 20 commits into from
Jun 6, 2021
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,13 @@ check-worktree:
$(GOBIN)/mockery:
@mkdir dist || true
@echo installing: mockery
@curl -L -o dist/mockery.tar.gz -- https://github.com/vektra/mockery/releases/download/v1.1.1/mockery_1.1.1_$(shell uname -s)_$(shell uname -m).tar.gz
@curl -L -o dist/mockery.tar.gz -- https://github.com/vektra/mockery/releases/download/v2.8.0/mockery_2.8.0_$(shell uname -s)_$(shell uname -m).tar.gz
@tar zxvf dist/mockery.tar.gz mockery
@rm dist/mockery.tar.gz
@chmod +x mockery
@mkdir -p $(GOBIN)
@mv mockery $(GOBIN)/mockery
@mockery -version
@mockery --version

$(GOBIN)/golangci-lint:
@mkdir dist || true
Expand Down
14 changes: 14 additions & 0 deletions build/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,20 @@ steps:
on:
- success

bump_brew_formula:
stage: Release
title: bump brew formula version
image: linuxbrew/linuxbrew:latest
environment:
- HOMEBREW_GITHUB_API_TOKEN=${{GITHUB_TOKEN}}
commands:
- brew bump-formula-pr --strict --no-browser --tag ${{CF_BRANCH_TAG_NORMALIZED}} --revision ${{CF_REVISION}} --fork-org codefresh-io
when:
steps:
- name: create_release
on:
- success

push_prod:
stage: Release
title: promote images
Expand Down
2 changes: 1 addition & 1 deletion cmd/commands/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func RunAppCreate(ctx context.Context, opts *AppCreateOptions) error {
return err
}

app, err := opts.AppOpts.Parse(opts.ProjectName, opts.CloneOptions.URL, opts.CloneOptions.Revision)
app, err := opts.AppOpts.Parse(opts.ProjectName, opts.CloneOptions.URL, opts.CloneOptions.Revision, opts.CloneOptions.RepoRoot)
if err != nil {
return fmt.Errorf("failed to parse application from flags: %v", err)
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/commands/assets/cluster_res_readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Cluster Resources
This directory contains all cluster resources that should be applied to cluster: `{CLUSTER}`.
For example `Namespace` resources that are shared by multiple applications on the same namespace.
165 changes: 165 additions & 0 deletions cmd/commands/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package commands

import (
"context"
_ "embed"
"fmt"
"os"

appset "github.com/argoproj-labs/applicationset/api/v1alpha1"
"github.com/argoproj-labs/argocd-autopilot/pkg/fs"
"github.com/argoproj-labs/argocd-autopilot/pkg/git"
"github.com/argoproj-labs/argocd-autopilot/pkg/log"
"github.com/argoproj-labs/argocd-autopilot/pkg/store"
"github.com/argoproj-labs/argocd-autopilot/pkg/util"
appsetv1alpha1 "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1"
argocdv1alpha1 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
"github.com/ghodss/yaml"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/go-git/go-billy/v5/memfs"
"github.com/spf13/cobra"
Expand All @@ -28,6 +34,17 @@ var (
die = util.Die
exit = os.Exit

DefaultApplicationSetGeneratorInterval int64 = 20

//go:embed assets/cluster_res_readme.md
clusterResReadmeTpl []byte

//go:embed assets/projects_readme.md
projectReadme []byte

//go:embed assets/apps_readme.md
appsReadme []byte

clone = func(ctx context.Context, cloneOpts *git.CloneOptions, filesystem fs.FS) (git.Repository, fs.FS, error) {
return cloneOpts.Clone(ctx, filesystem)
}
Expand Down Expand Up @@ -86,3 +103,151 @@ func addFlags(cmd *cobra.Command) (*BaseOptions, error) {

return o, nil
}

type createAppOptions struct {
name string
namespace string
repoURL string
revision string
srcPath string
destServer string
noFinalizer bool
}

func createApp(opts *createAppOptions) ([]byte, error) {
if opts.destServer == "" {
opts.destServer = store.Default.DestServer
}

app := &argocdv1alpha1.Application{
TypeMeta: metav1.TypeMeta{
Kind: argocdv1alpha1.ApplicationSchemaGroupVersionKind.Kind,
APIVersion: argocdv1alpha1.ApplicationSchemaGroupVersionKind.GroupVersion().String(),
},
ObjectMeta: metav1.ObjectMeta{
Namespace: opts.namespace,
Name: opts.name,
Labels: map[string]string{
"app.kubernetes.io/managed-by": store.Default.ManagedBy,
"app.kubernetes.io/name": opts.name,
},
Finalizers: []string{
"resources-finalizer.argocd.argoproj.io",
},
},
Spec: argocdv1alpha1.ApplicationSpec{
Project: "default",
Source: argocdv1alpha1.ApplicationSource{
RepoURL: opts.repoURL,
Path: opts.srcPath,
TargetRevision: opts.revision,
},
Destination: argocdv1alpha1.ApplicationDestination{
Server: opts.destServer,
Namespace: opts.namespace,
},
SyncPolicy: &argocdv1alpha1.SyncPolicy{
Automated: &argocdv1alpha1.SyncPolicyAutomated{
SelfHeal: true,
Prune: true,
},
SyncOptions: []string{
"allowEmpty=true",
},
},
IgnoreDifferences: []argocdv1alpha1.ResourceIgnoreDifferences{
{
Group: "argoproj.io",
Kind: "Application",
JSONPointers: []string{
"/status",
},
},
},
},
}
if opts.noFinalizer {
app.ObjectMeta.Finalizers = []string{}
}

return yaml.Marshal(app)
}

type createAppSetOptions struct {
name string
namespace string
appName string
appNamespace string
appProject string
repoURL string
revision string
srcPath string
destServer string
destNamespace string
noFinalizer bool
prune bool
appLabels map[string]string
generators []appset.ApplicationSetGenerator
}

func createAppSet(o *createAppSetOptions) ([]byte, error) {
if o.destServer == "" {
o.destServer = store.Default.DestServer
}

appSet := &appset.ApplicationSet{
TypeMeta: metav1.TypeMeta{
Kind: "ApplicationSet",
APIVersion: appset.GroupVersion.String(),
},
ObjectMeta: metav1.ObjectMeta{
Name: o.name,
Namespace: o.namespace,
},
Spec: appset.ApplicationSetSpec{
Generators: o.generators,
Template: appset.ApplicationSetTemplate{
ApplicationSetTemplateMeta: appset.ApplicationSetTemplateMeta{
Namespace: o.appNamespace,
Name: o.appName,
Labels: o.appLabels,
},
Spec: appsetv1alpha1.ApplicationSpec{
Source: appsetv1alpha1.ApplicationSource{
RepoURL: o.repoURL,
Path: o.srcPath,
TargetRevision: o.revision,
},
Destination: appsetv1alpha1.ApplicationDestination{
Server: o.destServer,
Namespace: o.destNamespace,
},
SyncPolicy: &appsetv1alpha1.SyncPolicy{
Automated: &appsetv1alpha1.SyncPolicyAutomated{
SelfHeal: true,
Prune: o.prune,
},
},
},
},
},
}

if o.appLabels == nil {
// default labels
appSet.Spec.Template.ApplicationSetTemplateMeta.Labels = map[string]string{
"app.kubernetes.io/managed-by": store.Default.ManagedBy,
"app.kubernetes.io/name": o.appName,
}
}

if o.appProject != "" {
appSet.Spec.Template.Spec.Project = o.appProject
}

if o.noFinalizer {
appSet.ObjectMeta.Finalizers = []string{}
}

return yaml.Marshal(appSet)
}
Loading