Skip to content

Commit

Permalink
refactor: workload, port and secret according to the new appConfig model
Browse files Browse the repository at this point in the history
the way of using namespace isn't changed and marked it as a todo
  • Loading branch information
SparkYuan committed Feb 26, 2024
1 parent d6e4ce4 commit 405ecc1
Show file tree
Hide file tree
Showing 84 changed files with 512 additions and 11,419 deletions.
79 changes: 40 additions & 39 deletions pkg/apis/core/v1/appconfiguration/appconfiguration.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package appconfiguration

import (
"kusionstack.io/kusion/pkg/modules/inputs/workload"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload"
)

type Accessory map[string]interface{}

// AppConfiguration is a developer-centric definition that describes how to run an Application. The application model is built on a decade
// AppConfiguration is a developer-centric definition that describes how to run an App. The application model is built on a decade
// of experience from AntGroup in operating a large-scale internal developer platform and combines the best ideas and practices from the
// community.
//
Expand All @@ -20,59 +20,60 @@ type Accessory map[string]interface{}
// import models.schema.v1.monitoring as m
// import models.schema.v1.database as d
//
// helloWorld: ac.AppConfiguration {
// # Built-in module
// workload: wl.Service {
// containers: {
// "main": c.Container {
// image: "ghcr.io/kusion-stack/samples/helloworld:latest"
// # Configure a HTTP readiness probe
// readinessProbe: p.Probe {
// probeHandler: p.Http {
// url: "http://localhost:80"
// }
// }
// }
// }
// }
// helloWorld: ac.AppConfiguration {
// # Built-in module
// workload: wl.Service {
// containers: {
// "main": c.Container {
// image: "ghcr.io/kusion-stack/samples/helloworld:latest"
// # Configure a HTTP readiness probe
// readinessProbe: p.Probe {
// probeHandler: p.Http {
// url: "http://localhost:80"
// }
// }
// }
// }
// }
//
// # extend accessories module base
// accessories: {
// # Built-in module
// "mysql" : d.MySQL {
// # extend accessories module base
// accessories: {
// # Built-in module, key represents the module source
// "kusionstack/mysql@v0.1" : d.MySQL {
// type: "cloud"
// version: "8.0"
// }
// # Built-in module
// "pro" : m.Prometheus {
// # Built-in module, key represents the module source
// "kusionstack/prometheus@v0.1" : m.Prometheus {
// path: "/metrics"
// }
// # Customized module
// "customize": customizedModule {
// # Customized module, key represents the module source
// "foo/customize": customizedModule {
// ...
// }
// }
//
// # extend pipeline module base
// pipeline: {
// # Step is a module
// "step" : Step {
// use: "exec"
// args: ["--test-all"]
// }
// }
// # extend pipeline module base
// pipeline: {
// # Step is a module
// "step" : Step {
// use: "exec"
// args: ["--test-all"]
// }
// }
//
// # Dependent app list
// dependency: {
// dependentApps: ["init-kusion"]
// }
// }
// # Dependent app list
// dependency: {
// dependentApps: ["init-kusion"]
// }
// }
type AppConfiguration struct {
// Name of the target Application.
// Name of the target App.
Name string `json:"name,omitempty" yaml:"name,omitempty"`
// Workload defines how to run your application code.
Workload *workload.Workload `json:"workload" yaml:"workload"`
// Accessories defines a collection of accessories that will be attached to the workload.
// The key in this map represents the module source. e.g. kusionstack/mysql@v0.1
Accessories map[string]*Accessory `json:"accessories,omitempty" yaml:"accessories,omitempty"`
// Labels and Annotations can be used to attach arbitrary metadata as key-value pairs to resources.
Labels map[string]string `json:"labels,omitempty" yaml:"labels,omitempty"`
Expand Down
11 changes: 8 additions & 3 deletions pkg/apis/core/v1/appconfiguration/workload/common.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package workload

Check failure on line 1 in pkg/apis/core/v1/appconfiguration/workload/common.go

View workflow job for this annotation

GitHub Actions / Golang Lint

: # kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload [kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload.test]

import "kusionstack.io/kusion/pkg/modules/inputs/workload/container"
import "kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload/container"

const (
FieldLabels = "labels"
FieldAnnotations = "annotations"
)

// Base defines set of attributes shared by different workload profile, e.g. Service and Job. You can inherit this Schema to reuse these
// common attributes.
type Base struct {
// The templates of containers to be run.
Containers map[string]container.Container `yaml:"containers,omitempty" json:"containers,omitempty"`
// The number of containers that should be run. Default is 2 to meet high availability requirements.
Replicas int `yaml:"replicas,omitempty" json:"replicas,omitempty"`
// The number of containers that should be run.
Replicas *int32 `yaml:"replicas,omitempty" json:"replicas,omitempty"`
// Secret
Secrets map[string]Secret `json:"secrets,omitempty" yaml:"secrets,omitempty"`
// Dirs configures one or more volumes to be mounted to the specified folder.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"gopkg.in/yaml.v2"
)

// Container describes how the Application's tasks are expected to be run.
// Container describes how the App's tasks are expected to be run.
type Container struct {
// Image to run for this container
Image string `yaml:"image" json:"image"`
Expand Down
8 changes: 5 additions & 3 deletions pkg/apis/core/v1/appconfiguration/workload/service.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package workload

import (
"kusionstack.io/kusion/pkg/modules/inputs/workload/network"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload/network"
)

type ServiceType string

const (
Deployment ServiceType = "Deployment"
Collaset ServiceType = "CollaSet"
ModuleService = "service"
ModuleServiceType = "type"
Deployment ServiceType = "Deployment"
Collaset ServiceType = "CollaSet"
)

// Service is a kind of workload profile that describes how to run your application code.
Expand Down
5 changes: 3 additions & 2 deletions pkg/apis/core/v1/appconfiguration/workload/workload.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ import (
type Type string

const (
TypeJob = "Job"
TypeService = "Service"
TypeJob = "Job"
TypeService = "Service"
FieldReplicas = "replicas"
)

type Header struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/core/v1/workspace.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type Workspace struct {
}

// ModuleConfigs is a set of multiple ModuleConfig, whose key is the module name.
// The module name format is "source@version".
type ModuleConfigs map[string]*ModuleConfig

// ModuleConfig is the config of a module, which contains a default and several patcher blocks.
Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/build/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (

func NewCmdBuild() *cobra.Command {
var (
short = i18n.T(`Build Kusion modules in a Stack to the Intent`)
short = i18n.T(`Build Kusion modules in a stack to the Intent`)

long = i18n.T(`
Build Kusion modules in a Stack to the Intent
Build Kusion modules in a stack to the Intent
The command must be executed in a Stack or by specifying a Stack directory with the -w flag.
The command must be executed in a stack or by specifying a stack directory with the -w flag.
You can provide a list of arguments to replace the placeholders defined in KCL,
and use the --output flag to output the built results to a file`)

Expand Down
6 changes: 3 additions & 3 deletions pkg/cmd/build/builders/appconfig_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package builders

import (
"kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration"
"kusionstack.io/kusion/pkg/modules"
"kusionstack.io/kusion/pkg/modules/generators"
"kusionstack.io/kusion/pkg/modules/inputs"
)

type AppsConfigBuilder struct {
Apps map[string]inputs.AppConfiguration
Apps map[string]appconfiguration.AppConfiguration
Workspace *v1.Workspace
}

Expand All @@ -22,7 +22,7 @@ func (acg *AppsConfigBuilder) Build(
}

var gfs []modules.NewGeneratorFunc
err := modules.ForeachOrdered(acg.Apps, func(appName string, app inputs.AppConfiguration) error {
err := modules.ForeachOrdered(acg.Apps, func(appName string, app appconfiguration.AppConfiguration) error {
gfs = append(gfs, generators.NewAppConfigurationGeneratorFunc(project, stack, appName, &app, acg.Workspace))
return nil
})
Expand Down
14 changes: 6 additions & 8 deletions pkg/cmd/build/builders/appconfig_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import (
"github.com/stretchr/testify/assert"

v1 "kusionstack.io/kusion/pkg/apis/core/v1"
appmodel "kusionstack.io/kusion/pkg/modules/inputs"
"kusionstack.io/kusion/pkg/modules/inputs/workload"
"kusionstack.io/kusion/pkg/modules/inputs/workload/network"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration/workload/network"
)

func TestAppsConfigBuilder_Build(t *testing.T) {
p, s := buildMockProjectAndStack()
appName, app := buildMockApp()
acg := &AppsConfigBuilder{
Apps: map[string]appmodel.AppConfiguration{
Apps: map[string]appconfiguration.AppConfiguration{
appName: *app,
},
Workspace: buildMockWorkspace(),
Expand All @@ -26,8 +26,8 @@ func TestAppsConfigBuilder_Build(t *testing.T) {
assert.NotNil(t, intent)
}

func buildMockApp() (string, *appmodel.AppConfiguration) {
return "app1", &appmodel.AppConfiguration{
func buildMockApp() (string, *appconfiguration.AppConfiguration) {
return "app1", &appconfiguration.AppConfiguration{
Workload: &workload.Workload{
Header: workload.Header{
Type: "Service",
Expand All @@ -37,10 +37,8 @@ func buildMockApp() (string, *appmodel.AppConfiguration) {
Type: "Deployment",
Ports: []network.Port{
{
Type: network.CSPAliCloud,
Port: 80,
Protocol: "TCP",
Public: true,
},
},
},
Expand Down
10 changes: 5 additions & 5 deletions pkg/cmd/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,21 @@ import (
yamlv3 "gopkg.in/yaml.v3"

"kusionstack.io/kusion/pkg/apis/core/v1"
"kusionstack.io/kusion/pkg/apis/core/v1/appconfiguration"
"kusionstack.io/kusion/pkg/cmd/build/builders"
"kusionstack.io/kusion/pkg/cmd/build/builders/kcl"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/modules/inputs"
"kusionstack.io/kusion/pkg/util/pretty"
"kusionstack.io/kusion/pkg/workspace"
)

func IntentWithSpinner(o *builders.Options, project *v1.Project, stack *v1.Stack) (*v1.Intent, error) {
var sp *pterm.SpinnerPrinter
if o.NoStyle {
fmt.Printf("Generating Intent in the Stack %s...\n", stack.Name)
fmt.Printf("Generating Intent in the stack %s...\n", stack.Name)
} else {
sp = &pretty.SpinnerT
sp, _ = sp.Start(fmt.Sprintf("Generating Intent in the Stack %s...", stack.Name))
sp, _ = sp.Start(fmt.Sprintf("Generating Intent in the stack %s...", stack.Name))
}

// style means color and prompt here. Currently, sp will be nil only when o.NoStyle is true
Expand Down Expand Up @@ -95,7 +95,7 @@ func Intent(o *builders.Options, p *v1.Project, s *v1.Stack) (*v1.Intent, error)
return i, nil
}

func buildAppConfigs(o *builders.Options, stack *v1.Stack) (map[string]inputs.AppConfiguration, error) {
func buildAppConfigs(o *builders.Options, stack *v1.Stack) (map[string]appconfiguration.AppConfiguration, error) {
o.Arguments[kcl.IncludeSchemaTypePath] = "true"
compileResult, err := kcl.Run(o, stack)
if err != nil {
Expand All @@ -110,7 +110,7 @@ func buildAppConfigs(o *builders.Options, stack *v1.Stack) (map[string]inputs.Ap
out := documents[0].YAMLString()

log.Debugf("unmarshal %s to app configs", out)
appConfigs := map[string]inputs.AppConfiguration{}
appConfigs := map[string]appconfiguration.AppConfiguration{}

// Note: we use the type of MapSlice in yaml.v2 to maintain the order of container
// environment variables, thus we unmarshal appConfigs with yaml.v2 rather than yaml.v3.
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/init/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ func (o *Options) Run() error {
pterm.Cyan("<ENTER>"))
pterm.Printfln("Press %s at any time to quit.", pterm.Cyan("^C"))
pterm.Println()
pterm.Bold.Println("Project Config:")
pterm.Bold.Println("project Config:")
}

// o.ProjectName is used to make root directory
Expand Down Expand Up @@ -157,7 +157,7 @@ func (o *Options) Run() error {
tc.StacksConfig = make(map[string]map[string]interface{})
for _, stack := range template.StackTemplates {
if !o.Yes {
pterm.Bold.Printfln("Stack Config: %s", pterm.Cyan(stack.Name))
pterm.Bold.Printfln("stack Config: %s", pterm.Cyan(stack.Name))
}
configs := make(map[string]interface{})
for _, f := range stack.Fields {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/operation/models/change.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ func (p *Changes) AllUnChange() bool {
func (p *Changes) Summary(writer io.Writer) {
// Create a fork of the default table, fill it with data and print it.
// Data can also be generated and inserted later.
tableHeader := []string{fmt.Sprintf("Stack: %s", p.stack.Name), "ID", "Action"}
tableHeader := []string{fmt.Sprintf("stack: %s", p.stack.Name), "ID", "Action"}
tableData := pterm.TableData{tableHeader}

for i, step := range p.Values() {
Expand Down
4 changes: 2 additions & 2 deletions pkg/engine/operation/models/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestChanges_Stack(t *testing.T) {
stack: tt.fields.stack,
}
if got := p.Stack(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Changes.Stack() = %v, want %v", got, tt.want)
t.Errorf("Changes.stack() = %v, want %v", got, tt.want)
}
})
}
Expand Down Expand Up @@ -341,7 +341,7 @@ func TestChanges_Project(t *testing.T) {
t.Run(tt.name, func(t *testing.T) {
p := NewChanges(tt.fields.project, tt.fields.stack, tt.fields.order)
if got := p.Project(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("Changes.Project() = %v, want %v", got, tt.want)
t.Errorf("Changes.project() = %v, want %v", got, tt.want)
}
})
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/printers/convertor/oam_convertor.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// APIs in core.oam.dev/v1beta1
const (
Application = "Application"
Application = "App"
)

func ToOAM(o *unstructured.Unstructured) runtime.Object {
Expand Down
2 changes: 1 addition & 1 deletion pkg/engine/states/remote/mysql/mysql_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (s *MysqlState) GetLatestState(q *states.StateQuery) (*states.State, error)
where := make(map[string]interface{})

if len(q.Project) == 0 {
msg := "no Project in query"
msg := "no project in query"
log.Errorf(msg)
return nil, fmt.Errorf(msg)
}
Expand Down
Loading

0 comments on commit 405ecc1

Please sign in to comment.