Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: module lint test #917

Merged
merged 9 commits into from
Mar 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion docs/design/kusion_module/kusion_modules.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ service Module {
type Intent.Resource struct {
...
// Add a new field to represent patchers
Patchers []Patcher
Patcher Patcher
}

// Kusion will patch these fields into the workload corresponding fields
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require (
github.com/aws/aws-sdk-go-v2/config v1.25.8
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.24.2
github.com/blang/semver/v4 v4.0.0
github.com/bytedance/mockey v1.2.4
github.com/bytedance/mockey v1.2.10
github.com/chai2010/gettext-go v1.0.2
github.com/davecgh/go-spew v1.1.1
github.com/didi/gendry v1.7.0
Expand Down
6 changes: 2 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,8 @@ github.com/bugsnag/bugsnag-go v0.0.0-20141110184014-b1d153021fcd h1:rFt+Y/IK1aEZ
github.com/bugsnag/osext v0.0.0-20130617224835-0dd3f918b21b h1:otBG+dV+YK+Soembjv71DPz3uX/V/6MMlSyD9JBQ6kQ=
github.com/bugsnag/panicwrap v0.0.0-20151223152923-e2c28503fcd0 h1:nvj0OLI3YqYXer/kZD8Ri1aaunCxIEsOst1BVJswV0o=
github.com/bwesterb/go-ristretto v1.2.3/go.mod h1:fUIoIZaG73pV5biE2Blr2xEzDoMj7NFEuV9ekS419A0=
github.com/bytedance/mockey v1.2.4 h1:gvqdl3AqEdY3PFnne09Pn3uFGBE4qEUJH3emWOQJoi4=
github.com/bytedance/mockey v1.2.4/go.mod h1:+Jm/fzWZAuhEDrPXVjDf/jLM2BlLXJkwk94zf2JZ3X4=
github.com/bytedance/mockey v1.2.10 h1:4JlMpkm7HMXmTUtItid+iCu2tm61wvq+ca1X2u7ymzE=
github.com/bytedance/mockey v1.2.10/go.mod h1:bNrUnI1u7+pAc0TYDgPATM+wF2yzHxmNH+iDXg4AOCU=
github.com/cenkalti/backoff/v3 v3.0.0 h1:ske+9nBpD9qZsTBoF41nW5L+AIuFBKMeze18XQ3eG1c=
github.com/cenkalti/backoff/v3 v3.0.0/go.mod h1:cIeZDE3IrqwwJl6VUwCN6trj1oXrTS4rc0ij+ULvLYs=
github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU=
Expand Down Expand Up @@ -737,7 +737,6 @@ go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.24.0 h1:FiJd5l1UOLj0wCgbSE0rwwXHzEdAZS6hiiSnxJN/D60=
go.uber.org/zap v1.24.0/go.mod h1:2kMP+WWQ8aoFoedH3T2sq6iJ2yDWpHbP0f6MQbS9Gkg=
golang.org/x/arch v0.0.0-20201008161808-52c3e6f60cff/go.mod h1:flIaEI6LNU6xOCD5PaJvn9wGP0agmIOqjrtsKGRguv4=
golang.org/x/arch v0.1.0 h1:oMxhUYsO9VsR1dcoVUjJjIGhx1LXol3989T/yZ59Xsw=
golang.org/x/arch v0.1.0/go.mod h1:5om86z9Hs0C8fWVUuoMHwpExlXzs5Tkyp9hOrfG7pp8=
golang.org/x/crypto v0.0.0-20180904163835-0709b304e793/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
Expand Down Expand Up @@ -1010,7 +1009,6 @@ oras.land/oras-go v1.2.3 h1:v8PJl+gEAntI1pJ/LCrDgsuk+1PKVavVEPsYIHFE5uY=
oras.land/oras-go v1.2.3/go.mod h1:M/uaPdYklze0Vf3AakfarnpoEckvw0ESbRdN8Z1vdJg=
oras.land/oras-go/v2 v2.3.0 h1:lqX1aXdN+DAmDTKjiDyvq85cIaI4RkIKp/PghWlAGIU=
oras.land/oras-go/v2 v2.3.0/go.mod h1:GeAwLuC4G/JpNwkd+bSZ6SkDMGaaYglt6YK2WvZP7uQ=
rsc.io/pdf v0.1.1/go.mod h1:n8OzWcQ6Sp37PL01nO98y4iUCRdTGarVfzxY20ICaU4=
sigs.k8s.io/controller-runtime v0.15.1 h1:9UvgKD4ZJGcj24vefUFgZFP3xej/3igL9BsOUTb/+4c=
sigs.k8s.io/controller-runtime v0.15.1/go.mod h1:7ngYvp1MLT+9GeZ+6lH3LOlcHkp/+tzA/fmHa4iq9kk=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
Expand Down
19 changes: 13 additions & 6 deletions pkg/apis/core/v1/resource.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package v1

import "encoding/json"
import (
"encoding/json"

v1 "k8s.io/api/core/v1"
)

type Resources []Resource

Expand All @@ -20,17 +24,20 @@ type Resource struct {
// DependsOn contains all resources this resource depends on
DependsOn []string `json:"dependsOn,omitempty" yaml:"dependsOn,omitempty"`

// Patchers contain fields should be patched into the workload corresponding fields
Patchers []Patcher
// Patcher contains fields should be patched into the workload corresponding fields
Patcher *Patcher `json:"patcher,omitempty" yaml:"patcher,omitempty"`

// Extensions specifies arbitrary metadata of this resource
Extensions map[string]interface{} `json:"extensions,omitempty" yaml:"extensions,omitempty"`
}

type Patcher struct {
Environments map[string]string `json:"environments" yaml:"environments"`
Labels map[string]string `json:"labels" yaml:"labels"`
Annotations map[string]string `json:"annotations" yaml:"annotations"`
// Environments represent the environment variables patched to all containers in the workload.
Environments []v1.EnvVar `json:"environments" yaml:"environments"`
// Labels represent the labels patched to both the workload and pod.
Labels map[string]string `json:"labels" yaml:"labels"`
// Annotations represent the annotations patched to both the workload and pod.
Annotations map[string]string `json:"annotations" yaml:"annotations"`
}

func (r *Resource) ResourceKey() string {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/build/builders/appconfig_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ func (acg *AppsConfigBuilder) Build(

var gfs []modules.NewGeneratorFunc
err := modules.ForeachOrdered(acg.Apps, func(appName string, app v1.AppConfiguration) error {
gfs = append(gfs, generators.NewAppConfigurationGeneratorFunc(project, stack, appName, &app, acg.Workspace))
gfs = append(gfs, generators.NewAppConfigurationGeneratorFunc(project.Name, stack.Name, appName, &app, acg.Workspace))
return nil
})
if err != nil {
Expand Down
22 changes: 0 additions & 22 deletions pkg/cmd/build/builders/appconfig_builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,28 +49,6 @@ func buildMockApp() (string, *v1.AppConfiguration) {
func buildMockWorkspace() *v1.Workspace {
return &v1.Workspace{
Name: "test",
Modules: v1.ModuleConfigs{
"database": {
Default: v1.GenericConfig{
"type": "aws",
"version": "5.7",
"instanceType": "db.t3.micro",
},
ModulePatcherConfigs: v1.ModulePatcherConfigs{
"smallClass": {
GenericConfig: v1.GenericConfig{
"instanceType": "db.t3.small",
},
ProjectSelector: []string{"foo", "bar"},
},
},
},
"port": {
Default: v1.GenericConfig{
"type": "aws",
},
},
},
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{
KubeConfig: "/etc/kubeconfig.yaml",
Expand Down
1 change: 1 addition & 0 deletions pkg/cmd/build/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ func buildAppConfigs(o *builders.Options, stack *v1.Stack) (map[string]v1.AppCon
// environment variables, thus we unmarshal appConfigs with yaml.v2 rather than yaml.v3.
err = yaml.Unmarshal([]byte(out), appConfigs)
if err != nil {
// todo wrap error to human readable messages
return nil, err
}

Expand Down
36 changes: 14 additions & 22 deletions pkg/cmd/build/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,28 +119,6 @@ resources:

ws = &v1.Workspace{
Name: "default",
Modules: v1.ModuleConfigs{
"database": {
Default: v1.GenericConfig{
"type": "aws",
"version": "5.7",
"instanceType": "db.t3.micro",
},
ModulePatcherConfigs: v1.ModulePatcherConfigs{
"smallClass": {
GenericConfig: v1.GenericConfig{
"instanceType": "db.t3.small",
},
ProjectSelector: []string{"foo", "bar"},
},
},
},
"port": {
Default: v1.GenericConfig{
"type": "aws",
},
},
},
Runtimes: &v1.RuntimeConfigs{
Kubernetes: &v1.KubernetesConfig{
KubeConfig: "/etc/kubeconfig.yaml",
Expand Down Expand Up @@ -230,6 +208,14 @@ func TestBuildIntent(t *testing.T) {
mockers: []*mockey.MockBuilder{
mockey.Mock(kcl.Run).Return(&kcl.CompileResult{Documents: []kclgo.KCLResult{apcMap}}, nil),
mockey.Mock(workspace.GetWorkspaceByDefaultOperator).Return(ws, nil),
mockey.Mock((*builders.AppsConfigBuilder).Build).To(func(
o *builders.Options, project *v1.Project,
stack *v1.Stack,
) (*v1.Intent,
error,
) {
return intentModel3, nil
}),
},
},
want: intentModel3,
Expand Down Expand Up @@ -274,6 +260,12 @@ func TestBuildIntent(t *testing.T) {
mockers: []*mockey.MockBuilder{
mockey.Mock(kcl.Run).Return(&kcl.CompileResult{Documents: []kclgo.KCLResult{apcMap}}, nil),
mockey.Mock(workspace.GetWorkspaceByDefaultOperator).Return(ws, nil),
mockey.Mock((*builders.AppsConfigBuilder).Build).To(func(
o *builders.Options, project *v1.Project,
stack *v1.Stack,
) (*v1.Intent, error) {
return intentModel3, nil
}),
},
},
want: intentModel3,
Expand Down
Loading
Loading