-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
ipam.go
86 lines (76 loc) · 2.23 KB
/
ipam.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
package windows
import (
"context"
"encoding/json"
jsonpatch "github.com/evanphx/json-patch/v5"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
)
const (
vpcCNIName = "amazon-vpc-cni"
vpcCNINamespace = metav1.NamespaceSystem
windowsIPAMField = "enable-windows-ipam"
)
// IPAM enables Windows IPAM in the VPC CNI ConfigMap.
type IPAM struct {
Clientset kubernetes.Interface
}
// Enable enables Windows IPAM in the VPC CNI ConfigMap.
func (w *IPAM) Enable(ctx context.Context) error {
configMaps := w.Clientset.CoreV1().ConfigMaps(metav1.NamespaceSystem)
vpcCNIConfig, err := configMaps.Get(ctx, vpcCNIName, metav1.GetOptions{})
if err != nil {
if !apierrors.IsNotFound(err) {
return errors.Wrapf(err, "error getting ConfigMap %q", vpcCNIName)
}
return createConfigMap(ctx, configMaps)
}
if val, ok := vpcCNIConfig.Data[windowsIPAMField]; ok && val == "true" {
logger.Info("Windows IPAM is already enabled")
return nil
}
patch, err := createPatch(vpcCNIConfig)
if err != nil {
return errors.Wrap(err, "error creating merge patch")
}
_, err = configMaps.Patch(ctx, vpcCNIName, types.StrategicMergePatchType, patch, metav1.PatchOptions{})
if err != nil {
return errors.Wrapf(err, "failed to patch resource %q", vpcCNIName)
}
return nil
}
func createPatch(cm *corev1.ConfigMap) ([]byte, error) {
oldData, err := json.Marshal(cm)
if err != nil {
return nil, err
}
cm.Data[windowsIPAMField] = "true"
modifiedData, err := json.Marshal(cm)
if err != nil {
return nil, err
}
return jsonpatch.CreateMergePatch(oldData, modifiedData)
}
func createConfigMap(ctx context.Context, configMaps corev1client.ConfigMapInterface) error {
cm := &corev1.ConfigMap{
TypeMeta: metav1.TypeMeta{
Kind: "ConfigMap",
APIVersion: "v1",
},
ObjectMeta: metav1.ObjectMeta{
Name: vpcCNIName,
Namespace: vpcCNINamespace,
},
Data: map[string]string{
windowsIPAMField: "true",
},
}
_, err := configMaps.Create(ctx, cm, metav1.CreateOptions{})
return err
}