From 10da7436756ff807787c328dd6a708398a2d86b1 Mon Sep 17 00:00:00 2001 From: Manoj Surudwad Date: Sat, 9 Nov 2024 01:04:27 +0530 Subject: [PATCH] feat: allows specific regional location image to be created for GCP (#1184) * bug: allows specific regional location image to be created for GCP * fix: revert go releaser changes * ci: update GHA secret for GCP credentials * build: set image storage location for gcp * ci: use GCP infra varaible fro GHA --------- Co-authored-by: Shalin Patel --- .github/workflows/gcp-e2e.yaml | 5 +++- .github/workflows/release-gcp-template.yaml | 3 +++ .../cmd/create-package-bundle.go | 2 ++ cmd/konvoy-image/cmd/gcp.go | 7 +++++ docs/cli/konvoy-image_build_gcp.md | 27 ++++++++++--------- magefile.go | 5 +++- pkg/app/build_gcp.go | 7 ++--- pkg/app/config.go | 4 +++ pkg/app/constants.go | 7 ++--- pkg/packer/manifests/gcp/packer.pkr.hcl | 8 +++++- 10 files changed, 53 insertions(+), 22 deletions(-) diff --git a/.github/workflows/gcp-e2e.yaml b/.github/workflows/gcp-e2e.yaml index 06ba902a2..077d13351 100644 --- a/.github/workflows/gcp-e2e.yaml +++ b/.github/workflows/gcp-e2e.yaml @@ -57,7 +57,7 @@ jobs: echo -n "${GOOGLE_APPLICATION_CREDENTIALS_E2E_BASE64}" | base64 --decode >> google-credentials.json echo "GOOGLE_APPLICATION_CREDENTIALS=google-credentials.json" >> $GITHUB_ENV env: - GOOGLE_APPLICATION_CREDENTIALS_E2E_BASE64: ${{ secrets.GOOGLE_APPLICATION_CREDENTIALS_E2E_BASE64 }} + GOOGLE_APPLICATION_CREDENTIALS_E2E_BASE64: ${{ secrets.GCP_NCN_CI_SERVICE_ACCOUNT_B64 }} - name: Run E2E test for ${{ matrix.os }} with ${{ matrix.buildConfig }} configuration uses: magefile/mage-action@v3 @@ -66,3 +66,6 @@ jobs: args: runE2e "${{ matrix.os }}" "${{ matrix.buildConfig }}" gcp true env: GITHUB_TOKEN: ${{ secrets.MESOSPHERECI_USER_TOKEN }} + GCP_PROJECT: ${{ vars.GCP_PROJECT }} + GCP_NETWORK: ${{ vars.GCP_NETWORK }} + GCP_IMAGE_LOCATIONS: ${{ vars.GCP_IMAGE_LOCATIONS }} diff --git a/.github/workflows/release-gcp-template.yaml b/.github/workflows/release-gcp-template.yaml index 980b5f5ca..413c5f58d 100644 --- a/.github/workflows/release-gcp-template.yaml +++ b/.github/workflows/release-gcp-template.yaml @@ -66,3 +66,6 @@ jobs: args: runE2e "${{ matrix.os }}" "${{ matrix.buildConfig }}" gcp false env: GITHUB_TOKEN: ${{ secrets.MESOSPHERECI_USER_TOKEN }} + GCP_PROJECT: ${{ vars.GCP_PROJECT }} + GCP_NETWORK: ${{ vars.GCP_NETWORK }} + GCP_IMAGE_LOCATIONS: ${{ vars.GCP_IMAGE_LOCATIONS }} diff --git a/cmd/konvoy-image-wrapper/cmd/create-package-bundle.go b/cmd/konvoy-image-wrapper/cmd/create-package-bundle.go index 534e7a301..e134894b1 100644 --- a/cmd/konvoy-image-wrapper/cmd/create-package-bundle.go +++ b/cmd/konvoy-image-wrapper/cmd/create-package-bundle.go @@ -81,6 +81,7 @@ func getKubernetesVerisonFromAnsible() (string, error) { return kubeVersion, nil } +//nolint:funlen // no need to split this function func (r *Runner) CreatePackageBundle(args []string) error { var ( osFlag string @@ -161,6 +162,7 @@ func (r *Runner) CreatePackageBundle(args []string) error { fetchKubernetesRPMs = false } if eusReposFlag { + //nolint:goconst // this is a flag r.env["EUS_REPOS"] = "true" } if fetchKubernetesRPMs { diff --git a/cmd/konvoy-image/cmd/gcp.go b/cmd/konvoy-image/cmd/gcp.go index 0ebb02c61..72cce6669 100644 --- a/cmd/konvoy-image/cmd/gcp.go +++ b/cmd/konvoy-image/cmd/gcp.go @@ -72,6 +72,13 @@ func addGCPArgs(fs *flag.FlagSet, gcp *app.GCPArgs) { "the project id to use when storing created image", ) + fs.StringSliceVar( + &gcp.ImageLocation, + "image-storage-locations", + []string{}, + "the location to use when storing created image", + ) + fs.StringVar( &gcp.Network, "network", diff --git a/docs/cli/konvoy-image_build_gcp.md b/docs/cli/konvoy-image_build_gcp.md index a5c946233..2dfc377e7 100644 --- a/docs/cli/konvoy-image_build_gcp.md +++ b/docs/cli/konvoy-image_build_gcp.md @@ -15,19 +15,20 @@ gcp ... images/gcp/centos-79.yaml ### Options ``` - --containerd-version string the version of containerd to install - --dry-run do not create artifacts, or delete them after creating. Recommended for tests. - --extra-vars strings flag passed Ansible's extra-vars - -h, --help help for gcp - --kubernetes-version string The version of kubernetes to install. Example: 1.21.6 - --network string the network to use when creating an image - --overrides strings a comma separated list of override YAML files - --packer-manifest string provide the path to a custom packer manifest - --packer-on-error string [advanced] set error strategy for packer. strategies [cleanup, abort, run-cleanup-provisioner] - --packer-path string the location of the packer binary (default "packer") - --project-id string the project id to use when storing created image - --region string the region in which to launch the instance (default "us-west1") - --work-dir string path to custom work directory generated by the generate command + --containerd-version string the version of containerd to install + --dry-run do not create artifacts, or delete them after creating. Recommended for tests. + --extra-vars strings flag passed Ansible's extra-vars + -h, --help help for gcp + --image-storage-locations strings the location to use when storing created image + --kubernetes-version string The version of kubernetes to install. Example: 1.21.6 + --network string the network to use when creating an image + --overrides strings a comma separated list of override YAML files + --packer-manifest string provide the path to a custom packer manifest + --packer-on-error string [advanced] set error strategy for packer. strategies [cleanup, abort, run-cleanup-provisioner] + --packer-path string the location of the packer binary (default "packer") + --project-id string the project id to use when storing created image + --region string the region in which to launch the instance (default "us-west1") + --work-dir string path to custom work directory generated by the generate command ``` ### Options inherited from parent commands diff --git a/magefile.go b/magefile.go index 19b9dfbd0..30da22ea3 100644 --- a/magefile.go +++ b/magefile.go @@ -182,7 +182,10 @@ func RunE2e(buildOS, buildConfig, buildInfra string, dryRun bool) error { } // extra args for gcp if buildInfra == gcp { - args = append(args, "--project-id=eng-ksphere-platform-e2e", "--network=kib-ssh-network") + args = append(args, + fmt.Sprintf("--project-id=%s", os.Getenv("GCP_PROJECT")), + fmt.Sprintf("--network=%s", os.Getenv("GCP_NETWORK")), + fmt.Sprintf("--image-storage-locations=%s", os.Getenv("GCP_IMAGE_LOCATIONS"))) } fmt.Printf("Running %s with args %v\n", wrapperCmd, args) return sh.RunV(wrapperCmd, args...) diff --git a/pkg/app/build_gcp.go b/pkg/app/build_gcp.go index 0500c7b75..a52325cc9 100644 --- a/pkg/app/build_gcp.go +++ b/pkg/app/build_gcp.go @@ -6,9 +6,10 @@ import ( ) type GCPArgs struct { - ProjectID string // the project ID to which the source VM belongs. - Region string // the region where the source VM is located. - Network string // the network in which to load image creation, should have . + ProjectID string // the project ID to which the source VM belongs. + Region string // the region where the source VM is located. + Network string // the network in which to load image creation, should have . + ImageLocation []string // the location where the image will be stored. } func ensureGCP() error { diff --git a/pkg/app/config.go b/pkg/app/config.go index b22b61393..eac7eb75b 100644 --- a/pkg/app/config.go +++ b/pkg/app/config.go @@ -585,6 +585,10 @@ func MergeGCPUserArgs(config Config, gcpArgs *GCPArgs) error { return fmt.Errorf("failed to set %s: %w", PackerGCPProjectIDPath, err) } + if err := config.Set(PackerGCPImageLocationPath, gcpArgs.ImageLocation); err != nil { + return fmt.Errorf("failed to set %s: %w", PackerGCPImageLocationPath, err) + } + if err := config.Set(PackerGCPNetworkPath, gcpArgs.Network); err != nil { return fmt.Errorf("failed to set %s: %w", PackerGCPNetworkPath, err) } diff --git a/pkg/app/constants.go b/pkg/app/constants.go index 969c20a69..609903b51 100644 --- a/pkg/app/constants.go +++ b/pkg/app/constants.go @@ -49,9 +49,10 @@ const ( PackerAzureTenantIDPath = "/packer/tenant_id" PackerAzureCloudEndpointPath = "/packer/cloud_environment_name" - PackerGCPProjectIDPath = "/packer/project_id" - PackerGCPNetworkPath = "/packer/network" - PackerGCPRegionPath = "/packer/region" + PackerGCPProjectIDPath = "/packer/project_id" + PackerGCPNetworkPath = "/packer/network" + PackerGCPRegionPath = "/packer/region" + PackerGCPImageLocationPath = "/packer/image_storage_locations" PackerVSphereTemplatePath = "/packer/template" PackerVSphereClusterPath = "/packer/cluster" diff --git a/pkg/packer/manifests/gcp/packer.pkr.hcl b/pkg/packer/manifests/gcp/packer.pkr.hcl index 9e3d574af..b8e35c6ec 100644 --- a/pkg/packer/manifests/gcp/packer.pkr.hcl +++ b/pkg/packer/manifests/gcp/packer.pkr.hcl @@ -80,6 +80,11 @@ variable "project_id" { default = "" } +variable "image_storage_locations" { + type = string + default = "" +} + variable "region" { type = string default = "" @@ -238,6 +243,7 @@ locals { ansible_extra_vars = "${var.ansible_extra_vars}" build_timestamp = local.timestamp zone = "${var.region}-a" + image_storage_locations = split(",", var.image_storage_locations) generated_image_name = "konvoy-${var.build_name}-${var.kubernetes_full_version}-${local.build_timestamp}" # clean_resource_name https://github.com/hashicorp/packer-plugin-googlecompute/blob/81d8d5a740c0d7fb0b02be93133ac17a11557f34/builder/googlecompute/template_funcs.go#L20 image_name = regex_replace(lower(local.generated_image_name), "[^-a-z0-9]", "-") @@ -267,6 +273,7 @@ source "googlecompute" "kib_image" { image_name = local.image_name network = var.network project_id = var.project_id + image_storage_locations = local.image_storage_locations region = var.region source_image = var.source_image source_image_family = var.distribution_family @@ -274,7 +281,6 @@ source "googlecompute" "kib_image" { ssh_username = var.ssh_username wait_to_add_ssh_keys = "20s" zone = local.zone - skip_create_image = var.dry_run }