Skip to content

Commit f10800b

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

File tree

3 files changed

+45
-29
lines changed

3 files changed

+45
-29
lines changed

pkg/kustomize.go

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
package e2eutils
22

33
import (
4+
"bytes"
45
"context"
56
"e2eutils/pkg/argo"
7+
"fmt"
8+
"io"
69
"os"
7-
"strings"
810

11+
"gopkg.in/yaml.v3"
12+
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
913
"sigs.k8s.io/e2e-framework/klient/decoder"
1014
"sigs.k8s.io/e2e-framework/klient/k8s"
1115
"sigs.k8s.io/kustomize/api/krusty"
16+
"sigs.k8s.io/kustomize/kyaml/errors"
1217
"sigs.k8s.io/kustomize/kyaml/filesys"
1318
)
1419

@@ -50,20 +55,39 @@ func prepareKubernetesManifests(applicationSource argo.ApplicationSource) ([]k8s
5055
return objects, nil
5156
}
5257

53-
func BuildKustomization(path string) ([]string, error) {
58+
func BuildKustomization(path string) ([]*unstructured.Unstructured, error) {
5459
fSys := filesys.MakeFsOnDisk()
55-
kustomizationDir := "../" + path
5660
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
5761

58-
objects, err := k.Run(fSys, kustomizationDir)
62+
resMap, err := k.Run(fSys, path)
5963
if err != nil {
60-
return nil, err
64+
return nil, fmt.Errorf("running kustomize: %w", err)
6165
}
6266

63-
yaml, err := objects.AsYaml()
67+
yamlData, err := resMap.AsYaml()
6468
if err != nil {
65-
return nil, err
69+
return nil, fmt.Errorf("convert resmap to yaml: %w", err)
6670
}
6771

68-
return strings.Split(string(yaml), "---"), nil
72+
dec := yaml.NewDecoder(bytes.NewReader(yamlData))
73+
74+
var objects []*unstructured.Unstructured
75+
for {
76+
var o unstructured.Unstructured
77+
if err := dec.Decode(&o.Object); err != nil {
78+
if errors.Is(err, io.EOF) {
79+
break
80+
}
81+
return nil, fmt.Errorf("decode yaml: %w", err)
82+
}
83+
84+
// skip empty documents
85+
if len(o.Object) == 0 {
86+
continue
87+
}
88+
89+
objects = append(objects, &o)
90+
}
91+
92+
return objects, nil
6993
}

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)