Skip to content

Use previewct get-credentials in werft job #14091

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

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .werft/jobs/build/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export const GCLOUD_SERVICE_ACCOUNT_PATH = "/mnt/secrets/gcp-sa/service-account.
export const CORE_DEV_KUBECONFIG_PATH = "/workspace/gitpod/kubeconfigs/core-dev";
export const HARVESTER_KUBECONFIG_PATH = "/workspace/gitpod/kubeconfigs/harvester";
export const PREVIEW_K3S_KUBECONFIG_PATH = "/workspace/gitpod/kubeconfigs/k3s";
export const GLOBAL_KUBECONFIG_PATH = process.env.HOME + "/.kube/config"
20 changes: 17 additions & 3 deletions .werft/jobs/build/prepare.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,18 @@
import {exec, execStream} from "../../util/shell";
import { Werft } from "../../util/werft";
import { CORE_DEV_KUBECONFIG_PATH, GCLOUD_SERVICE_ACCOUNT_PATH, HARVESTER_KUBECONFIG_PATH } from "./const";
import {
CORE_DEV_KUBECONFIG_PATH,
GCLOUD_SERVICE_ACCOUNT_PATH,
GLOBAL_KUBECONFIG_PATH,
HARVESTER_KUBECONFIG_PATH
} from "./const";
import { JobConfig } from "./job-config";
import {certReady} from "../../util/certs";
import {vmExists} from "../../vm/vm";

