-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
aws_node.go
109 lines (97 loc) · 2.95 KB
/
aws_node.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
package defaultaddons
import (
"fmt"
"strings"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"github.com/weaveworks/eksctl/pkg/addons"
appsv1 "k8s.io/api/apps/v1"
apierrs "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"github.com/weaveworks/eksctl/pkg/kubernetes"
)
const (
// AWSNode is the name of the aws-node addon
AWSNode = "aws-node"
awsNodeImageFormatPrefix = "%s.dkr.ecr.%s.%s/amazon-k8s-cni"
)
// UpdateAWSNode will update the `aws-node` add-on and returns true
// if an update is available.
func UpdateAWSNode(rawClient kubernetes.RawClientInterface, region string, plan bool) (bool, error) {
clusterDaemonSet, err := rawClient.ClientSet().AppsV1().DaemonSets(metav1.NamespaceSystem).Get(AWSNode, metav1.GetOptions{})
if err != nil {
if apierrs.IsNotFound(err) {
logger.Warning("%q was not found", AWSNode)
return false, nil
}
return false, errors.Wrapf(err, "getting %q", AWSNode)
}
// if DaemonSets is present, go through our list of assets
list, err := LoadAsset(AWSNode, "yaml")
if err != nil {
return false, err
}
tagMismatch := true
for _, rawObj := range list.Items {
resource, err := rawClient.NewRawResource(rawObj.Object)
if err != nil {
return false, err
}
switch resource.GVK.Kind {
case "DaemonSet":
daemonSet, ok := resource.Info.Object.(*appsv1.DaemonSet)
if !ok {
return false, fmt.Errorf("expected type %T; got %T", &appsv1.Deployment{}, resource.Info.Object)
}
container := &daemonSet.Spec.Template.Spec.Containers[0]
imageParts := strings.Split(container.Image, ":")
if len(imageParts) != 2 {
return false, fmt.Errorf("invalid container image: %s", container.Image)
}
container.Image = awsNodeImageFormatPrefix + ":" + imageParts[1]
if err := addons.UseRegionalImage(&daemonSet.Spec.Template, region); err != nil {
return false, err
}
tagMismatch, err = addons.ImageTagsDiffer(
container.Image,
clusterDaemonSet.Spec.Template.Spec.Containers[0].Image,
)
if err != nil {
return false, err
}
case "CustomResourceDefinition":
if plan {
// eniconfigs.crd.k8s.amazonaws.com CRD is only partially defined in the
// manifest, and causes a range of issue in plan mode, we can skip it
logger.Info(resource.LogAction(plan, "replaced"))
continue
}
case "ServiceAccount":
// Leave service account if it exists
// to avoid overwriting annotations
_, exists, err := resource.Get()
if err != nil {
return false, err
}
if exists {
logger.Info(resource.LogAction(plan, "skipped existing"))
continue
}
}
status, err := resource.CreateOrReplace(plan)
if err != nil {
return false, err
}
logger.Info(status)
}
if plan {
if tagMismatch {
logger.Critical("(plan) %q is not up-to-date", AWSNode)
return true, nil
}
logger.Info("(plan) %q is already up-to-date", AWSNode)
return false, nil
}
logger.Info("%q is now up-to-date", AWSNode)
return false, nil
}