Skip to content

Commit

Permalink
feat: add AppConfiguration, Deploy, and Job models (#451)
Browse files Browse the repository at this point in the history
  • Loading branch information
SparkYuan authored Aug 9, 2023
1 parent 464df41 commit 4d2c49c
Show file tree
Hide file tree
Showing 24 changed files with 959 additions and 30 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/constraint.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Reference from:
# https://github.com/c-bata/go-prompt/blob/master/.github/workflows/test.yml
name: Constraints
on:
pull_request:
types: [opened, edited, synchronize, reopened]
jobs:
# Lints Pull Request commits with commitlint.
#
# Rules can be referenced:
# https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional
CommitLint:
name: Commit Lint
runs-on: ubuntu-latest
if: contains(fromJSON('["pull_request"]'), github.event_name)
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: wagoid/commitlint-github-action@v5

# Lints Pull Request title, because the title will be used as the
# commit message in branch main.
#
# Configuration detail can be referenced:
# https://github.com/marketplace/actions/pull-request-title-rules
PullRequestTitleLint:
name: Pull Request Title Lint
runs-on: ubuntu-latest
if: contains(fromJSON('["pull_request"]'), github.event_name)
steps:
- uses: deepakputhraya/action-pr-title@master
with:
allowed_prefixes: 'build,chore,ci,docs,feat,fix,perf,refactor,revert,style,test' # title should start with the given prefix
disallowed_prefixes: 'WIP,[WIP]' # title should not start with the given prefix
prefix_case_sensitive: false # title prefix are case insensitive
min_length: 5 # Min length of the title
max_length: 80 # Max length of the title
github_token: ${{ github.token }} # Default: ${{ github.token }}

11 changes: 6 additions & 5 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go 1.19
Expand All @@ -33,12 +33,13 @@ jobs:
uses: shogo82148/actions-goveralls@v1
with:
path-to-profile: coverage.out
Lint:
name: Lint checks

GolangLint:
name: Golang Lint
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Set up Go 1.19
Expand All @@ -49,4 +50,4 @@ jobs:
uses: golangci/golangci-lint-action@v3
with:
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
version: v1.52.2
version: v1.52.2
4 changes: 2 additions & 2 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ linters:
linters-settings:
gofumpt:
# Select the Go version to target. The default is `1.15`.
lang-version: "1.16"
lang-version: "1.19"
# Choose whether or not to use the extra rules that are disabled
# by default
extra-rules: false
Expand All @@ -60,4 +60,4 @@ issues:
- "ifElseChain: rewrite if-else to switch statement"
- "S1000: should use for range instead of for { select {} }"
- "SA4004: the surrounding loop is unconditionally terminated"
- "copylocks: call of c\\.Post copies lock value: kcl-lang\\.io/kcl-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex"
- "copylocks: call of c\\.Post copies lock value: kcl-lang\\.io/kcl-go/pkg/spec/gpyrpc\\.Ping_Args contains google\\.golang\\.org/protobuf/internal/impl\\.MessageState contains sync\\.Mutex"
21 changes: 21 additions & 0 deletions commitlint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Copyright The Karbour Authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

// This file is the configuration file of [commitlint](https://commitlint.js.org/#/).
//
// Rules can be referenced:
// https://github.com/conventional-changelog/commitlint/tree/master/%40commitlint/config-conventional
module.exports = {extends: ['@commitlint/config-conventional']}
3 changes: 1 addition & 2 deletions pkg/cmd/preview/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,8 @@ import (
"path/filepath"
"testing"

"github.com/stretchr/testify/assert"

"github.com/bytedance/mockey"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

compilecmd "kusionstack.io/kusion/pkg/cmd/compile"
Expand Down
38 changes: 36 additions & 2 deletions pkg/cmd/spec/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import (

"github.com/acarl005/stripansi"
"github.com/pterm/pterm"

"gopkg.in/yaml.v3"

"kusionstack.io/kusion/pkg/generator"
"kusionstack.io/kusion/pkg/generator/appconfiguration"
"kusionstack.io/kusion/pkg/generator/kcl"
"kusionstack.io/kusion/pkg/log"
"kusionstack.io/kusion/pkg/models"
appconfigmodel "kusionstack.io/kusion/pkg/models/appconfiguration"
"kusionstack.io/kusion/pkg/projectstack"
"kusionstack.io/kusion/pkg/util/pretty"
)
Expand Down Expand Up @@ -66,18 +68,50 @@ func GenerateSpec(o *generator.Options, project *projectstack.Project, stack *pr
switch gt {
case projectstack.KCLGenerator:
g = &kcl.Generator{}
case projectstack.AppConfigurationGenerator:
appConfig, err := buildAppConfig(o, stack)
if err != nil {
return nil, err
}
g = &appconfiguration.Generator{AppConfiguration: appConfig}
default:
return nil, fmt.Errorf("unknow generator type:%s", gt)
}
}

spec, err := g.GenerateSpec(o, stack)
spec, err := g.GenerateSpec(o, project, stack)
if err != nil {
return nil, errors.New(stripansi.Strip(err.Error()))
}
return spec, nil
}

func buildAppConfig(o *generator.Options, stack *projectstack.Stack) (*appconfigmodel.AppConfiguration, error) {
compileResult, err := kcl.Run(o, stack)
if err != nil {
return nil, err
}

documents := compileResult.Documents
if len(documents) != 1 {
return nil, fmt.Errorf("invalide more than one AppConfiguration are found in the compile result")
}

out, err := yaml.Marshal(documents[0])
if err != nil {
return nil, err
}

log.Debugf("unmarshal %s to app config", out)
appConfig := &appconfigmodel.AppConfiguration{}
err = yaml.Unmarshal(out, appConfig)
if err != nil {
return nil, err
}

return appConfig, nil
}

func GenerateSpecFromFile(filePath string) (*models.Spec, error) {
b, err := os.ReadFile(filePath)
if err != nil {
Expand Down
33 changes: 33 additions & 0 deletions pkg/generator/appconfiguration/app_configuration_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package appconfiguration

import (
"kusionstack.io/kusion/pkg/generator"
"kusionstack.io/kusion/pkg/generator/appconfiguration/generators"
"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration"
"kusionstack.io/kusion/pkg/projectstack"
)

type Generator struct {
*appconfiguration.AppConfiguration
}

func (acg *Generator) GenerateSpec(
o *generator.Options,
project *projectstack.Project,
stack *projectstack.Stack,
) (*models.Spec, error) {
spec := &models.Spec{
Resources: []models.Resource{},
}

g, err := generators.NewAppConfigurationGenerator(project.Name, acg.AppConfiguration)
if err != nil {
return nil, err
}
if err = g.Generate(spec); err != nil {
return nil, err
}

return spec, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package generators

import (
"fmt"

"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration"
)

type appConfigurationGenerator struct {
projectName string
ac *appconfiguration.AppConfiguration
}

func NewAppConfigurationGenerator(projectName string, ac *appconfiguration.AppConfiguration) (Generator, error) {
if len(projectName) == 0 {
return nil, fmt.Errorf("project name must not be empty")
}

if ac == nil {
return nil, fmt.Errorf("can not find app configuration when generating the Spec")
}

return &appConfigurationGenerator{
projectName: projectName,
ac: ac,
}, nil
}

func (g *appConfigurationGenerator) Generate(spec *models.Spec) error {
if spec.Resources == nil {
spec.Resources = make(models.Resources, 0)
}

gfs := []NewGeneratorFunc{
NewNamespaceGeneratorFunc(g.projectName),
NewComponentsGeneratorFunc(g.projectName, g.ac.Components),
}

if err := callGenerators(spec, gfs...); err != nil {
return err
}

return nil
}
55 changes: 55 additions & 0 deletions pkg/generator/appconfiguration/generators/components_generator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package generators

import (
"fmt"

"kusionstack.io/kusion/pkg/models"
"kusionstack.io/kusion/pkg/models/appconfiguration/component"
)

type componentsGenerator struct {
projectName string
components map[string]component.Component
}

func NewComponentsGenerator(projectName string, components map[string]component.Component) (Generator, error) {
if len(projectName) == 0 {
return nil, fmt.Errorf("project name must not be empty")
}

return &componentsGenerator{
projectName: projectName,
components: components,
}, nil
}

func NewComponentsGeneratorFunc(projectName string, components map[string]component.Component) NewGeneratorFunc {
return func() (Generator, error) {
return NewComponentsGenerator(projectName, components)
}
}

func (g *componentsGenerator) Generate(spec *models.Spec) error {
if spec.Resources == nil {
spec.Resources = make(models.Resources, 0)
}

if g.components != nil {
if err := foreachOrderedComponents(g.components, func(compName string, comp component.Component) error {
gfs := []NewGeneratorFunc{
NewDeploymentGeneratorFunc(g.projectName, compName, &comp),
NewJobGeneratorFunc(g.projectName, compName, &comp),
}

if err := callGenerators(spec, gfs...); err != nil {
return err
}

return nil
}); err != nil {
return err
}
}

return nil
}
Loading

0 comments on commit 4d2c49c

Please sign in to comment.