|
|
@@ -19,6 +19,7 @@ import (
|
|
|
"context"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
+ "strings"
|
|
|
|
|
|
secretmanager "cloud.google.com/go/secretmanager/apiv1"
|
|
|
|
|
|
@@ -32,6 +33,7 @@ import (
|
|
|
"golang.org/x/oauth2/jwt"
|
|
|
"google.golang.org/api/option"
|
|
|
v1 "k8s.io/api/core/v1"
|
|
|
+ apierrors "k8s.io/apimachinery/pkg/api/errors"
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
utilpointer "k8s.io/utils/pointer"
|
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
|
@@ -53,10 +55,73 @@ type GcpProvider struct {
|
|
|
clusterLocation string
|
|
|
clusterName string
|
|
|
controllerClass string
|
|
|
+ access gcpAccessConfig
|
|
|
+}
|
|
|
+
|
|
|
+type gcpAccessConfig struct {
|
|
|
+ Credentials string
|
|
|
+ ProjectID string
|
|
|
+ ServiceAccountName string
|
|
|
+ ClusterLocation string
|
|
|
+ ClusterName string
|
|
|
+}
|
|
|
+
|
|
|
+func newGCPAccessConfigFromEnv() gcpAccessConfig {
|
|
|
+ return gcpAccessConfig{
|
|
|
+ Credentials: os.Getenv("GCP_SERVICE_ACCOUNT_KEY"),
|
|
|
+ ProjectID: os.Getenv("GCP_FED_PROJECT_ID"),
|
|
|
+ ServiceAccountName: os.Getenv("GCP_KSA_NAME"),
|
|
|
+ ClusterLocation: os.Getenv("GCP_FED_REGION"),
|
|
|
+ ClusterName: os.Getenv("GCP_GKE_CLUSTER"),
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func (c gcpAccessConfig) missingStaticEnv() []string {
|
|
|
+ var missing []string
|
|
|
+ if c.Credentials == "" {
|
|
|
+ missing = append(missing, "GCP_SERVICE_ACCOUNT_KEY")
|
|
|
+ }
|
|
|
+ if c.ProjectID == "" {
|
|
|
+ missing = append(missing, "GCP_FED_PROJECT_ID")
|
|
|
+ }
|
|
|
+ return missing
|
|
|
+}
|
|
|
+
|
|
|
+func (c gcpAccessConfig) missingManagedEnv() []string {
|
|
|
+ var missing []string
|
|
|
+ if c.ServiceAccountName == "" {
|
|
|
+ missing = append(missing, "GCP_KSA_NAME")
|
|
|
+ }
|
|
|
+ if c.ClusterLocation == "" {
|
|
|
+ missing = append(missing, "GCP_FED_REGION")
|
|
|
+ }
|
|
|
+ if c.ClusterName == "" {
|
|
|
+ missing = append(missing, "GCP_GKE_CLUSTER")
|
|
|
+ }
|
|
|
+ return missing
|
|
|
+}
|
|
|
+
|
|
|
+func skipIfGCPStaticEnvMissing(access gcpAccessConfig) {
|
|
|
+ if missing := access.missingStaticEnv(); len(missing) > 0 {
|
|
|
+ Skip("missing GCP e2e environment: " + strings.Join(missing, ", "))
|
|
|
+ }
|
|
|
+}
|
|
|
+
|
|
|
+func skipIfGCPManagedEnvMissing(access gcpAccessConfig) {
|
|
|
+ if missing := access.missingManagedEnv(); len(missing) > 0 {
|
|
|
+ Skip("missing GCP managed identity environment: " + strings.Join(missing, ", "))
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
func NewGCPProvider(f *framework.Framework, credentials, projectID string,
|
|
|
clusterLocation string, clusterName string, serviceAccountName string, serviceAccountNamespace string, controllerClass string) *GcpProvider {
|
|
|
+ access := gcpAccessConfig{
|
|
|
+ Credentials: credentials,
|
|
|
+ ProjectID: projectID,
|
|
|
+ ServiceAccountName: serviceAccountName,
|
|
|
+ ClusterLocation: clusterLocation,
|
|
|
+ ClusterName: clusterName,
|
|
|
+ }
|
|
|
prov := &GcpProvider{
|
|
|
credentials: credentials,
|
|
|
projectID: projectID,
|
|
|
@@ -66,30 +131,22 @@ func NewGCPProvider(f *framework.Framework, credentials, projectID string,
|
|
|
ServiceAccountName: serviceAccountName,
|
|
|
ServiceAccountNamespace: serviceAccountNamespace,
|
|
|
controllerClass: controllerClass,
|
|
|
+ access: access,
|
|
|
}
|
|
|
|
|
|
BeforeEach(func() {
|
|
|
+ skipIfGCPStaticEnvMissing(prov.access)
|
|
|
prov.CreateSAKeyStore()
|
|
|
prov.CreateReferentSAKeyStore()
|
|
|
- prov.CreateSpecifcSASecretStore()
|
|
|
- prov.CreatePodIDStore()
|
|
|
- })
|
|
|
-
|
|
|
- AfterEach(func() {
|
|
|
- prov.DeleteSpecifcSASecretStore()
|
|
|
})
|
|
|
|
|
|
return prov
|
|
|
}
|
|
|
|
|
|
func NewFromEnv(f *framework.Framework, controllerClass string) *GcpProvider {
|
|
|
- projectID := os.Getenv("GCP_FED_PROJECT_ID")
|
|
|
- credentials := os.Getenv("GCP_SERVICE_ACCOUNT_KEY")
|
|
|
- serviceAccountName := os.Getenv("GCP_KSA_NAME")
|
|
|
+ access := newGCPAccessConfigFromEnv()
|
|
|
serviceAccountNamespace := "default"
|
|
|
- clusterLocation := os.Getenv("GCP_FED_REGION")
|
|
|
- clusterName := os.Getenv("GCP_GKE_CLUSTER")
|
|
|
- return NewGCPProvider(f, credentials, projectID, clusterLocation, clusterName, serviceAccountName, serviceAccountNamespace, controllerClass)
|
|
|
+ return NewGCPProvider(f, access.Credentials, access.ProjectID, access.ClusterLocation, access.ClusterName, access.ServiceAccountName, serviceAccountNamespace, controllerClass)
|
|
|
}
|
|
|
|
|
|
func (s *GcpProvider) getClient(ctx context.Context) (client *secretmanager.Client, err error) {
|
|
|
@@ -286,5 +343,8 @@ func (s *GcpProvider) DeleteSpecifcSASecretStore() {
|
|
|
Name: s.SAClusterSecretStoreName(),
|
|
|
},
|
|
|
})
|
|
|
+ if apierrors.IsNotFound(err) {
|
|
|
+ return
|
|
|
+ }
|
|
|
Expect(err).ToNot(HaveOccurred())
|
|
|
}
|