/
apisix.go
222 lines (212 loc) · 5.43 KB
/
apisix.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
219
220
221
222
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to You 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 scaffold
import (
"fmt"
"strings"
"github.com/gruntwork-io/terratest/modules/k8s"
ginkgo "github.com/onsi/ginkgo/v2"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
var (
_apisixConfigMap = `
kind: ConfigMap
apiVersion: v1
metadata:
name: apisix-gw-config.yaml
data:
config.yaml: |
%s
`
_apisixDeployment = `
apiVersion: apps/v1
kind: Deployment
metadata:
name: apisix-deployment-e2e-test
spec:
replicas: 1
selector:
matchLabels:
app: apisix-deployment-e2e-test
strategy:
rollingUpdate:
maxSurge: 50%%
maxUnavailable: 1
type: RollingUpdate
template:
metadata:
labels:
app: apisix-deployment-e2e-test
spec:
terminationGracePeriodSeconds: 0
initContainers:
- name: wait-etcd
image: localhost:5000/busybox:dev
imagePullPolicy: IfNotPresent
command: ['sh', '-c', "until nc -z %s 2379 ; do echo waiting for wait-etcd; sleep 2; done;"]
containers:
- livenessProbe:
failureThreshold: 3
initialDelaySeconds: 1
periodSeconds: 2
successThreshold: 1
tcpSocket:
port: 9080
timeoutSeconds: 2
readinessProbe:
failureThreshold: 3
initialDelaySeconds: 1
periodSeconds: 2
successThreshold: 1
tcpSocket:
port: 9080
timeoutSeconds: 2
image: "localhost:5000/apisix:dev"
imagePullPolicy: IfNotPresent
name: apisix-deployment-e2e-test
ports:
- containerPort: 9080
name: "http"
protocol: "TCP"
- containerPort: 9180
name: "http-admin"
protocol: "TCP"
- containerPort: 9443
name: "https"
protocol: "TCP"
volumeMounts:
- mountPath: /usr/local/apisix/conf/config.yaml
name: apisix-config-yaml-configmap
subPath: config.yaml
volumes:
- configMap:
name: apisix-gw-config.yaml
name: apisix-config-yaml-configmap
`
_apisixService = `
apiVersion: v1
kind: Service
metadata:
name: apisix-service-e2e-test
spec:
selector:
app: apisix-deployment-e2e-test
ports:
- name: http
port: 9080
protocol: TCP
targetPort: 9080
- name: http-admin
port: 9180
protocol: TCP
targetPort: 9180
- name: https
port: 9443
protocol: TCP
targetPort: 9443
- name: tcp
port: 9100
protocol: TCP
targetPort: 9100
- name: tcp-tls
port: 9110
protocol: TCP
targetPort: 9110
- name: udp
port: 9200
protocol: UDP
targetPort: 9200
- name: http-control
port: 9090
protocol: TCP
targetPort: 9090
type: NodePort
`
)
type APISIXConfig struct {
// Used for template rendering.
EtcdServiceFQDN string
}
func (s *Scaffold) newAPISIXConfigMap(cm *APISIXConfig) error {
if cm == nil {
return fmt.Errorf("config not allowed to be empty")
}
data, err := s.renderConfig(s.opts.APISIXConfigPath, cm)
if err != nil {
return err
}
data = indent(data)
configData := fmt.Sprintf(_apisixConfigMap, data)
if err := s.CreateResourceFromString(configData); err != nil {
return err
}
return nil
}
func (s *Scaffold) newAPISIX() (*corev1.Service, error) {
deployment := fmt.Sprintf(_apisixDeployment, EtcdServiceName)
if err := s.CreateResourceFromString(
s.FormatRegistry(deployment),
); err != nil {
return nil, err
}
if err := s.CreateResourceFromString(_apisixService); err != nil {
return nil, err
}
svc, err := k8s.GetServiceE(s.t, s.kubectlOptions, "apisix-service-e2e-test")
if err != nil {
return nil, err
}
return svc, nil
}
func indent(data string) string {
list := strings.Split(data, "\n")
for i := 0; i < len(list); i++ {
list[i] = " " + list[i]
}
return strings.Join(list, "\n")
}
func (s *Scaffold) waitAllAPISIXPodsAvailable() error {
opts := metav1.ListOptions{
LabelSelector: "app=apisix-deployment-e2e-test",
}
condFunc := func() (bool, error) {
items, err := k8s.ListPodsE(s.t, s.kubectlOptions, opts)
if err != nil {
return false, err
}
if len(items) == 0 {
ginkgo.GinkgoT().Log("no apisix pods created")
return false, nil
}
for _, item := range items {
foundPodReady := false
for _, cond := range item.Status.Conditions {
if cond.Type != corev1.PodReady {
continue
}
foundPodReady = true
if cond.Status != "True" {
return false, nil
}
}
if !foundPodReady {
return false, nil
}
}
return true, nil
}
return waitExponentialBackoff(condFunc)
}