Skip to content

Commit 21f0cd4

Browse files
committed
fix: cleanup kustomization
1 parent 43f8e33 commit 21f0cd4

File tree

3 files changed

+44
-71
lines changed

3 files changed

+44
-71
lines changed

pkg/kustomize.go

Lines changed: 31 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,50 @@
11
package e2eutils
22

33
import (
4-
"context"
5-
"e2eutils/pkg/argo"
6-
"os"
7-
"strings"
4+
"bytes"
5+
"fmt"
6+
"io"
87

9-
"sigs.k8s.io/e2e-framework/klient/decoder"
10-
"sigs.k8s.io/e2e-framework/klient/k8s"
8+
"gopkg.in/yaml.v3"
9+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1110
"sigs.k8s.io/kustomize/api/krusty"
11+
"sigs.k8s.io/kustomize/kyaml/errors"
1212
"sigs.k8s.io/kustomize/kyaml/filesys"
1313
)
1414

15-
func GetKubernetesManifests(argoApplication argo.Application) ([]k8s.Object, error) {
16-
var objects []k8s.Object
17-
var err error
18-
19-
if argoApplication.Spec.Source != nil && argoApplication.Spec.Source.Path != "" {
20-
objects, err = prepareKubernetesManifests(*argoApplication.Spec.Source)
21-
if err != nil {
22-
return nil, err
23-
}
24-
25-
return objects, nil
26-
}
27-
28-
var source argo.ApplicationSource
29-
for _, source = range argoApplication.Spec.Sources {
30-
if source.Path == "" {
31-
continue
32-
}
15+
func BuildKustomization(path string) ([]*unstructured.Unstructured, error) {
16+
fSys := filesys.MakeFsOnDisk()
17+
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
3318

34-
objects, err = prepareKubernetesManifests(source)
35-
if err != nil {
36-
return nil, err
37-
}
19+
resMap, err := k.Run(fSys, path)
20+
if err != nil {
21+
return nil, fmt.Errorf("running kustomize: %w", err)
3822
}
3923

40-
return objects, nil
41-
}
42-
43-
func prepareKubernetesManifests(applicationSource argo.ApplicationSource) ([]k8s.Object, error) {
44-
realPath := os.DirFS("../" + applicationSource.Path)
45-
46-
objects, err := decoder.DecodeAllFiles(context.TODO(), realPath, "*.yaml")
24+
yamlData, err := resMap.AsYaml()
4725
if err != nil {
48-
return nil, err
26+
return nil, fmt.Errorf("convert resmap to yaml: %w", err)
4927
}
50-
return objects, nil
51-
}
5228

53-
func BuildKustomization(path string) ([]string, error) {
54-
fSys := filesys.MakeFsOnDisk()
55-
kustomizationDir := "../" + path
56-
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
29+
dec := yaml.NewDecoder(bytes.NewReader(yamlData))
5730

58-
objects, err := k.Run(fSys, kustomizationDir)
59-
if err != nil {
60-
return nil, err
61-
}
31+
var objects []*unstructured.Unstructured
32+
for {
33+
var o unstructured.Unstructured
34+
if err := dec.Decode(&o.Object); err != nil {
35+
if errors.Is(err, io.EOF) {
36+
break
37+
}
38+
return nil, fmt.Errorf("decode yaml: %w", err)
39+
}
6240

63-
yaml, err := objects.AsYaml()
64-
if err != nil {
65-
return nil, err
41+
// skip empty documents
42+
if len(o.Object) == 0 {
43+
continue
44+
}
45+
46+
objects = append(objects, &o)
6647
}
6748

68-
return strings.Split(string(yaml), "---"), nil
49+
return objects, nil
6950
}

pkg/test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func deployHelmChart(applicationSource argo.ApplicationSource, namespace string,
117117
}
118118

119119
func CheckJobsCompleted(ctx context.Context, client klient.Client, namespace string) error {
120-
kubeConfig := client.RESTConfig()
120+
kubeConfig := GetRestConfig()
121121
clientSet, err := kubernetes.NewForConfig(kubeConfig)
122122
if err != nil {
123123
return err
@@ -143,7 +143,7 @@ func CheckJobsCompleted(ctx context.Context, client klient.Client, namespace str
143143
}
144144

145145
func DeploymentBecameReady(ctx context.Context, client klient.Client, namespace string) error {
146-
kubeConfig := client.RESTConfig()
146+
kubeConfig := GetRestConfig()
147147
clientSet, err := kubernetes.NewForConfig(kubeConfig)
148148
if err != nil {
149149
return err
@@ -174,7 +174,7 @@ func DeploymentBecameReady(ctx context.Context, client klient.Client, namespace
174174
}
175175

176176
func DaemonSetBecameReady(ctx context.Context, client klient.Client, namespace string) error {
177-
kubeConfig := client.RESTConfig()
177+
kubeConfig := GetRestConfig()
178178
clientSet, err := kubernetes.NewForConfig(kubeConfig)
179179
if err != nil {
180180
return err
@@ -205,7 +205,7 @@ func DaemonSetBecameReady(ctx context.Context, client klient.Client, namespace s
205205
}
206206

207207
func PersistentVolumeClaimIsBound(ctx context.Context, client klient.Client, namespace string) error {
208-
kubeConfig := client.RESTConfig()
208+
kubeConfig := GetRestConfig()
209209
clientSet, err := kubernetes.NewForConfig(kubeConfig)
210210
if err != nil {
211211
return err
@@ -236,7 +236,7 @@ func PersistentVolumeClaimIsBound(ctx context.Context, client klient.Client, nam
236236
}
237237

238238
func SnapshotIsReadyToUse(ctx context.Context, client klient.Client, namespace string) error {
239-
kubeConfig := client.RESTConfig()
239+
kubeConfig := GetRestConfig()
240240
dynClient, err := dynamic.NewForConfig(kubeConfig)
241241
if err != nil {
242242
return err

test/k3s_system_upgrade_controller_test.go

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
1111
"sigs.k8s.io/e2e-framework/pkg/envconf"
1212
"sigs.k8s.io/e2e-framework/pkg/features"
13-
"sigs.k8s.io/yaml"
1413
)
1514

1615
func TestK3sSystemUpgradeController(t *testing.T) {
@@ -22,49 +21,42 @@ func TestK3sSystemUpgradeController(t *testing.T) {
2221

2322
client := e2eutils.GetClient()
2423

25-
var kustomization []string
24+
var kustomization []*unstructured.Unstructured
2625
var namespace string
2726

2827
install := features.
2928
New("Kustomization").
3029
Setup(func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
3130
if update.Spec.Sources != nil {
32-
kustomization, err = e2eutils.BuildKustomization(update.Spec.Sources[0].Path)
31+
kustomization, err = e2eutils.BuildKustomization("../" + update.Spec.Sources[0].Path)
3332
} else {
34-
kustomization, err = e2eutils.BuildKustomization(current.Spec.Sources[0].Path)
33+
kustomization, err = e2eutils.BuildKustomization("../" + current.Spec.Sources[0].Path)
3534
}
3635
require.NoError(t, err)
3736

3837
return ctx
3938
}).
4039
Assess("Deployment",
4140
func(ctx context.Context, t *testing.T, cfg *envconf.Config) context.Context {
42-
for _, resource := range kustomization {
43-
var object unstructured.Unstructured
44-
err = yaml.Unmarshal([]byte(resource), &object.Object)
45-
require.NoError(t, err)
46-
41+
// deploy namespace
42+
for _, object := range kustomization {
4743
if object.GetKind() != "Namespace" {
4844
continue
4945
}
5046

51-
err = e2eutils.Apply(*clientSet, &object)
47+
err = e2eutils.Apply(*clientSet, object)
5248
require.NoError(t, err)
5349

5450
// give k8s api some time to create a resource
5551
time.Sleep(100 * time.Millisecond)
5652
}
5753

58-
for _, resource := range kustomization {
59-
var object unstructured.Unstructured
60-
err = yaml.Unmarshal([]byte(resource), &object.Object)
61-
require.NoError(t, err)
62-
54+
for _, object := range kustomization {
6355
if object.GetKind() == "Namespace" {
6456
continue
6557
}
6658

67-
err = e2eutils.Apply(*clientSet, &object)
59+
err = e2eutils.Apply(*clientSet, object)
6860
require.NoError(t, err)
6961

7062
// give k8s api some time to create a resource

0 commit comments

Comments
 (0)