-
Notifications
You must be signed in to change notification settings - Fork 10
/
k8s.go
145 lines (125 loc) · 3.93 KB
/
k8s.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
// Copyright The HTNN Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package k8s
import (
"bytes"
"context"
"errors"
"io"
"os"
"testing"
"time"
"github.com/stretchr/testify/require"
istiov1b1 "istio.io/client-go/pkg/apis/networking/v1beta1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/yaml"
"sigs.k8s.io/controller-runtime/pkg/client"
gwapiv1 "sigs.k8s.io/gateway-api/apis/v1"
mosniov1 "mosn.io/htnn/controller/api/v1"
"mosn.io/htnn/pkg/log"
)
var (
logger = log.DefaultLogger.WithName("k8s")
)
const (
DefaultNamespace = "e2e"
)
func Prepare(t *testing.T, client client.Client, source string) {
data, err := os.ReadFile(source)
require.NoError(t, err)
input := bytes.NewBuffer(data)
decoder := yaml.NewYAMLOrJSONDecoder(input, 4096)
resources, err := readResources(decoder)
if err != nil {
require.NoErrorf(t, err, "error parsing manifest", "manifest: %s", input.String())
}
for _, res := range resources {
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
namespacedName := types.NamespacedName{Namespace: res.GetNamespace(), Name: res.GetName()}
fetchedObj := res.DeepCopy()
err := client.Get(ctx, namespacedName, fetchedObj)
if err != nil {
if !apierrors.IsNotFound(err) {
require.NoErrorf(t, err, "error getting resource")
}
logger.Info("Creating", "name", res.GetName(), "kind", res.GetKind())
err = client.Create(ctx, res.DeepCopy())
require.NoErrorf(t, err, "error creating resource")
continue
}
res.SetResourceVersion(fetchedObj.GetResourceVersion())
logger.Info("Updating", "name", res.GetName(), "kind", res.GetKind())
err = client.Update(ctx, res.DeepCopy())
require.NoErrorf(t, err, "error updating resource")
}
}
func readResources(decoder *yaml.YAMLOrJSONDecoder) ([]unstructured.Unstructured, error) {
var resources []unstructured.Unstructured
for {
uObj := unstructured.Unstructured{}
if err := decoder.Decode(&uObj); err != nil {
if errors.Is(err, io.EOF) {
break
}
return nil, err
}
if len(uObj.Object) == 0 {
continue
}
ns := uObj.GetNamespace()
if ns == "" {
uObj.SetNamespace(DefaultNamespace)
}
resources = append(resources, uObj)
}
return resources, nil
}
func deleteResource(t *testing.T, ctx context.Context, k8sClient client.Client, obj client.Object, opts ...client.DeleteOption) {
err := k8sClient.Delete(ctx, obj, opts...)
if err != nil && !apierrors.IsNotFound(err) {
require.NoError(t, err)
}
logger.Info("Deleted", "name", obj.GetName(), "kind", obj.GetObjectKind())
}
func CleanUp(t *testing.T, c client.Client) {
ctx := context.Background()
var policies mosniov1.HTTPFilterPolicyList
err := c.List(ctx, &policies)
require.NoError(t, err)
for _, e := range policies.Items {
deleteResource(t, ctx, c, &e)
}
var consumers mosniov1.ConsumerList
err = c.List(ctx, &consumers)
require.NoError(t, err)
for _, e := range consumers.Items {
deleteResource(t, ctx, c, &e)
}
var httproutes gwapiv1.HTTPRouteList
err = c.List(ctx, &httproutes)
require.NoError(t, err)
for _, e := range httproutes.Items {
deleteResource(t, ctx, c, &e)
}
var virtualservices istiov1b1.VirtualServiceList
err = c.List(ctx, &virtualservices)
require.NoError(t, err)
for _, e := range virtualservices.Items {
deleteResource(t, ctx, c, e)
}
// let HTNN to clean up EnvoyFilter
}