/
zync_types.go
334 lines (307 loc) · 13.1 KB
/
zync_types.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
Copyright 2021.
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 v1alpha1
import (
"github.com/3scale/saas-operator/pkg/util"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/pointer"
)
var (
zyncDefaultImage defaultImageSpec = defaultImageSpec{
Name: pointer.StringPtr("quay.io/3scale/zync"),
Tag: pointer.StringPtr("nightly"),
PullPolicy: (*corev1.PullPolicy)(pointer.StringPtr(string(corev1.PullIfNotPresent))),
}
zyncDefaultGrafanaDashboard defaultGrafanaDashboardSpec = defaultGrafanaDashboardSpec{
SelectorKey: pointer.StringPtr("monitoring-key"),
SelectorValue: pointer.StringPtr("middleware"),
}
zyncDefaultConfigRailsEnvironment string = "development"
zyncDefaultConfigRailsLogLevel string = "info"
zyncDefaultConfigRailsMaxThreads int32 = 10
zyncDefaultConfigBugsnagSpec BugsnagSpec = BugsnagSpec{}
zyncDefaultAPIHPA defaultHorizontalPodAutoscalerSpec = defaultHorizontalPodAutoscalerSpec{
MinReplicas: pointer.Int32Ptr(2),
MaxReplicas: pointer.Int32Ptr(4),
ResourceUtilization: pointer.Int32Ptr(90),
ResourceName: pointer.StringPtr("cpu"),
}
zyncDefaultAPIPDB defaultPodDisruptionBudgetSpec = defaultPodDisruptionBudgetSpec{
MaxUnavailable: util.IntStrPtr(intstr.FromInt(1)),
}
zyncDefaultAPIReplicas int32 = 2
zyncDefaultAPIResources defaultResourceRequirementsSpec = defaultResourceRequirementsSpec{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("250m"),
corev1.ResourceMemory: resource.MustParse("250Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("750m"),
corev1.ResourceMemory: resource.MustParse("512Mi"),
},
}
zyncDefaultAPILivenessProbe defaultProbeSpec = defaultProbeSpec{
InitialDelaySeconds: pointer.Int32Ptr(10),
TimeoutSeconds: pointer.Int32Ptr(30),
PeriodSeconds: pointer.Int32Ptr(10),
SuccessThreshold: pointer.Int32Ptr(1),
FailureThreshold: pointer.Int32Ptr(3),
}
zyncDefaultAPIReadinessProbe defaultProbeSpec = defaultProbeSpec{
InitialDelaySeconds: pointer.Int32Ptr(30),
TimeoutSeconds: pointer.Int32Ptr(10),
PeriodSeconds: pointer.Int32Ptr(10),
SuccessThreshold: pointer.Int32Ptr(1),
FailureThreshold: pointer.Int32Ptr(3),
}
zyncDefaultQueHPA defaultHorizontalPodAutoscalerSpec = defaultHorizontalPodAutoscalerSpec{
MinReplicas: pointer.Int32Ptr(2),
MaxReplicas: pointer.Int32Ptr(4),
ResourceUtilization: pointer.Int32Ptr(90),
ResourceName: pointer.StringPtr("cpu"),
}
zyncDefaultQuePDB defaultPodDisruptionBudgetSpec = defaultPodDisruptionBudgetSpec{
MaxUnavailable: util.IntStrPtr(intstr.FromInt(1)),
}
zyncDefaultQueReplicas int32 = 2
zyncDefaultQueResources defaultResourceRequirementsSpec = defaultResourceRequirementsSpec{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("250m"),
corev1.ResourceMemory: resource.MustParse("250Mi"),
},
Limits: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse("750m"),
corev1.ResourceMemory: resource.MustParse("512Mi"),
},
}
zyncDefaultQueLivenessProbe defaultProbeSpec = defaultProbeSpec{
InitialDelaySeconds: pointer.Int32Ptr(10),
TimeoutSeconds: pointer.Int32Ptr(30),
PeriodSeconds: pointer.Int32Ptr(10),
SuccessThreshold: pointer.Int32Ptr(1),
FailureThreshold: pointer.Int32Ptr(3),
}
zyncDefaultQueReadinessProbe defaultProbeSpec = defaultProbeSpec{
InitialDelaySeconds: pointer.Int32Ptr(30),
TimeoutSeconds: pointer.Int32Ptr(10),
PeriodSeconds: pointer.Int32Ptr(10),
SuccessThreshold: pointer.Int32Ptr(1),
FailureThreshold: pointer.Int32Ptr(3),
}
)
// ZyncSpec defines the desired state of Zync
type ZyncSpec struct {
// Image specification for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Image *ImageSpec `json:"image,omitempty"`
// Application specific configuration options for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
Config ZyncConfig `json:"config"`
// Configures the Grafana Dashboard for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
GrafanaDashboard *GrafanaDashboardSpec `json:"grafanaDashboard,omitempty"`
// Configures the main zync api component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
API *APISpec `json:"api,omitempty"`
// Configures the zync que component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Que *QueSpec `json:"que,omitempty"`
}
// Default implements defaulting for ZyncSpec
func (spec *ZyncSpec) Default() {
spec.Image = InitializeImageSpec(spec.Image, zyncDefaultImage)
spec.Config.Default()
if spec.API == nil {
spec.API = &APISpec{}
}
spec.API.Default()
if spec.Que == nil {
spec.Que = &QueSpec{}
}
spec.Que.Default()
spec.GrafanaDashboard = InitializeGrafanaDashboardSpec(spec.GrafanaDashboard, zyncDefaultGrafanaDashboard)
}
// APISpec is the configuration for main Zync api component
type APISpec struct {
// Pod Disruption Budget for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
PDB *PodDisruptionBudgetSpec `json:"pdb,omitempty"`
// Horizontal Pod Autoscaler for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
HPA *HorizontalPodAutoscalerSpec `json:"hpa,omitempty"`
// Number of replicas (ignored if hpa is enabled) for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Replicas *int32 `json:"replicas,omitempty"`
// Resource requirements for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Resources *ResourceRequirementsSpec `json:"resources,omitempty"`
// Liveness probe for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
LivenessProbe *ProbeSpec `json:"livenessProbe,omitempty"`
// Readiness probe for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
ReadinessProbe *ProbeSpec `json:"readinessProbe,omitempty"`
// Describes node affinity scheduling rules for the pod.
// +optional
NodeAffinity *corev1.NodeAffinity `json:"nodeAffinity,omitempty" protobuf:"bytes,1,opt,name=nodeAffinity"`
// If specified, the pod's tolerations.
// +optional
Tolerations []corev1.Toleration `json:"tolerations,omitempty" protobuf:"bytes,22,opt,name=tolerations"`
}
// Default implements defaulting for the each main zync api component
func (spec *APISpec) Default() {
spec.HPA = InitializeHorizontalPodAutoscalerSpec(spec.HPA, zyncDefaultAPIHPA)
spec.Replicas = intOrDefault(spec.Replicas, &zyncDefaultAPIReplicas)
spec.PDB = InitializePodDisruptionBudgetSpec(spec.PDB, zyncDefaultAPIPDB)
spec.Resources = InitializeResourceRequirementsSpec(spec.Resources, zyncDefaultAPIResources)
spec.LivenessProbe = InitializeProbeSpec(spec.LivenessProbe, zyncDefaultAPILivenessProbe)
spec.ReadinessProbe = InitializeProbeSpec(spec.ReadinessProbe, zyncDefaultAPIReadinessProbe)
}
// QueSpec is the configuration for Zync que
type QueSpec struct {
// Pod Disruption Budget for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
PDB *PodDisruptionBudgetSpec `json:"pdb,omitempty"`
// Horizontal Pod Autoscaler for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
HPA *HorizontalPodAutoscalerSpec `json:"hpa,omitempty"`
// Number of replicas (ignored if hpa is enabled) for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Replicas *int32 `json:"replicas,omitempty"`
// Resource requirements for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Resources *ResourceRequirementsSpec `json:"resources,omitempty"`
// Liveness probe for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
LivenessProbe *ProbeSpec `json:"livenessProbe,omitempty"`
// Readiness probe for the component
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
ReadinessProbe *ProbeSpec `json:"readinessProbe,omitempty"`
// Describes node affinity scheduling rules for the pod.
// +optional
NodeAffinity *corev1.NodeAffinity `json:"nodeAffinity,omitempty" protobuf:"bytes,1,opt,name=nodeAffinity"`
// If specified, the pod's tolerations.
// +optional
Tolerations []corev1.Toleration `json:"tolerations,omitempty" protobuf:"bytes,22,opt,name=tolerations"`
}
// Default implements defaulting for the each zync que
func (spec *QueSpec) Default() {
spec.HPA = InitializeHorizontalPodAutoscalerSpec(spec.HPA, zyncDefaultQueHPA)
spec.Replicas = intOrDefault(spec.Replicas, &zyncDefaultQueReplicas)
spec.PDB = InitializePodDisruptionBudgetSpec(spec.PDB, zyncDefaultQuePDB)
spec.Resources = InitializeResourceRequirementsSpec(spec.Resources, zyncDefaultQueResources)
spec.LivenessProbe = InitializeProbeSpec(spec.LivenessProbe, zyncDefaultQueLivenessProbe)
spec.ReadinessProbe = InitializeProbeSpec(spec.ReadinessProbe, zyncDefaultQueReadinessProbe)
}
// ZyncConfig configures app behavior for Zync
type ZyncConfig struct {
// Rails configuration options for zync components
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Rails *ZyncRailsSpec `json:"rails,omitempty"`
// External Secret common configuration
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
ExternalSecret ExternalSecret `json:"externalSecret,omitempty"`
// A reference to the secret holding the database DSN
// +operator-sdk:csv:customresourcedefinitions:type=spec
DatabaseDSN SecretReference `json:"databaseDSN"`
// A reference to the secret holding the secret-key-base
// +operator-sdk:csv:customresourcedefinitions:type=spec
SecretKeyBase SecretReference `json:"secretKeyBase"`
// A reference to the secret holding the zync authentication token
// +operator-sdk:csv:customresourcedefinitions:type=spec
ZyncAuthToken SecretReference `json:"zyncAuthToken"`
// Options for configuring Bugsnag integration
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Bugsnag *BugsnagSpec `json:"bugsnag,omitempty"`
}
// Default sets default values for any value not specifically set in the ZyncConfig struct
func (cfg *ZyncConfig) Default() {
if cfg.Rails == nil {
cfg.Rails = &ZyncRailsSpec{}
}
if cfg.Bugsnag == nil {
cfg.Bugsnag = &zyncDefaultConfigBugsnagSpec
}
cfg.ExternalSecret.SecretStoreRef = InitializeExternalSecretSecretStoreReferenceSpec(cfg.ExternalSecret.SecretStoreRef, defaultExternalSecretSecretStoreReference)
cfg.ExternalSecret.RefreshInterval = durationOrDefault(cfg.ExternalSecret.RefreshInterval, &defaultExternalSecretRefreshInterval)
cfg.Rails.Default()
}
// ZyncRailsSpec configures rails for system components
type ZyncRailsSpec struct {
// Rails environment
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
Environment *string `json:"environment,omitempty"`
// Rails log level (debug, info, warn, error, fatal or unknown)
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +kubebuilder:validation:Enum=debug;info;warn;error;fatal;unknown
// +optional
LogLevel *string `json:"logLevel,omitempty"`
// Rails max threads (only applies to api)
// +operator-sdk:csv:customresourcedefinitions:type=spec
// +optional
MaxThreads *int32 `json:"maxThreads,omitempty"`
}
// Default applies defaults for ZyncRailsSpec
func (zrs *ZyncRailsSpec) Default() {
zrs.Environment = stringOrDefault(zrs.Environment, pointer.StringPtr(zyncDefaultConfigRailsEnvironment))
zrs.LogLevel = stringOrDefault(zrs.LogLevel, pointer.StringPtr(zyncDefaultConfigRailsLogLevel))
zrs.MaxThreads = intOrDefault(zrs.MaxThreads, pointer.Int32Ptr(zyncDefaultConfigRailsMaxThreads))
}
// ZyncStatus defines the observed state of Zync
type ZyncStatus struct{}
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// Zync is the Schema for the zyncs API
type Zync struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec ZyncSpec `json:"spec,omitempty"`
Status ZyncStatus `json:"status,omitempty"`
}
// Default implements defaulting for the Zync resource
func (z *Zync) Default() {
z.Spec.Default()
}
// +kubebuilder:object:root=true
// ZyncList contains a list of Zync
type ZyncList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Zync `json:"items"`
}
func init() {
SchemeBuilder.Register(&Zync{}, &ZyncList{})
}