sigs.k8s.io/cluster-api-provider-aws@v1.5.5/test/e2e/shared/identity.go (about)

     1  //go:build e2e
     2  // +build e2e
     3  
     4  /*
     5  Copyright 2021 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11  	http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package shared
    21  
    22  import (
    23  	"context"
    24  
    25  	. "github.com/onsi/gomega"
    26  	corev1 "k8s.io/api/core/v1"
    27  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    28  
    29  	infrav1 "sigs.k8s.io/cluster-api-provider-aws/api/v1beta1"
    30  )
    31  
    32  const (
    33  	credsSecretName = "e2e-account-creds" //nolint:gosec
    34  	capaNamespace   = "capa-system"
    35  	idName          = "e2e-account"
    36  )
    37  
    38  func SetupStaticCredentials(e2eCtx *E2EContext) {
    39  	Expect(e2eCtx).NotTo(BeNil(), "e2eCtx is required for SetupStaticCredentials")
    40  	Expect(e2eCtx.Environment.BootstrapAccessKey).NotTo(BeNil(), "e2eCtx.Environment.BootstrapAccessKey is required for SetupStaticCredentials")
    41  
    42  	ctx := context.TODO()
    43  	secret := &corev1.Secret{
    44  		ObjectMeta: metav1.ObjectMeta{
    45  			Name:      credsSecretName,
    46  			Namespace: capaNamespace,
    47  		},
    48  		StringData: map[string]string{
    49  			"AccessKeyID":     *e2eCtx.Environment.BootstrapAccessKey.AccessKeyId,
    50  			"SecretAccessKey": *e2eCtx.Environment.BootstrapAccessKey.SecretAccessKey,
    51  		},
    52  	}
    53  
    54  	client := e2eCtx.Environment.BootstrapClusterProxy.GetClient()
    55  	Byf("Creating credentials secret %s in namespace %s", secret.Name, secret.Namespace)
    56  	Eventually(func() error {
    57  		return client.Create(ctx, secret)
    58  	}, e2eCtx.E2EConfig.GetIntervals("", "wait-create-identity")...).Should(Succeed())
    59  
    60  	id := &infrav1.AWSClusterStaticIdentity{
    61  		ObjectMeta: metav1.ObjectMeta{
    62  			Name: idName,
    63  		},
    64  		Spec: infrav1.AWSClusterStaticIdentitySpec{
    65  			SecretRef: credsSecretName,
    66  			AWSClusterIdentitySpec: infrav1.AWSClusterIdentitySpec{
    67  				AllowedNamespaces: &infrav1.AllowedNamespaces{},
    68  			},
    69  		},
    70  	}
    71  
    72  	Byf("Creating AWSClusterStaticIdentity %s", id.Name)
    73  	Eventually(func() error {
    74  		return client.Create(ctx, id)
    75  	}, e2eCtx.E2EConfig.GetIntervals("", "wait-create-identity")...).Should(Succeed())
    76  }
    77  
    78  func CleanupStaticCredentials(ctx context.Context, e2eCtx *E2EContext) {
    79  	Expect(ctx).NotTo(BeNil(), "ctx is required for SetupStaticCredentials")
    80  	Expect(e2eCtx).NotTo(BeNil(), "e2eCtx is required for SetupStaticCredentials")
    81  
    82  	id := &infrav1.AWSClusterStaticIdentity{
    83  		ObjectMeta: metav1.ObjectMeta{
    84  			Name: idName,
    85  		},
    86  	}
    87  
    88  	Byf("Deleting AWSClusterStaticIdentity %s", idName)
    89  	client := e2eCtx.Environment.BootstrapClusterProxy.GetClient()
    90  	Eventually(func() error {
    91  		return client.Delete(ctx, id)
    92  	}, e2eCtx.E2EConfig.GetIntervals("", "wait-create-identity")...).Should(Succeed())
    93  
    94  	//NOTE: secrets should be cleared up when the namespaces are deleted
    95  }