-
Notifications
You must be signed in to change notification settings - Fork 34
/
patch_kube_proxy.go
121 lines (96 loc) · 3.48 KB
/
patch_kube_proxy.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
package usecases
import (
"context"
"errors"
"fmt"
"github.com/berops/claudie/internal/kubectl"
"github.com/berops/claudie/internal/utils"
"github.com/berops/claudie/proto/pb"
"gopkg.in/yaml.v3"
)
func (u *Usecases) PatchKubeProxyConfigMap(ctx context.Context, request *pb.PatchKubeProxyConfigMapRequest) (*pb.PatchKubeProxyConfigMapResponse, error) {
logger := utils.CreateLoggerWithClusterName(utils.GetClusterID(request.DesiredCluster.ClusterInfo))
logger.Info().Msgf("Patching kube-proxy ConfigMap")
k := kubectl.Kubectl{
Kubeconfig: request.DesiredCluster.Kubeconfig,
}
configMap, err := k.KubectlGet("cm kube-proxy", "-oyaml", "-n kube-system")
if err != nil {
return nil, err
}
if configMap == nil {
return &pb.PatchKubeProxyConfigMapResponse{}, nil
}
var desiredKubeconfig map[string]interface{}
if err := yaml.Unmarshal([]byte(request.DesiredCluster.Kubeconfig), &desiredKubeconfig); err != nil {
return nil, fmt.Errorf("failed to unmarshal kubeconfig, malformed yaml : %w", err)
}
var rawConfigMap map[string]interface{}
if err := yaml.Unmarshal(configMap, &rawConfigMap); err != nil {
return nil, fmt.Errorf("failed to update cluster info config map, malformed yaml : %w", err)
}
// get the new api server address
desiredCluster, err := extractClusterFromKubeconfig(desiredKubeconfig)
if err != nil {
return nil, fmt.Errorf("failed to extract cluster data from kubeconfing of desired state")
}
if err := updateKubeconfigServerEndpoint(rawConfigMap, desiredCluster["server"]); err != nil {
return nil, fmt.Errorf("failed to patch kube-proxy kubeconfig: %w", err)
}
b, err := yaml.Marshal(rawConfigMap)
if err != nil {
return nil, fmt.Errorf("failed to marshal patched config map : %w", err)
}
if err = k.KubectlApplyString(string(b), "-n kube-system"); err != nil {
return nil, fmt.Errorf("failed to patch config map: %w", err)
}
// Delete old kube-proxy pods to use updated config-map
if err := k.KubectlDeleteResource("pods", "-l k8s-app=kube-proxy", "-n kube-system"); err != nil {
return nil, fmt.Errorf("failed to restart kube-proxy pods: %w", err)
}
logger.Info().Msgf("Kube-proxy Config Map patched successfully")
return &pb.PatchKubeProxyConfigMapResponse{}, nil
}
func extractClusterFromKubeconfig(root map[string]any) (map[string]any, error) {
clustersSlice, ok := root["clusters"].([]any)
if !ok {
return nil, errors.New("expected 'clusters' to be an []any")
}
if len(clustersSlice) == 0 {
return nil, errors.New("no clusters to patch")
}
cluster, ok := clustersSlice[0].(map[string]any)
if !ok {
return nil, errors.New("expected clusters to be of type map[string]any")
}
clusterData, ok := cluster["cluster"].(map[string]any)
if !ok {
return nil, errors.New("expeted clusters of the kubeconfig to be of type map[string]any")
}
return clusterData, nil
}
func updateKubeconfigServerEndpoint(root map[string]any, val any) error {
data, ok := root["data"].(map[string]any)
if !ok {
return errors.New("expected 'data' field to be a map[string]any")
}
kubeConf, ok := data["kubeconfig.conf"].(string)
if !ok {
return errors.New("expected 'kubeconfig.conf' to be a string")
}
var kubeConfMap map[string]any
if err := yaml.Unmarshal([]byte(kubeConf), &kubeConfMap); err != nil {
return err
}
cluster, err := extractClusterFromKubeconfig(kubeConfMap)
if err != nil {
return err
}
cluster["server"] = val
b, err := yaml.Marshal(kubeConfMap)
if err != nil {
return err
}
data["kubeconfig.conf"] = string(b)
return nil
}