Skip to content

Commit

Permalink
feat: add stack name as part of workload's name (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan authored Aug 17, 2023
1 parent 0d53f9a commit b9ead6f
Show file tree
Hide file tree
Showing 5 changed files with 112 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,22 @@ type AppsGenerator struct {
}

func (acg *AppsGenerator) GenerateSpec(
o *generator.Options,
_ *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))
var gfs []appconfiguration.NewGeneratorFunc
err := appconfiguration.ForeachOrdered(acg.Apps, func(appName string, app appmodel.AppConfiguration) error {
gfs = append(gfs, NewAppConfigurationGeneratorFunc(project, stack, appName, &app))
return nil
})
if err != nil {
return nil, err
}
if err := appconfiguration.CallGenerators(spec, gfs...); err != nil {
return nil, err
}
Expand All @@ -37,16 +40,19 @@ func (acg *AppsGenerator) GenerateSpec(
}

type appConfigurationGenerator struct {
projectName string
appName string
app *appmodel.AppConfiguration
project *projectstack.Project
stack *projectstack.Stack
appName string
app *appmodel.AppConfiguration
}

func NewAppConfigurationGenerator(
projectName, appName string,
project *projectstack.Project,
stack *projectstack.Stack,
app *appmodel.AppConfiguration,
appName string,
) (appconfiguration.Generator, error) {
if len(projectName) == 0 {
if len(project.Name) == 0 {
return nil, fmt.Errorf("project name must not be empty")
}

Expand All @@ -59,18 +65,21 @@ func NewAppConfigurationGenerator(
}

return &appConfigurationGenerator{
projectName: projectName,
appName: appName,
app: app,
project: project,
stack: stack,
appName: appName,
app: app,
}, nil
}

func NewAppConfigurationGeneratorFunc(
projectName, appName string,
project *projectstack.Project,
stack *projectstack.Stack,
appName string,
app *appmodel.AppConfiguration,
) appconfiguration.NewGeneratorFunc {
return func() (appconfiguration.Generator, error) {
return NewAppConfigurationGenerator(projectName, appName, app)
return NewAppConfigurationGenerator(project, stack, app, appName)
}
}

Expand All @@ -80,8 +89,8 @@ func (g *appConfigurationGenerator) Generate(spec *models.Spec) error {
}

gfs := []appconfiguration.NewGeneratorFunc{
NewNamespaceGeneratorFunc(g.projectName),
workload.NewWorkloadGeneratorFunc(g.projectName, g.appName, g.app.Workload),
NewNamespaceGeneratorFunc(g.project.Name),
workload.NewWorkloadGeneratorFunc(g.project, nil, g.app.Workload, g.appName),
}

if err := appconfiguration.CallGenerators(spec, gfs...); err != nil {
Expand Down
39 changes: 26 additions & 13 deletions pkg/generator/appconfiguration/generator/workload/job_generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,38 @@ import (
"kusionstack.io/kusion/pkg/generator/appconfiguration"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload"
"kusionstack.io/kusion/pkg/projectstack"
)

type jobGenerator struct {
projectName string
appName string
job *workload.Job
project *projectstack.Project
stack *projectstack.Stack
appName string
job *workload.Job
}

func NewJobGenerator(projectName, appName string, job *workload.Job) (appconfiguration.Generator, error) {
func NewJobGenerator(
project *projectstack.Project,
stack *projectstack.Stack,
appName string,
job *workload.Job,
) (appconfiguration.Generator, error) {
return &jobGenerator{
projectName: projectName,
appName: appName,
job: job,
project: project,
stack: stack,
appName: appName,
job: job,
}, nil
}

func NewJobGeneratorFunc(projectName, appName string, job *workload.Job) appconfiguration.NewGeneratorFunc {
func NewJobGeneratorFunc(
project *projectstack.Project,
stack *projectstack.Stack,
appName string,
job *workload.Job,
) appconfiguration.NewGeneratorFunc {
return func() (appconfiguration.Generator, error) {
return NewJobGenerator(projectName, appName, job)
return NewJobGenerator(project, stack, appName, job)
}
}

Expand All @@ -41,10 +54,10 @@ func (g *jobGenerator) Generate(spec *models.Spec) error {
}

meta := metav1.ObjectMeta{
Namespace: g.projectName,
Name: appconfiguration.UniqueAppName(g.projectName, g.appName),
Namespace: g.project.Name,
Name: appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName),
Labels: appconfiguration.MergeMaps(
appconfiguration.UniqueAppLabels(g.projectName, g.appName),
appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
g.job.Labels,
),
Annotations: appconfiguration.MergeMaps(
Expand All @@ -60,7 +73,7 @@ func (g *jobGenerator) Generate(spec *models.Spec) error {
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: appconfiguration.MergeMaps(
appconfiguration.UniqueAppLabels(g.projectName, g.appName),
appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
g.job.Labels,
),
Annotations: appconfiguration.MergeMaps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,27 @@ import (
"kusionstack.io/kusion/pkg/generator/appconfiguration"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload"
"kusionstack.io/kusion/pkg/projectstack"
)

// workloadServiceGenerator is a struct for generating service
// workload resources.
type workloadServiceGenerator struct {
projectName string
appName string
service *workload.Service
project *projectstack.Project
stack *projectstack.Stack
appName string
service *workload.Service
}

// NewWorkloadServiceGenerator returns a new workloadServiceGenerator
// instance.
func NewWorkloadServiceGenerator(
projectName string,
project *projectstack.Project,
stack *projectstack.Stack,
appName string,
service *workload.Service,
) (appconfiguration.Generator, error) {
if len(projectName) == 0 {
if len(project.Name) == 0 {
return nil, fmt.Errorf("project name must not be empty")
}

Expand All @@ -40,28 +43,30 @@ func NewWorkloadServiceGenerator(
}

return &workloadServiceGenerator{
projectName: projectName,
appName: appName,
service: service,
project: project,
stack: stack,
appName: appName,
service: service,
}, nil
}

// NewWorkloadServiceGeneratorFunc returns a new NewGeneratorFunc that
// returns a workloadServiceGenerator instance.
func NewWorkloadServiceGeneratorFunc(
projectName string,
project *projectstack.Project,
stack *projectstack.Stack,
appName string,
service *workload.Service,
) appconfiguration.NewGeneratorFunc {
return func() (appconfiguration.Generator, error) {
return NewWorkloadServiceGenerator(projectName, appName, service)
return NewWorkloadServiceGenerator(project, stack, appName, service)
}
}

// Generate generates a service workload resource to the given spec.
func (g *workloadServiceGenerator) Generate(spec *models.Spec) error {
lrs := g.service
if lrs == nil {
service := g.service
if service == nil {
return nil
}

Expand All @@ -72,7 +77,7 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error {

// Create a slice of containers based on the app's
// containers.
containers, err := toOrderedContainers(lrs.Containers)
containers, err := toOrderedContainers(service.Containers)
if err != nil {
return err
}
Expand All @@ -86,24 +91,24 @@ func (g *workloadServiceGenerator) Generate(spec *models.Spec) error {
},
ObjectMeta: metav1.ObjectMeta{
Labels: appconfiguration.MergeMaps(
appconfiguration.UniqueAppLabels(g.projectName, g.appName),
appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
g.service.Labels,
),
Annotations: appconfiguration.MergeMaps(
g.service.Annotations,
),
Name: appconfiguration.UniqueAppName(g.projectName, g.appName),
Namespace: g.projectName,
Name: appconfiguration.UniqueAppName(g.project.Name, g.stack.Name, g.appName),
Namespace: g.project.Name,
},
Spec: appsv1.DeploymentSpec{
Replicas: appconfiguration.IntPtr(int32(lrs.Replicas)),
Replicas: appconfiguration.GenericPtr(int32(service.Replicas)),
Selector: &metav1.LabelSelector{
MatchLabels: appconfiguration.UniqueAppLabels(g.projectName, g.appName),
MatchLabels: appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
},
Template: v1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: appconfiguration.MergeMaps(
appconfiguration.UniqueAppLabels(g.projectName, g.appName),
appconfiguration.UniqueAppLabels(g.project.Name, g.appName),
g.service.Labels,
),
Annotations: appconfiguration.MergeMaps(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,42 @@ import (
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload"
"kusionstack.io/kusion/pkg/models/appconfiguration/workload/container"
"kusionstack.io/kusion/pkg/projectstack"
)

type workloadGenerator struct {
projectName string
appName string
workload *workload.Workload
project *projectstack.Project
stack *projectstack.Stack
appName string
workload *workload.Workload
}

func NewWorkloadGenerator(projectName, appName string, workload *workload.Workload) (appconfiguration.Generator, error) {
if len(projectName) == 0 {
func NewWorkloadGenerator(
project *projectstack.Project,
stack *projectstack.Stack,
workload *workload.Workload,
appName string,
) (appconfiguration.Generator, error) {
if len(project.Name) == 0 {
return nil, fmt.Errorf("project name must not be empty")
}

return &workloadGenerator{
projectName: projectName,
appName: appName,
workload: workload,
project: project,
stack: stack,
appName: appName,
workload: workload,
}, nil
}

func NewWorkloadGeneratorFunc(projectName, appName string, workload *workload.Workload) appconfiguration.NewGeneratorFunc {
func NewWorkloadGeneratorFunc(
project *projectstack.Project,
stack *projectstack.Stack,
workload *workload.Workload,
appName string,
) appconfiguration.NewGeneratorFunc {
return func() (appconfiguration.Generator, error) {
return NewWorkloadGenerator(projectName, appName, workload)
return NewWorkloadGenerator(project, stack, workload, appName)
}
}

Expand All @@ -41,13 +54,13 @@ func (g *workloadGenerator) Generate(spec *models.Spec) error {
}

if g.workload != nil {
gfs := []appconfiguration.NewGeneratorFunc{}
var gfs []appconfiguration.NewGeneratorFunc

switch g.workload.Type {
case workload.WorkloadTypeService:
gfs = append(gfs, NewWorkloadServiceGeneratorFunc(g.projectName, g.appName, g.workload.Service))
gfs = append(gfs, NewWorkloadServiceGeneratorFunc(g.project, g.stack, g.appName, g.workload.Service))
case workload.WorkloadTypeJob:
gfs = append(gfs, NewJobGeneratorFunc(g.projectName, g.appName, g.workload.Job))
gfs = append(gfs, NewJobGeneratorFunc(g.project, g.stack, g.appName, g.workload.Job))
}

if err := appconfiguration.CallGenerators(spec, gfs...); err != nil {
Expand All @@ -61,11 +74,10 @@ func (g *workloadGenerator) Generate(spec *models.Spec) error {
func toOrderedContainers(appContainers map[string]container.Container) ([]corev1.Container, error) {
// Create a slice of containers based on the app's
// containers.
containers := []corev1.Container{}
var 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{}
// Create a slice of env vars based on the container's env vars.
var envs []corev1.EnvVar
for k, v := range c.Env {
envs = append(envs, corev1.EnvVar{
Name: k,
Expand Down
Loading

0 comments on commit b9ead6f

Please sign in to comment.