From 28bed19b7d1f96bdb798493d128c606979f97a5d Mon Sep 17 00:00:00 2001 From: Dayuan Date: Wed, 16 Aug 2023 20:39:58 +0800 Subject: [PATCH] refactor: reorg generators packages and simplify namings --- pkg/cmd/spec/generator.go | 12 +-- .../app_configurations_generator.go | 34 ------- .../generator/app_configurations_generator.go | 92 +++++++++++++++++++ .../namespace_generator.go | 13 +-- .../namespace_generator_test.go | 2 +- .../workload}/job_generator.go | 31 ++++--- .../workload/service_generator.go} | 31 ++++--- .../generator/workload/workload_generator.go | 90 ++++++++++++++++++ .../generators/app_configuration_generator.go | 57 ------------ .../generators/workload_generator.go | 55 ----------- .../{generators => }/types.go | 2 +- .../appconfiguration/{generators => }/util.go | 61 +++--------- 12 files changed, 243 insertions(+), 237 deletions(-) delete mode 100644 pkg/generator/appconfiguration/app_configurations_generator.go create mode 100644 pkg/generator/appconfiguration/generator/app_configurations_generator.go rename pkg/generator/appconfiguration/{generators => generator}/namespace_generator.go (64%) rename pkg/generator/appconfiguration/{generators => generator}/namespace_generator_test.go (98%) rename pkg/generator/appconfiguration/{generators => generator/workload}/job_generator.go (68%) rename pkg/generator/appconfiguration/{generators/workload_service_generator.go => generator/workload/service_generator.go} (74%) create mode 100644 pkg/generator/appconfiguration/generator/workload/workload_generator.go delete mode 100644 pkg/generator/appconfiguration/generators/app_configuration_generator.go delete mode 100644 pkg/generator/appconfiguration/generators/workload_generator.go rename pkg/generator/appconfiguration/{generators => }/types.go (86%) rename pkg/generator/appconfiguration/{generators => }/util.go (60%) diff --git a/pkg/cmd/spec/generator.go b/pkg/cmd/spec/generator.go index ab90ce01..d656e586 100644 --- a/pkg/cmd/spec/generator.go +++ b/pkg/cmd/spec/generator.go @@ -12,11 +12,11 @@ import ( "gopkg.in/yaml.v3" "kusionstack.io/kusion/pkg/generator" - "kusionstack.io/kusion/pkg/generator/appconfiguration" + appgenerator "kusionstack.io/kusion/pkg/generator/appconfiguration/generator" "kusionstack.io/kusion/pkg/generator/kcl" "kusionstack.io/kusion/pkg/log" "kusionstack.io/kusion/pkg/models" - appconfigmodel "kusionstack.io/kusion/pkg/models/appconfiguration" + appmodel "kusionstack.io/kusion/pkg/models/appconfiguration" "kusionstack.io/kusion/pkg/projectstack" "kusionstack.io/kusion/pkg/util/pretty" ) @@ -59,7 +59,7 @@ func GenerateSpec(o *generator.Options, project *projectstack.Project, stack *pr var g generator.Generator pg := project.Generator - // default Generator + // default AppsGenerator if pg == nil { g = &kcl.Generator{} } else { @@ -73,7 +73,7 @@ func GenerateSpec(o *generator.Options, project *projectstack.Project, stack *pr if err != nil { return nil, err } - g = &appconfiguration.Generator{Apps: appConfigs} + g = &appgenerator.AppsGenerator{Apps: appConfigs} default: return nil, fmt.Errorf("unknow generator type:%s", gt) } @@ -86,7 +86,7 @@ func GenerateSpec(o *generator.Options, project *projectstack.Project, stack *pr return spec, nil } -func buildAppConfigs(o *generator.Options, stack *projectstack.Stack) (map[string]appconfigmodel.AppConfiguration, error) { +func buildAppConfigs(o *generator.Options, stack *projectstack.Stack) (map[string]appmodel.AppConfiguration, error) { compileResult, err := kcl.Run(o, stack) if err != nil { return nil, err @@ -103,7 +103,7 @@ func buildAppConfigs(o *generator.Options, stack *projectstack.Stack) (map[strin } log.Debugf("unmarshal %s to app configs", out) - appConfigs := map[string]appconfigmodel.AppConfiguration{} + appConfigs := map[string]appmodel.AppConfiguration{} err = yaml.Unmarshal(out, appConfigs) if err != nil { return nil, err diff --git a/pkg/generator/appconfiguration/app_configurations_generator.go b/pkg/generator/appconfiguration/app_configurations_generator.go deleted file mode 100644 index dfa2b84b..00000000 --- a/pkg/generator/appconfiguration/app_configurations_generator.go +++ /dev/null @@ -1,34 +0,0 @@ -package appconfiguration - -import ( - "kusionstack.io/kusion/pkg/generator" - "kusionstack.io/kusion/pkg/generator/appconfiguration/generators" - "kusionstack.io/kusion/pkg/models" - appmodel "kusionstack.io/kusion/pkg/models/appconfiguration" - "kusionstack.io/kusion/pkg/projectstack" -) - -type Generator struct { - Apps map[string]appmodel.AppConfiguration -} - -func (acg *Generator) GenerateSpec( - o *generator.Options, - project *projectstack.Project, - stack *projectstack.Stack, -) (*models.Spec, error) { - spec := &models.Spec{ - Resources: []models.Resource{}, - } - - gfs := []generators.NewGeneratorFunc{} - generators.ForeachOrdered(acg.Apps, func(appName string, app appmodel.AppConfiguration) error { - gfs = append(gfs, generators.NewAppConfigurationGeneratorFunc(project.Name, appName, &app)) - return nil - }) - if err := generators.CallGenerators(spec, gfs...); err != nil { - return nil, err - } - - return spec, nil -} diff --git a/pkg/generator/appconfiguration/generator/app_configurations_generator.go b/pkg/generator/appconfiguration/generator/app_configurations_generator.go new file mode 100644 index 00000000..a7796b95 --- /dev/null +++ b/pkg/generator/appconfiguration/generator/app_configurations_generator.go @@ -0,0 +1,92 @@ +package generator + +import ( + "fmt" + + "kusionstack.io/kusion/pkg/generator" + "kusionstack.io/kusion/pkg/generator/appconfiguration" + "kusionstack.io/kusion/pkg/generator/appconfiguration/generator/workload" + "kusionstack.io/kusion/pkg/models" + appmodel "kusionstack.io/kusion/pkg/models/appconfiguration" + "kusionstack.io/kusion/pkg/projectstack" +) + +type AppsGenerator struct { + Apps map[string]appmodel.AppConfiguration +} + +func (acg *AppsGenerator) GenerateSpec( + o *generator.Options, + project *projectstack.Project, + stack *projectstack.Stack, +) (*models.Spec, error) { + spec := &models.Spec{ + Resources: []models.Resource{}, + } + + gfs := []appconfiguration.NewGeneratorFunc{} + appconfiguration.ForeachOrdered(acg.Apps, func(appName string, app appmodel.AppConfiguration) error { + gfs = append(gfs, NewAppConfigurationGeneratorFunc(project.Name, appName, &app)) + return nil + }) + if err := appconfiguration.CallGenerators(spec, gfs...); err != nil { + return nil, err + } + + return spec, nil +} + +type appConfigurationGenerator struct { + projectName string + appName string + app *appmodel.AppConfiguration +} + +func NewAppConfigurationGenerator( + projectName, appName string, + app *appmodel.AppConfiguration, +) (appconfiguration.Generator, error) { + if len(projectName) == 0 { + return nil, fmt.Errorf("project name must not be empty") + } + + if len(appName) == 0 { + return nil, fmt.Errorf("app name must not be empty") + } + + if app == nil { + return nil, fmt.Errorf("can not find app configuration when generating the Spec") + } + + return &appConfigurationGenerator{ + projectName: projectName, + appName: appName, + app: app, + }, nil +} + +func NewAppConfigurationGeneratorFunc( + projectName, appName string, + app *appmodel.AppConfiguration, +) appconfiguration.NewGeneratorFunc { + return func() (appconfiguration.Generator, error) { + return NewAppConfigurationGenerator(projectName, appName, app) + } +} + +func (g *appConfigurationGenerator) Generate(spec *models.Spec) error { + if spec.Resources == nil { + spec.Resources = make(models.Resources, 0) + } + + gfs := []appconfiguration.NewGeneratorFunc{ + NewNamespaceGeneratorFunc(g.projectName), + workload.NewWorkloadGeneratorFunc(g.projectName, g.appName, g.app.Workload), + } + + if err := appconfiguration.CallGenerators(spec, gfs...); err != nil { + return err + } + + return nil +} diff --git a/pkg/generator/appconfiguration/generators/namespace_generator.go b/pkg/generator/appconfiguration/generator/namespace_generator.go similarity index 64% rename from pkg/generator/appconfiguration/generators/namespace_generator.go rename to pkg/generator/appconfiguration/generator/namespace_generator.go index 93a60ac4..f54e6143 100644 --- a/pkg/generator/appconfiguration/generators/namespace_generator.go +++ b/pkg/generator/appconfiguration/generator/namespace_generator.go @@ -1,4 +1,4 @@ -package generators +package generator import ( "fmt" @@ -6,6 +6,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "kusionstack.io/kusion/pkg/generator/appconfiguration" "kusionstack.io/kusion/pkg/models" ) @@ -13,7 +14,7 @@ type namespaceGenerator struct { projectName string } -func NewNamespaceGenerator(projectName string) (Generator, error) { +func NewNamespaceGenerator(projectName string) (appconfiguration.Generator, error) { if len(projectName) == 0 { return nil, fmt.Errorf("project name must not be empty") } @@ -23,8 +24,8 @@ func NewNamespaceGenerator(projectName string) (Generator, error) { }, nil } -func NewNamespaceGeneratorFunc(projectName string) NewGeneratorFunc { - return func() (Generator, error) { +func NewNamespaceGeneratorFunc(projectName string) appconfiguration.NewGeneratorFunc { + return func() (appconfiguration.Generator, error) { return NewNamespaceGenerator(projectName) } } @@ -42,8 +43,8 @@ func (g *namespaceGenerator) Generate(spec *models.Spec) error { ObjectMeta: metav1.ObjectMeta{Name: g.projectName}, } - return appendToSpec( - kubernetesResourceID(ns.TypeMeta, ns.ObjectMeta), + return appconfiguration.AppendToSpec( + appconfiguration.KubernetesResourceID(ns.TypeMeta, ns.ObjectMeta), ns, spec, ) diff --git a/pkg/generator/appconfiguration/generators/namespace_generator_test.go b/pkg/generator/appconfiguration/generator/namespace_generator_test.go similarity index 98% rename from pkg/generator/appconfiguration/generators/namespace_generator_test.go rename to pkg/generator/appconfiguration/generator/namespace_generator_test.go index 5f9b9680..535922fb 100644 --- a/pkg/generator/appconfiguration/generators/namespace_generator_test.go +++ b/pkg/generator/appconfiguration/generator/namespace_generator_test.go @@ -1,4 +1,4 @@ -package generators +package generator import ( "testing" diff --git a/pkg/generator/appconfiguration/generators/job_generator.go b/pkg/generator/appconfiguration/generator/workload/job_generator.go similarity index 68% rename from pkg/generator/appconfiguration/generators/job_generator.go rename to pkg/generator/appconfiguration/generator/workload/job_generator.go index 000db029..ba89b53c 100644 --- a/pkg/generator/appconfiguration/generators/job_generator.go +++ b/pkg/generator/appconfiguration/generator/workload/job_generator.go @@ -1,10 +1,11 @@ -package generators +package workload import ( batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "kusionstack.io/kusion/pkg/generator/appconfiguration" "kusionstack.io/kusion/pkg/models" "kusionstack.io/kusion/pkg/models/appconfiguration/workload" ) @@ -15,7 +16,7 @@ type jobGenerator struct { job *workload.Job } -func NewJobGenerator(projectName, appName string, job *workload.Job) (Generator, error) { +func NewJobGenerator(projectName, appName string, job *workload.Job) (appconfiguration.Generator, error) { return &jobGenerator{ projectName: projectName, appName: appName, @@ -23,8 +24,8 @@ func NewJobGenerator(projectName, appName string, job *workload.Job) (Generator, }, nil } -func NewJobGeneratorFunc(projectName, appName string, job *workload.Job) NewGeneratorFunc { - return func() (Generator, error) { +func NewJobGeneratorFunc(projectName, appName string, job *workload.Job) appconfiguration.NewGeneratorFunc { + return func() (appconfiguration.Generator, error) { return NewJobGenerator(projectName, appName, job) } } @@ -41,12 +42,12 @@ func (g *jobGenerator) Generate(spec *models.Spec) error { meta := metav1.ObjectMeta{ Namespace: g.projectName, - Name: uniqueAppName(g.projectName, g.appName), - Labels: mergeMaps( - uniqueAppLabels(g.projectName, g.appName), + Name: appconfiguration.UniqueAppName(g.projectName, g.appName), + Labels: appconfiguration.MergeMaps( + appconfiguration.UniqueAppLabels(g.projectName, g.appName), g.job.Labels, ), - Annotations: mergeMaps( + Annotations: appconfiguration.MergeMaps( g.job.Annotations, ), } @@ -58,11 +59,11 @@ func (g *jobGenerator) Generate(spec *models.Spec) error { jobSpec := batchv1.JobSpec{ Template: corev1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: mergeMaps( - uniqueAppLabels(g.projectName, g.appName), + Labels: appconfiguration.MergeMaps( + appconfiguration.UniqueAppLabels(g.projectName, g.appName), g.job.Labels, ), - Annotations: mergeMaps( + Annotations: appconfiguration.MergeMaps( g.job.Annotations, ), }, @@ -81,8 +82,8 @@ func (g *jobGenerator) Generate(spec *models.Spec) error { }, Spec: jobSpec, } - return appendToSpec( - kubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), + return appconfiguration.AppendToSpec( + appconfiguration.KubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), resource, spec, ) @@ -101,8 +102,8 @@ func (g *jobGenerator) Generate(spec *models.Spec) error { Schedule: job.Schedule, }, } - return appendToSpec( - kubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), + return appconfiguration.AppendToSpec( + appconfiguration.KubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), resource, spec, ) diff --git a/pkg/generator/appconfiguration/generators/workload_service_generator.go b/pkg/generator/appconfiguration/generator/workload/service_generator.go similarity index 74% rename from pkg/generator/appconfiguration/generators/workload_service_generator.go rename to pkg/generator/appconfiguration/generator/workload/service_generator.go index 43a13f6b..2f7ce8db 100644 --- a/pkg/generator/appconfiguration/generators/workload_service_generator.go +++ b/pkg/generator/appconfiguration/generator/workload/service_generator.go @@ -1,4 +1,4 @@ -package generators +package workload import ( "fmt" @@ -7,6 +7,7 @@ import ( v1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "kusionstack.io/kusion/pkg/generator/appconfiguration" "kusionstack.io/kusion/pkg/models" "kusionstack.io/kusion/pkg/models/appconfiguration/workload" ) @@ -25,7 +26,7 @@ func NewWorkloadServiceGenerator( projectName string, appName string, service *workload.Service, -) (Generator, error) { +) (appconfiguration.Generator, error) { if len(projectName) == 0 { return nil, fmt.Errorf("project name must not be empty") } @@ -51,8 +52,8 @@ func NewWorkloadServiceGeneratorFunc( projectName string, appName string, service *workload.Service, -) NewGeneratorFunc { - return func() (Generator, error) { +) appconfiguration.NewGeneratorFunc { + return func() (appconfiguration.Generator, error) { return NewWorkloadServiceGenerator(projectName, appName, service) } } @@ -84,28 +85,28 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error { Kind: "Deployment", }, ObjectMeta: metav1.ObjectMeta{ - Labels: mergeMaps( - uniqueAppLabels(g.projectName, g.appName), + Labels: appconfiguration.MergeMaps( + appconfiguration.UniqueAppLabels(g.projectName, g.appName), g.service.Labels, ), - Annotations: mergeMaps( + Annotations: appconfiguration.MergeMaps( g.service.Annotations, ), - Name: uniqueAppName(g.projectName, g.appName), + Name: appconfiguration.UniqueAppName(g.projectName, g.appName), Namespace: g.projectName, }, Spec: appsv1.DeploymentSpec{ - Replicas: IntPtr(int32(lrs.Replicas)), + Replicas: appconfiguration.IntPtr(int32(lrs.Replicas)), Selector: &metav1.LabelSelector{ - MatchLabels: uniqueAppLabels(g.projectName, g.appName), + MatchLabels: appconfiguration.UniqueAppLabels(g.projectName, g.appName), }, Template: v1.PodTemplateSpec{ ObjectMeta: metav1.ObjectMeta{ - Labels: mergeMaps( - uniqueAppLabels(g.projectName, g.appName), + Labels: appconfiguration.MergeMaps( + appconfiguration.UniqueAppLabels(g.projectName, g.appName), g.service.Labels, ), - Annotations: mergeMaps( + Annotations: appconfiguration.MergeMaps( g.service.Annotations, ), }, @@ -117,8 +118,8 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error { } // Add the Deployment resource to the spec. - return appendToSpec( - kubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), + return appconfiguration.AppendToSpec( + appconfiguration.KubernetesResourceID(resource.TypeMeta, resource.ObjectMeta), resource, spec, ) diff --git a/pkg/generator/appconfiguration/generator/workload/workload_generator.go b/pkg/generator/appconfiguration/generator/workload/workload_generator.go new file mode 100644 index 00000000..58224be0 --- /dev/null +++ b/pkg/generator/appconfiguration/generator/workload/workload_generator.go @@ -0,0 +1,90 @@ +package workload + +import ( + "fmt" + + corev1 "k8s.io/api/core/v1" + + "kusionstack.io/kusion/pkg/generator/appconfiguration" + "kusionstack.io/kusion/pkg/models" + "kusionstack.io/kusion/pkg/models/appconfiguration/workload" + "kusionstack.io/kusion/pkg/models/appconfiguration/workload/container" +) + +type workloadGenerator struct { + projectName string + appName string + workload *workload.Workload +} + +func NewWorkloadGenerator(projectName, appName string, workload *workload.Workload) (appconfiguration.Generator, error) { + if len(projectName) == 0 { + return nil, fmt.Errorf("project name must not be empty") + } + + return &workloadGenerator{ + projectName: projectName, + appName: appName, + workload: workload, + }, nil +} + +func NewWorkloadGeneratorFunc(projectName, appName string, workload *workload.Workload) appconfiguration.NewGeneratorFunc { + return func() (appconfiguration.Generator, error) { + return NewWorkloadGenerator(projectName, appName, workload) + } +} + +func (g *workloadGenerator) Generate(spec *models.Spec) error { + if spec.Resources == nil { + spec.Resources = make(models.Resources, 0) + } + + if g.workload != nil { + gfs := []appconfiguration.NewGeneratorFunc{} + + switch g.workload.Type { + case workload.WorkloadTypeService: + gfs = append(gfs, NewWorkloadServiceGeneratorFunc(g.projectName, g.appName, g.workload.Service)) + case workload.WorkloadTypeJob: + gfs = append(gfs, NewJobGeneratorFunc(g.projectName, g.appName, g.workload.Job)) + } + + if err := appconfiguration.CallGenerators(spec, gfs...); err != nil { + return err + } + } + + return nil +} + +func toOrderedContainers(appContainers map[string]container.Container) ([]corev1.Container, error) { + // Create a slice of containers based on the app's + // containers. + containers := []corev1.Container{} + if err := appconfiguration.ForeachOrdered(appContainers, func(containerName string, c container.Container) error { + // Create a slice of env vars based on the container's + // envvars. + envs := []corev1.EnvVar{} + for k, v := range c.Env { + envs = append(envs, corev1.EnvVar{ + Name: k, + Value: v, + }) + } + + // Create a container object and append it to the containers slice. + containers = append(containers, corev1.Container{ + Name: containerName, + Image: c.Image, + Command: c.Command, + Args: c.Args, + WorkingDir: c.WorkingDir, + Env: envs, + }) + return nil + }); err != nil { + return nil, err + } + return containers, nil +} diff --git a/pkg/generator/appconfiguration/generators/app_configuration_generator.go b/pkg/generator/appconfiguration/generators/app_configuration_generator.go deleted file mode 100644 index 1be5439e..00000000 --- a/pkg/generator/appconfiguration/generators/app_configuration_generator.go +++ /dev/null @@ -1,57 +0,0 @@ -package generators - -import ( - "fmt" - - "kusionstack.io/kusion/pkg/models" - "kusionstack.io/kusion/pkg/models/appconfiguration" -) - -type appConfigurationGenerator struct { - projectName string - appName string - app *appconfiguration.AppConfiguration -} - -func NewAppConfigurationGenerator(projectName, appName string, app *appconfiguration.AppConfiguration) (Generator, error) { - if len(projectName) == 0 { - return nil, fmt.Errorf("project name must not be empty") - } - - if len(appName) == 0 { - return nil, fmt.Errorf("app name must not be empty") - } - - if app == nil { - return nil, fmt.Errorf("can not find app configuration when generating the Spec") - } - - return &appConfigurationGenerator{ - projectName: projectName, - appName: appName, - app: app, - }, nil -} - -func NewAppConfigurationGeneratorFunc(projectName, appName string, app *appconfiguration.AppConfiguration) NewGeneratorFunc { - return func() (Generator, error) { - return NewAppConfigurationGenerator(projectName, appName, app) - } -} - -func (g *appConfigurationGenerator) Generate(spec *models.Spec) error { - if spec.Resources == nil { - spec.Resources = make(models.Resources, 0) - } - - gfs := []NewGeneratorFunc{ - NewNamespaceGeneratorFunc(g.projectName), - NewWorkloadGeneratorFunc(g.projectName, g.appName, g.app.Workload), - } - - if err := CallGenerators(spec, gfs...); err != nil { - return err - } - - return nil -} diff --git a/pkg/generator/appconfiguration/generators/workload_generator.go b/pkg/generator/appconfiguration/generators/workload_generator.go deleted file mode 100644 index b8f88b4e..00000000 --- a/pkg/generator/appconfiguration/generators/workload_generator.go +++ /dev/null @@ -1,55 +0,0 @@ -package generators - -import ( - "fmt" - - "kusionstack.io/kusion/pkg/models" - "kusionstack.io/kusion/pkg/models/appconfiguration/workload" -) - -type workloadGenerator struct { - projectName string - appName string - workload *workload.Workload -} - -func NewWorkloadGenerator(projectName, appName string, workload *workload.Workload) (Generator, error) { - if len(projectName) == 0 { - return nil, fmt.Errorf("project name must not be empty") - } - - return &workloadGenerator{ - projectName: projectName, - appName: appName, - workload: workload, - }, nil -} - -func NewWorkloadGeneratorFunc(projectName, appName string, workload *workload.Workload) NewGeneratorFunc { - return func() (Generator, error) { - return NewWorkloadGenerator(projectName, appName, workload) - } -} - -func (g *workloadGenerator) Generate(spec *models.Spec) error { - if spec.Resources == nil { - spec.Resources = make(models.Resources, 0) - } - - if g.workload != nil { - gfs := []NewGeneratorFunc{} - - switch g.workload.Type { - case workload.WorkloadTypeService: - gfs = append(gfs, NewWorkloadServiceGeneratorFunc(g.projectName, g.appName, g.workload.Service)) - case workload.WorkloadTypeJob: - gfs = append(gfs, NewJobGeneratorFunc(g.projectName, g.appName, g.workload.Job)) - } - - if err := CallGenerators(spec, gfs...); err != nil { - return err - } - } - - return nil -} diff --git a/pkg/generator/appconfiguration/generators/types.go b/pkg/generator/appconfiguration/types.go similarity index 86% rename from pkg/generator/appconfiguration/generators/types.go rename to pkg/generator/appconfiguration/types.go index 277a06e6..015f0e5e 100644 --- a/pkg/generator/appconfiguration/generators/types.go +++ b/pkg/generator/appconfiguration/types.go @@ -1,4 +1,4 @@ -package generators +package appconfiguration import "kusionstack.io/kusion/pkg/models" diff --git a/pkg/generator/appconfiguration/generators/util.go b/pkg/generator/appconfiguration/util.go similarity index 60% rename from pkg/generator/appconfiguration/generators/util.go rename to pkg/generator/appconfiguration/util.go index 8bd11924..e3d269e2 100644 --- a/pkg/generator/appconfiguration/generators/util.go +++ b/pkg/generator/appconfiguration/util.go @@ -1,18 +1,17 @@ -package generators +package appconfiguration import ( "sort" - corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" + "kusionstack.io/kusion/pkg/generator" "kusionstack.io/kusion/pkg/models" - "kusionstack.io/kusion/pkg/models/appconfiguration/workload/container" ) // CallGeneratorFuncs calls each NewGeneratorFunc in the given slice -// and returns a slice of Generator instances. +// and returns a slice of AppsGenerator instances. func CallGeneratorFuncs(newGenerators ...NewGeneratorFunc) ([]Generator, error) { gs := make([]Generator, 0, len(newGenerators)) for _, newGenerator := range newGenerators { @@ -25,7 +24,7 @@ func CallGeneratorFuncs(newGenerators ...NewGeneratorFunc) ([]Generator, error) return gs, nil } -// CallGenerators calls the Generate method of each Generator instance +// CallGenerators calls the Generate method of each AppsGenerator instance // returned by the given NewGeneratorFuncs. func CallGenerators(spec *models.Spec, newGenerators ...NewGeneratorFunc) error { gs, err := CallGeneratorFuncs(newGenerators...) @@ -68,12 +67,12 @@ func IntPtr[T any](i T) *T { return &i } -// mergeMaps merges multiple map[string]string into one +// MergeMaps merges multiple map[string]string into one // map[string]string. // If a map is nil, it skips it and moves on to the next one. For each // non-nil map, it iterates over its key-value pairs and adds them to // the merged map. Finally, it returns the merged map. -func mergeMaps(maps ...map[string]string) map[string]string { +func MergeMaps(maps ...map[string]string) map[string]string { merged := make(map[string]string) for _, m := range maps { @@ -88,9 +87,9 @@ func mergeMaps(maps ...map[string]string) map[string]string { return merged } -// kubernetesResourceID returns the unique ID of a Kubernetes resource +// KubernetesResourceID returns the unique ID of a Kubernetes resource // based on its type and metadata. -func kubernetesResourceID(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) string { +func KubernetesResourceID(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta) string { // resource id example: apps/v1:Deployment:code-city:code-citydev id := typeMeta.APIVersion + ":" + typeMeta.Kind + ":" if objectMeta.Namespace != "" { @@ -100,9 +99,9 @@ func kubernetesResourceID(typeMeta metav1.TypeMeta, objectMeta metav1.ObjectMeta return id } -// appendToSpec adds a Kubernetes resource to a spec's resources +// AppendToSpec adds a Kubernetes resource to a spec's resources // slice. -func appendToSpec(resourceID string, resource any, spec *models.Spec) error { +func AppendToSpec(resourceID string, resource any, spec *models.Spec) error { unstructured, err := runtime.DefaultUnstructuredConverter.ToUnstructured(resource) if err != nil { return err @@ -118,49 +117,17 @@ func appendToSpec(resourceID string, resource any, spec *models.Spec) error { return nil } -// uniqueAppName returns a unique name for a workload based on +// UniqueAppName returns a unique name for a workload based on // its project and app name. -func uniqueAppName(projectName, appName string) string { +func UniqueAppName(projectName, appName string) string { return projectName + appName } -// uniqueAppLabels returns a map of labels that identify a +// UniqueAppLabels returns a map of labels that identify a // app based on its project and name. -func uniqueAppLabels(projectName, appName string) map[string]string { +func UniqueAppLabels(projectName, appName string) map[string]string { return map[string]string{ "app.kubernetes.io/part-of": projectName, "app.kubernetes.io/name": appName, } } - -func toOrderedContainers(appContainers map[string]container.Container) ([]corev1.Container, error) { - // Create a slice of containers based on the app's - // containers. - containers := []corev1.Container{} - if err := ForeachOrdered(appContainers, func(containerName string, c container.Container) error { - // Create a slice of env vars based on the container's - // envvars. - envs := []corev1.EnvVar{} - for k, v := range c.Env { - envs = append(envs, corev1.EnvVar{ - Name: k, - Value: v, - }) - } - - // Create a container object and append it to the containers - // slice. - containers = append(containers, corev1.Container{ - Name: containerName, - Image: c.Image, - Command: c.Command, - Args: c.Args, - WorkingDir: c.WorkingDir, - Env: envs, - }) - return nil - }); err != nil { - return nil, err - } - return containers, nil -}