@@ -30,6 +30,7 @@ import (
30
30
"github.com/aws/aws-sdk-go/service/ec2"
31
31
32
32
v1 "k8s.io/api/core/v1"
33
+ "k8s.io/apimachinery/pkg/types"
33
34
cloudprovider "k8s.io/cloud-provider"
34
35
)
35
36
@@ -138,32 +139,79 @@ func (i *instances) InstanceMetadata(ctx context.Context, node *v1.Node) (*cloud
138
139
// getInstance returns the instance if the instance with the given node info still exists.
139
140
// If false an error will be returned, the instance will be immediately deleted by the cloud controller manager.
140
141
func (i * instances ) getInstance (ctx context.Context , node * v1.Node ) (* ec2.Instance , error ) {
141
- var request * ec2.DescribeInstancesInput
142
142
if node .Spec .ProviderID == "" {
143
- // get Instance by private DNS name
144
- request = & ec2.DescribeInstancesInput {
145
- Filters : []* ec2.Filter {
146
- newEc2Filter ("private-dns-name" , node .Name ),
147
- },
148
- }
143
+ // get Instance by node name
149
144
klog .V (4 ).Infof ("looking for node by private DNS name %v" , node .Name )
150
- } else {
151
- // get Instance by provider ID
152
- instanceID , err := parseInstanceIDFromProviderID (node .Spec .ProviderID )
145
+ return getInstanceByPrivateDNSName (ctx , types .NodeName (node .Name ), i .ec2 )
146
+ }
147
+
148
+ // get Instance by provider ID
149
+ klog .V (4 ).Infof ("looking for node by provider ID %v" , node .Spec .ProviderID )
150
+ return getInstanceByProviderID (ctx , node .Spec .ProviderID , i .ec2 )
151
+ }
152
+
153
+ // getInstanceByPrivateDNSName returns the instance if the instance with the given nodeName exists.
154
+ // If false an error will be returned, the instance will be immediately deleted by the cloud controller manager.
155
+ func getInstanceByPrivateDNSName (ctx context.Context , nodeName types.NodeName , ec2Client EC2 ) (* ec2.Instance , error ) {
156
+ request := & ec2.DescribeInstancesInput {
157
+ Filters : []* ec2.Filter {
158
+ newEc2Filter ("private-dns-name" , string (nodeName )),
159
+ },
160
+ }
161
+ klog .V (4 ).Infof ("looking for node by private DNS name %v" , nodeName )
162
+
163
+ instances := []* ec2.Instance {}
164
+ var nextToken * string
165
+ for {
166
+ response , err := ec2Client .DescribeInstances (request )
153
167
if err != nil {
154
- return nil , err
168
+ return nil , fmt .Errorf ("error describing ec2 instances: %v" , err )
169
+ }
170
+
171
+ for _ , reservation := range response .Reservations {
172
+ instances = append (instances , reservation .Instances ... )
155
173
}
156
174
157
- request = & ec2.DescribeInstancesInput {
158
- InstanceIds : []* string {aws .String (instanceID )},
175
+ nextToken = response .NextToken
176
+ if aws .StringValue (nextToken ) == "" {
177
+ break
159
178
}
160
- klog .V (4 ).Infof ("looking for node by provider ID %v" , node .Spec .ProviderID )
179
+ request .NextToken = nextToken
180
+ }
181
+
182
+ if len (instances ) == 0 {
183
+ return nil , cloudprovider .InstanceNotFound
184
+ }
185
+
186
+ if len (instances ) > 1 {
187
+ return nil , fmt .Errorf ("getInstance: multiple instances found" )
188
+ }
189
+
190
+ state := instances [0 ].State .Name
191
+ if * state == ec2 .InstanceStateNameTerminated {
192
+ return nil , fmt .Errorf ("instance %v is terminated" , instances [0 ].InstanceId )
193
+ }
194
+
195
+ return instances [0 ], nil
196
+ }
197
+
198
+ // getInstanceByProviderID returns the instance if the instance with the given providerID exists.
199
+ // If false an error will be returned, the instance will be immediately deleted by the cloud controller manager.
200
+ func getInstanceByProviderID (ctx context.Context , providerID string , ec2Client EC2 ) (* ec2.Instance , error ) {
201
+ instanceID , err := parseInstanceIDFromProviderID (providerID )
202
+ if err != nil {
203
+ return nil , err
204
+ }
205
+
206
+ request := & ec2.DescribeInstancesInput {
207
+ InstanceIds : []* string {aws .String (instanceID )},
161
208
}
209
+ klog .V (4 ).Infof ("looking for node by provider ID %v" , providerID )
162
210
163
211
instances := []* ec2.Instance {}
164
212
var nextToken * string
165
213
for {
166
- response , err := i . ec2 .DescribeInstances (request )
214
+ response , err := ec2Client .DescribeInstances (request )
167
215
if err != nil {
168
216
return nil , fmt .Errorf ("error describing ec2 instances: %v" , err )
169
217
}
0 commit comments