-
Notifications
You must be signed in to change notification settings - Fork 363
/
kubernetes_helpers.go
200 lines (166 loc) · 6.58 KB
/
kubernetes_helpers.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
// Copyright Envoy Gateway Authors
// SPDX-License-Identifier: Apache-2.0
// The full text of the Apache license is available in the LICENSE file at
// the root of the repo.
package v1alpha1
import (
"encoding/json"
"fmt"
jsonpatch "github.com/evanphx/json-patch"
appv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/util/strategicpatch"
"k8s.io/utils/ptr"
)
// DefaultKubernetesDeploymentReplicas returns the default replica settings.
func DefaultKubernetesDeploymentReplicas() *int32 {
repl := int32(DefaultDeploymentReplicas)
return &repl
}
// DefaultKubernetesDeploymentStrategy returns the default deployment strategy settings.
func DefaultKubernetesDeploymentStrategy() *appv1.DeploymentStrategy {
return &appv1.DeploymentStrategy{
Type: appv1.RollingUpdateDeploymentStrategyType,
}
}
// DefaultKubernetesContainerImage returns the default envoyproxy image.
func DefaultKubernetesContainerImage(image string) *string {
return ptr.To(image)
}
// DefaultKubernetesDeployment returns a new KubernetesDeploymentSpec with default settings.
func DefaultKubernetesDeployment(image string) *KubernetesDeploymentSpec {
return &KubernetesDeploymentSpec{
Replicas: DefaultKubernetesDeploymentReplicas(),
Strategy: DefaultKubernetesDeploymentStrategy(),
Pod: DefaultKubernetesPod(),
Container: DefaultKubernetesContainer(image),
}
}
// DefaultKubernetesPod returns a new KubernetesPodSpec with default settings.
func DefaultKubernetesPod() *KubernetesPodSpec {
return &KubernetesPodSpec{}
}
// DefaultKubernetesContainer returns a new KubernetesContainerSpec with default settings.
func DefaultKubernetesContainer(image string) *KubernetesContainerSpec {
return &KubernetesContainerSpec{
Resources: DefaultResourceRequirements(),
Image: DefaultKubernetesContainerImage(image),
}
}
// DefaultResourceRequirements returns a new ResourceRequirements with default settings.
func DefaultResourceRequirements() *corev1.ResourceRequirements {
return &corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceCPU: resource.MustParse(DefaultDeploymentCPUResourceRequests),
corev1.ResourceMemory: resource.MustParse(DefaultDeploymentMemoryResourceRequests),
},
}
}
// DefaultKubernetesService returns a new KubernetesServiceSpec with default settings.
func DefaultKubernetesService() *KubernetesServiceSpec {
return &KubernetesServiceSpec{
Type: DefaultKubernetesServiceType(),
ExternalTrafficPolicy: DefaultKubernetesServiceExternalTrafficPolicy(),
}
}
// DefaultKubernetesServiceType returns a new KubernetesServiceType with default settings.
func DefaultKubernetesServiceType() *ServiceType {
return GetKubernetesServiceType(ServiceTypeLoadBalancer)
}
// GetKubernetesServiceType returns the KubernetesServiceType pointer.
func GetKubernetesServiceType(serviceType ServiceType) *ServiceType {
return &serviceType
}
func DefaultKubernetesServiceExternalTrafficPolicy() *ServiceExternalTrafficPolicy {
return GetKubernetesServiceExternalTrafficPolicy(ServiceExternalTrafficPolicyLocal)
}
func GetKubernetesServiceExternalTrafficPolicy(serviceExternalTrafficPolicy ServiceExternalTrafficPolicy) *ServiceExternalTrafficPolicy {
return &serviceExternalTrafficPolicy
}
// defaultKubernetesDeploymentSpec fill a default KubernetesDeploymentSpec if unspecified.
func (deployment *KubernetesDeploymentSpec) defaultKubernetesDeploymentSpec(image string) {
if deployment.Replicas == nil {
deployment.Replicas = DefaultKubernetesDeploymentReplicas()
}
if deployment.Strategy == nil {
deployment.Strategy = DefaultKubernetesDeploymentStrategy()
}
if deployment.Pod == nil {
deployment.Pod = DefaultKubernetesPod()
}
if deployment.Container == nil {
deployment.Container = DefaultKubernetesContainer(image)
}
if deployment.Container.Resources == nil {
deployment.Container.Resources = DefaultResourceRequirements()
}
if deployment.Container.Image == nil {
deployment.Container.Image = DefaultKubernetesContainerImage(image)
}
}
func (hpa *KubernetesHorizontalPodAutoscalerSpec) setDefault() {
if len(hpa.Metrics) == 0 {
hpa.Metrics = DefaultEnvoyProxyHpaMetrics()
}
}
// ApplyMergePatch applies a merge patch to a deployment based on the merge type
func (deployment *KubernetesDeploymentSpec) ApplyMergePatch(old *appv1.Deployment) (*appv1.Deployment, error) {
if deployment.Patch == nil {
return old, nil
}
var patchedJSON []byte
var err error
// Serialize the current deployment to JSON
originalJSON, err := json.Marshal(old)
if err != nil {
return nil, fmt.Errorf("error marshaling original deployment: %w", err)
}
switch {
case deployment.Patch.Type == nil || *deployment.Patch.Type == StrategicMerge:
patchedJSON, err = strategicpatch.StrategicMergePatch(originalJSON, deployment.Patch.Value.Raw, appv1.Deployment{})
case *deployment.Patch.Type == JSONMerge:
patchedJSON, err = jsonpatch.MergePatch(originalJSON, deployment.Patch.Value.Raw)
default:
return nil, fmt.Errorf("unsupported merge type: %s", *deployment.Patch.Type)
}
if err != nil {
return nil, fmt.Errorf("error applying merge patch: %w", err)
}
// Deserialize the patched JSON into a new deployment object
var patchedDeployment appv1.Deployment
if err := json.Unmarshal(patchedJSON, &patchedDeployment); err != nil {
return nil, fmt.Errorf("error unmarshaling patched deployment: %w", err)
}
return &patchedDeployment, nil
}
// ApplyMergePatch applies a merge patch to a service based on the merge type
func (service *KubernetesServiceSpec) ApplyMergePatch(old *corev1.Service) (*corev1.Service, error) {
if service.Patch == nil {
return old, nil
}
var patchedJSON []byte
var err error
// Serialize the current deployment to JSON
originalJSON, err := json.Marshal(old)
if err != nil {
return nil, fmt.Errorf("error marshaling original deployment: %w", err)
}
switch {
case service.Patch.Type == nil || *service.Patch.Type == StrategicMerge:
patchedJSON, err = strategicpatch.StrategicMergePatch(originalJSON, service.Patch.Value.Raw, corev1.Service{})
case *service.Patch.Type == JSONMerge:
patchedJSON, err = jsonpatch.MergePatch(originalJSON, service.Patch.Value.Raw)
default:
return nil, fmt.Errorf("unsupported merge type: %s", *service.Patch.Type)
}
if err != nil {
return nil, fmt.Errorf("error applying merge patch: %w", err)
}
// Deserialize the patched JSON into a new service object
var patchedService corev1.Service
if err := json.Unmarshal(patchedJSON, &patchedService); err != nil {
return nil, fmt.Errorf("error unmarshaling patched service: %w", err)
}
return &patchedService, nil
}