generated from kyma-project/template-repository
/
infra.go
153 lines (127 loc) · 3.41 KB
/
infra.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package testinfra
import (
"bytes"
"context"
"fmt"
"github.com/onsi/ginkgo/v2"
"io"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/serializer/yaml"
yamlutil "k8s.io/apimachinery/pkg/util/yaml"
"k8s.io/client-go/rest"
"os"
"path"
"regexp"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
)
var _ Infra = &infra{}
type infra struct {
InfraEnv
InfraDSL
clusters map[ClusterType]*clusterInfo
}
func (i *infra) KCP() ClusterInfo {
return i.clusters[ClusterTypeKcp]
}
func (i *infra) SKR() ClusterInfo {
return i.clusters[ClusterTypeSkr]
}
func (i *infra) Garden() ClusterInfo {
return i.clusters[ClusterTypeGarden]
}
func (i *infra) Stop() error {
i.stopControllers()
var lastErr error
for name, cluster := range i.clusters {
ginkgo.By(fmt.Sprintf("Stopping cluster %s", name))
if err := cluster.env.Stop(); err != nil {
lastErr = err
}
}
return lastErr
}
// =======================
var _ ClusterInfo = &clusterInfo{}
type clusterInfo struct {
ClusterEnv
ClusterDSL
crdDirs []string
env *envtest.Environment
cfg *rest.Config
scheme *runtime.Scheme
client client.Client
}
func (c *clusterInfo) Scheme() *runtime.Scheme {
return c.scheme
}
func (c *clusterInfo) Client() client.Client {
return c.client
}
func (c *clusterInfo) Cfg() *rest.Config {
return c.cfg
}
func (c *clusterInfo) EnsureCrds(ctx context.Context) error {
var files []string
rx := regexp.MustCompile(".+\\.yaml")
for _, dir := range c.crdDirs {
entries, err := os.ReadDir(dir)
if err != nil {
return fmt.Errorf("error reading dir %s: %w", dir, err)
}
for _, en := range entries {
if rx.Match([]byte(en.Name())) {
files = append(files, path.Join(dir, en.Name()))
}
}
}
for _, fn := range files {
b, err := os.ReadFile(fn)
if err != nil {
return fmt.Errorf("error reading CRD manifest %s: %w", fn, err)
}
decoder := yamlutil.NewYAMLOrJSONDecoder(bytes.NewReader(b), 1000)
docCount := 0
for {
docCount++
var rawObj runtime.RawExtension
if err := decoder.Decode(&rawObj); err != nil {
if err == io.EOF {
break
}
return fmt.Errorf("error deconding document #%d in %s: %w", docCount, fn, err)
}
if rawObj.Raw == nil {
// empty yaml doc
continue
}
obj, _, err := yaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, nil)
if err != nil {
return fmt.Errorf("error deconding rawObj into UnstructuredJSONScheme in document #%d in %s: %w", docCount, fn, err)
}
u, ok := obj.(*unstructured.Unstructured)
if !ok {
unstructuredData, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
if err != nil {
return fmt.Errorf("error converting obj to unstructured in document #%d in %s: %w", docCount, fn, err)
}
u = &unstructured.Unstructured{Object: unstructuredData}
}
uu := u.DeepCopy()
err = c.client.Get(ctx, client.ObjectKeyFromObject(uu), uu)
if client.IgnoreNotFound(err) != nil {
return fmt.Errorf("error getting obj %s of kind %s/%s to check if it exist: %w", uu.GetName(), uu.GetAPIVersion(), uu.GetKind(), err)
}
if err == nil {
// this object exist
continue
}
err = c.client.Create(ctx, u)
if err != nil {
return fmt.Errorf("error creating %s/%s/%s: %w", u.GetAPIVersion(), u.GetKind(), u.GetName(), err)
}
}
}
return nil
}