github.com/argoproj/argo-cd/v2@v2.10.9/hack/gen-resources/generators/application_generator.go (about) 1 package generator 2 3 import ( 4 "context" 5 "log" 6 "math/rand" 7 "time" 8 9 "github.com/argoproj/argo-cd/v2/util/settings" 10 11 "github.com/argoproj/argo-cd/v2/util/db" 12 13 "github.com/argoproj/argo-cd/v2/hack/gen-resources/util" 14 15 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 16 17 "k8s.io/client-go/kubernetes" 18 19 "github.com/argoproj/argo-cd/v2/pkg/apis/application/v1alpha1" 20 appclientset "github.com/argoproj/argo-cd/v2/pkg/client/clientset/versioned" 21 ) 22 23 type ApplicationGenerator struct { 24 argoClientSet *appclientset.Clientset 25 clientSet *kubernetes.Clientset 26 db db.ArgoDB 27 } 28 29 func NewApplicationGenerator(argoClientSet *appclientset.Clientset, clientSet *kubernetes.Clientset, db db.ArgoDB) Generator { 30 return &ApplicationGenerator{argoClientSet, clientSet, db} 31 } 32 33 func (pg *ApplicationGenerator) buildRandomSource(repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) { 34 seed := rand.New(rand.NewSource(time.Now().Unix())) 35 repoNumber := seed.Int() % len(repositories) 36 return &v1alpha1.ApplicationSource{ 37 RepoURL: repositories[repoNumber].Repo, 38 Path: "helm-guestbook", 39 TargetRevision: "master", 40 }, nil 41 } 42 43 func (ag *ApplicationGenerator) buildSource(opts *util.GenerateOpts, repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) { 44 switch opts.ApplicationOpts.SourceOpts.Strategy { 45 case "Random": 46 return ag.buildRandomSource(repositories) 47 } 48 return ag.buildRandomSource(repositories) 49 } 50 51 func (pg *ApplicationGenerator) buildRandomDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) { 52 seed := rand.New(rand.NewSource(time.Now().Unix())) 53 clusterNumber := seed.Int() % len(clusters) 54 return &v1alpha1.ApplicationDestination{ 55 Namespace: opts.Namespace, 56 Name: clusters[clusterNumber].Name, 57 }, nil 58 } 59 60 func (ag *ApplicationGenerator) buildDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) { 61 switch opts.ApplicationOpts.DestinationOpts.Strategy { 62 case "Random": 63 return ag.buildRandomDestination(opts, clusters) 64 } 65 return ag.buildRandomDestination(opts, clusters) 66 } 67 68 func (pg *ApplicationGenerator) Generate(opts *util.GenerateOpts) error { 69 settingsMgr := settings.NewSettingsManager(context.TODO(), pg.clientSet, opts.Namespace) 70 repositories, err := db.NewDB(opts.Namespace, settingsMgr, pg.clientSet).ListRepositories(context.TODO()) 71 if err != nil { 72 return err 73 } 74 clusters, err := db.NewDB(opts.Namespace, settingsMgr, pg.clientSet).ListClusters(context.TODO()) 75 if err != nil { 76 return err 77 } 78 applications := pg.argoClientSet.ArgoprojV1alpha1().Applications(opts.Namespace) 79 for i := 0; i < opts.ApplicationOpts.Samples; i++ { 80 log.Printf("Generate application #%v", i) 81 source, err := pg.buildSource(opts, repositories) 82 if err != nil { 83 return err 84 } 85 log.Printf("Pick source \"%s\"", source) 86 destination, err := pg.buildDestination(opts, clusters.Items) 87 if err != nil { 88 return err 89 } 90 log.Printf("Pick destination \"%s\"", destination) 91 log.Printf("Create application") 92 _, err = applications.Create(context.TODO(), &v1alpha1.Application{ 93 ObjectMeta: v1.ObjectMeta{ 94 GenerateName: "application-", 95 Namespace: opts.Namespace, 96 Labels: labels, 97 }, 98 Spec: v1alpha1.ApplicationSpec{ 99 Project: "default", 100 Destination: *destination, 101 Source: source, 102 }, 103 }, v1.CreateOptions{}) 104 if err != nil { 105 return err 106 } 107 } 108 return nil 109 } 110 111 func (ag *ApplicationGenerator) Clean(opts *util.GenerateOpts) error { 112 log.Printf("Clean applications") 113 applications := ag.argoClientSet.ArgoprojV1alpha1().Applications(opts.Namespace) 114 return applications.DeleteCollection(context.TODO(), v1.DeleteOptions{}, v1.ListOptions{ 115 LabelSelector: "app.kubernetes.io/generated-by=argocd-generator", 116 }) 117 }