Skip to content

Commit 4583b3e

Browse files
committed
fix: cleanup kustomization
1 parent 2a71784 commit 4583b3e

File tree

2 files changed

+64
-54
lines changed

2 files changed

+64
-54
lines changed

pkg/kustomize.go

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -2,91 +2,53 @@ package e2eutils
22

33
import (
44
"bytes"
5-
"context"
6-
"e2eutils/pkg/argo"
5+
"errors"
76
"fmt"
87
"io"
9-
"os"
8+
"strings"
109

1110
"gopkg.in/yaml.v3"
1211
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
13-
"sigs.k8s.io/e2e-framework/klient/decoder"
14-
"sigs.k8s.io/e2e-framework/klient/k8s"
1512
"sigs.k8s.io/kustomize/api/krusty"
16-
"sigs.k8s.io/kustomize/kyaml/errors"
1713
"sigs.k8s.io/kustomize/kyaml/filesys"
1814
)
1915

20-
func GetKubernetesManifests(argoApplication argo.Application) ([]k8s.Object, error) {
21-
var objects []k8s.Object
22-
var err error
23-
24-
if argoApplication.Spec.Source != nil && argoApplication.Spec.Source.Path != "" {
25-
objects, err = prepareKubernetesManifests(*argoApplication.Spec.Source)
26-
if err != nil {
27-
return nil, err
28-
}
29-
30-
return objects, nil
31-
}
32-
33-
var source argo.ApplicationSource
34-
for _, source = range argoApplication.Spec.Sources {
35-
if source.Path == "" {
36-
continue
37-
}
38-
39-
objects, err = prepareKubernetesManifests(source)
40-
if err != nil {
41-
return nil, err
42-
}
43-
}
44-
45-
return objects, nil
46-
}
47-
48-
func prepareKubernetesManifests(applicationSource argo.ApplicationSource) ([]k8s.Object, error) {
49-
realPath := os.DirFS("../" + applicationSource.Path)
50-
51-
objects, err := decoder.DecodeAllFiles(context.TODO(), realPath, "*.yaml")
52-
if err != nil {
53-
return nil, err
16+
func BuildKustomization(path string) ([]*unstructured.Unstructured, error) {
17+
if strings.TrimSpace(path) == "" {
18+
return nil, errors.New("kustomization path must not be empty")
5419
}
55-
return objects, nil
56-
}
5720

58-
func BuildKustomization(path string) ([]*unstructured.Unstructured, error) {
59-
fSys := filesys.MakeFsOnDisk()
60-
k := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
21+
fs := filesys.MakeFsOnDisk()
22+
kustomizer := krusty.MakeKustomizer(krusty.MakeDefaultOptions())
6123

62-
resMap, err := k.Run(fSys, path)
24+
resMap, err := kustomizer.Run(fs, path)
6325
if err != nil {
64-
return nil, fmt.Errorf("running kustomize: %w", err)
26+
return nil, fmt.Errorf("failed to run kustomize: %w", err)
6527
}
6628

6729
yamlData, err := resMap.AsYaml()
6830
if err != nil {
69-
return nil, fmt.Errorf("convert resmap to yaml: %w", err)
31+
return nil, fmt.Errorf("failed to convert resource map to YAML: %w", err)
7032
}
7133

72-
dec := yaml.NewDecoder(bytes.NewReader(yamlData))
34+
decoder := yaml.NewDecoder(bytes.NewReader(yamlData))
7335

7436
var objects []*unstructured.Unstructured
7537
for {
76-
var o unstructured.Unstructured
77-
if err := dec.Decode(&o.Object); err != nil {
38+
var obj unstructured.Unstructured
39+
if err := decoder.Decode(&obj.Object); err != nil {
7840
if errors.Is(err, io.EOF) {
7941
break
8042
}
81-
return nil, fmt.Errorf("decode yaml: %w", err)
43+
return nil, fmt.Errorf("failed to decode YAML: %w", err)
8244
}
8345

8446
// skip empty documents
85-
if len(o.Object) == 0 {
47+
if len(obj.Object) == 0 {
8648
continue
8749
}
8850

89-
objects = append(objects, &o)
51+
objects = append(objects, &obj)
9052
}
9153

9254
return objects, nil

pkg/manifest.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package e2eutils
2+
3+
import (
4+
"context"
5+
"e2eutils/pkg/argo"
6+
"os"
7+
8+
"sigs.k8s.io/e2e-framework/klient/decoder"
9+
"sigs.k8s.io/e2e-framework/klient/k8s"
10+
)
11+
12+
func GetKubernetesManifests(argoApplication argo.Application) ([]k8s.Object, error) {
13+
var objects []k8s.Object
14+
var err error
15+
16+
if argoApplication.Spec.Source != nil && argoApplication.Spec.Source.Path != "" {
17+
objects, err = prepareKubernetesManifests(*argoApplication.Spec.Source)
18+
if err != nil {
19+
return nil, err
20+
}
21+
22+
return objects, nil
23+
}
24+
25+
var source argo.ApplicationSource
26+
for _, source = range argoApplication.Spec.Sources {
27+
if source.Path == "" {
28+
continue
29+
}
30+
31+
objects, err = prepareKubernetesManifests(source)
32+
if err != nil {
33+
return nil, err
34+
}
35+
}
36+
37+
return objects, nil
38+
}
39+
40+
func prepareKubernetesManifests(applicationSource argo.ApplicationSource) ([]k8s.Object, error) {
41+
realPath := os.DirFS("../" + applicationSource.Path)
42+
43+
objects, err := decoder.DecodeAllFiles(context.TODO(), realPath, "*.yaml")
44+
if err != nil {
45+
return nil, err
46+
}
47+
return objects, nil
48+
}

0 commit comments

Comments
 (0)