@@ -4,13 +4,14 @@ import (
4
4
"context"
5
5
"errors"
6
6
"fmt"
7
- clocktesting "k8s.io/utils/clock/testing"
8
7
"os"
9
8
"reflect"
10
9
"strconv"
11
10
"testing"
12
11
"time"
13
12
13
+ clocktesting "k8s.io/utils/clock/testing"
14
+
14
15
apierrors "k8s.io/apimachinery/pkg/api/errors"
15
16
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
16
17
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -325,15 +326,29 @@ func (fake *fakeDynamicClient) ApplyStatus(ctx context.Context, name string, obj
325
326
}
326
327
327
328
func TestShouldSync (t * testing.T ) {
329
+ defaultCRManifest := makeFakeManifest (operandName , credentialRequestNamespace , operandNamespace )
330
+ defaultCR := resourceread .ReadCredentialRequestsOrDie (defaultCRManifest )
331
+
332
+ emptyAnnotationCR := defaultCR .DeepCopy ()
333
+ emptyAnnotationCR .SetAnnotations (map [string ]string {EnvVarsAnnotationKey : "" })
334
+
335
+ singleEnvVarAnnotationCR := emptyAnnotationCR .DeepCopy ()
336
+ singleEnvVarAnnotationCR .SetAnnotations (map [string ]string {EnvVarsAnnotationKey : "NODE_ROLEARN" })
337
+
338
+ multipleEnvVarAnnotationCR := emptyAnnotationCR .DeepCopy ()
339
+ multipleEnvVarAnnotationCR .SetAnnotations (map [string ]string {EnvVarsAnnotationKey : "NODE_POOL_ID,NODE_PROVIDER_ID,NODE_SERVICE_ACCOUNT_EMAIL,NODE_PROJECT_NUMBER" })
340
+
328
341
tests := []struct {
329
342
name string
343
+ credentialsRequest * unstructured.Unstructured
330
344
cloudCredential * opv1.CloudCredential
331
345
envVars map [string ]string
332
346
expectedShouldSync bool
333
347
expectedError bool
334
348
}{
335
349
{
336
- name : "Default mode" ,
350
+ name : "Default mode" ,
351
+ credentialsRequest : defaultCR ,
337
352
cloudCredential : & opv1.CloudCredential {
338
353
ObjectMeta : metav1.ObjectMeta {
339
354
Name : clusterCloudCredentialName ,
@@ -346,7 +361,8 @@ func TestShouldSync(t *testing.T) {
346
361
expectedError : false ,
347
362
},
348
363
{
349
- name : "Manual mode without short-term credentials" ,
364
+ name : "Manual mode without short-term credentials" ,
365
+ credentialsRequest : defaultCR ,
350
366
cloudCredential : & opv1.CloudCredential {
351
367
ObjectMeta : metav1.ObjectMeta {
352
368
Name : clusterCloudCredentialName ,
@@ -359,7 +375,8 @@ func TestShouldSync(t *testing.T) {
359
375
expectedError : false ,
360
376
},
361
377
{
362
- name : "Manual mode with AWS STS enabled" ,
378
+ name : "Manual mode with AWS STS enabled" ,
379
+ credentialsRequest : defaultCR ,
363
380
cloudCredential : & opv1.CloudCredential {
364
381
ObjectMeta : metav1.ObjectMeta {
365
382
Name : clusterCloudCredentialName ,
@@ -375,7 +392,8 @@ func TestShouldSync(t *testing.T) {
375
392
expectedError : false ,
376
393
},
377
394
{
378
- name : "Manual mode with GCP WIF enabled" ,
395
+ name : "Manual mode with GCP WIF enabled" ,
396
+ credentialsRequest : defaultCR ,
379
397
cloudCredential : & opv1.CloudCredential {
380
398
ObjectMeta : metav1.ObjectMeta {
381
399
Name : clusterCloudCredentialName ,
@@ -394,7 +412,8 @@ func TestShouldSync(t *testing.T) {
394
412
expectedError : false ,
395
413
},
396
414
{
397
- name : "Manual mode with partial GCP WIF configuration" ,
415
+ name : "Manual mode with partial GCP WIF configuration" ,
416
+ credentialsRequest : defaultCR ,
398
417
cloudCredential : & opv1.CloudCredential {
399
418
ObjectMeta : metav1.ObjectMeta {
400
419
Name : clusterCloudCredentialName ,
@@ -413,10 +432,113 @@ func TestShouldSync(t *testing.T) {
413
432
},
414
433
{
415
434
name : "Error getting cloud credential" ,
435
+ credentialsRequest : defaultCR ,
416
436
cloudCredential : nil ,
417
437
expectedShouldSync : false ,
418
438
expectedError : true ,
419
439
},
440
+ {
441
+ name : "Empty annotation" ,
442
+ credentialsRequest : emptyAnnotationCR ,
443
+ cloudCredential : & opv1.CloudCredential {
444
+ ObjectMeta : metav1.ObjectMeta {
445
+ Name : clusterCloudCredentialName ,
446
+ },
447
+ Spec : opv1.CloudCredentialSpec {
448
+ CredentialsMode : opv1 .CloudCredentialsModeManual ,
449
+ },
450
+ },
451
+ expectedShouldSync : false , // CredentialsRequest has the annotation, but it's empty
452
+ expectedError : false ,
453
+ },
454
+ {
455
+ name : "Single annotation with env. var set" ,
456
+ credentialsRequest : singleEnvVarAnnotationCR ,
457
+ cloudCredential : & opv1.CloudCredential {
458
+ ObjectMeta : metav1.ObjectMeta {
459
+ Name : clusterCloudCredentialName ,
460
+ },
461
+ Spec : opv1.CloudCredentialSpec {
462
+ CredentialsMode : opv1 .CloudCredentialsModeManual ,
463
+ },
464
+ },
465
+ envVars : map [string ]string {
466
+ "NODE_ROLEARN" : "arn:aws:iam::123456789012:role/test-role" ,
467
+ },
468
+ expectedShouldSync : true , // The env. var is set, so we should sync
469
+ expectedError : false ,
470
+ },
471
+ {
472
+ name : "Single annotation with env. var unset" ,
473
+ credentialsRequest : singleEnvVarAnnotationCR ,
474
+ cloudCredential : & opv1.CloudCredential {
475
+ ObjectMeta : metav1.ObjectMeta {
476
+ Name : clusterCloudCredentialName ,
477
+ },
478
+ Spec : opv1.CloudCredentialSpec {
479
+ CredentialsMode : opv1 .CloudCredentialsModeManual ,
480
+ },
481
+ },
482
+ envVars : map [string ]string {},
483
+ expectedShouldSync : false ,
484
+ expectedError : false ,
485
+ },
486
+ {
487
+ name : "Single annotation with ROLEARN env. var set" ,
488
+ credentialsRequest : singleEnvVarAnnotationCR ,
489
+ cloudCredential : & opv1.CloudCredential {
490
+ ObjectMeta : metav1.ObjectMeta {
491
+ Name : clusterCloudCredentialName ,
492
+ },
493
+ Spec : opv1.CloudCredentialSpec {
494
+ CredentialsMode : opv1 .CloudCredentialsModeManual ,
495
+ },
496
+ },
497
+ envVars : map [string ]string {
498
+ "ROLEARN" : "arn:aws:iam::123456789012:role/test-role" ,
499
+ },
500
+ expectedShouldSync : false , // The CredentialsRequests annotation asked for NODE_ROLEARN and that one is not set. It takes precedence over ROLEARN.
501
+ expectedError : false ,
502
+ },
503
+ {
504
+ name : "Multiple annotations with env. var set" ,
505
+ credentialsRequest : multipleEnvVarAnnotationCR ,
506
+ cloudCredential : & opv1.CloudCredential {
507
+ ObjectMeta : metav1.ObjectMeta {
508
+ Name : clusterCloudCredentialName ,
509
+ },
510
+ Spec : opv1.CloudCredentialSpec {
511
+ CredentialsMode : opv1 .CloudCredentialsModeManual ,
512
+ },
513
+ },
514
+ envVars : map [string ]string {
515
+ "NODE_POOL_ID" : "test-pool" ,
516
+ "NODE_PROVIDER_ID" : "test-provider" ,
517
+ "NODE_SERVICE_ACCOUNT_EMAIL" :
"[email protected] " ,
518
+ "NODE_PROJECT_NUMBER" : "123456789" ,
519
+ },
520
+ expectedShouldSync : true , // All env. var are set
521
+ expectedError : false ,
522
+ },
523
+ {
524
+ name : "Multiple annotations with some env. var unset" ,
525
+ credentialsRequest : multipleEnvVarAnnotationCR ,
526
+ cloudCredential : & opv1.CloudCredential {
527
+ ObjectMeta : metav1.ObjectMeta {
528
+ Name : clusterCloudCredentialName ,
529
+ },
530
+ Spec : opv1.CloudCredentialSpec {
531
+ CredentialsMode : opv1 .CloudCredentialsModeManual ,
532
+ },
533
+ },
534
+ envVars : map [string ]string {
535
+ "NODE_POOL_ID" : "test-pool" ,
536
+ "NODE_PROVIDER_ID" : "test-provider" ,
537
+ "NODE_SERVICE_ACCOUNT_EMAIL" :
"[email protected] " ,
538
+ },
539
+ expectedShouldSync : false , // NODE_PROJECT_NUMBER is not set
540
+ expectedError : false ,
541
+ },
420
542
}
421
543
422
544
for _ , tc := range tests {
@@ -443,7 +565,7 @@ func TestShouldSync(t *testing.T) {
443
565
}()
444
566
445
567
// Act
446
- shouldSync , err := shouldSync (cloudCredentialInformer .Operator ().V1 ().CloudCredentials ().Lister ())
568
+ shouldSync , err := shouldSync (cloudCredentialInformer .Operator ().V1 ().CloudCredentials ().Lister (), tc . credentialsRequest )
447
569
448
570
// Assert
449
571
if tc .expectedError && err == nil {
0 commit comments