Skip to content

Commit 4287dc7

Browse files
authored
Merge pull request #117 from Peefy/kcl-run-params
feat: add kcl run params field
2 parents 1c1f739 + 4d296ff commit 4287dc7

File tree

4 files changed

+114
-102
lines changed

4 files changed

+114
-102
lines changed

api/v1alpha1/kclrun_types.go

Lines changed: 32 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
kc "github.com/fluxcd/kustomize-controller/api/v1"
2323
"github.com/fluxcd/pkg/apis/meta"
2424
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
25+
runtime "k8s.io/apimachinery/pkg/runtime"
2526
)
2627

2728
const (
@@ -97,7 +98,7 @@ type KCLRunSpec struct {
9798
// +optional
9899
Force bool `json:"force,omitempty"`
99100

100-
// The interval at which to reconcile the Kustomization.
101+
// The interval at which to reconcile the KCL Module.
101102
// This interval is approximate and may be subject to jitter to ensure
102103
// efficient use of resources.
103104
// +kubebuilder:validation:Type=string
@@ -106,7 +107,7 @@ type KCLRunSpec struct {
106107
Interval metav1.Duration `json:"interval"`
107108

108109
// The interval at which to retry a previously failed reconciliation.
109-
// When not specified, the controller uses the KustomizationSpec.Interval
110+
// When not specified, the controller uses the KCLRunSpec.Interval
110111
// value to retry failures.
111112
// +kubebuilder:validation:Type=string
112113
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$"
@@ -118,10 +119,13 @@ type KCLRunSpec struct {
118119
// +optional
119120
Path string `json:"path,omitempty"`
120121

121-
// PostBuild describes which actions to perform on the YAML manifest
122-
// generated by building the KCL module.
122+
// Params are the parameters in key-value pairs format.
123123
// +optional
124-
PostBuild *PostBuild `json:"postBuild,omitempty"`
124+
Params map[string]runtime.RawExtension `json:"params,omitempty" yaml:"params,omitempty"`
125+
126+
// Config is the KCL compile config.
127+
// +optional
128+
Config *ConfigSpec `json:"config,omitempty" yaml:"config,omitempty"`
125129

126130
// Prune enables garbage collection.
127131
// +required
@@ -157,47 +161,32 @@ type CommonMetadata struct {
157161
Labels map[string]string `json:"labels,omitempty"`
158162
}
159163

160-
// PostBuild describes which actions to perform on the YAML manifest
161-
// generated by building the kustomize overlay.
162-
type PostBuild struct {
163-
// Substitute holds a map of key/value pairs.
164-
// The variables defined in your YAML manifests that match any of the keys
165-
// defined in the map will be substituted with the set value.
166-
// Includes support for bash string replacement functions
167-
// e.g. ${var:=default}, ${var:position} and ${var/substring/replacement}.
164+
// ConfigSpec defines the compile config.
165+
type ConfigSpec struct {
166+
// Arguments is the list of top level dynamic arguments for the kcl option function, e.g., env="prod"
168167
// +optional
169-
Substitute map[string]string `json:"substitute,omitempty"`
170-
171-
// SubstituteFrom holds references to ConfigMaps and Secrets containing
172-
// the variables and their values to be substituted in the YAML manifests.
173-
// The ConfigMap and the Secret data keys represent the var names, and they
174-
// must match the vars declared in the manifests for the substitution to
175-
// happen.
168+
Arguments []string `json:"arguments,omitempty" yaml:"arguments,omitempty"`
169+
// Settings is the list of kcl setting files including all of the CLI config.
176170
// +optional
177-
SubstituteFrom []SubstituteReference `json:"substituteFrom,omitempty"`
178-
}
179-
180-
// SubstituteReference contains a reference to a resource containing
181-
// the variables name and value.
182-
type SubstituteReference struct {
183-
// Kind of the values referent, valid values are ('Secret', 'ConfigMap').
184-
// +kubebuilder:validation:Enum=Secret;ConfigMap
185-
// +required
186-
Kind string `json:"kind"`
187-
188-
// Name of the values referent. Should reside in the same namespace as the
189-
// referring resource.
190-
// +kubebuilder:validation:MinLength=1
191-
// +kubebuilder:validation:MaxLength=253
192-
// +required
193-
Name string `json:"name"`
194-
195-
// Optional indicates whether the referenced resource must exist, or whether to
196-
// tolerate its absence. If true and the referenced resource is absent, proceed
197-
// as if the resource was present but empty, without any variables defined.
198-
// +kubebuilder:default:=false
171+
Settings []string `json:"settings,omitempty" yaml:"settings,omitempty"`
172+
// Overrides is the list of override paths and values, e.g., app.image="v2"
173+
// +optional
174+
Overrides []string `json:"overrides,omitempty" yaml:"overrides,omitempty"`
175+
// PathSelectors is the list of path selectors to select output result, e.g., a.b.c
176+
// +optional
177+
PathSelectors []string `json:"pathSelectors,omitempty" yaml:"pathSelectors,omitempty"`
178+
// Vendor denotes running kcl in the vendor mode.
179+
// +optional
180+
Vendor bool `json:"vendor,omitempty" yaml:"vendor,omitempty"`
181+
// SortKeys denotes sorting the output result keys, e.g., `{b = 1, a = 2} => {a = 2, b = 1}`.
182+
// +optional
183+
SortKeys bool `json:"sortKeys,omitempty" yaml:"sortKeys,omitempty"`
184+
// ShowHidden denotes output the hidden attribute in the result.
185+
// +optional
186+
ShowHidden bool `json:"showHidden,omitempty" yaml:"showHidden,omitempty"`
187+
// DisableNone denotes running kcl and disable dumping None values.
199188
// +optional
200-
Optional bool `json:"optional,omitempty"`
189+
DisableNone bool `json:"disableNone,omitempty" yaml:"disableNone,omitempty"`
201190
}
202191

203192
// KCLRunStatus defines the observed state of KCLRun

config/crd/bases/krm.kcl.dev.fluxcd_kclruns.yaml

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,49 @@ spec:
5656
description: Labels to be added to the object's metadata.
5757
type: object
5858
type: object
59+
config:
60+
description: Config is the KCL compile config.
61+
properties:
62+
arguments:
63+
description: Arguments is the list of top level dynamic arguments
64+
for the kcl option function, e.g., env="prod"
65+
items:
66+
type: string
67+
type: array
68+
disableNone:
69+
description: DisableNone denotes running kcl and disable dumping
70+
None values.
71+
type: boolean
72+
overrides:
73+
description: Overrides is the list of override paths and values,
74+
e.g., app.image="v2"
75+
items:
76+
type: string
77+
type: array
78+
pathSelectors:
79+
description: PathSelectors is the list of path selectors to select
80+
output result, e.g., a.b.c
81+
items:
82+
type: string
83+
type: array
84+
settings:
85+
description: Settings is the list of kcl setting files including
86+
all of the CLI config.
87+
items:
88+
type: string
89+
type: array
90+
showHidden:
91+
description: ShowHidden denotes output the hidden attribute in
92+
the result.
93+
type: boolean
94+
sortKeys:
95+
description: SortKeys denotes sorting the output result keys,
96+
e.g., `{b = 1, a = 2} => {a = 2, b = 1}`.
97+
type: boolean
98+
vendor:
99+
description: Vendor denotes running kcl in the vendor mode.
100+
type: boolean
101+
type: object
59102
dependsOn:
60103
description: |-
61104
DependsOn may contain a meta.NamespacedObjectReference slice
@@ -111,7 +154,7 @@ spec:
111154
type: array
112155
interval:
113156
description: |-
114-
The interval at which to reconcile the Kustomization.
157+
The interval at which to reconcile the KCL Module.
115158
This interval is approximate and may be subject to jitter to ensure
116159
efficient use of resources.
117160
pattern: ^([0-9]+(\.[0-9]+)?(ms|s|m|h))+$
@@ -150,6 +193,12 @@ spec:
150193
required:
151194
- secretRef
152195
type: object
196+
params:
197+
additionalProperties:
198+
type: object
199+
x-kubernetes-preserve-unknown-fields: true
200+
description: Params are the parameters in key-value pairs format.
201+
type: object
153202
path:
154203
description: |-
155204
Path to the directory containing the kcl.mod file.
@@ -164,67 +213,13 @@ spec:
164213
165214
If not set, it defaults to true.
166215
type: boolean
167-
postBuild:
168-
description: |-
169-
PostBuild describes which actions to perform on the YAML manifest
170-
generated by building the kustomize overlay.
171-
properties:
172-
substitute:
173-
additionalProperties:
174-
type: string
175-
description: |-
176-
Substitute holds a map of key/value pairs.
177-
The variables defined in your YAML manifests that match any of the keys
178-
defined in the map will be substituted with the set value.
179-
Includes support for bash string replacement functions
180-
e.g. ${var:=default}, ${var:position} and ${var/substring/replacement}.
181-
type: object
182-
substituteFrom:
183-
description: |-
184-
SubstituteFrom holds references to ConfigMaps and Secrets containing
185-
the variables and their values to be substituted in the YAML manifests.
186-
The ConfigMap and the Secret data keys represent the var names, and they
187-
must match the vars declared in the manifests for the substitution to
188-
happen.
189-
items:
190-
description: |-
191-
SubstituteReference contains a reference to a resource containing
192-
the variables name and value.
193-
properties:
194-
kind:
195-
description: Kind of the values referent, valid values are
196-
('Secret', 'ConfigMap').
197-
enum:
198-
- Secret
199-
- ConfigMap
200-
type: string
201-
name:
202-
description: |-
203-
Name of the values referent. Should reside in the same namespace as the
204-
referring resource.
205-
maxLength: 253
206-
minLength: 1
207-
type: string
208-
optional:
209-
default: false
210-
description: |-
211-
Optional indicates whether the referenced resource must exist, or whether to
212-
tolerate its absence. If true and the referenced resource is absent, proceed
213-
as if the resource was present but empty, without any variables defined.
214-
type: boolean
215-
required:
216-
- kind
217-
- name
218-
type: object
219-
type: array
220-
type: object
221216
prune:
222217
description: Prune enables garbage collection.
223218
type: boolean
224219
retryInterval:
225220
description: |-
226221
The interval at which to retry a previously failed reconciliation.
227-
When not specified, the controller uses the KustomizationSpec.Interval
222+
When not specified, the controller uses the KCLRunSpec.Interval
228223
value to retry failures.
229224
pattern: ^([0-9]+(\.[0-9]+)?(ms|s|m|h))+$
230225
type: string

internal/controller/kclrun_controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func (r *KCLRunReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctr
210210
return ctrl.Result{}, err
211211
}
212212
// Compile the KCL source code into the Kubernetes manifests
213-
res, err := kcl.CompileKclPackage(dirPath)
213+
res, err := kcl.CompileKclPackage(&obj, dirPath)
214214
if err != nil {
215215
conditions.MarkFalse(&obj, meta.ReadyCondition, "FetchFailed", err.Error())
216216
log.Error(err, "failed to compile the KCL source code")

internal/kcl/kcl.go

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
package kcl
22

33
import (
4+
"encoding/json"
5+
"fmt"
46
"os"
57
"path/filepath"
68

9+
"github.com/kcl-lang/flux-kcl-controller/api/v1alpha1"
710
"kcl-lang.io/kcl-go/pkg/kcl"
811
"kcl-lang.io/kpm/pkg/client"
912
"kcl-lang.io/kpm/pkg/opt"
1013
)
1114

1215
// Compile the KCL source code into kubernetes manifests.
13-
func CompileKclPackage(pkgPath string) (*kcl.KCLResultList, error) {
16+
func CompileKclPackage(obj *v1alpha1.KCLRun, pkgPath string) (*kcl.KCLResultList, error) {
1417
cli, _ := client.NewKpmClient()
1518
opts := opt.DefaultCompileOptions()
1619

@@ -23,9 +26,34 @@ func CompileKclPackage(pkgPath string) (*kcl.KCLResultList, error) {
2326
settings := filepath.Join(pkgPath, "kcl.yaml")
2427
_, err = os.Stat(settings)
2528
if err == nil {
26-
opts.Option.Merge(kcl.WithSettings(settings))
29+
opts.Merge(kcl.WithSettings(settings))
2730
opts.SetHasSettingsYaml(true)
2831
}
32+
if obj != nil {
33+
if obj.Spec.Config != nil {
34+
for _, s := range obj.Spec.Config.Settings {
35+
opts.Merge(kcl.WithSettings(s))
36+
opts.SetHasSettingsYaml(true)
37+
}
38+
opts.SetVendor(obj.Spec.Config.Vendor)
39+
opts.Merge(
40+
kcl.WithOptions(obj.Spec.Config.Arguments...),
41+
kcl.WithOverrides(obj.Spec.Config.Overrides...),
42+
kcl.WithSelectors(obj.Spec.Config.PathSelectors...),
43+
kcl.WithSortKeys(obj.Spec.Config.SortKeys),
44+
kcl.WithShowHidden(obj.Spec.Config.ShowHidden),
45+
kcl.WithDisableNone(obj.Spec.Config.DisableNone),
46+
)
47+
}
48+
if obj.Spec.Params != nil {
49+
paramsBytes, err := json.Marshal(obj.Spec.Params)
50+
if err != nil {
51+
return nil, err
52+
}
53+
opts.Merge(kcl.WithOptions(fmt.Sprintf("params=%s", string(paramsBytes))))
54+
}
55+
}
56+
opts.Merge()
2957

3058
return cli.CompileWithOpts(opts)
3159
}

0 commit comments

Comments
 (0)