mirror of
https://github.com/hanzoai/cd.git
synced 2026-10-30 14:56:45 +00:00
go fix applied the modernizers: promoted-field keys in struct literals,
strings.Cut, SplitSeq, new(expr), slices and maps helpers, wg.Go, reflect.TypeFor
and errors.AsType. omitzero is left out, since omitting a zero struct such as
time.Time{} is a per-field wire decision and not a mechanical one.
Two changes the modernizers could not make for themselves. AsType needs its type
argument to be an error, so the git exit-status reader embeds error in the
interface it asks for. And an error built by concatenating a tag list into its
own format string now says so with a verb, which is what vet was pointing at.
Co-authored-by: Hanzo Dev <[email protected]>
50 lines
1.4 KiB
Go
50 lines
1.4 KiB
Go
package generator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"log"
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
"github.com/hanzoai/cd/hack/gen-resources/util"
|
|
"github.com/hanzoai/cd/pkg/apis/application/v1alpha1"
|
|
appclientset "github.com/hanzoai/cd/pkg/client/clientset/versioned"
|
|
)
|
|
|
|
type ProjectGenerator struct {
|
|
clientSet *appclientset.Clientset
|
|
}
|
|
|
|
func NewProjectGenerator(clientSet *appclientset.Clientset) Generator {
|
|
return &ProjectGenerator{clientSet}
|
|
}
|
|
|
|
func (pg *ProjectGenerator) Generate(opts *util.GenerateOpts) error {
|
|
projects := pg.clientSet.AppsV1alpha1().AppProjects(opts.Namespace)
|
|
for i := 0; i < opts.ProjectOpts.Samples; i++ {
|
|
log.Printf("Generate project #%v", i)
|
|
_, err := projects.Create(context.TODO(), &v1alpha1.AppProject{
|
|
GenerateName: "project-",
|
|
Namespace: opts.Namespace,
|
|
Labels: labels,
|
|
Spec: v1alpha1.AppProjectSpec{
|
|
Description: "generated-project",
|
|
},
|
|
}, metav1.CreateOptions{})
|
|
if err != nil {
|
|
log.Printf("Project #%v failed to generate", i)
|
|
return fmt.Errorf("error in generated-project: %w", err)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (pg *ProjectGenerator) Clean(opts *util.GenerateOpts) error {
|
|
log.Print("Clean projects")
|
|
projects := pg.clientSet.AppsV1alpha1().AppProjects(opts.Namespace)
|
|
return projects.DeleteCollection(context.TODO(), metav1.DeleteOptions{}, metav1.ListOptions{
|
|
LabelSelector: "app.kubernetes.io/generated-by=cd-generator",
|
|
})
|
|
}
|