Skip to content

Commit 62a7bde

Browse files
authored
Merge pull request #125 from Peefy/feat-more-type-tags-and-tests
feat: add more kclrun type tag and unit tests
2 parents 908aa05 + 2b02992 commit 62a7bde

File tree

6 files changed

+182
-26
lines changed

6 files changed

+182
-26
lines changed

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,10 @@ fmt:
6767
vet:
6868
go vet ./...
6969

70+
# Run go build
71+
build:
72+
go build ./...
73+
7074
# Generate code
7175
generate: controller-gen
7276
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

api/v1alpha1/kclrun_types.go

Lines changed: 50 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,20 @@ type KCLRunSpec struct {
4242
// applied to all resources. Any existing label or annotation will be
4343
// overridden if its key matches a common one.
4444
// +optional
45-
CommonMetadata *CommonMetadata `json:"commonMetadata,omitempty"`
45+
CommonMetadata *CommonMetadata `json:"commonMetadata,omitempty" yaml:"commonMetadata,omitempty"`
4646

4747
// DependsOn may contain a meta.NamespacedObjectReference slice
4848
// with references to Kustomization resources that must be ready before this
4949
// Kustomization can be reconciled.
5050
// +optional
51-
DependsOn []meta.NamespacedObjectReference `json:"dependsOn,omitempty"`
51+
DependsOn []meta.NamespacedObjectReference `json:"dependsOn,omitempty" yaml:"dependsOn,omitempty"`
5252

5353
// Timeout is the time to wait for any individual Kubernetes operation (like Jobs
5454
// for hooks) during the performance. Defaults to '5m0s'.
5555
// +kubebuilder:validation:Type=string
5656
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$"
5757
// +optional
58-
Timeout *metav1.Duration `json:"timeout,omitempty"`
58+
Timeout *metav1.Duration `json:"timeout,omitempty" yaml:"timeout,omitempty"`
5959

6060
// PersistentClient tells the controller to use a persistent Kubernetes
6161
// client for this release. When enabled, the client will be reused for the
@@ -65,7 +65,7 @@ type KCLRunSpec struct {
6565
// If not set, it defaults to true.
6666
//
6767
// +optional
68-
PersistentClient *bool `json:"persistentClient,omitempty"`
68+
PersistentClient *bool `json:"persistentClient,omitempty" yaml:"persistentClient,omitempty"`
6969

7070
// The KubeConfig for reconciling the controller on a remote cluster.
7171
// When used in combination with `KCLRunSpec.ServiceAccountName`,
@@ -75,49 +75,49 @@ type KCLRunSpec struct {
7575
// a controller level fallback for when `KCLRunSpec.ServiceAccountName`
7676
// is empty.
7777
// +optional
78-
KubeConfig *meta.KubeConfigReference `json:"kubeConfig,omitempty"`
78+
KubeConfig *meta.KubeConfigReference `json:"kubeConfig,omitempty" yaml:"kubeConfig,omitempty"`
7979

8080
// The name of the Kubernetes service account to impersonate
8181
// when reconciling this KCL source.
8282
// +kubebuilder:validation:MinLength=1
8383
// +kubebuilder:validation:MaxLength=253
8484
// +optional
85-
ServiceAccountName string `json:"serviceAccountName,omitempty"`
85+
ServiceAccountName string `json:"serviceAccountName,omitempty" yaml:"serviceAccountName,omitempty"`
8686

8787
// TargetNamespace to target when performing operations for the KCL.
8888
// Defaults to the namespace of the KCL source.
8989
// +kubebuilder:validation:MinLength=1
9090
// +kubebuilder:validation:MaxLength=63
9191
// +kubebuilder:validation:Optional
9292
// +optional
93-
TargetNamespace string `json:"targetNamespace,omitempty"`
93+
TargetNamespace string `json:"targetNamespace,omitempty" yaml:"targetNamespace,omitempty"`
9494

9595
// Force instructs the controller to recreate resources
9696
// when patching fails due to an immutable field change.
9797
// +kubebuilder:default:=false
9898
// +optional
99-
Force bool `json:"force,omitempty"`
99+
Force bool `json:"force,omitempty" yaml:"force,omitempty"`
100100

101101
// The interval at which to reconcile the KCL Module.
102102
// This interval is approximate and may be subject to jitter to ensure
103103
// efficient use of resources.
104104
// +kubebuilder:validation:Type=string
105105
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$"
106106
// +required
107-
Interval metav1.Duration `json:"interval"`
107+
Interval metav1.Duration `json:"interval" yaml:"interval"`
108108

109109
// The interval at which to retry a previously failed reconciliation.
110110
// When not specified, the controller uses the KCLRunSpec.Interval
111111
// value to retry failures.
112112
// +kubebuilder:validation:Type=string
113113
// +kubebuilder:validation:Pattern="^([0-9]+(\\.[0-9]+)?(ms|s|m|h))+$"
114114
// +optional
115-
RetryInterval *metav1.Duration `json:"retryInterval,omitempty"`
115+
RetryInterval *metav1.Duration `json:"retryInterval,omitempty" yaml:"retryInterval,omitempty"`
116116

117117
// Path to the directory containing the kcl.mod file.
118118
// Defaults to 'None', which translates to the root path of the SourceRef.
119119
// +optional
120-
Path string `json:"path,omitempty"`
120+
Path string `json:"path,omitempty" yaml:"path,omitempty"`
121121

122122
// Params are the parameters in key-value pairs format.
123123
// +optional
@@ -127,6 +127,11 @@ type KCLRunSpec struct {
127127
// +optional
128128
Config *ConfigSpec `json:"config,omitempty" yaml:"config,omitempty"`
129129

130+
// ConfigReference holds references to ConfigMaps and Secrets containing
131+
// the KCL compile config. The ConfigMap and the Secret data keys represent the config names.
132+
// +optional
133+
ConfigReference *ConfigReference `json:"configReference,omitempty" yaml:"configReference,omitempty"`
134+
130135
// Prune enables garbage collection.
131136
// +required
132137
Prune bool `json:"prune"`
@@ -189,59 +194,79 @@ type ConfigSpec struct {
189194
DisableNone bool `json:"disableNone,omitempty" yaml:"disableNone,omitempty"`
190195
}
191196

197+
// ConfigReference contains a reference to a resource containing the KCL compile config.
198+
type ConfigReference struct {
199+
// Kind of the values referent, valid values are ('Secret', 'ConfigMap').
200+
// +kubebuilder:validation:Enum=Secret;ConfigMap
201+
// +required
202+
Kind string `json:"kind" yaml:"kind"`
203+
// Name of the values referent. Should reside in the same namespace as the
204+
// referring resource.
205+
// +kubebuilder:validation:MinLength=1
206+
// +kubebuilder:validation:MaxLength=253
207+
// +required
208+
Name string `json:"name" yaml:"name"`
209+
// Optional indicates whether the referenced resource must exist, or whether to
210+
// tolerate its absence. If true and the referenced resource is absent, proceed
211+
// as if the resource was present but empty, without any variables defined.
212+
// +kubebuilder:default:=false
213+
// +optional
214+
Optional bool `json:"optional,omitempty" yaml:"optional,omitempty"`
215+
}
216+
192217
// KCLRunStatus defines the observed state of KCLRun
193218
type KCLRunStatus struct {
194-
meta.ReconcileRequestStatus `json:",inline"`
219+
meta.ReconcileRequestStatus `json:",inline" yaml:",inline"`
195220

196221
// ObservedGeneration is the last reconciled generation.
197222
// +optional
198-
ObservedGeneration int64 `json:"observedGeneration,omitempty"`
223+
ObservedGeneration int64 `json:"observedGeneration,omitempty" yaml:"observedGeneration,omitempty"`
199224

200225
// +optional
201-
Conditions []metav1.Condition `json:"conditions,omitempty"`
226+
Conditions []metav1.Condition `json:"conditions,omitempty" yaml:"conditions,omitempty"`
202227

203228
// The last successfully applied revision.
204229
// Equals the Revision of the applied Artifact from the referenced Source.
205230
// +optional
206-
LastAppliedRevision string `json:"lastAppliedRevision,omitempty"`
231+
LastAppliedRevision string `json:"lastAppliedRevision,omitempty" yaml:"lastAppliedRevision,omitempty"`
207232

208233
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
209234
// Important: Run "make" to regenerate code after modifying this file
210235

211236
// LastAttemptedRevision is the revision of the last reconciliation attempt.
212237
// +optional
213-
LastAttemptedRevision string `json:"lastAttemptedRevision,omitempty"`
238+
LastAttemptedRevision string `json:"lastAttemptedRevision,omitempty" yaml:"lastAttemptedRevision,omitempty"`
214239

215240
// LastAttemptedRevisionDigest is the digest of the last reconciliation attempt.
216241
// This is only set for OCIRepository sources.
217242
// +optional
218-
LastAttemptedRevisionDigest string `json:"lastAttemptedRevisionDigest,omitempty"`
243+
LastAttemptedRevisionDigest string `json:"lastAttemptedRevisionDigest,omitempty" yaml:"lastAttemptedRevisionDigest,omitempty"`
219244

220245
// Inventory contains the list of Kubernetes resource object references that
221246
// have been successfully applied.
222247
// +optional
223-
Inventory *ResourceInventory `json:"inventory,omitempty"`
248+
Inventory *ResourceInventory `json:"inventory,omitempty" yaml:"inventory,omitempty"`
224249
}
225250

226251
//+kubebuilder:object:root=true
227252
//+kubebuilder:subresource:status
228253

229254
// KCLRun is the Schema for the kclruns API
230255
type KCLRun struct {
231-
metav1.TypeMeta `json:",inline"`
232-
metav1.ObjectMeta `json:"metadata,omitempty"`
256+
metav1.TypeMeta `json:",inline" yaml:",inline"`
257+
metav1.ObjectMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
233258

234-
Spec KCLRunSpec `json:"spec,omitempty"`
235-
Status KCLRunStatus `json:"status,omitempty"`
259+
Spec KCLRunSpec `json:"spec,omitempty" yaml:"spec,omitempty"`
260+
Status KCLRunStatus `json:"status,omitempty" yaml:"status,omitempty"`
236261
}
237262

238263
//+kubebuilder:object:root=true
239264

240265
// KCLRunList contains a list of KCLRun
241266
type KCLRunList struct {
242-
metav1.TypeMeta `json:",inline"`
243-
metav1.ListMeta `json:"metadata,omitempty"`
244-
Items []KCLRun `json:"items"`
267+
metav1.TypeMeta `json:",inline" yaml:",inline"`
268+
metav1.ListMeta `json:"metadata,omitempty" yaml:"metadata,omitempty"`
269+
Items []KCLRun `json:"items" yaml:"items"`
245270
}
246271

247272
func init() {

api/v1alpha1/kclrun_types_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package v1alpha1
2+
3+
import (
4+
"bytes"
5+
"os"
6+
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
10+
"k8s.io/apimachinery/pkg/util/yaml"
11+
)
12+
13+
func TestKCLRunDeserialize(t *testing.T) {
14+
yamlContent, err := os.ReadFile("testdata/kclrun.yaml")
15+
assert.NoError(t, err)
16+
kclRun := &KCLRun{}
17+
decoder := yaml.NewYAMLOrJSONDecoder(bytes.NewReader(yamlContent), 100)
18+
err = decoder.Decode(kclRun)
19+
assert.NoError(t, err)
20+
assert.Equal(t, "example-kclrun", kclRun.ObjectMeta.Name)
21+
assert.Equal(t, "default", kclRun.ObjectMeta.Namespace)
22+
assert.Equal(t, &v1.Duration{Duration: 600000000000}, kclRun.Spec.Timeout)
23+
assert.True(t, kclRun.Spec.Prune)
24+
assert.Equal(t, true, kclRun.Spec.Prune)
25+
assert.NotNil(t, kclRun.Spec.CommonMetadata)
26+
assert.Equal(t, "example_value", kclRun.Spec.CommonMetadata.Annotations["some_annotation"])
27+
assert.Equal(t, "my-app", kclRun.Spec.CommonMetadata.Labels["app"])
28+
assert.Contains(t, kclRun.Spec.DependsOn[0].Name, "my-kustomization")
29+
assert.NotNil(t, kclRun.Spec.KubeConfig)
30+
assert.Equal(t, "my-kubeconfig", kclRun.Spec.KubeConfig.SecretRef.Name)
31+
assert.Equal(t, "my-service-account", kclRun.Spec.ServiceAccountName)
32+
assert.Equal(t, "my-target-namespace", kclRun.Spec.TargetNamespace)
33+
assert.False(t, kclRun.Spec.Force)
34+
assert.NotNil(t, kclRun.Spec.RetryInterval)
35+
assert.Equal(t, "/path/to/kcl_mod_file_path", kclRun.Spec.Path)
36+
assert.NotNil(t, kclRun.Spec.Config)
37+
assert.Contains(t, kclRun.Spec.Config.Arguments, "env=\"prod\"")
38+
assert.NotNil(t, kclRun.Spec.ConfigReference)
39+
assert.Equal(t, "ConfigMap", kclRun.Spec.ConfigReference.Kind)
40+
assert.Equal(t, "config-map-reference", kclRun.Spec.ConfigReference.Name)
41+
}

api/v1alpha1/testdata/kclrun.yaml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
apiVersion: kclrun.example.com/v1
2+
kind: KCLRun
3+
metadata:
4+
name: example-kclrun
5+
namespace: default
6+
spec:
7+
commonMetadata:
8+
annotations:
9+
some_annotation: "example_value"
10+
labels:
11+
app: my-app
12+
dependsOn:
13+
- name: my-kustomization
14+
namespace: default
15+
kind: Kustomization
16+
timeout: "10m"
17+
persistentClient: true
18+
kubeConfig:
19+
secretRef:
20+
name: my-kubeconfig
21+
serviceAccountName: my-service-account
22+
targetNamespace: my-target-namespace
23+
force: false
24+
interval: "5m"
25+
retryInterval: "1m"
26+
path: /path/to/kcl_mod_file_path
27+
config:
28+
arguments:
29+
- env="prod"
30+
- debug="true"
31+
settings:
32+
- setting1.yaml
33+
- setting2.yaml
34+
overrides:
35+
- app.image="v2"
36+
pathSelectors:
37+
- path.to.output
38+
vendor: false
39+
sortKeys: true
40+
showHidden: false
41+
disableNone: true
42+
configReference:
43+
kind: ConfigMap
44+
name: config-map-reference
45+
optional: false
46+
prune: true
47+
healthChecks:
48+
- kind: Pod
49+
name: my-pod
50+
namespace: default
51+
wait: false
52+
sourceRef:
53+
kind: OCIRepository
54+
name: podinfo
55+
suspend: false

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,36 @@ spec:
9999
description: Vendor denotes running kcl in the vendor mode.
100100
type: boolean
101101
type: object
102+
configReference:
103+
description: |-
104+
ConfigReference holds references to ConfigMaps and Secrets containing
105+
the KCL compile config. The ConfigMap and the Secret data keys represent the config names.
106+
properties:
107+
kind:
108+
description: Kind of the values referent, valid values are ('Secret',
109+
'ConfigMap').
110+
enum:
111+
- Secret
112+
- ConfigMap
113+
type: string
114+
name:
115+
description: |-
116+
Name of the values referent. Should reside in the same namespace as the
117+
referring resource.
118+
maxLength: 253
119+
minLength: 1
120+
type: string
121+
optional:
122+
default: false
123+
description: |-
124+
Optional indicates whether the referenced resource must exist, or whether to
125+
tolerate its absence. If true and the referenced resource is absent, proceed
126+
as if the resource was present but empty, without any variables defined.
127+
type: boolean
128+
required:
129+
- kind
130+
- name
131+
type: object
102132
dependsOn:
103133
description: |-
104134
DependsOn may contain a meta.NamespacedObjectReference slice

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ require (
1919
github.com/onsi/ginkgo/v2 v2.20.2
2020
github.com/onsi/gomega v1.34.2
2121
github.com/spf13/pflag v1.0.5
22+
github.com/stretchr/testify v1.9.0
2223
k8s.io/apimachinery v0.31.0
2324
k8s.io/client-go v0.31.0
2425
kcl-lang.io/kcl-go v0.9.3
@@ -121,13 +122,13 @@ require (
121122
github.com/otiai10/copy v1.14.0 // indirect
122123
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
123124
github.com/pjbgf/sha1cd v0.3.0 // indirect
125+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
124126
github.com/powerman/rpc-codec v1.2.2 // indirect
125127
github.com/russross/blackfriday/v2 v2.1.0 // indirect
126128
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
127129
github.com/sirupsen/logrus v1.9.3 // indirect
128130
github.com/skeema/knownhosts v1.2.2 // indirect
129131
github.com/spf13/cobra v1.8.1 // indirect
130-
github.com/stretchr/objx v0.5.2 // indirect
131132
github.com/syndtr/gocapability v0.0.0-20200815063812-42c35b437635 // indirect
132133
github.com/thoas/go-funk v0.9.3 // indirect
133134
github.com/ulikunitz/xz v0.5.12 // indirect

0 commit comments

Comments
 (0)