-
Notifications
You must be signed in to change notification settings - Fork 70
/
Copy pathservicemesh.go
129 lines (114 loc) · 4.29 KB
/
servicemesh.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
package test
import (
"context"
socommon "github.com/openshift-knative/serverless-operator/pkg/common"
networkingv1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
)
func ServiceMeshControlPlaneV2(name, namespace string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "maistra.io/v2",
"kind": "ServiceMeshControlPlane",
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
"spec": map[string]interface{}{
"version": "v2.4",
},
},
}
}
func ServiceMeshMemberRollV1(name, namespace string, members ...string) *unstructured.Unstructured {
return &unstructured.Unstructured{
Object: map[string]interface{}{
"apiVersion": "maistra.io/v1",
"kind": "ServiceMeshMemberRoll",
"metadata": map[string]interface{}{
"name": name,
"namespace": namespace,
},
"spec": map[string]interface{}{
"members": members,
},
},
}
}
func serviceMeshControlPlaneV2Schema() schema.GroupVersionResource {
return schema.GroupVersionResource{
Group: "maistra.io",
Version: "v2",
Resource: "servicemeshcontrolplanes",
}
}
func CreateServiceMeshControlPlaneV2(ctx *Context, smcp *unstructured.Unstructured) *unstructured.Unstructured {
// When cleaning-up SMCP, wait until it doesn't exist, as it takes a while, which would break subsequent tests
ctx.AddToCleanup(func() error {
ctx.T.Logf("Waiting for ServiceMeshControlPlane %q to not exist", smcp.GetName())
_, err := WaitForUnstructuredState(ctx, serviceMeshControlPlaneV2Schema(), smcp.GetName(), smcp.GetNamespace(), DoesUnstructuredNotExist)
return err
})
return CreateUnstructured(ctx, serviceMeshControlPlaneV2Schema(), smcp)
}
func WaitForServiceMeshControlPlaneReady(ctx *Context, name, namespace string) {
// We use v2 schema for Readiness even if we install a "v.1.1" (v1 schema doesn't have "conditions")
_, err := WaitForUnstructuredState(ctx, serviceMeshControlPlaneV2Schema(), name, namespace, IsUnstructuredReady)
if err != nil {
ctx.T.Fatalf("Error waiting for ServiceMeshControlPlane readiness: %v", err)
}
}
func GetServiceMeshControlPlaneVersion(ctx *Context, name, namespace string) (string, bool, error) {
smcp, err := ctx.Clients.Dynamic.Resource(serviceMeshControlPlaneV2Schema()).Namespace(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
ctx.T.Fatalf("Error getting SMCP %s: %v", name, err)
}
return unstructured.NestedString(smcp.Object, "spec", "version")
}
func CreateServiceMeshMemberRollV1(ctx *Context, smmr *unstructured.Unstructured) *unstructured.Unstructured {
smmrGvr := schema.GroupVersionResource{
Group: "maistra.io",
Version: "v1",
Resource: "servicemeshmemberrolls",
}
return CreateUnstructured(ctx, smmrGvr, smmr)
}
func AllowFromServingSystemNamespaceNetworkPolicy(namespace string) *networkingv1.NetworkPolicy {
return &networkingv1.NetworkPolicy{
ObjectMeta: metav1.ObjectMeta{
Name: "allow-from-system-namespace",
Namespace: namespace,
},
Spec: networkingv1.NetworkPolicySpec{
Ingress: []networkingv1.NetworkPolicyIngressRule{
{
From: []networkingv1.NetworkPolicyPeer{
{
NamespaceSelector: &metav1.LabelSelector{
MatchLabels: map[string]string{
socommon.ServerlessCommonLabelKey: socommon.ServerlessCommonLabelValue,
},
},
},
},
},
},
PolicyTypes: []networkingv1.PolicyType{
networkingv1.PolicyTypeIngress,
},
},
}
}
func CreateNetworkPolicy(ctx *Context, networkPolicy *networkingv1.NetworkPolicy) *networkingv1.NetworkPolicy {
createdNetworkPolicy, err := ctx.Clients.Kube.NetworkingV1().NetworkPolicies(networkPolicy.Namespace).Create(context.Background(), networkPolicy, metav1.CreateOptions{})
if err != nil {
ctx.T.Fatalf("Error creating NetworkPolicy %s: %v", networkPolicy.GetName(), err)
}
ctx.AddToCleanup(func() error {
ctx.T.Logf("Cleaning up NetworkPolicy %s", createdNetworkPolicy.GetName())
return ctx.Clients.Kube.NetworkingV1().NetworkPolicies(createdNetworkPolicy.Namespace).Delete(context.Background(), createdNetworkPolicy.GetName(), metav1.DeleteOptions{})
})
return createdNetworkPolicy
}