Skip to content

Commit

Permalink
feat: tool for generate argocd resources (#8037)
Browse files Browse the repository at this point in the history
feat: tool for generate argocd resources  (#8037)

Signed-off-by: pashavictorovich <pavel@codefresh.io>
  • Loading branch information
pasha-codefresh authored Jan 14, 2022
1 parent 47187bd commit f652897
Show file tree
Hide file tree
Showing 13 changed files with 656 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ DIST_DIR=${CURRENT_DIR}/dist
CLI_NAME=argocd
BIN_NAME=argocd

GEN_RESOURCES_CLI_NAME=argocd-resources-gen

HOST_OS:=$(shell go env GOOS)
HOST_ARCH:=$(shell go env GOARCH)

Expand Down Expand Up @@ -222,6 +224,10 @@ cli: test-tools-image
cli-local: clean-debug
CGO_ENABLED=0 go build -v -ldflags '${LDFLAGS}' -o ${DIST_DIR}/${CLI_NAME} ./cmd

.PHONY: gen-resources-cli-local
gen-resources-cli-local: clean-debug
CGO_ENABLED=0 go build -v -ldflags '${LDFLAGS}' -o ${DIST_DIR}/${GEN_RESOURCES_CLI_NAME} ./hack/gen-resources/cmd

.PHONY: release-cli
release-cli: clean-debug
make BIN_NAME=argocd-darwin-amd64 GOOS=darwin argocd-all
Expand Down
76 changes: 76 additions & 0 deletions hack/gen-resources/cmd/commands/all.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package commands

import (
"log"
"os"

generator "github.com/argoproj/argo-cd/v2/hack/gen-resources/generators"
"github.com/argoproj/argo-cd/v2/hack/gen-resources/tools"

"github.com/spf13/cobra"
)

func NewAllResourcesCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "all",
Short: "Manage all resources",
Long: "Manage all resources",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
os.Exit(1)
},
}
command.AddCommand(NewAllResourcesGenerationCommand(opts))
command.AddCommand(NewAllResourcesCleanCommand(opts))
return command
}

func NewAllResourcesGenerationCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "generate",
Short: "Generate all resources",
Long: "Generate all resources",
Run: func(c *cobra.Command, args []string) {
clientSet := tools.ConnectToK8sArgoClientSet()
pg := generator.NewProjectGenerator(clientSet)
ag := generator.NewApplicationGenerator(clientSet)
rg := generator.NewRepoGenerator(tools.ConnectToK8sClientSet())
err := pg.Generate(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
err = ag.Generate(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
err = rg.Generate(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
command.PersistentFlags().IntVar(&opts.Samples, "samples", 1, "Amount of samples")
return command
}

func NewAllResourcesCleanCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "clean",
Short: "Clean all resources",
Long: "Clean all resources",
Run: func(c *cobra.Command, args []string) {
clientSet := tools.ConnectToK8sArgoClientSet()
pg := generator.NewProjectGenerator(clientSet)
ag := generator.NewApplicationGenerator(clientSet)
err := pg.Clean(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
err = ag.Clean(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
return command
}
59 changes: 59 additions & 0 deletions hack/gen-resources/cmd/commands/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package commands

import (
"log"
"os"

generator "github.com/argoproj/argo-cd/v2/hack/gen-resources/generators"
"github.com/argoproj/argo-cd/v2/hack/gen-resources/tools"

"github.com/spf13/cobra"
)

func NewApplicationCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "application",
Short: "Manage applications",
Long: "Manage applications",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
os.Exit(1)
},
}
command.AddCommand(NewApplicationGenerationCommand(opts))
command.AddCommand(NewApplicationCleanCommand(opts))
return command
}

func NewApplicationGenerationCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "generate",
Short: "Generate applications",
Long: "Generate applications",
Run: func(c *cobra.Command, args []string) {
pg := generator.NewApplicationGenerator(tools.ConnectToK8sArgoClientSet())
err := pg.Generate(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
command.PersistentFlags().IntVar(&opts.Samples, "samples", 1, "Amount of samples")
return command
}

func NewApplicationCleanCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "clean",
Short: "Clean applications",
Long: "Clean applications",
Run: func(c *cobra.Command, args []string) {
pg := generator.NewApplicationGenerator(tools.ConnectToK8sArgoClientSet())
err := pg.Clean(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
return command
}
60 changes: 60 additions & 0 deletions hack/gen-resources/cmd/commands/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package commands

import (
"log"
"os"

"github.com/argoproj/argo-cd/v2/hack/gen-resources/tools"

generator "github.com/argoproj/argo-cd/v2/hack/gen-resources/generators"

"github.com/spf13/cobra"
)

func NewProjectCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "project",
Short: "Manage projects",
Long: "Manage projects",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
os.Exit(1)
},
}
command.AddCommand(NewProjectGenerationCommand(opts))
command.AddCommand(NewProjectCleanCommand(opts))
return command
}

func NewProjectGenerationCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "generate",
Short: "Generate projects",
Long: "Generate projects",
Run: func(c *cobra.Command, args []string) {
pg := generator.NewProjectGenerator(tools.ConnectToK8sArgoClientSet())
err := pg.Generate(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
command.PersistentFlags().IntVar(&opts.Samples, "samples", 1, "Amount of samples")
return command
}

func NewProjectCleanCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "clean",
Short: "Clean projects",
Long: "Clean projects",
Run: func(c *cobra.Command, args []string) {
pg := generator.NewProjectGenerator(tools.ConnectToK8sArgoClientSet())
err := pg.Clean(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
return command
}
61 changes: 61 additions & 0 deletions hack/gen-resources/cmd/commands/repos.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package commands

import (
"log"
"os"

"github.com/argoproj/argo-cd/v2/hack/gen-resources/tools"

generator "github.com/argoproj/argo-cd/v2/hack/gen-resources/generators"

"github.com/spf13/cobra"
)

func NewReposCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "repos",
Short: "Manage repos",
Long: "Manage repos",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
os.Exit(1)
},
}
command.AddCommand(NewReposGenerationCommand(opts))
command.AddCommand(NewReposCleanCommand(opts))
return command
}

func NewReposGenerationCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "generate",
Short: "Generate repos",
Long: "Generate repos",
Run: func(c *cobra.Command, args []string) {
rg := generator.NewRepoGenerator(tools.ConnectToK8sClientSet())
err := rg.Generate(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
command.PersistentFlags().StringVar(&opts.GithubToken, "token", "", "Github token")
command.PersistentFlags().IntVar(&opts.Samples, "samples", 1, "Amount of samples")
return command
}

func NewReposCleanCommand(opts *generator.GenerateOpts) *cobra.Command {
var command = &cobra.Command{
Use: "clean",
Short: "Clean repos",
Long: "Clean repos",
Run: func(c *cobra.Command, args []string) {
pg := generator.NewRepoGenerator(tools.ConnectToK8sClientSet())
err := pg.Clean(opts)
if err != nil {
log.Fatalf("Something went wrong, %v", err.Error())
}
},
}
return command
}
42 changes: 42 additions & 0 deletions hack/gen-resources/cmd/commands/root.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package commands

import (
"github.com/spf13/cobra"

generator "github.com/argoproj/argo-cd/v2/hack/gen-resources/generators"

cmdutil "github.com/argoproj/argo-cd/v2/cmd/util"
"github.com/argoproj/argo-cd/v2/util/cli"
)

const (
cliName = "argocd-generator"
)

func init() {
cobra.OnInitialize(initConfig)
}

func initConfig() {
cli.SetLogFormat(cmdutil.LogFormat)
cli.SetLogLevel(cmdutil.LogLevel)
}

// NewCommand returns a new instance of an argocd command
func NewCommand() *cobra.Command {
var generateOpts generator.GenerateOpts
var command = &cobra.Command{
Use: cliName,
Short: "Generator for argocd resources",
Run: func(c *cobra.Command, args []string) {
c.HelpFunc()(c, args)
},
DisableAutoGenTag: true,
}
command.AddCommand(NewProjectCommand(&generateOpts))
command.AddCommand(NewApplicationCommand(&generateOpts))
command.AddCommand(NewAllResourcesCommand(&generateOpts))
command.AddCommand(NewReposCommand(&generateOpts))
command.PersistentFlags().StringVar(&generateOpts.Namespace, "kube-namespace", "argocd", "Name of the namespace on which Argo agent should be installed [$KUBE_NAMESPACE]")
return command
}
16 changes: 16 additions & 0 deletions hack/gen-resources/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package main

import (
"fmt"
"os"

"github.com/argoproj/argo-cd/v2/hack/gen-resources/cmd/commands"
)

func main() {
command := commands.NewCommand()
if err := command.Execute(); err != nil {
fmt.Println(err)
os.Exit(1)
}
}
54 changes: 54 additions & 0 deletions hack/gen-resources/generators/application_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package generator

import (
"context"

v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1"
appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned"
)

type ApplicationGenerator struct {
clientSet *appclientset.Clientset
}

func NewApplicationGenerator(clientSet *appclientset.Clientset) Generator {
return &ApplicationGenerator{clientSet}
}

func (pg *ApplicationGenerator) Generate(opts *GenerateOpts) error {
applications := pg.clientSet.ArgoprojV1alpha1().Applications(opts.Namespace)
for i := 0; i < opts.Samples; i++ {
_, err := applications.Create(context.TODO(), &v1alpha1.Application{
ObjectMeta: v1.ObjectMeta{
GenerateName: "application-",
Namespace: opts.Namespace,
Labels: labels,
},
Spec: v1alpha1.ApplicationSpec{
Project: "default",
Destination: v1alpha1.ApplicationDestination{
Namespace: opts.Namespace,
Name: "in-cluster",
},
Source: v1alpha1.ApplicationSource{
RepoURL: "https://github.com/argoproj/argocd-example-apps",
Path: "helm-guestbook",
TargetRevision: "master",
},
},
}, v1.CreateOptions{})
if err != nil {
return err
}
}
return nil
}

func (ag *ApplicationGenerator) Clean(opts *GenerateOpts) error {
applications := ag.clientSet.ArgoprojV1alpha1().Applications(opts.Namespace)
return applications.DeleteCollection(context.TODO(), v1.DeleteOptions{}, v1.ListOptions{
LabelSelector: "app.kubernetes.io/generated-by=argocd-generator",
})
}
Loading

0 comments on commit f652897

Please sign in to comment.