Skip to content

Commit b774d7a

Browse files
vulkoingimroboquat
authored andcommitted
Clean up bash scripts in favour of previewctl
1 parent 5400e26 commit b774d7a

18 files changed

+93
-155
lines changed

.werft/jobs/build/deploy-to-preview-environment.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import * as fs from "fs";
22
import { exec } from "../../util/shell";
33
import { MonitoringSatelliteInstaller } from "../../observability/monitoring-satellite";
4-
54
import { Werft } from "../../util/werft";
65
import { Analytics, JobConfig } from "./job-config";
76
import * as VM from "../../vm/vm";
87
import { Installer } from "./installer/installer";
9-
import { previewNameFromBranchName } from "../../util/preview";
108

119
// used by Installer
1210
const STACKDRIVER_SERVICEACCOUNT = JSON.parse(
@@ -82,7 +80,7 @@ export async function deployToPreviewEnvironment(werft: Werft, jobConfig: JobCon
8280
const sliceID = "Install monitoring satellite";
8381
const monitoringSatelliteInstaller = new MonitoringSatelliteInstaller({
8482
branch: jobConfig.observability.branch,
85-
previewName: previewNameFromBranchName(jobConfig.repository.branch),
83+
previewName: exec(`previewctl get name --branch=${jobConfig.repository.branch}`).stdout.trim(),
8684
stackdriverServiceAccount: STACKDRIVER_SERVICEACCOUNT,
8785
werft: werft,
8886
});

.werft/jobs/build/installer/installer.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export class Installer {
2424
const environment = {
2525
VERSION: this.options.version,
2626
DEV_KUBE_PATH: CORE_DEV_KUBECONFIG_PATH,
27-
DEV_KUBE_CONTEXT: "gke_gitpod-core-dev_europe-west1-b_core-dev",
27+
DEV_KUBE_CONTEXT: "dev",
2828
PREVIEW_K3S_KUBE_PATH: PREVIEW_K3S_KUBECONFIG_PATH,
2929
PREVIEW_NAME: this.options.previewName,
3030
GITPOD_ANALYTICS: this.options.analytics,

.werft/jobs/build/prepare.ts

Lines changed: 11 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
1-
import {exec, execStream} from "../../util/shell";
2-
import { Werft } from "../../util/werft";
3-
import {
4-
CORE_DEV_KUBECONFIG_PATH,
5-
GCLOUD_SERVICE_ACCOUNT_PATH,
6-
GLOBAL_KUBECONFIG_PATH,
7-
HARVESTER_KUBECONFIG_PATH
8-
} from "./const";
9-
import { JobConfig } from "./job-config";
1+
import {execStream} from "../../util/shell";
2+
import {Werft} from "../../util/werft";
3+
import {GCLOUD_SERVICE_ACCOUNT_PATH} from "./const";
4+
import {JobConfig} from "./job-config";
105
import {certReady} from "../../util/certs";
116
import {vmExists} from "../../vm/vm";
7+
import {configureAccess, configureDocker} from "../../util/preview";
128

139
const phaseName = "prepare";
1410
const prepareSlices = {
@@ -22,14 +18,10 @@ export async function prepare(werft: Werft, config: JobConfig) {
2218
werft.phase(phaseName);
2319
try {
2420
werft.log(prepareSlices.CONFIGURE_CORE_DEV, prepareSlices.CONFIGURE_CORE_DEV);
25-
activateCoreDevServiceAccount();
21+
await configureAccess(werft)
2622
configureDocker();
27-
await installPreviewCTL();
28-
configureStaticClustersAccess();
29-
configureGlobalKubernetesContext();
3023
werft.done(prepareSlices.CONFIGURE_CORE_DEV);
31-
if (!config.withPreview)
32-
{
24+
if (!config.withPreview) {
3325
return
3426
}
3527
await decideHarvesterVMCreation(werft, config);
@@ -40,68 +32,9 @@ export async function prepare(werft: Werft, config: JobConfig) {
4032
werft.done(phaseName);
4133
}
4234

43-
function activateCoreDevServiceAccount() {
44-
const rc = exec(`gcloud auth activate-service-account --key-file "${GCLOUD_SERVICE_ACCOUNT_PATH}"`, {
45-
slice: prepareSlices.CONFIGURE_CORE_DEV,
46-
}).code;
47-
48-
if (rc != 0) {
49-
throw new Error("Failed to activate core-dev service account.");
50-
}
51-
}
52-
53-
function configureDocker() {
54-
const rcDocker = exec("gcloud auth configure-docker --quiet", { slice: prepareSlices.CONFIGURE_CORE_DEV }).code;
55-
const rcDockerRegistry = exec("gcloud auth configure-docker europe-docker.pkg.dev --quiet", {
56-
slice: prepareSlices.CONFIGURE_CORE_DEV,
57-
}).code;
58-
59-
if (rcDocker != 0 || rcDockerRegistry != 0) {
60-
throw new Error("Failed to configure docker with gcloud.");
61-
}
62-
}
63-
64-
function configureGlobalKubernetesContext() {
65-
const rc = exec(`KUBECONFIG=${GLOBAL_KUBECONFIG_PATH} previewctl get-credentials --gcp-service-account=${GCLOUD_SERVICE_ACCOUNT_PATH}`, { slice: prepareSlices.CONFIGURE_K8S }).code;
66-
67-
if (rc != 0) {
68-
throw new Error("Failed to configure global kubernetes context.");
69-
}
70-
}
71-
72-
export async function installPreviewCTL() {
73-
try {
74-
await execStream(`leeway build dev/preview/previewctl:install -Dversion=$(date +%F_T%H-%M-%S) --dont-test`, {
75-
slice: "Install previewctl",
76-
dontCheckRc: false
77-
})
78-
}catch (e) {
79-
throw new Error("Failed to install previewctl.");
80-
}
81-
}
82-
83-
function configureStaticClustersAccess() {
84-
const rcCoreDev = exec(
85-
`KUBECONFIG=${CORE_DEV_KUBECONFIG_PATH} gcloud container clusters get-credentials core-dev --zone europe-west1-b --project gitpod-core-dev`,
86-
{ slice: prepareSlices.CONFIGURE_CORE_DEV },
87-
).code;
88-
if (rcCoreDev != 0) {
89-
throw new Error("Failed to get core-dev kubeconfig credentials.");
90-
}
91-
92-
const rcHarvester = exec(
93-
`cp /mnt/secrets/harvester-kubeconfig/harvester-kubeconfig.yml ${HARVESTER_KUBECONFIG_PATH}`,
94-
{ slice: prepareSlices.CONFIGURE_CORE_DEV },
95-
).code;
96-
97-
if (rcHarvester != 0) {
98-
throw new Error("Failed to get Harvester kubeconfig credentials.");
99-
}
100-
}
101-
10235
async function decideHarvesterVMCreation(werft: Werft, config: JobConfig) {
10336
// always try to create - usually it will be no-op, but if tf changed for any reason we would reconcile
104-
if (config.withPreview && (!vmExists({ name: config.previewEnvironment.destname }) || config.cleanSlateDeployment || config.recreatePreview || config.recreateVm)) {
37+
if (config.withPreview && (!vmExists({name: config.previewEnvironment.destname}) || config.cleanSlateDeployment || config.recreatePreview || config.recreateVm)) {
10538
await createVM(werft, config);
10639
}
10740
werft.done(prepareSlices.BOOT_VM);
@@ -123,7 +56,7 @@ async function createVM(werft: Werft, config: JobConfig) {
12356
"TF_VAR_vm_memory": `${memory}Gi`,
12457
}
12558

126-
if (config.storageClass.length > 0){
59+
if (config.storageClass.length > 0) {
12760
environment["TF_VAR_vm_storage_class"] = config.storageClass
12861
}
12962

@@ -133,11 +66,11 @@ async function createVM(werft: Werft, config: JobConfig) {
13366
.map(([key, value]) => `${key}="${value}"`)
13467
.join(" ")
13568

136-
if (config.recreatePreview){
69+
if (config.recreatePreview) {
13770
werft.log(prepareSlices.BOOT_VM, "Recreating environment");
13871
await execStream(`${variables} \
13972
leeway run dev/preview:delete-preview`, {slice: prepareSlices.BOOT_VM});
140-
}else if (config.cleanSlateDeployment || config.recreateVm) {
73+
} else if (config.cleanSlateDeployment || config.recreateVm) {
14174
werft.log(prepareSlices.BOOT_VM, "Cleaning previously created VM");
14275
// -replace=... forces recreation of the resource
14376
await execStream(`${variables} \

.werft/observability/monitoring-satellite.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class MonitoringSatelliteInstaller {
1818
public async install(slice: string) {
1919
const environment = {
2020
DEV_KUBE_PATH: CORE_DEV_KUBECONFIG_PATH,
21-
DEV_KUBE_CONTEXT: "gke_gitpod-core-dev_europe-west1-b_core-dev",
21+
DEV_KUBE_CONTEXT: "dev",
2222
PREVIEW_K3S_KUBE_PATH: PREVIEW_K3S_KUBECONFIG_PATH,
2323
PREVIEW_NAME: this.options.previewName,
2424
};

.werft/platform-delete-preview-environment.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {Werft} from "./util/werft";
22
import * as Tracing from "./observability/tracing";
3-
import {configureGlobalKubernetesContext, HarvesterPreviewEnvironment} from "./util/preview";
3+
import {configureAccess, HarvesterPreviewEnvironment} from "./util/preview";
44
import {SpanStatusCode} from "@opentelemetry/api";
55
import * as fs from "fs";
66

@@ -49,7 +49,7 @@ async function deletePreviewEnvironment() {
4949
}
5050
werft.done(SLICES.VALIDATE_CONFIGURATION);
5151

52-
await configureGlobalKubernetesContext(werft)
52+
await configureAccess(werft)
5353

5454
const preview = new HarvesterPreviewEnvironment(werft, previewName);
5555
if (DRY_RUN) {

.werft/platform-delete-preview-environments-cron.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import {Werft} from "./util/werft";
22
import * as Tracing from "./observability/tracing";
33
import {SpanStatusCode} from "@opentelemetry/api";
44
import {exec, execStream} from "./util/shell";
5-
import {configureGlobalKubernetesContext, HarvesterPreviewEnvironment, PreviewEnvironment} from "./util/preview";
5+
import {configureAccess, HarvesterPreviewEnvironment, PreviewEnvironment} from "./util/preview";
66
import {GCLOUD_SERVICE_ACCOUNT_PATH} from "./jobs/build/const";
77
import * as fs from "fs";
88

@@ -66,7 +66,7 @@ async function getStalePreviewEnvironments(slice: string): Promise<PreviewEnviro
6666
}
6767

6868
async function deletePreviewEnvironments() {
69-
await configureGlobalKubernetesContext(werft)
69+
await configureAccess(werft)
7070

7171
werft.phase("Fetching stale preview environments");
7272
let stalePreviews: PreviewEnvironment[];

.werft/util/preview.ts

Lines changed: 59 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,18 @@
11
import {createHash} from "crypto";
22
import * as VM from "../vm/vm";
3-
import {exec} from "./shell";
3+
import {exec, execStream} from "./shell";
44
import {Werft} from "./werft";
5-
import {installPreviewCTL} from "../jobs/build/prepare";
6-
import {GCLOUD_SERVICE_ACCOUNT_PATH, GLOBAL_KUBECONFIG_PATH} from "../jobs/build/const";
5+
import {
6+
CORE_DEV_KUBECONFIG_PATH,
7+
GCLOUD_SERVICE_ACCOUNT_PATH,
8+
GLOBAL_KUBECONFIG_PATH,
9+
HARVESTER_KUBECONFIG_PATH
10+
} from "../jobs/build/const";
711

812
const SLICES = {
13+
CONFIGURE_DOCKER: "Configuring Docker",
914
CONFIGURE_GCP_ACCESS: "Activating service account",
10-
CONFIGURE_K8S_ACCESS: "Installing dev/harvester contexts account",
15+
CONFIGURE_K8S_ACCESS: "Installing dev/harvester contexts",
1116
INSTALL_PREVIEWCTL: "Install previewctl",
1217
};
1318

@@ -71,7 +76,7 @@ export class HarvesterPreviewEnvironment {
7176
}
7277
}
7378

74-
export async function configureGlobalKubernetesContext(werft: Werft) {
79+
export async function configureAccess(werft: Werft) {
7580
werft.phase("Configure access");
7681
try {
7782
exec(`gcloud auth activate-service-account --key-file "${GCLOUD_SERVICE_ACCOUNT_PATH}"`, {
@@ -82,15 +87,59 @@ export async function configureGlobalKubernetesContext(werft: Werft) {
8287
werft.fail(SLICES.CONFIGURE_GCP_ACCESS, err);
8388
}
8489

85-
await installPreviewCTL()
90+
try {
91+
await installPreviewCTL()
92+
} catch (e) {
93+
throw new Error("Failed to install Previewctl")
94+
}
95+
96+
try {
97+
exec(`KUBECONFIG=${GLOBAL_KUBECONFIG_PATH} previewctl get-credentials --gcp-service-account=${GCLOUD_SERVICE_ACCOUNT_PATH}`, {
98+
slice: SLICES.CONFIGURE_K8S_ACCESS
99+
});
100+
101+
exec(`mkdir -p $(dirname ${HARVESTER_KUBECONFIG_PATH})`)
102+
103+
exec(
104+
`kubectl --context=harvester config view --minify --flatten > ${HARVESTER_KUBECONFIG_PATH}`, {
105+
slice: SLICES.CONFIGURE_K8S_ACCESS
106+
},
107+
)
108+
109+
exec(
110+
`kubectl --context=dev config view --minify --flatten > ${CORE_DEV_KUBECONFIG_PATH}`, {
111+
slice: SLICES.CONFIGURE_K8S_ACCESS
112+
},
113+
)
114+
werft.done(SLICES.CONFIGURE_K8S_ACCESS);
115+
} catch (e) {
116+
werft.fail(SLICES.CONFIGURE_K8S_ACCESS, e);
117+
throw new Error("Failed to configure kubernetes contexts");
118+
}
119+
120+
werft.done("Configure access");
121+
}
122+
123+
export async function installPreviewCTL() {
124+
try {
125+
await execStream(`leeway build dev/preview/previewctl:install -Dversion=$(date +%F_T%H-%M-%S) --dont-test`, {
126+
slice: "Install previewctl",
127+
})
128+
} catch (e) {
129+
throw new Error("Failed to install previewctl.");
130+
}
131+
}
86132

87-
const rc = exec(`KUBECONFIG=${GLOBAL_KUBECONFIG_PATH} previewctl get-credentials --gcp-service-account=${GCLOUD_SERVICE_ACCOUNT_PATH}`, {
88-
slice: SLICES.CONFIGURE_K8S_ACCESS
133+
export function configureDocker() {
134+
const rcDocker = exec("gcloud auth configure-docker --quiet", {slice: SLICES.CONFIGURE_DOCKER}).code;
135+
const rcDockerRegistry = exec("gcloud auth configure-docker europe-docker.pkg.dev --quiet", {
136+
slice: SLICES.CONFIGURE_DOCKER,
89137
}).code;
90138

91-
if (rc != 0) {
92-
throw new Error("Failed to configure global kubernetes context.");
139+
if (rcDocker != 0 || rcDockerRegistry != 0) {
140+
throw new Error("Failed to configure docker with gcloud.");
93141
}
94142
}
95143

144+
96145
export type PreviewEnvironment = HarvesterPreviewEnvironment;

.werft/vm/vm.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,10 +69,12 @@ export function waitForVMReadiness(options: { name: string; timeoutSeconds: numb
6969
export async function installPreviewContext(options: { name: string; slice: string }) {
7070
try {
7171
await execStream(
72-
`previewctl install-context --branch=${options.name} --timeout=10m`,
72+
`previewctl install-context --private-key-path=/workspace/.ssh/id_rsa_harvester_vm --gcp-service-account=${GCLOUD_SERVICE_ACCOUNT_PATH} --branch=${options.name} --timeout=10m`,
7373
{slice: options.slice},
7474
);
7575

76+
exec(`mkdir -p $(dirname ${PREVIEW_K3S_KUBECONFIG_PATH})`)
77+
7678
exec(
7779
`kubectl --context=${options.name} config view --minify --flatten > ${PREVIEW_K3S_KUBECONFIG_PATH}`,
7880
{dontCheckRc: true, slice: options.slice},

dev/preview/BUILD.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ scripts:
3030
export TF_VAR_dev_kube_context="${TF_VAR_dev_kube_context:-dev}"
3131
export TF_VAR_harvester_kube_path="${TF_VAR_harvester_kube_path:-$HOME/.kube/config}"
3232
export TF_VAR_harvester_kube_context="${TF_VAR_harvester_kube_context:-harvester}"
33-
export TF_VAR_preview_name="${TF_VAR_preview_name:-$(previewctl get-name)}"
33+
export TF_VAR_preview_name="${TF_VAR_preview_name:-$(previewctl get name)}"
3434
export TF_VAR_vm_cpu="${TF_VAR_vm_cpu:-6}"
3535
export TF_VAR_vm_memory="${TF_VAR_vm_memory:-12Gi}"
3636
export TF_VAR_vm_storage_class="${TF_VAR_vm_storage_class:-longhorn-gitpod-k3s-202209251218-onereplica}"
@@ -43,7 +43,7 @@ scripts:
4343
export GOOGLE_APPLICATION_CREDENTIALS="${GOOGLE_APPLICATION_CREDENTIALS:-$PREVIEW_ENV_DEV_SA_KEY_PATH}"
4444
export GOOGLE_BACKEND_CREDENTIALS="${GOOGLE_BACKEND_CREDENTIALS:-$PREVIEW_ENV_DEV_SA_KEY_PATH}"
4545
export TF_VAR_kubeconfig_path="${TF_VAR_kubeconfig_path:-$HOME/.kube/config}"
46-
export TF_VAR_preview_name="${TF_VAR_preview_name:-$(previewctl get-name)}"
46+
export TF_VAR_preview_name="${TF_VAR_preview_name:-$(previewctl get name)}"
4747
./workflow/preview/deploy-harvester.sh
4848
4949
- name: deploy-gitpod

dev/preview/install-k3s-kubeconfig.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ set -euo pipefail
77

88
THIS_DIR="$(dirname "$0")"
99

10-
source "$THIS_DIR/util/preview-name-from-branch.sh"
11-
1210
PRIVATE_KEY=$HOME/.ssh/vm_id_rsa
1311
PUBLIC_KEY=$HOME/.ssh/vm_id_rsa.pub
1412
USER="ubuntu"
@@ -30,9 +28,9 @@ done
3028

3129
if [ -z "${VM_NAME:-}" ]; then
3230
if [[ "${BRANCH}" == "" ]]; then
33-
VM_NAME="$(preview-name-from-branch)"
31+
VM_NAME="$(previewctl get name)"
3432
else
35-
VM_NAME="$(preview-name-from-branch "$BRANCH")"
33+
VM_NAME="$(previewctl get name --branch "$BRANCH")"
3634
fi
3735
fi
3836

dev/preview/previewctl/pkg/preview/status.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,10 @@ func (c *Config) GetStatus(ctx context.Context) (Status, error) {
4444
if c.vmiCreationTime.After(time.Now().Add(-120 * time.Minute)) {
4545
logEntry.WithFields(log.Fields{
4646
"created": c.vmiCreationTime,
47-
}).Debug("VM created in the past 20 mins, assuming active")
47+
}).Debug("VM created in the past 120 mins, assuming active")
4848

4949
c.status.Active = true
50-
c.status.Reason = fmt.Sprintf("VM created in the past 20 mins, assuming active: [%v]", c.vmiCreationTime.Time)
50+
c.status.Reason = fmt.Sprintf("VM created in the past 120 mins, assuming active: [%v]", c.vmiCreationTime.Time)
5151
return c.status, nil
5252
}
5353

dev/preview/ssh-vm.sh

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ set -euo pipefail
77

88
THIS_DIR="$(dirname "$0")"
99

10-
source "$THIS_DIR/util/preview-name-from-branch.sh"
11-
1210
PRIVATE_KEY=$HOME/.ssh/vm_id_rsa
1311
PUBLIC_KEY=$HOME/.ssh/vm_id_rsa.pub
1412
PORT=8022
@@ -30,9 +28,9 @@ done
3028

3129
if [ -z "${VM_NAME:-}" ]; then
3230
if [[ "${BRANCH}" == "" ]]; then
33-
VM_NAME="$(preview-name-from-branch)"
31+
VM_NAME="$(previewctl get name)"
3432
else
35-
VM_NAME="$(preview-name-from-branch "$BRANCH")"
33+
VM_NAME="$(previewctl get name --branch "$BRANCH")"
3634
fi
3735
fi
3836

dev/preview/util/portforward-monitoring-satellite-core-dev.sh

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55

66
THIS_DIR="$(dirname "$0")"
77

8-
source "$THIS_DIR/preview-name-from-branch.sh"
9-
10-
PREVIEW_NAME="$(preview-name-from-branch)"
8+
PREVIEW_NAME="$(previewctl get name)"
119
NAMESPACE="staging-${PREVIEW_NAME}"
1210

1311
function log {

0 commit comments

Comments
 (0)