-
Notifications
You must be signed in to change notification settings - Fork 34
/
patch.go
157 lines (140 loc) · 4.77 KB
/
patch.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
package nodes
import (
"encoding/json"
"fmt"
"strings"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
comm "github.com/berops/claudie/internal/command"
"github.com/berops/claudie/internal/kubectl"
"github.com/berops/claudie/internal/nodes"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
)
const (
ProviderIdFormat = "claudie://%s"
patchProviderIDPathFormat = "{\"spec\":{\"providerID\":\"%s\"}}"
)
type patchJson struct {
Op string `json:"op"`
Path string `json:"path"`
Value any `json:"value"`
}
type PatchAnnotations struct {
MetadataAnnotations `json:"metadata"`
}
type MetadataAnnotations struct {
Annotations map[string]string `json:"annotations"`
}
type Patcher struct {
clusterID string
desiredNodepools []*pb.NodePool
kc kubectl.Kubectl
logger zerolog.Logger
}
func NewPatcher(cluster *pb.K8Scluster, logger zerolog.Logger) *Patcher {
kc := kubectl.Kubectl{Kubeconfig: cluster.Kubeconfig, MaxKubectlRetries: 3}
clusterID := utils.GetClusterID(cluster.ClusterInfo)
if log.Logger.GetLevel() == zerolog.DebugLevel {
kc.Stdout = comm.GetStdOut(clusterID)
kc.Stderr = comm.GetStdErr(clusterID)
}
return &Patcher{
kc: kc,
desiredNodepools: cluster.ClusterInfo.NodePools,
clusterID: clusterID,
logger: logger,
}
}
func (p *Patcher) PatchProviderID() error {
var err error
for _, np := range p.desiredNodepools {
for _, node := range np.GetNodes() {
nodeName := strings.TrimPrefix(node.Name, fmt.Sprintf("%s-", p.clusterID))
patchPath := fmt.Sprintf(patchProviderIDPathFormat, fmt.Sprintf(ProviderIdFormat, nodeName))
if err1 := p.kc.KubectlPatch("node", nodeName, patchPath); err1 != nil {
p.logger.Err(err1).Str("node", nodeName).Msgf("Error while patching node with patch %s", patchPath)
err = fmt.Errorf("error while patching one or more nodes with providerID")
}
}
}
return err
}
func (p *Patcher) PatchLabels() error {
var err error
for _, np := range p.desiredNodepools {
nodeLabels, err1 := nodes.GetAllLabels(np, nil)
if err1 != nil {
return fmt.Errorf("failed to create labels for %s : %w, %w", np.Name, err, err1)
}
for _, node := range np.Nodes {
nodeName := strings.TrimPrefix(node.Name, fmt.Sprintf("%s-", p.clusterID))
for key, value := range nodeLabels {
patchPath, err1 := buildJSONPatchString("replace", "/metadata/labels/"+key, value)
if err1 != nil {
return fmt.Errorf("failed to create label %s patch path for %s : %w, %w", key, np.Name, err, err1)
}
if err1 := p.kc.KubectlPatch("node", nodeName, patchPath, "--type", "json"); err1 != nil {
p.logger.Err(err1).Str("node", nodeName).Msgf("Failed to patch labels on node with path %s", patchPath)
err = fmt.Errorf("error while patching one or more nodes with labels")
}
}
}
}
return err
}
func (p *Patcher) PatchAnnotations() error {
var err error
for _, np := range p.desiredNodepools {
nodeAnnotations := np.Annotations
for _, node := range np.Nodes {
nodeName := strings.TrimPrefix(node.Name, fmt.Sprintf("%s-", p.clusterID))
patchPath, err1 := buildJSONAnnotationPatch(nodeAnnotations)
if err1 != nil {
return fmt.Errorf("failed to create annotations patch for %s : %w, %w", np.Name, err, err1)
}
if err1 := p.kc.KubectlPatch("node", nodeName, patchPath, "--type", "merge"); err1 != nil {
p.logger.Err(err1).Str("node", nodeName).Msgf("Failed to patch annotations on node with path %s", patchPath)
err = fmt.Errorf("error while patching one or more nodes with annotations")
}
}
}
return err
}
func (p *Patcher) PatchTaints() error {
var err error
for _, np := range p.desiredNodepools {
patchPath, err1 := buildJSONPatchString("replace", "/spec/taints", nodes.GetAllTaints(np))
if err1 != nil {
return fmt.Errorf("failed to create taints patch path for %s : %w", np.Name, err)
}
for _, node := range np.Nodes {
nodeName := strings.TrimPrefix(node.Name, fmt.Sprintf("%s-", p.clusterID))
if err1 := p.kc.KubectlPatch("node", nodeName, patchPath, "--type", "json"); err1 != nil {
p.logger.Err(err1).Str("node", nodeName).Msgf("Failed to patch taints on node with path %s", patchPath)
err = fmt.Errorf("error while patching one or more nodes with taints")
}
}
}
return err
}
func buildJSONAnnotationPatch(data map[string]string) (string, error) {
metadata := PatchAnnotations{
MetadataAnnotations{
Annotations: data,
},
}
jsonPatch, err := json.Marshal(metadata)
if err != nil {
return "", err
}
return string(jsonPatch), nil
}
func buildJSONPatchString(op, path string, value any) (string, error) {
patchJson := patchJson{Op: op, Path: path, Value: value}
b, err := json.Marshal(patchJson)
if err != nil {
return "", err
}
return "[" + string(b) + "]", nil
}