-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: tool for generate argocd resources (#8037)
feat: tool for generate argocd resources (#8037) Signed-off-by: pashavictorovich <pavel@codefresh.io>
- Loading branch information
1 parent
47187bd
commit f652897
Showing
13 changed files
with
656 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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", | ||
}) | ||
} |
Oops, something went wrong.