This repository has been archived by the owner on Jan 19, 2024. It is now read-only.
/
helm_mock_executor.go
243 lines (224 loc) · 5.17 KB
/
helm_mock_executor.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
package helm
import (
"strings"
"helm.sh/helm/v3/pkg/chart"
)
// HelmMockExecutor mocks Helm operations
type HelmMockExecutor struct {
}
// NewHelmMockExecutor creates a new HelmMockExecutor
func NewHelmMockExecutor() *HelmMockExecutor {
return &HelmMockExecutor{}
}
const userService = `---
apiVersion: v1
kind: Service
metadata:
name: carts
labels:
app: carts
app.kubernetes.io/name: carts
spec:
type: LoadBalancer
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: carts
app.kubernetes.io/name: carts
`
const userDeployment = `---
apiVersion: apps/v1
kind: Deployment
metadata:
name: carts
spec:
replicas: {{ .Values.replicas }}
strategy:
rollingUpdate:
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app: carts
template:
metadata:
labels:
app: carts
spec:
containers:
- name: carts
image: "docker.io/keptnexamples/carts:0.8.1"
imagePullPolicy: IfNotPresent
`
const renderedUserDeployment = `---
apiVersion: apps/v1
kind: Deployment
metadata:
name: carts
spec:
replicas: 1
strategy:
rollingUpdate:
maxUnavailable: 0
type: RollingUpdate
selector:
matchLabels:
app: carts
template:
metadata:
labels:
app: carts
spec:
containers:
- name: carts
image: "docker.io/keptnexamples/carts:0.8.1"
imagePullPolicy: IfNotPresent
`
// GeneratedCanaryDestinationRule is a DestinationRule manifest for tests
const GeneratedCanaryDestinationRule = `---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
creationTimestamp: null
name: carts-canary
spec:
host: carts-canary.sockshop-NAMESPACE_PLACEHOLDER.svc.cluster.local
`
// GeneratedPrimaryDestinationRule is a DestinationRule manifest for tests
const GeneratedPrimaryDestinationRule = `---
apiVersion: networking.istio.io/v1alpha3
kind: DestinationRule
metadata:
creationTimestamp: null
name: carts-primary
spec:
host: carts-primary.sockshop-NAMESPACE_PLACEHOLDER.svc.cluster.local
`
// GeneratedCanaryService is a Service manifest for tests
const GeneratedCanaryService = `---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
name: carts-canary
labels:
app: carts
app.kubernetes.io/name: carts
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: carts
app.kubernetes.io/name: carts
type: LoadBalancer
status:
loadBalancer: {}
`
// GeneratedPrimaryService is a Service manifest for tests
const GeneratedPrimaryService = `---
apiVersion: v1
kind: Service
metadata:
creationTimestamp: null
name: carts-primary
labels:
app: carts-primary
app.kubernetes.io/name: carts-primary
spec:
ports:
- name: http
port: 80
protocol: TCP
targetPort: 8080
selector:
app: carts-primary
app.kubernetes.io/name: carts-primary
type: LoadBalancer
status:
loadBalancer: {}
`
// GeneratedVirtualService is a VirtualService manifest for tests
const GeneratedVirtualService = `---
apiVersion: networking.istio.io/v1alpha3
kind: VirtualService
metadata:
creationTimestamp: null
name: carts
spec:
gateways:
- public-gateway.istio-system
- mesh
hosts:
- carts.sockshop-NAMESPACE_PLACEHOLDER.svc.cluster.local
- carts
http:
- route:
- destination:
host: carts-canary.sockshop-NAMESPACE_PLACEHOLDER.svc.cluster.local
- destination:
host: carts-primary.sockshop-NAMESPACE_PLACEHOLDER.svc.cluster.local
weight: 100
`
// GeneratedPrimaryDeployment is a Deployment manifest for tests
const GeneratedPrimaryDeployment = `---
apiVersion: apps/v1
kind: Deployment
metadata:
creationTimestamp: null
name: carts-primary
spec:
replicas: 1
selector:
matchLabels:
app: carts-primary
strategy:
rollingUpdate:
maxUnavailable: 0
type: RollingUpdate
template:
metadata:
creationTimestamp: null
labels:
app: carts-primary
spec:
containers:
- image: docker.io/keptnexamples/carts:0.8.1
imagePullPolicy: IfNotPresent
name: carts
resources: {}
status: {}
`
const valuesContent = `
image: docker.io/keptnexamples/carts:0.8.1
replicas: 1
`
const chartContent = `
apiVersion: v2
type: application
description: A Helm Chart for service carts
name: carts
version: 0.1.0
`
// GetManifest returns test/sample manifests
func (h *HelmMockExecutor) GetManifest(releaseName, namespace string) (string, error) {
if strings.HasSuffix(releaseName, "-generated") {
genManifests := GeneratedPrimaryDeployment + GeneratedCanaryService + GeneratedPrimaryService + GeneratedCanaryDestinationRule +
GeneratedPrimaryDestinationRule + GeneratedVirtualService
return strings.ReplaceAll(genManifests, "NAMESPACE_PLACEHOLDER", namespace), nil
}
return userDeployment + userService, nil
}
// UpgradeChart does not execute any action
func (h *HelmMockExecutor) UpgradeChart(ch *chart.Chart, releaseName, namespace string, vals map[string]interface{}) error {
return nil
}
// UninstallRelease does not execute any action
func (h *HelmMockExecutor) UninstallRelease(releaseName, namespace string) error {
return nil
}