-
Notifications
You must be signed in to change notification settings - Fork 404
/
mockice.go
218 lines (189 loc) · 6.39 KB
/
mockice.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
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package mockice
import (
"fmt"
"log"
"os"
"github.com/pkg/errors"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/dynamic"
)
var (
podPort int32 = 8080
svcPort int32 = 80
defaultImage string = "eu.gcr.io/kyma-project/external/hudymi/mockice:0.1.3"
)
func Start(client dynamic.Interface, namespace, name, testID string) (string, error) {
_, err := createConfigMap(client, namespace, name, testID)
if err != nil {
return "", err
}
_, err = createPod(client, namespace, name, testID)
if err != nil {
Stop(client, namespace, name)
return "", err
}
_, err = createService(client, namespace, name, testID)
if err != nil {
Stop(client, namespace, name)
return "", err
}
return fmt.Sprintf("%s.%s.svc.cluster.local:%d", name, namespace, svcPort), nil
}
func Stop(client dynamic.Interface, namespace, name string) {
logOnDeleteError(deleteResource(client, "configmaps", namespace, name), "ConfigMap", namespace, name)
logOnDeleteError(deleteResource(client, "pods", namespace, name), "Pod", namespace, name)
logOnDeleteError(deleteResource(client, "services", namespace, name), "Service", namespace, name)
}
func logOnDeleteError(err error, kind, namespace, name string) {
if err != nil {
log.Println(fmt.Sprintf("Cannot delete %s %s/%s, because: %v", kind, namespace, name, err))
}
}
func ReadmeURL(host string) string {
return fmt.Sprintf("http://%s/README.md", host)
}
func AsynAPIFileURL(host string) string {
return fmt.Sprintf("http://%s/streetlights.yml", host)
}
func deleteResource(client dynamic.Interface, resource, namespace, name string) error {
groupVersion := schema.GroupVersionResource{Group: "", Version: "v1", Resource: resource}
return client.Resource(groupVersion).Namespace(namespace).Delete(name, nil)
}
func createConfigMap(client dynamic.Interface, namespace, name, testID string) (*v1.ConfigMap, error) {
configMap := fixConfigMap(namespace, name, testID)
resource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "configmaps"}
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&configMap)
if err != nil {
return nil, errors.Wrap(err, "while converting ConfigMap to map[string]interface{}")
}
configMap = v1.ConfigMap{}
err = create(client, resource, namespace, obj, &configMap)
return &configMap, err
}
func createPod(client dynamic.Interface, namespace, name, testID string) (*v1.Pod, error) {
pod, err := fixPod(namespace, name, testID)
if err != nil {
return nil, err
}
resource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "pods"}
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&pod)
if err != nil {
return nil, errors.Wrap(err, "while converting Pod to map[string]interface{}")
}
pod = v1.Pod{}
err = create(client, resource, namespace, obj, &pod)
return &pod, err
}
func createService(client dynamic.Interface, namespace, name, testID string) (*v1.Service, error) {
svc := fixService(namespace, name, testID)
resource := schema.GroupVersionResource{Group: "", Version: "v1", Resource: "services"}
obj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(&svc)
if err != nil {
return nil, errors.Wrap(err, "while converting Service to map[string]interface{}")
}
svc = v1.Service{}
err = create(client, resource, namespace, obj, &svc)
return &svc, err
}
func create(client dynamic.Interface, resource schema.GroupVersionResource, namespace string, unstructuredMap map[string]interface{}, obj interface{}) error {
result, err := client.Resource(resource).Namespace(namespace).Create(&unstructured.Unstructured{Object: unstructuredMap}, metav1.CreateOptions{})
if err != nil {
return errors.Wrap(err, "while creating resource")
}
err = runtime.DefaultUnstructuredConverter.FromUnstructured(result.Object, obj)
if err != nil {
return errors.Wrap(err, "while converting Unstructured resource")
}
return nil
}
func fixPod(namespace, name, testID string) (v1.Pod, error) {
image := os.Getenv("MOCKICE_IMAGE")
if image == "" {
image = defaultImage
}
return v1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Annotations: map[string]string{"sidecar.istio.io/inject": "false"},
Labels: map[string]string{"owner": "rafter-tests", "app": name, "test-id": testID},
},
Spec: v1.PodSpec{
Volumes: []v1.Volume{
{
Name: "config",
VolumeSource: v1.VolumeSource{
ConfigMap: &v1.ConfigMapVolumeSource{LocalObjectReference: v1.LocalObjectReference{
Name: name,
}},
},
},
},
Containers: []v1.Container{
{
Name: "mockice",
Image: image,
ImagePullPolicy: v1.PullIfNotPresent,
Args: []string{"--verbose", "--config", "/app/config.yaml"},
VolumeMounts: []v1.VolumeMount{{
Name: "config",
MountPath: "/app/config.yaml",
ReadOnly: true,
SubPath: "config.yaml",
}},
Ports: []v1.ContainerPort{{
Name: "http",
ContainerPort: podPort,
Protocol: v1.ProtocolTCP,
}},
},
},
},
}, nil
}
func fixConfigMap(namespace, name, testID string) v1.ConfigMap {
return v1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{"owner": "rafter-tests", "app": name, "test-id": testID},
},
Data: map[string]string{
"config.yaml": fmt.Sprintf(`
address: :%d
endpoints:
- name: README.md
defaultResponseCode: 200
defaultResponseContent: "# Test markdown"
defaultResponseContentType: text/markdown; charset=utf-8
- name: streetlights.yml
defaultResponseCode: 200
defaultResponseContent: %q
defaultResponseContentType: text/plain; charset=utf-8
`, podPort, asyncAPIFile),
},
}
}
func fixService(namespace, name, testID string) v1.Service {
return v1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
Labels: map[string]string{"owner": "rafter-tests", "app": name, "test-id": testID},
},
Spec: v1.ServiceSpec{
Ports: []v1.ServicePort{{
Port: svcPort,
TargetPort: intstr.IntOrString{IntVal: podPort},
Protocol: v1.ProtocolTCP,
Name: "http",
}},
Selector: map[string]string{"owner": "rafter-tests", "app": name},
},
}
}