const phaseName = "prepare";
const prepareSlices = {
CONFIGURE_K8S: "Configuring k8s access.",
CONFIGURE_CORE_DEV: "Configuring core-dev access.",
BOOT_VM: "Booting VM.",
WAIT_CERTIFICATES: "Waiting for certificates to be ready for the preview.",
Expand All @@ -19,6 +25,7 @@ export async function prepare(werft: Werft, config: JobConfig) {
activateCoreDevServiceAccount();
configureDocker();
configureStaticClustersAccess();
configureGlobalKubernetesContext();
werft.done(prepareSlices.CONFIGURE_CORE_DEV);
if (!config.withPreview)
{
Expand Down Expand Up @@ -53,6 +60,14 @@ function configureDocker() {
}
}

function configureGlobalKubernetesContext() {
const rc = exec(`previewctl get-credentials --gcp-service-account=${GCLOUD_SERVICE_ACCOUNT_PATH} --kube-save-path=${GLOBAL_KUBECONFIG_PATH}`, { slice: prepareSlices.CONFIGURE_K8S }).code;

if (rc != 0) {
throw new Error("Failed to configure global kubernetes context.");
}
}

function configureStaticClustersAccess() {
const rcCoreDev = exec(
`KUBECONFIG=${CORE_DEV_KUBECONFIG_PATH} gcloud container clusters get-credentials core-dev --zone europe-west1-b --project gitpod-core-dev`,
Expand Down Expand Up @@ -90,8 +105,7 @@ async function createVM(werft: Werft, config: JobConfig) {
// We pass the GCP credentials explicitly, otherwise for some reason TF doesn't pick them up
const commonVars = `GOOGLE_BACKEND_CREDENTIALS=${GCLOUD_SERVICE_ACCOUNT_PATH} \
GOOGLE_APPLICATION_CREDENTIALS=${GCLOUD_SERVICE_ACCOUNT_PATH} \
TF_VAR_dev_kube_path=${CORE_DEV_KUBECONFIG_PATH} \
TF_VAR_harvester_kube_path=${HARVESTER_KUBECONFIG_PATH} \
TF_VAR_kubeconfig_path=${GLOBAL_KUBECONFIG_PATH} \
TF_VAR_preview_name=${config.previewEnvironment.destname} \
TF_VAR_vm_cpu=${cpu} \
TF_VAR_vm_memory=${memory}Gi \
Expand Down
9 changes: 6 additions & 3 deletions .werft/util/certs.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import {exec, ExecOptions, execStream} from "./shell";
import {CORE_DEV_KUBECONFIG_PATH, GCLOUD_SERVICE_ACCOUNT_PATH, HARVESTER_KUBECONFIG_PATH} from "../jobs/build/const";
import {
CORE_DEV_KUBECONFIG_PATH,
GCLOUD_SERVICE_ACCOUNT_PATH,
GLOBAL_KUBECONFIG_PATH,
} from "../jobs/build/const";
import { Werft } from "./werft";
import { reportCertificateError } from "../util/slack";
import {JobConfig} from "../jobs/build/job-config";
Expand All @@ -21,8 +25,7 @@ export async function certReady(werft: Werft, config: JobConfig, slice: string):
// We pass the GCP credentials explicitly, otherwise for some reason TF doesn't pick them up
const commonVars = `GOOGLE_BACKEND_CREDENTIALS=${GCLOUD_SERVICE_ACCOUNT_PATH} \
GOOGLE_APPLICATION_CREDENTIALS=${GCLOUD_SERVICE_ACCOUNT_PATH} \
TF_VAR_dev_kube_path=${CORE_DEV_KUBECONFIG_PATH} \
TF_VAR_harvester_kube_path=${HARVESTER_KUBECONFIG_PATH} \
TF_VAR_kubeconfig_path=${GLOBAL_KUBECONFIG_PATH} \
TF_VAR_preview_name=${config.previewEnvironment.destname} \
TF_VAR_vm_cpu=${cpu} \
TF_VAR_vm_memory=${memory}Gi \
Expand Down
5 changes: 2 additions & 3 deletions .werft/vm/vm.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
CORE_DEV_KUBECONFIG_PATH,
GCLOUD_SERVICE_ACCOUNT_PATH,
GLOBAL_KUBECONFIG_PATH,
HARVESTER_KUBECONFIG_PATH,
PREVIEW_K3S_KUBECONFIG_PATH
} from "../jobs/build/const";
Expand All @@ -19,8 +19,7 @@ export async function deleteVM(options: { name: string }) {
await execStream(`DESTROY=true \
GOOGLE_APPLICATION_CREDENTIALS=${GCLOUD_SERVICE_ACCOUNT_PATH} \
GOOGLE_BACKEND_CREDENTIALS=${GCLOUD_SERVICE_ACCOUNT_PATH} \
TF_VAR_dev_kube_path=${CORE_DEV_KUBECONFIG_PATH} \
TF_VAR_harvester_kube_path=${HARVESTER_KUBECONFIG_PATH} \
TF_VAR_kubeconfig_path=${GLOBAL_KUBECONFIG_PATH} \
TF_VAR_preview_name=${options.name} \
./dev/preview/workflow/preview/deploy-harvester.sh`,
{slice: "Deleting VM."})
Expand Down
17 changes: 10 additions & 7 deletions dev/preview/infrastructure/harvester/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ terraform {
required_providers {
harvester = {
source = "harvester/harvester"
version = ">=0.5.1"
version = ">=0.5.3"
}
k8s = {
source = "hashicorp/kubernetes"
Expand All @@ -23,18 +23,21 @@ terraform {
}

provider "harvester" {
alias = "harvester"
kubeconfig = var.harvester_kube_path
alias = "harvester"
kubeconfig = var.kubeconfig_path
kubecontext = "harvester"
}

provider "k8s" {
alias = "dev"
config_path = var.dev_kube_path
alias = "dev"
config_path = var.kubeconfig_path
config_context = var.dev_kube_context
}

provider "k8s" {
alias = "harvester"
config_path = var.harvester_kube_path
alias = "harvester"
config_path = var.kubeconfig_path
config_context = var.harvester_kube_context
}

provider "google" {
Expand Down
16 changes: 12 additions & 4 deletions dev/preview/infrastructure/harvester/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,22 @@ variable "preview_name" {
description = "The preview environment's name"
}

variable "harvester_kube_path" {
variable "kubeconfig_path" {
type = string
description = "The path to the Harvester Cluster kubeconfig"
default = "/home/gitpod/.kube/config"
description = "The path to the kubernetes config"
}

variable "dev_kube_path" {
variable "harvester_kube_context" {
type = string
description = "The path to the Dev Cluster kubeconfig"
default = "harvester"
description = "The name of the harvester kube context"
}

variable "dev_kube_context" {
type = string
default = "dev"
description = "The name of the dev kube context"
}

variable "vm_memory" {
Expand Down