-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathgarbage_collection_test.go
163 lines (144 loc) · 5.77 KB
/
garbage_collection_test.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
/*
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 nodeclaim_test
import (
"encoding/base64"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
coretest "sigs.k8s.io/karpenter/pkg/test"
"github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1"
awserrors "github.com/aws/karpenter-provider-aws/pkg/errors"
"github.com/aws/karpenter-provider-aws/pkg/utils"
environmentaws "github.com/aws/karpenter-provider-aws/test/pkg/environment/aws"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
)
var _ = Describe("GarbageCollection", func() {
var customAMI string
var instanceInput *ec2.RunInstancesInput
var instanceProfileName string
var roleName string
BeforeEach(func() {
securityGroups := env.GetSecurityGroups(map[string]string{"karpenter.sh/discovery": env.ClusterName})
subnets := env.GetSubnetNameAndIds(map[string]string{"karpenter.sh/discovery": env.ClusterName})
Expect(securityGroups).ToNot(HaveLen(0))
Expect(subnets).ToNot(HaveLen(0))
customAMI = env.GetCustomAMI("/aws/service/eks/optimized-ami/%s/amazon-linux-2/recommended/image_id", 1)
instanceProfileName = fmt.Sprintf("KarpenterNodeInstanceProfile-%s", env.ClusterName)
roleName = fmt.Sprintf("KarpenterNodeRole-%s", env.ClusterName)
instanceInput = &ec2.RunInstancesInput{
InstanceType: aws.String("c5.large"),
IamInstanceProfile: &ec2.IamInstanceProfileSpecification{
Name: aws.String(instanceProfileName),
},
SecurityGroupIds: lo.Map(securityGroups, func(s environmentaws.SecurityGroup, _ int) *string {
return s.GroupIdentifier.GroupId
}),
SubnetId: aws.String(subnets[0].ID),
BlockDeviceMappings: []*ec2.BlockDeviceMapping{
{
DeviceName: aws.String("/dev/xvda"),
Ebs: &ec2.EbsBlockDevice{
Encrypted: aws.Bool(true),
DeleteOnTermination: aws.Bool(true),
VolumeType: aws.String(ec2.VolumeTypeGp3),
VolumeSize: aws.Int64(20),
},
},
},
ImageId: aws.String(customAMI), // EKS AL2-based AMI
TagSpecifications: []*ec2.TagSpecification{
{
ResourceType: aws.String(ec2.ResourceTypeInstance),
Tags: []*ec2.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", env.ClusterName)),
Value: aws.String("owned"),
},
{
Key: aws.String(corev1beta1.NodePoolLabelKey),
Value: aws.String(nodePool.Name),
},
{
Key: aws.String(v1beta1.LabelNodeClass),
Value: aws.String(nodeClass.Name),
},
},
},
},
MinCount: aws.Int64(1),
MaxCount: aws.Int64(1),
}
})
It("should succeed to garbage collect an Instance that was launched by a NodeClaim but has no Instance mapping", func() {
// Update the userData for the instance input with the correct NodePool
rawContent, err := os.ReadFile("testdata/al2_userdata_input.sh")
Expect(err).ToNot(HaveOccurred())
instanceInput.UserData = lo.ToPtr(base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf(string(rawContent), env.ClusterName,
env.ClusterEndpoint, env.ExpectCABundle(), nodePool.Name))))
env.ExpectInstanceProfileCreated(instanceProfileName, roleName)
DeferCleanup(func() {
env.ExpectInstanceProfileDeleted(instanceProfileName, roleName)
})
// Create an instance manually to mock Karpenter launching an instance
out := env.EventuallyExpectRunInstances(instanceInput)
Expect(out.Instances).To(HaveLen(1))
// Always ensure that we cleanup the instance
DeferCleanup(func() {
_, err := env.EC2API.TerminateInstances(&ec2.TerminateInstancesInput{
InstanceIds: []*string{out.Instances[0].InstanceId},
})
if awserrors.IsNotFound(err) {
return
}
Expect(err).ToNot(HaveOccurred())
})
// Wait for the node to register with the cluster
node := env.EventuallyExpectCreatedNodeCount("==", 1)[0]
// Update the tags to add the karpenter.sh/managed-by tag
_, err = env.EC2API.CreateTagsWithContext(env.Context, &ec2.CreateTagsInput{
Resources: []*string{out.Instances[0].InstanceId},
Tags: []*ec2.Tag{
{
Key: aws.String(corev1beta1.ManagedByAnnotationKey),
Value: aws.String(env.ClusterName),
},
},
})
Expect(err).ToNot(HaveOccurred())
// Eventually expect the node and the instance to be removed (shutting-down)
env.EventuallyExpectNotFound(node)
Eventually(func(g Gomega) {
g.Expect(lo.FromPtr(env.GetInstanceByID(aws.StringValue(out.Instances[0].InstanceId)).State.Name)).To(Equal("shutting-down"))
}, time.Second*10).Should(Succeed())
})
It("should succeed to garbage collect an Instance that was deleted without the cluster's knowledge", func() {
// Disable the interruption queue for the garbage collection coretest
env.ExpectSettingsOverridden(v1.EnvVar{Name: "INTERRUPTION_QUEUE", Value: ""})
pod := coretest.Pod()
env.ExpectCreated(nodeClass, nodePool, pod)
env.EventuallyExpectHealthy(pod)
node := env.ExpectCreatedNodeCount("==", 1)[0]
_, err := env.EC2API.TerminateInstances(&ec2.TerminateInstancesInput{
InstanceIds: aws.StringSlice([]string{lo.Must(utils.ParseInstanceID(node.Spec.ProviderID))}),
})
Expect(err).ToNot(HaveOccurred())
// The garbage collection mechanism should eventually delete this NodeClaim and Node
env.EventuallyExpectNotFound(node)
})
})