@@ -4,13 +4,14 @@ import (
44 "context"
55 "errors"
66 "fmt"
7- clocktesting "k8s.io/utils/clock/testing"
87 "os"
98 "reflect"
109 "strconv"
1110 "testing"
1211 "time"
1312
13+ clocktesting "k8s.io/utils/clock/testing"
14+
1415 apierrors "k8s.io/apimachinery/pkg/api/errors"
1516 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1617 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
@@ -325,15 +326,29 @@ func (fake *fakeDynamicClient) ApplyStatus(ctx context.Context, name string, obj
325326}
326327
327328func 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+
328341 tests := []struct {
329342 name string
343+ credentialsRequest * unstructured.Unstructured
330344 cloudCredential * opv1.CloudCredential
331345 envVars map [string ]string
332346 expectedShouldSync bool
333347 expectedError bool
334348 }{
335349 {
336- name : "Default mode" ,
350+ name : "Default mode" ,
351+ credentialsRequest : defaultCR ,
337352 cloudCredential : & opv1.CloudCredential {
338353 ObjectMeta : metav1.ObjectMeta {
339354 Name : clusterCloudCredentialName ,
@@ -346,7 +361,8 @@ func TestShouldSync(t *testing.T) {
346361 expectedError : false ,
347362 },
348363 {
349- name : "Manual mode without short-term credentials" ,
364+ name : "Manual mode without short-term credentials" ,
365+ credentialsRequest : defaultCR ,
350366 cloudCredential : & opv1.CloudCredential {
351367 ObjectMeta : metav1.ObjectMeta {
352368 Name : clusterCloudCredentialName ,
@@ -359,7 +375,8 @@ func TestShouldSync(t *testing.T) {
359375 expectedError : false ,
360376 },
361377 {
362- name : "Manual mode with AWS STS enabled" ,
378+ name : "Manual mode with AWS STS enabled" ,
379+ credentialsRequest : defaultCR ,
363380 cloudCredential : & opv1.CloudCredential {
364381 ObjectMeta : metav1.ObjectMeta {
365382 Name : clusterCloudCredentialName ,
@@ -375,7 +392,8 @@ func TestShouldSync(t *testing.T) {
375392 expectedError : false ,
376393 },
377394 {
378- name : "Manual mode with GCP WIF enabled" ,
395+ name : "Manual mode with GCP WIF enabled" ,
396+ credentialsRequest : defaultCR ,
379397 cloudCredential : & opv1.CloudCredential {
380398 ObjectMeta : metav1.ObjectMeta {
381399 Name : clusterCloudCredentialName ,
@@ -394,7 +412,8 @@ func TestShouldSync(t *testing.T) {
394412 expectedError : false ,
395413 },
396414 {
397- name : "Manual mode with partial GCP WIF configuration" ,
415+ name : "Manual mode with partial GCP WIF configuration" ,
416+ credentialsRequest : defaultCR ,
398417 cloudCredential : & opv1.CloudCredential {
399418 ObjectMeta : metav1.ObjectMeta {
400419 Name : clusterCloudCredentialName ,
@@ -413,10 +432,113 @@ func TestShouldSync(t *testing.T) {
413432 },
414433 {
415434 name : "Error getting cloud credential" ,
435+ credentialsRequest : defaultCR ,
416436 cloudCredential : nil ,
417437 expectedShouldSync : false ,
418438 expectedError : true ,
419439 },
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" : "test@example.com" ,
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" : "test@example.com" ,
538+ },
539+ expectedShouldSync : false , // NODE_PROJECT_NUMBER is not set
540+ expectedError : false ,
541+ },
420542 }
421543
422544 for _ , tc := range tests {
@@ -443,7 +565,7 @@ func TestShouldSync(t *testing.T) {
443565 }()
444566
445567 // Act
446- shouldSync , err := shouldSync (cloudCredentialInformer .Operator ().V1 ().CloudCredentials ().Lister ())
568+ shouldSync , err := shouldSync (cloudCredentialInformer .Operator ().V1 ().CloudCredentials ().Lister (), tc . credentialsRequest )
447569
448570 // Assert
449571 if tc .expectedError && err == nil {
0 commit comments