Skip to content

Commit f4d08f2

Browse files
authored
feat: support setting KCL setting-files when building an Intent (#649)
1 parent 681d5b3 commit f4d08f2

File tree

10 files changed

+88
-48
lines changed

10 files changed

+88
-48
lines changed

go.mod

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ require (
6363
k8s.io/component-base v0.27.2
6464
k8s.io/kubectl v0.27.1
6565
k8s.io/utils v0.0.0-20230726121419-3b25d923346b
66-
kcl-lang.io/kcl-go v0.6.0-alpha.1
66+
kcl-lang.io/kcl-go v0.7.0
6767
kcl-lang.io/kcl-plugin v0.5.0
68-
kcl-lang.io/kpm v0.3.6
68+
kcl-lang.io/kpm v0.4.2
6969
kusionstack.io/kube-api v0.0.0-20230817144216-4714955f3801
7070
sigs.k8s.io/controller-runtime v0.15.1
7171
)
@@ -241,7 +241,7 @@ require (
241241
gopkg.in/warnings.v0 v0.1.2 // indirect
242242
k8s.io/klog/v2 v2.100.1 // indirect
243243
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
244-
kcl-lang.io/kcl-artifact-go v0.6.0-alpha.1 // indirect
244+
kcl-lang.io/kcl-artifact-go v0.7.1 // indirect
245245
lukechampine.com/frand v1.4.2 // indirect
246246
oras.land/oras-go v1.2.3 // indirect
247247
oras.land/oras-go/v2 v2.3.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -852,14 +852,14 @@ k8s.io/kubectl v0.27.1 h1:9T5c5KdpburYiW8XKQSH0Uly1kMNE90aGSnbYUZNdcA=
852852
k8s.io/kubectl v0.27.1/go.mod h1:QsAkSmrRsKTPlAFzF8kODGDl4p35BIwQnc9XFhkcsy8=
853853
k8s.io/utils v0.0.0-20230726121419-3b25d923346b h1:sgn3ZU783SCgtaSJjpcVVlRqd6GSnlTLKgpAAttJvpI=
854854
k8s.io/utils v0.0.0-20230726121419-3b25d923346b/go.mod h1:OLgZIPagt7ERELqWJFomSt595RzquPNLL48iOWgYOg0=
855-
kcl-lang.io/kcl-artifact-go v0.6.0-alpha.1 h1:RaRbhPGTGbG8s2w0qAh6J3V+TmwfMZtryxiyxVTvLKA=
856-
kcl-lang.io/kcl-artifact-go v0.6.0-alpha.1/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
857-
kcl-lang.io/kcl-go v0.6.0-alpha.1 h1:BhMQ2GNRp9AccWxB2QydvZF6g6iVlPkkSSPa+RAdYx0=
858-
kcl-lang.io/kcl-go v0.6.0-alpha.1/go.mod h1:tHMTzp0dtah8Hn6h6UfnwbrEv737+TKf0xjIJkUkuaU=
855+
kcl-lang.io/kcl-artifact-go v0.7.1 h1:tPSJxVKNm3+QseqtyDwtfx9qTeAioyHNYJIUsz7djqU=
856+
kcl-lang.io/kcl-artifact-go v0.7.1/go.mod h1:c07mqi9Hu2UjPW7lYfHhAAWOlZiB7lo7Vkr4jL5ov/M=
857+
kcl-lang.io/kcl-go v0.7.0 h1:mcYpQA+0JvlJ48rGec10GaASsRclJgAY18tz3AjWbEc=
858+
kcl-lang.io/kcl-go v0.7.0/go.mod h1:ZEA8AA/L+vvp68PeBX16aR1Se81hLAWNp+7Qa2cYWdo=
859859
kcl-lang.io/kcl-plugin v0.5.0 h1:eoh6y4l81rwA8yhJXU4hN7YmJeTUNB1nfYCP9OffSxc=
860860
kcl-lang.io/kcl-plugin v0.5.0/go.mod h1:QnZ5OLcyBw5nOnHpChRHtvBq8wvjwiHu/ZZ8j1dfz48=
861-
kcl-lang.io/kpm v0.3.6 h1:iprev+h4d1PmWMTEQhMT33NLFRxIQ7KB703r4GeOlLc=
862-
kcl-lang.io/kpm v0.3.6/go.mod h1:4mFfj1q7QAu3286xeCCQi+Sr37HkJk1GJC5ZGlSa46U=
861+
kcl-lang.io/kpm v0.4.2 h1:rEjfUdX+2kmwwuBaYvsNURQ5D97C3qLNBYAd+XT+DbU=
862+
kcl-lang.io/kpm v0.4.2/go.mod h1:9C/Q9RhRDinbhN7HzQxEexw00HVWUYwWbarMuC1E4xM=
863863
kusionstack.io/kube-api v0.0.0-20230817144216-4714955f3801 h1:rWNki+bYgDqEgia/t8Vfk681NqTV0EbTXc8+JnkvZyM=
864864
kusionstack.io/kube-api v0.0.0-20230817144216-4714955f3801/go.mod h1:QIQrH+MK9xuV+mXCAkk6DN8z6b8oyf4XN0VRccmHH/k=
865865
lukechampine.com/frand v1.4.2 h1:RzFIpOvkMXuPMBb9maa4ND4wjBn71E1Jpf8BzJHMaVw=

pkg/cmd/apply/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ func (o *Options) Run() error {
6969
IsKclPkg: o.IsKclPkg,
7070
WorkDir: o.WorkDir,
7171
Filenames: o.Filenames,
72+
Settings: o.Settings,
7273
Arguments: o.Arguments,
7374
NoStyle: o.NoStyle,
7475
}

pkg/cmd/build/build.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,11 @@ func NewCmdBuild() *cobra.Command {
2727
# Build main.k with work directory
2828
kusion build -w appops/demo/dev
2929
30-
# Build main.k and write result into output.yaml
30+
# Build configurations and write result into an output.yaml
3131
kusion build -o output.yaml
32+
33+
# Build configurations with arguments from settings.yaml
34+
kusion build -Y settings.yaml
3235
3336
# Build without output style and color
3437
kusion build --no-style=true`)
@@ -61,6 +64,8 @@ func NewCmdBuild() *cobra.Command {
6164
func (o *Options) AddBuildFlags(cmd *cobra.Command) {
6265
cmd.Flags().StringVarP(&o.WorkDir, "workdir", "w", "",
6366
i18n.T("Specify the work directory"))
67+
cmd.Flags().StringSliceVarP(&o.Settings, "setting", "Y", []string{},
68+
i18n.T("Specify the command line setting files"))
6469
cmd.Flags().StringToStringVarP(&o.Arguments, "argument", "D", map[string]string{},
6570
i18n.T("Specify the top-level argument"))
6671
}

pkg/cmd/build/builders/kcl/kcl_builder.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"os"
88
"os/exec"
99
"path"
10+
"path/filepath"
1011
"strings"
1112

1213
kcl "kcl-lang.io/kcl-go"
@@ -66,7 +67,7 @@ func (g *Builder) Build(o *builders.Options, _ *project.Project, stack *stack.St
6667
}
6768

6869
func Run(o *builders.Options, stack *stack.Stack) (*CompileResult, error) {
69-
optList, err := BuildOptions(o.WorkDir, o.Arguments)
70+
optList, err := BuildKCLOptions(o)
7071
if err != nil {
7172
return nil, err
7273
}
@@ -146,8 +147,23 @@ func readCRDs(workDir string) ([]interface{}, error) {
146147
return visitor.Visit()
147148
}
148149

149-
func BuildOptions(workDir string, arguments map[string]string) ([]kcl.Option, error) {
150+
func BuildKCLOptions(o *builders.Options) ([]kcl.Option, error) {
150151
optList := make([]kcl.Option, 0)
152+
settings := o.Settings
153+
workDir := o.WorkDir
154+
arguments := o.Arguments
155+
156+
// build settings option
157+
for _, setting := range settings {
158+
if workDir != "" {
159+
setting = filepath.Join(workDir, setting)
160+
}
161+
settingOptions := kcl.WithSettings(setting)
162+
if settingOptions.Err != nil {
163+
return nil, settingOptions.Err
164+
}
165+
optList = append(optList, settingOptions)
166+
}
151167

152168
// build arguments option
153169
for k, v := range arguments {

pkg/cmd/build/options.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ type Options struct {
2323
type Flags struct {
2424
Output string
2525
WorkDir string
26+
Settings []string
2627
Arguments map[string]string
2728
NoStyle bool
2829
}
@@ -34,6 +35,7 @@ func NewBuildOptions() *Options {
3435
Filenames: []string{},
3536
Flags: Flags{
3637
Arguments: map[string]string{},
38+
Settings: make([]string, 0),
3739
},
3840
}
3941
}
@@ -74,6 +76,7 @@ func (o *Options) Run() error {
7476
IsKclPkg: o.IsKclPkg,
7577
WorkDir: o.WorkDir,
7678
Filenames: o.Filenames,
79+
Settings: o.Settings,
7780
Arguments: o.Arguments,
7881
NoStyle: o.NoStyle,
7982
},
@@ -119,5 +122,9 @@ func (o *Options) PreSet(preCheck func(cur string) bool) error {
119122
o.IsKclPkg = true
120123
return nil
121124
}
125+
126+
if len(o.Settings) == 0 {
127+
o.Settings = []string{project.KclFile}
128+
}
122129
return nil
123130
}

pkg/cmd/build/options_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@ func TestCompileOptions_preSet(t *testing.T) {
4545
Output string
4646
}
4747
type want struct {
48-
Output string
48+
Settings []string
49+
Output string
4950
}
5051

5152
tests := []struct {
@@ -59,7 +60,8 @@ func TestCompileOptions_preSet(t *testing.T) {
5960
Output: "",
6061
},
6162
want: want{
62-
Output: "",
63+
Output: "",
64+
Settings: []string{"kcl.yaml"},
6365
},
6466
},
6567
}
@@ -74,6 +76,7 @@ func TestCompileOptions_preSet(t *testing.T) {
7476

7577
wantOpt := NewBuildOptions()
7678
wantOpt.Output = tt.want.Output
79+
wantOpt.Settings = tt.want.Settings
7780

7881
assert.Equal(t, wantOpt, o)
7982
})

pkg/cmd/build/util.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -62,23 +62,25 @@ func Intent(o *builders.Options, p *project.Project, s *stack.Stack) (*intent.In
6262
pg := p.Generator
6363

6464
// default AppsConfigBuilder
65+
var bt project.BuilderType
6566
if pg == nil {
66-
builder = &kcl.Builder{}
67+
bt = project.AppConfigurationBuilder
6768
} else {
68-
gt := pg.Type
69-
// we can add more generators here
70-
switch gt {
71-
case project.KCLBuilder:
72-
builder = &kcl.Builder{}
73-
case project.AppConfigurationBuilder:
74-
appConfigs, err := buildAppConfigs(o, s)
75-
if err != nil {
76-
return nil, err
77-
}
78-
builder = &builders.AppsConfigBuilder{Apps: appConfigs}
79-
default:
80-
return nil, fmt.Errorf("unknow generator type:%s", gt)
69+
bt = pg.Type
70+
}
71+
72+
// we can add more generators here
73+
switch bt {
74+
case project.KCLBuilder:
75+
builder = &kcl.Builder{}
76+
case project.AppConfigurationBuilder:
77+
appConfigs, err := buildAppConfigs(o, s)
78+
if err != nil {
79+
return nil, err
8180
}
81+
builder = &builders.AppsConfigBuilder{Apps: appConfigs}
82+
default:
83+
return nil, fmt.Errorf("unknow generator type:%s", bt)
8284
}
8385

8486
i, err := builder.Build(o, p, s)

pkg/cmd/build/util_test.go

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
var (
22-
spec1 = `
22+
intent1 = `
2323
resources:
2424
- id: v1:Namespace:default
2525
type: Kubernetes
@@ -32,7 +32,7 @@ resources:
3232
spec: {}
3333
status: {}
3434
`
35-
specModel1 = &intent.Intent{
35+
intentModel1 = &intent.Intent{
3636
Resources: []intent.Resource{
3737
{
3838
ID: "v1:Namespace:default",
@@ -51,7 +51,7 @@ resources:
5151
},
5252
}
5353

54-
spec2 = `
54+
intent2 = `
5555
resources:
5656
- id: v1:Namespace:default
5757
type: Kubernetes
@@ -69,7 +69,7 @@ resources:
6969
name: kube-system
7070
`
7171

72-
specModel2 = &intent.Intent{
72+
intentModel2 = &intent.Intent{
7373
Resources: []intent.Resource{
7474
{
7575
ID: "v1:Namespace:default",
@@ -96,7 +96,7 @@ resources:
9696
},
9797
}
9898

99-
specModel3 = &intent.Intent{
99+
intentModel3 = &intent.Intent{
100100
Resources: []intent.Resource{
101101
{
102102
ID: "v1:Namespace:default",
@@ -119,7 +119,7 @@ resources:
119119
}
120120
)
121121

122-
func TestGenerateSpecFromFile(t *testing.T) {
122+
func TestBuildIntentFromFile(t *testing.T) {
123123
tests := []struct {
124124
name string
125125
path string
@@ -130,14 +130,14 @@ func TestGenerateSpecFromFile(t *testing.T) {
130130
{
131131
name: "test1",
132132
path: "kusion_intent.yaml",
133-
content: spec1,
134-
want: specModel1,
133+
content: intent1,
134+
want: intentModel1,
135135
},
136136
{
137137
name: "test2",
138138
path: "kusion_intent.yaml",
139-
content: spec2,
140-
want: specModel2,
139+
content: intent2,
140+
want: intentModel2,
141141
},
142142
{
143143
name: "test3",
@@ -162,7 +162,7 @@ func TestGenerateSpecFromFile(t *testing.T) {
162162
}
163163
}
164164

165-
func TestGenerateSpec(t *testing.T) {
165+
func TestBuildIntent(t *testing.T) {
166166
apc := &appconfigmodel.AppConfiguration{}
167167
var apcMap map[string]interface{}
168168
tmp, _ := json.Marshal(apc)
@@ -181,21 +181,26 @@ func TestGenerateSpec(t *testing.T) {
181181
wantErr bool
182182
}{
183183
{
184-
name: "nil generator", args: struct {
184+
name: "nil builder", args: struct {
185185
o *builders.Options
186186
project *project.Project
187187
stack *stack.Stack
188188
mocker *mockey.MockBuilder
189189
}{
190-
o: &builders.Options{},
191-
project: &project.Project{},
192-
stack: &stack.Stack{},
193-
mocker: mockey.Mock((*kcl.Builder).Build).Return(nil, nil),
190+
o: &builders.Options{Arguments: map[string]string{}},
191+
project: &project.Project{
192+
Configuration: project.Configuration{
193+
Name: "default",
194+
},
195+
}, stack: &stack.Stack{},
196+
mocker: mockey.Mock(kcl.Run).Return(&kcl.CompileResult{
197+
Documents: []kclgo.KCLResult{apcMap},
198+
}, nil),
194199
},
195-
want: nil,
200+
want: intentModel3,
196201
},
197202
{
198-
name: "kcl generator", args: struct {
203+
name: "kcl builder", args: struct {
199204
o *builders.Options
200205
project *project.Project
201206
stack *stack.Stack
@@ -215,7 +220,7 @@ func TestGenerateSpec(t *testing.T) {
215220
want: nil,
216221
},
217222
{
218-
name: "app generator", args: struct {
223+
name: "app builder", args: struct {
219224
o *builders.Options
220225
project *project.Project
221226
stack *stack.Stack
@@ -235,7 +240,7 @@ func TestGenerateSpec(t *testing.T) {
235240
Documents: []kclgo.KCLResult{apcMap},
236241
}, nil),
237242
},
238-
want: specModel3,
243+
want: intentModel3,
239244
},
240245
}
241246

pkg/cmd/preview/options.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (o *Options) Run() error {
125125
IsKclPkg: o.IsKclPkg,
126126
WorkDir: o.WorkDir,
127127
Filenames: o.Filenames,
128+
Settings: o.Settings,
128129
Arguments: o.Arguments,
129130
NoStyle: o.NoStyle,
130131
}

0 commit comments

Comments
 (0)