Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use a gitlab repository for config-sync-repo #328

Open
wants to merge 13 commits into
base: main
Choose a base branch
from
7 changes: 2 additions & 5 deletions test/integration/cymbal-shop/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ import (
"testing"
"time"

"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/gruntwork-io/terratest/modules/shell"
"github.com/terraform-google-modules/enterprise-application/test/integration/testutils"

"github.com/gruntwork-io/terratest/modules/retry"
)
Expand All @@ -46,9 +46,6 @@ func getServiceIpAddress(t *testing.T, serviceName string, namespace string) (st
}
return shell.RunCommandAndGetStdOutE(t, kubectlCmd)
}
func connectToFleet(t *testing.T, clusterName string, location string, project string) {
gcloud.Runf(t, "container fleet memberships get-credentials %s --location=%s --project=%s", clusterName, location, project)
}

func TestCymbalShopE2E(t *testing.T) {
multitenant := tft.NewTFBlueprintTest(t, tft.WithTFDir("../../../2-multitenant/envs/development"))
Expand All @@ -61,7 +58,7 @@ func TestCymbalShopE2E(t *testing.T) {
splitClusterMembership := strings.Split(clusterMembership, "/")
clusterName := splitClusterMembership[len(splitClusterMembership)-1]

connectToFleet(t, clusterName, clusterLocation, clusterProjectId)
testutils.ConnectToFleet(t, clusterName, clusterLocation, clusterProjectId)
t.Run("Cymbal-Shop End-to-End Test", func(t *testing.T) {
jar, err := cookiejar.New(nil)
if err != nil {
Expand Down
107 changes: 86 additions & 21 deletions test/integration/fleetscope/fleetscope_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,62 @@ import (
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/gcloud"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/tft"
"github.com/GoogleCloudPlatform/cloud-foundation-toolkit/infra/blueprint-test/pkg/utils"
"github.com/gruntwork-io/terratest/modules/k8s"
"github.com/stretchr/testify/assert"
"github.com/terraform-google-modules/enterprise-application/test/integration/testutils"
)

func retrieveNamespace(t *testing.T, options *k8s.KubectlOptions) (string, error) {
return k8s.RunKubectlAndGetOutputE(t, options, "get", "ns", "config-management-system", "-o", "json")
}

func retrieveCreds(t *testing.T, options *k8s.KubectlOptions) (string, error) {
return k8s.RunKubectlAndGetOutputE(t, options, "get", "secret", "git-creds", "--namespace=config-management-system", "--output=yaml")
}

func configureConfigSyncNamespace(t *testing.T, options *k8s.KubectlOptions) (string, error) {
_, err := retrieveNamespace(t, options)
// namespace does not exist
if err != nil {
return k8s.RunKubectlAndGetOutputE(t, options, "create", "ns", "config-management-system")
} else {
fmt.Println("Namespace already exists")
return "", err
}
}

// Create token credentials on config-management-system namespace
func createTokenCredentials(t *testing.T, options *k8s.KubectlOptions, user string, token string) (string, error) {
_, err := retrieveCreds(t, options)
if err != nil {
return k8s.RunKubectlAndGetOutputE(t, options, "create", "secret", "generic", "git-creds", "--namespace=config-management-system", fmt.Sprintf("--from-literal=username=%s", user), fmt.Sprintf("--from-literal=token=%s", token))
} else {
// delete existing credentials
_, err = k8s.RunKubectlAndGetOutputE(t, options, "delete", "secret", "git-creds", "--namespace=config-management-system")
if err != nil {
t.Fatal(err)
}
// create new credentials using token
return k8s.RunKubectlAndGetOutputE(t, options, "create", "secret", "generic", "git-creds", "--namespace=config-management-system", fmt.Sprintf("--from-literal=username=%s", user), fmt.Sprintf("--from-literal=token=%s", token))
}

}

// To use config-sync with a gitlab token, the namespace and credentials (token) must exist before running fleetscope code
func applyPreRequisites(t *testing.T, options *k8s.KubectlOptions, token string) error {
_, err := configureConfigSyncNamespace(t, options)
if err != nil {
t.Fatal(err)
}

_, err = createTokenCredentials(t, options, "root", token)
if err != nil {
t.Fatal(err)
}

return err
}

func TestFleetscope(t *testing.T) {
setup := tft.NewTFBlueprintTest(t, tft.WithTFDir("../../setup"))
bootstrap := tft.NewTFBlueprintTest(t,
Expand All @@ -38,6 +90,13 @@ func TestFleetscope(t *testing.T) {
"bucket": backend_bucket,
}

gitlabSecretProject := setup.GetStringOutput("gitlab_secret_project")
gitlabPersonalTokenSecretName := setup.GetStringOutput("gitlab_pat_secret_name")
token, err := testutils.GetSecretFromSecretManager(t, gitlabPersonalTokenSecretName, gitlabSecretProject)
if err != nil {
t.Fatal(err)
}

for _, envName := range testutils.EnvNames(t) {
envName := envName
t.Run(envName, func(t *testing.T) {
Expand All @@ -47,9 +106,24 @@ func TestFleetscope(t *testing.T) {
tft.WithBackendConfig(backendConfig),
)

// retrieve cluster location and fleet membership from 2-multitenant
clusterProjectId := multitenant.GetJsonOutput("cluster_project_id").String()
clusterLocation := multitenant.GetJsonOutput("cluster_regions").Array()[0].String()
clusterMembership := multitenant.GetJsonOutput("cluster_membership_ids").Array()[0].String()

// extract clusterName from fleet membership id
splitClusterMembership := strings.Split(clusterMembership, "/")
clusterName := splitClusterMembership[len(splitClusterMembership)-1]

testutils.ConnectToFleet(t, clusterName, clusterLocation, clusterProjectId)

config_sync_url := fmt.Sprintf("%s/root/config-sync-%s.git", setup.GetStringOutput("gitlab_url"), envName)

vars := map[string]interface{}{
"remote_state_bucket": backend_bucket,
"namespace_ids": setup.GetJsonOutput("teams").Value().(map[string]interface{}),
"remote_state_bucket": backend_bucket,
"namespace_ids": setup.GetJsonOutput("teams").Value().(map[string]interface{}),
"config_sync_secret_type": "token",
"config_sync_repository_url": config_sync_url,
}

fleetscope := tft.NewTFBlueprintTest(t,
Expand All @@ -60,6 +134,15 @@ func TestFleetscope(t *testing.T) {
tft.WithParallelism(1),
)

fleetscope.DefineApply(func(assert *assert.Assertions) {
k8sOpts := k8s.NewKubectlOptions(fmt.Sprintf("gke_%s_%s_%s", clusterProjectId, clusterLocation, clusterName), "", "")
err := applyPreRequisites(t, k8sOpts, token)
if err != nil {
t.Fatal(err)
}
fleetscope.DefaultApply(assert)
})

fleetscope.DefineVerify(func(assert *assert.Assertions) {
fleetscope.DefaultVerify(assert)

Expand All @@ -69,23 +152,6 @@ func TestFleetscope(t *testing.T) {
clusterProjectID := multitenant.GetStringOutput("cluster_project_id")
clusterProjectNumber := multitenant.GetStringOutput("cluster_project_number")

// Service Account
rootReconcilerRoles := []string{"roles/source.reader"}
rootReconcilerSa := fmt.Sprintf("root-reconciler@%s.iam.gserviceaccount.com", clusterProjectID)
iamReconcilerFilter := fmt.Sprintf("bindings.members:'serviceAccount:%s'", rootReconcilerSa)
iamReconcilerCommonArgs := gcloud.WithCommonArgs([]string{"--flatten", "bindings", "--filter", iamReconcilerFilter, "--format", "json"})
projectPolicyOp := gcloud.Run(t, fmt.Sprintf("projects get-iam-policy %s", clusterProjectID), iamReconcilerCommonArgs).Array()
saReconcilerListRoles := testutils.GetResultFieldStrSlice(projectPolicyOp, "bindings.role")
assert.Subset(saReconcilerListRoles, rootReconcilerRoles, fmt.Sprintf("service account %s should have \"roles/source.reader\" project level role", rootReconcilerSa))

svcRoles := []string{"roles/iam.workloadIdentityUser"}
svcSa := fmt.Sprintf("%s.svc.id.goog[config-management-system/root-reconciler]", clusterProjectID)
iamSvcFilter := fmt.Sprintf("bindings.members:serviceAccount:'%s'", svcSa)
iamSvcCommonArgs := gcloud.WithCommonArgs([]string{"--flatten", "bindings", "--filter", iamSvcFilter, "--format", "json"})
svcPolicyOp := gcloud.Run(t, fmt.Sprintf("iam service-accounts get-iam-policy %s --project %s", rootReconcilerSa, clusterProjectID), iamSvcCommonArgs).Array()
saSvcListRoles := testutils.GetResultFieldStrSlice(svcPolicyOp, "bindings.role")
assert.Subset(saSvcListRoles, svcRoles, fmt.Sprintf("service account %s should have \"roles/iam.workloadIdentityUser\" project level role", svcSa))

membershipNames := []string{}
for _, region := range clusterRegions {
membershipName := fmt.Sprintf("projects/%[1]s/locations/%[2]s/memberships/cluster-%[2]s-%[3]s", clusterProjectID, region, envName)
Expand Down Expand Up @@ -126,10 +192,9 @@ func TestFleetscope(t *testing.T) {
membershipName := fmt.Sprintf("projects/%[1]s/locations/%[2]s/memberships/cluster-%[2]s-%[3]s", fleetProjectNumber, region, envName)
configmanagementPath := fmt.Sprintf("membershipSpecs.%s.configmanagement", membershipName)

assert.Equal("gcpserviceaccount", gkeFeatureOp.Get(configmanagementPath+".configSync.git.secretType").String(), fmt.Sprintf("Hub Feature %s should have git secret type equal to gcpserviceaccount", membershipName))
assert.Equal("token", gkeFeatureOp.Get(configmanagementPath+".configSync.git.secretType").String(), fmt.Sprintf("Hub Feature %s should have git secret type equal to 'token'", membershipName))
assert.Equal("unstructured", gkeFeatureOp.Get(configmanagementPath+".configSync.sourceFormat").String(), fmt.Sprintf("Hub Feature %s should have source format equal to unstructured", membershipName))
assert.Equal("1.19.0", gkeFeatureOp.Get(configmanagementPath+".version").String(), fmt.Sprintf("Hub Feature %s should have source format equal to unstructured", membershipName))
assert.Equal(rootReconcilerSa, gkeFeatureOp.Get(configmanagementPath+".configSync.git.gcpServiceAccountEmail").String(), fmt.Sprintf("Hub Feature %s should have git service account type equal to %s", membershipName, rootReconcilerSa))
}
}
case "policycontroller":
Expand Down
71 changes: 71 additions & 0 deletions test/integration/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,64 @@ require (
)

require (
filippo.io/edwards25519 v1.1.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/alexflint/go-filemutex v1.3.0 // indirect
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
github.com/aws/aws-sdk-go-v2 v1.32.5 // indirect
github.com/aws/aws-sdk-go-v2/aws/protocol/eventstream v1.6.7 // indirect
github.com/aws/aws-sdk-go-v2/config v1.28.5 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.46 // indirect
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.20 // indirect
github.com/aws/aws-sdk-go-v2/feature/s3/manager v1.17.41 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.24 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.24 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.1 // indirect
github.com/aws/aws-sdk-go-v2/internal/v4a v1.3.24 // indirect
github.com/aws/aws-sdk-go-v2/service/acm v1.30.6 // indirect
github.com/aws/aws-sdk-go-v2/service/autoscaling v1.51.0 // indirect
github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs v1.44.0 // indirect
github.com/aws/aws-sdk-go-v2/service/dynamodb v1.37.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ec2 v1.193.0 // indirect
github.com/aws/aws-sdk-go-v2/service/ecr v1.36.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ecs v1.52.0 // indirect
github.com/aws/aws-sdk-go-v2/service/iam v1.38.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.12.1 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/checksum v1.4.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/endpoint-discovery v1.10.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.12.5 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/s3shared v1.18.5 // indirect
github.com/aws/aws-sdk-go-v2/service/kms v1.37.6 // indirect
github.com/aws/aws-sdk-go-v2/service/lambda v1.69.0 // indirect
github.com/aws/aws-sdk-go-v2/service/rds v1.91.0 // indirect
github.com/aws/aws-sdk-go-v2/service/route53 v1.46.2 // indirect
github.com/aws/aws-sdk-go-v2/service/s3 v1.69.0 // indirect
github.com/aws/aws-sdk-go-v2/service/secretsmanager v1.34.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sns v1.33.6 // indirect
github.com/aws/aws-sdk-go-v2/service/sqs v1.37.1 // indirect
github.com/aws/aws-sdk-go-v2/service/ssm v1.56.0 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.24.6 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.28.5 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.33.1 // indirect
github.com/aws/smithy-go v1.22.1 // indirect
github.com/bgentry/go-netrc v0.0.0-20140422174119-9fd32a8b3d3d // indirect
github.com/boombuler/barcode v1.0.1 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.5 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/go-errors/errors v1.5.0 // indirect
github.com/go-logr/logr v1.4.2 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-sql-driver/mysql v1.8.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.5.4 // indirect
github.com/google/gnostic-models v0.6.8 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/go-querystring v1.1.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/gruntwork-io/go-commons v0.17.1 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-getter/v2 v2.2.3 // indirect
Expand All @@ -37,35 +82,61 @@ require (
github.com/hashicorp/hcl/v2 v2.22.0 // indirect
github.com/hashicorp/terraform-config-inspect v0.0.0-20241129133400-c404f8227ea6 // indirect
github.com/hashicorp/terraform-json v0.24.0 // indirect
github.com/imdario/mergo v0.3.16 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
github.com/jackc/pgx/v5 v5.7.1 // indirect
github.com/jackc/puddle/v2 v2.2.2 // indirect
github.com/jinzhu/copier v0.4.0 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
github.com/mattn/go-shellwords v1.0.12 // indirect
github.com/mattn/go-zglob v0.0.4 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mitchellh/go-testing-interface v1.14.2-0.20210821155943-2d9075ca8770 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/moby/spdystream v0.2.0 // indirect
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/otiai10/mint v1.6.3 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/pquerna/otp v1.4.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
github.com/tidwall/sjson v1.2.5 // indirect
github.com/tmccombs/hcl2json v0.6.4 // indirect
github.com/ulikunitz/xz v0.5.11 // indirect
github.com/urfave/cli/v2 v2.25.7 // indirect
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 // indirect
github.com/zclconf/go-cty v1.15.1 // indirect
golang.org/x/crypto v0.31.0 // indirect
golang.org/x/mod v0.22.0 // indirect
golang.org/x/net v0.33.0 // indirect
golang.org/x/oauth2 v0.24.0 // indirect
golang.org/x/sync v0.10.0 // indirect
golang.org/x/sys v0.28.0 // indirect
golang.org/x/term v0.27.0 // indirect
golang.org/x/text v0.21.0 // indirect
golang.org/x/time v0.8.0 // indirect
golang.org/x/tools v0.22.0 // indirect
google.golang.org/protobuf v1.36.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
k8s.io/api v0.28.4 // indirect
k8s.io/apimachinery v0.28.4 // indirect
k8s.io/client-go v0.28.4 // indirect
k8s.io/klog/v2 v2.100.1 // indirect
k8s.io/kube-openapi v0.0.0-20231010175941-2dd684a91f00 // indirect
k8s.io/utils v0.0.0-20230726121419-3b25d923346b // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/kyaml v0.18.1 // indirect
sigs.k8s.io/structured-merge-diff/v4 v4.3.0 // indirect
sigs.k8s.io/yaml v1.4.0 // indirect
)
Loading
Loading