@@ -19,14 +19,19 @@ import (
19
19
"fmt"
20
20
"net"
21
21
22
- "github.com/oracle/oci-go-sdk/v49/core"
23
- "k8s.io/apimachinery/pkg/labels"
24
-
25
- "github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
26
22
"github.com/pkg/errors"
27
23
api "k8s.io/api/core/v1"
24
+ "k8s.io/apimachinery/pkg/labels"
28
25
"k8s.io/apimachinery/pkg/types"
29
26
cloudprovider "k8s.io/cloud-provider"
27
+
28
+ "github.com/oracle/oci-cloud-controller-manager/pkg/oci/client"
29
+ "github.com/oracle/oci-go-sdk/v65/containerengine"
30
+ "github.com/oracle/oci-go-sdk/v65/core"
31
+ )
32
+
33
+ const (
34
+ VirtualNodePoolIdAnnotation = "oci.oraclecloud.com/virtual-node-pool-id"
30
35
)
31
36
32
37
var _ cloudprovider.Instances = & CloudProvider {}
@@ -50,7 +55,7 @@ func (cp *CloudProvider) getCompartmentIDByInstanceID(instanceID string) (string
50
55
return "" , errors .Wrap (err , "error listing all the nodes using node informer" )
51
56
}
52
57
for _ , node := range nodeList {
53
- providerID , err := MapProviderIDToInstanceID (node .Spec .ProviderID )
58
+ providerID , err := MapProviderIDToResourceID (node .Spec .ProviderID )
54
59
if err != nil {
55
60
return "" , errors .New ("Failed to map providerID to instanceID" )
56
61
}
@@ -146,14 +151,18 @@ func (cp *CloudProvider) NodeAddresses(ctx context.Context, name types.NodeName)
146
151
// nodeaddresses are being queried. i.e. local metadata services cannot be used
147
152
// in this method to obtain nodeaddresses.
148
153
func (cp * CloudProvider ) NodeAddressesByProviderID (ctx context.Context , providerID string ) ([]api.NodeAddress , error ) {
149
- cp .logger .With ("instanceID " , providerID ).Debug ("Getting node addresses by provider id" )
154
+ cp .logger .With ("resourceID " , providerID ).Debug ("Getting node addresses by provider id" )
150
155
151
- instanceID , err := MapProviderIDToInstanceID (providerID )
156
+ resourceID , err := MapProviderIDToResourceID (providerID )
152
157
if err != nil {
153
- return nil , errors .Wrap (err , "MapProviderIDToInstanceID" )
158
+ return nil , errors .Wrap (err , "MapProviderIDToResourceOCID" )
159
+ }
160
+
161
+ if IsVirtualNodeId (resourceID ) {
162
+ return []api.NodeAddress {}, nil
154
163
}
155
- return cp .extractNodeAddresses (ctx , instanceID )
156
164
165
+ return cp .extractNodeAddresses (ctx , resourceID )
157
166
}
158
167
159
168
// InstanceID returns the cloud provider ID of the node with the specified NodeName.
@@ -191,21 +200,27 @@ func (cp *CloudProvider) InstanceType(ctx context.Context, name types.NodeName)
191
200
192
201
// InstanceTypeByProviderID returns the type of the specified instance.
193
202
func (cp * CloudProvider ) InstanceTypeByProviderID (ctx context.Context , providerID string ) (string , error ) {
194
- cp .logger .With ("instanceID " , providerID ).Debug ("Getting instance type by provider id" )
203
+ cp .logger .With ("resourceID " , providerID ).Debug ("Getting instance type by provider id" )
195
204
196
- instanceID , err := MapProviderIDToInstanceID (providerID )
205
+ resourceID , err := MapProviderIDToResourceID (providerID )
197
206
if err != nil {
198
- return "" , errors .Wrap (err , "MapProviderIDToInstanceID " )
207
+ return "" , errors .Wrap (err , "MapProviderIDToResourceOCID " )
199
208
}
200
- item , exists , err := cp .instanceCache .GetByKey (instanceID )
209
+
210
+ if IsVirtualNodeId (resourceID ) {
211
+ // Virtual nodes don't have an instance type, return empty string
212
+ return "" , nil
213
+ }
214
+
215
+ item , exists , err := cp .instanceCache .GetByKey (resourceID )
201
216
if err != nil {
202
217
return "" , errors .Wrap (err , "error fetching instance from instanceCache, will retry" )
203
218
}
204
219
if exists {
205
220
return * item .(* core.Instance ).Shape , nil
206
221
}
207
222
cp .logger .Debug ("Unable to find the instance information from instanceCache. Calling OCI API" )
208
- inst , err := cp .client .Compute ().GetInstance (ctx , instanceID )
223
+ inst , err := cp .client .Compute ().GetInstance (ctx , resourceID )
209
224
if err != nil {
210
225
return "" , errors .Wrap (err , "GetInstance" )
211
226
}
@@ -232,13 +247,18 @@ func (cp *CloudProvider) CurrentNodeName(ctx context.Context, hostname string) (
232
247
// provider id still is running. If false is returned with no error, the
233
248
// instance will be immediately deleted by the cloud controller manager.
234
249
func (cp * CloudProvider ) InstanceExistsByProviderID (ctx context.Context , providerID string ) (bool , error ) {
235
- //Please do not try to optimise it by using InstanceCache because we prefer correctness over efficiency here
236
- cp .logger .With ("instanceID " , providerID ).Debug ("Checking instance exists by provider id" )
237
- instanceID , err := MapProviderIDToInstanceID (providerID )
250
+ //Please do not try to optimise it by using Cache because we prefer correctness over efficiency here
251
+ cp .logger .With ("resourceID " , providerID ).Debug ("Checking instance exists by provider id" )
252
+ resourceID , err := MapProviderIDToResourceID (providerID )
238
253
if err != nil {
239
254
return false , err
240
255
}
241
- instance , err := cp .client .Compute ().GetInstance (ctx , instanceID )
256
+
257
+ if IsVirtualNodeId (resourceID ) {
258
+ return cp .virtualNodeExistsByResourceID (ctx , resourceID )
259
+ }
260
+
261
+ instance , err := cp .client .Compute ().GetInstance (ctx , resourceID )
242
262
if client .IsNotFound (err ) {
243
263
return false , nil
244
264
}
@@ -252,13 +272,18 @@ func (cp *CloudProvider) InstanceExistsByProviderID(ctx context.Context, provide
252
272
// InstanceShutdownByProviderID returns true if the instance is shutdown in cloudprovider.
253
273
func (cp * CloudProvider ) InstanceShutdownByProviderID (ctx context.Context , providerID string ) (bool , error ) {
254
274
//Please do not try to optimise it by using InstanceCache because we prefer correctness over efficiency here
255
- cp .logger .With ("instanceID " , providerID ).Debug ("Checking instance is stopped by provider id" )
256
- instanceID , err := MapProviderIDToInstanceID (providerID )
275
+ cp .logger .With ("resourceID " , providerID ).Debug ("Checking instance is stopped by provider id" )
276
+ resourceID , err := MapProviderIDToResourceID (providerID )
257
277
if err != nil {
258
278
return false , err
259
279
}
260
280
261
- instance , err := cp .client .Compute ().GetInstance (ctx , instanceID )
281
+ if IsVirtualNodeId (resourceID ) {
282
+ // This does not apply to virtual nodes
283
+ return false , nil
284
+ }
285
+
286
+ instance , err := cp .client .Compute ().GetInstance (ctx , resourceID )
262
287
if err != nil {
263
288
return false , err
264
289
}
@@ -280,3 +305,56 @@ func (cp *CloudProvider) getCompartmentIDByNodeName(nodeName string) (string, er
280
305
cp .logger .Debug ("CompartmentID annotation is not present" )
281
306
return "" , errors .New ("compartmentID annotation missing in the node. Would retry" )
282
307
}
308
+
309
+ func (cp * CloudProvider ) getVirtualNodePoolIdByVirtualNodeId (virtualNodeId string ) (string , error ) {
310
+ nodeList , err := cp .NodeLister .List (labels .Everything ())
311
+ if err != nil {
312
+ return "" , errors .Wrap (err , "error listing nodes using node informer" )
313
+ }
314
+ for _ , node := range nodeList {
315
+ resourceID , err := MapProviderIDToResourceID (node .Spec .ProviderID )
316
+ if err != nil {
317
+ // If providerId is empty ignore this node
318
+ continue
319
+ }
320
+ if virtualNodeId == resourceID {
321
+ if virtualNodePoolId , ok := node .Annotations [VirtualNodePoolIdAnnotation ]; ok && virtualNodePoolId != "" {
322
+ return virtualNodePoolId , nil
323
+ }
324
+ }
325
+ }
326
+ return "" , errors .Errorf ("could not get virtualNodePoolId for virtualNodeId %s, annotation missing" , virtualNodeId )
327
+ }
328
+
329
+ func (cp * CloudProvider ) virtualNodeExistsByResourceID (ctx context.Context , resourceID string ) (bool , error ) {
330
+ item , exists , err := cp .virtualNodeCache .GetByKey (resourceID )
331
+ if err != nil {
332
+ return false , errors .Wrap (err , "Error fetching virtual node from virtualNodeCache, will retry" )
333
+ }
334
+
335
+ var virtualNodePoolId string
336
+ if exists {
337
+ virtualNodePoolId = * item .(* containerengine.VirtualNode ).VirtualNodePoolId
338
+ } else {
339
+ virtualNodePoolId , err = cp .getVirtualNodePoolIdByVirtualNodeId (resourceID )
340
+ if err != nil {
341
+ return false , err
342
+ }
343
+ }
344
+
345
+ virtualNode , err := cp .client .ContainerEngine ().GetVirtualNode (ctx , resourceID , virtualNodePoolId )
346
+ if client .IsNotFound (err ) {
347
+ return false , nil
348
+ }
349
+ if err != nil {
350
+ return false , err
351
+ }
352
+
353
+ if ! exists {
354
+ if err := cp .virtualNodeCache .Add (virtualNode ); err != nil {
355
+ return false , errors .Wrap (err , "Failed to add virtual node in virtualNodeCache" )
356
+ }
357
+ }
358
+
359
+ return ! client .IsVirtualNodeInTerminalState (virtualNode ), nil
360
+ }
0 commit comments