Skip to content

Commit

Permalink
refactor: reorg generators packages and simplify namings (#459)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan authored Aug 17, 2023
1 parent a22f2f0 commit 0d53f9a
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 237 deletions.
12 changes: 6 additions & 6 deletions pkg/cmd/spec/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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 {
Expand All @@ -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)
}
Expand All @@ -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
Expand All @@ -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
Expand Down
34 changes: 0 additions & 34 deletions pkg/generator/appconfiguration/app_configurations_generator.go

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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
}
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
package generators
package generator

import (
"fmt"

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"
)

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")
}
Expand All @@ -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)
}
}
Expand All @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generators
package generator

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -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"
)
Expand All @@ -15,16 +16,16 @@ 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,
job: job,
}, 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)
}
}
Expand All @@ -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,
),
}
Expand All @@ -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,
),
},
Expand All @@ -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,
)
Expand All @@ -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,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package generators
package workload

import (
"fmt"
Expand All @@ -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"
)
Expand All @@ -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")
}
Expand All @@ -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)
}
}
Expand Down Expand Up @@ -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,
),
},
Expand All @@ -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,
)
Expand Down
Loading

0 comments on commit 0d53f9a

Please sign in to comment.