-
Notifications
You must be signed in to change notification settings - Fork 109
/
set_replicas_on_machine_deployment.go
executable file
·201 lines (163 loc) · 5.2 KB
/
set_replicas_on_machine_deployment.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
/*
Copyright 2021 Flant JSC
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 hooks
import (
"github.com/flant/addon-operator/pkg/module_manager/go_hook"
"github.com/flant/addon-operator/sdk"
"github.com/flant/shell-operator/pkg/kube_events_manager/types"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/utils/pointer"
"github.com/deckhouse/deckhouse/modules/040-node-manager/hooks/internal/capi/v1beta1"
"github.com/deckhouse/deckhouse/modules/040-node-manager/hooks/internal/mcm/v1alpha1"
ngv1 "github.com/deckhouse/deckhouse/modules/040-node-manager/hooks/internal/v1"
)
var _ = sdk.RegisterFunc(&go_hook.HookConfig{
Queue: "/modules/node-manager/set_replicas_on_machine_deployment",
Kubernetes: []go_hook.KubernetesConfig{
{
Name: "mds",
ApiVersion: "machine.sapcloud.io/v1alpha1",
Kind: "MachineDeployment",
WaitForSynchronization: pointer.Bool(false),
NamespaceSelector: &types.NamespaceSelector{
NameSelector: &types.NameSelector{
MatchNames: []string{"d8-cloud-instance-manager"},
},
},
FilterFunc: setReplicasFilterMD,
},
{
Name: "capi_mds",
ApiVersion: "cluster.x-k8s.io/v1beta1",
Kind: "MachineDeployment",
WaitForSynchronization: pointer.Bool(false),
NamespaceSelector: &types.NamespaceSelector{
NameSelector: &types.NameSelector{
MatchNames: []string{"d8-cloud-instance-manager"},
},
},
FilterFunc: capiSetReplicasFilterMD,
},
{
Name: "ngs",
ApiVersion: "deckhouse.io/v1",
Kind: "NodeGroup",
WaitForSynchronization: pointer.Bool(false),
FilterFunc: setReplicasFilterNG,
},
},
}, handleSetReplicas)
type setReplicasNodeGroup struct {
Name string
Min int32
Max int32
}
type setReplicasMachineDeployment struct {
Name string
NodeGroup string
Replicas int32
}
func setReplicasFilterNG(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
var ng ngv1.NodeGroup
err := sdk.FromUnstructured(obj, &ng)
if err != nil {
return nil, err
}
var min, max int32
if ng.Spec.StaticInstances != nil {
count := ng.Spec.StaticInstances.Count
min, max = count, count
}
if ng.Spec.CloudInstances.MinPerZone != nil {
min = *ng.Spec.CloudInstances.MinPerZone
}
if ng.Spec.CloudInstances.MaxPerZone != nil {
max = *ng.Spec.CloudInstances.MaxPerZone
}
return setReplicasNodeGroup{
Name: ng.Name,
Min: min,
Max: max,
}, nil
}
func setReplicasFilterMD(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
var md v1alpha1.MachineDeployment
err := sdk.FromUnstructured(obj, &md)
if err != nil {
return nil, err
}
return setReplicasMachineDeployment{
Name: md.Name,
NodeGroup: md.Labels["node-group"],
Replicas: md.Spec.Replicas,
}, nil
}
func capiSetReplicasFilterMD(obj *unstructured.Unstructured) (go_hook.FilterResult, error) {
var md v1beta1.MachineDeployment
err := sdk.FromUnstructured(obj, &md)
if err != nil {
return nil, err
}
var replicas int32
if md.Spec.Replicas != nil {
replicas = *md.Spec.Replicas
}
return setReplicasMachineDeployment{
Name: md.Name,
NodeGroup: md.Labels["node-group"],
Replicas: replicas,
}, nil
}
func calculateReplicasAndPatchMachineDeployment(
input *go_hook.HookInput, snap []go_hook.FilterResult, nodeGroups map[string]setReplicasNodeGroup, apiGroup string) {
for _, sn := range snap {
md := sn.(setReplicasMachineDeployment)
ng, ok := nodeGroups[md.NodeGroup]
if !ok {
input.LogEntry.Warnf("can't find NodeGroup %s to get min and max instances per zone", md.NodeGroup)
continue
}
var desiredReplicas = md.Replicas
switch {
case ng.Min >= ng.Max:
desiredReplicas = ng.Max
case md.Replicas == 0:
desiredReplicas = ng.Min
case md.Replicas <= ng.Min:
desiredReplicas = ng.Min
case md.Replicas > ng.Max:
desiredReplicas = ng.Max
}
if desiredReplicas == md.Replicas {
// replicas not changed, we don't need to patch deployment
continue
}
patch := map[string]interface{}{
"spec": map[string]interface{}{
"replicas": desiredReplicas,
},
}
input.PatchCollector.MergePatch(patch, apiGroup, "MachineDeployment", "d8-cloud-instance-manager", md.Name)
}
}
func handleSetReplicas(input *go_hook.HookInput) error {
nodeGroups := make(map[string]setReplicasNodeGroup)
snap := input.Snapshots["ngs"]
for _, sn := range snap {
ng := sn.(setReplicasNodeGroup)
nodeGroups[ng.Name] = ng
}
calculateReplicasAndPatchMachineDeployment(input, input.Snapshots["mds"], nodeGroups, "machine.sapcloud.io/v1alpha1")
calculateReplicasAndPatchMachineDeployment(input, input.Snapshots["capi_mds"], nodeGroups, "cluster.x-k8s.io/v1beta1")
return nil
}