Files
zeekayandHanzo Dev 1ab6e799c1 cd: the module is github.com/hanzoai/cd, where it is actually hosted
go.mod still declared `module github.com/hanzoai/deploy` after the repo was
renamed. A Go module path is not cosmetic — it is the resolvable location. So
`go get github.com/hanzoai/cd` could not work, and anything importing this
module had to spell a name that no longer exists at that address. The build log
of v3.6.0 is where it surfaced: the compiler was still emitting
github.com/hanzoai/deploy/cmd/cd/commands.

3541 import paths across 690 .go, 19 .proto, 4 .yaml, 3 .sh, 2 .mod, Procfile
and Makefile. Plus the 62 ghcr.io/hanzoai/deploy image references, moved to
ghcr.io/hanzoai/cd to match hanzo.yml's build target — a distinct string from
the module path, replaced separately so neither could clobber the other.

go mod tidy clean, CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build ./... clean.

Co-authored-by: Hanzo Dev <[email protected]>
2026-07-28 21:16:24 -07:00

114 lines
3.8 KiB
Go

package generator
import (
"context"
"log"
"math/rand"
"time"
"github.com/hanzoai/cd/util/settings"
"github.com/hanzoai/cd/util/db"
"github.com/hanzoai/cd/hack/gen-resources/util"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
appclientset "github.com/hanzoai/cd/pkg/client/clientset/versioned"
)
type ApplicationGenerator struct {
argoClientSet *appclientset.Clientset
clientSet *kubernetes.Clientset
}
func NewApplicationGenerator(argoClientSet *appclientset.Clientset, clientSet *kubernetes.Clientset) Generator {
return &ApplicationGenerator{argoClientSet, clientSet}
}
func (generator *ApplicationGenerator) buildRandomSource(repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) {
seed := rand.New(rand.NewSource(time.Now().Unix()))
repoNumber := seed.Int() % len(repositories)
return &v1alpha1.ApplicationSource{
RepoURL: repositories[repoNumber].Repo,
Path: "helm-guestbook",
TargetRevision: "master",
}, nil
}
func (generator *ApplicationGenerator) buildSource(opts *util.GenerateOpts, repositories []*v1alpha1.Repository) (*v1alpha1.ApplicationSource, error) {
if opts.ApplicationOpts.SourceOpts.Strategy == "Random" {
return generator.buildRandomSource(repositories)
}
return generator.buildRandomSource(repositories)
}
func (generator *ApplicationGenerator) buildRandomDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) {
seed := rand.New(rand.NewSource(time.Now().Unix()))
clusterNumber := seed.Int() % len(clusters)
return &v1alpha1.ApplicationDestination{
Namespace: opts.Namespace,
Name: clusters[clusterNumber].Name,
}, nil
}
func (generator *ApplicationGenerator) buildDestination(opts *util.GenerateOpts, clusters []v1alpha1.Cluster) (*v1alpha1.ApplicationDestination, error) {
if opts.ApplicationOpts.DestinationOpts.Strategy == "Random" {
return generator.buildRandomDestination(opts, clusters)
}
return generator.buildRandomDestination(opts, clusters)
}
func (generator *ApplicationGenerator) Generate(opts *util.GenerateOpts) error {
settingsMgr := settings.NewSettingsManager(context.TODO(), generator.clientSet, opts.Namespace)
repositories, err := db.NewDB(opts.Namespace, settingsMgr, generator.clientSet).ListRepositories(context.TODO())
if err != nil {
return err
}
clusters, err := db.NewDB(opts.Namespace, settingsMgr, generator.clientSet).ListClusters(context.TODO())
if err != nil {
return err
}
applications := generator.argoClientSet.ArgoprojV1alpha1().Applications(opts.Namespace)
for i := 0; i < opts.ApplicationOpts.Samples; i++ {
log.Printf("Generate application #%v", i)
source, err := generator.buildSource(opts, repositories)
if err != nil {
return err
}
log.Printf("Pick source %q", source)
destination, err := generator.buildDestination(opts, clusters.Items)
if err != nil {
return err
}
log.Printf("Pick destination %q", destination)
log.Print("Create application")
_, err = applications.Create(context.TODO(), &v1alpha1.Application{
ObjectMeta: metav1.ObjectMeta{
GenerateName: "application-",
Namespace: opts.Namespace,
Labels: labels,
},
Spec: v1alpha1.ApplicationSpec{
Project: "default",
Destination: *destination,
Source: source,
},
}, metav1.CreateOptions{})
if err != nil {
return err
}
}
return nil
}
func (generator *ApplicationGenerator) Clean(opts *util.GenerateOpts) error {
log.Print("Clean applications")
applications := generator.argoClientSet.ArgoprojV1alpha1().Applications(opts.Namespace)
return applications.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{
LabelSelector: "app.kubernetes.io/generated-by=cd-generator",
})
}