Skip to content

Commit f693c9f

Browse files
Add an evergreen function to build kubectl plugin and push to S3
1 parent b29fb4a commit f693c9f

File tree

4 files changed

+185
-1
lines changed

4 files changed

+185
-1
lines changed

.evergreen-functions.yml

Lines changed: 49 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -488,12 +488,60 @@ functions:
488488
- rh_pyxis
489489
binary: scripts/evergreen/run_python.sh scripts/preflight_images.py --image ${image_name} --submit "${preflight_submit}"
490490

491+
# build_multi_cluster_binary:
492+
# - command: subprocess.exec
493+
# type: setup
494+
# params:
495+
# working_dir: src/github.com/mongodb/mongodb-kubernetes
496+
# binary: scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh
497+
498+
install_macnotary:
499+
- command: shell.exec
500+
type: setup
501+
params:
502+
include_expansions_in_env:
503+
- notary_service_url
504+
script: |
505+
set -Eeu pipefail
506+
507+
curl "${notary_service_url}" --output macos-notary.zip
508+
unzip -u macos-notary.zip
509+
chmod 755 ./linux_amd64/macnotary
510+
511+
install_goreleaser:
512+
- command: shell.exec
513+
type: setup
514+
include_expansions_in_env:
515+
- goreleaser_pro_tar_gz
516+
params:
517+
working_dir: src/github.com/mongodb/mongodb-kubernetes
518+
script: |
519+
set -Eeu pipefail
520+
curl -fL "${goreleaser_pro_tar_gz}" --output goreleaser_Linux_x86_64.tar.gz
521+
tar -xf goreleaser_Linux_x86_64.tar.gz
522+
chmod 755 ./goreleaser
523+
491524
build_multi_cluster_binary:
492525
- command: subprocess.exec
493526
type: setup
494527
params:
528+
include_expansions_in_env:
529+
- github_commit
530+
- GRS_USERNAME
531+
- GRS_PASSWORD
532+
- PKCS11_URI
533+
- ARTIFACTORY_URL
534+
- ARTIFACTORY_PASSWORD
535+
- SIGNING_IMAGE_URI
536+
- macos_notary_keyid
537+
- macos_notary_secret
538+
- workdir
539+
- triggered_by_git_tag
540+
env:
541+
MACOS_NOTARY_KEY: ${macos_notary_keyid}
542+
MACOS_NOTARY_SECRET: ${macos_notary_secret}
495543
working_dir: src/github.com/mongodb/mongodb-kubernetes
496-
binary: scripts/evergreen/build_multi_cluster_kubeconfig_creator.sh
544+
binary: scripts/evergreen/run_python.sh scripts/release/kubectl-mongodb/python/build_kubectl_plugin.py
497545

498546
build_and_push_appdb_database:
499547
- command: subprocess.exec

.evergreen.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ variables:
8383
- func: clone
8484
- func: download_kube_tools
8585
- func: setup_building_host
86+
- func: install_goreleaser
8687
- func: build_multi_cluster_binary
8788

8889
- &setup_and_teardown_group_gke_code_snippets
@@ -92,6 +93,7 @@ variables:
9293
- func: setup_gcloud_cli
9394
- func: setup_mongosh
9495
- func: download_kube_tools
96+
- func: install_goreleaser
9597
- func: build_multi_cluster_binary
9698
teardown_group:
9799
- func: upload_code_snippets_logs
@@ -420,6 +422,8 @@ tasks:
420422
commands:
421423
- func: clone
422424
- func: setup_building_host
425+
- func: install_goreleaser
426+
- func: install_macnotary
423427
- func: build_multi_cluster_binary
424428
- func: pipeline
425429
vars:

scripts/release/kubectl-mongodb/kubectl_mac_notarize.sh

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ if [[ -f "./dist/kubectl-mongodb_darwin_amd64_v1/kubectl-mongodb" && -f "./dist/
2929
-b com.mongodb.mongodb-kubectl-mongodb \
3030
-o ./dist/kubectl-mongodb_macos_signed.zip
3131

32+
echo "printing output zip file content"
33+
unzip -l ./dist/kubectl-mongodb_macos_signed.zip
34+
echo "done"
35+
36+
echo "running unzip -t to verify that the op zip file of macnotary is correct"
37+
unzip -t ./dist/kubectl-mongodb_macos_signed.zip
38+
echo "done"
39+
3240
echo "replacing original files"
3341
unzip -oj ./dist/kubectl-mongodb_macos_signed.zip dist/kubectl-mongodb_darwin_amd64_v1/kubectl-mongodb -d ./dist/kubectl-mongodb_darwin_amd64_v1/
3442
unzip -oj ./dist/kubectl-mongodb_macos_signed.zip dist/kubectl-mongodb_darwin_arm64/kubectl-mongodb -d ./dist/kubectl-mongodb_darwin_arm64/
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
import os
2+
import subprocess
3+
import sys
4+
5+
import boto3
6+
from botocore.exceptions import ClientError, NoCredentialsError, PartialCredentialsError
7+
8+
# from lib.base_logger import logger
9+
10+
S3_BUCKET_NAME = "mongodb-kubernetes-dev"
11+
AWS_REGION = "eu-north-1"
12+
S3_BUCKET_KUBECTL_PLUGIN_SUBPATH = "kubectl-mongodb"
13+
14+
COMMIT_SHA_ENV_VAR = "github_commit"
15+
16+
GORELEASER_DIST_DIR = "dist"
17+
18+
19+
def run_goreleaser():
20+
try:
21+
command = ["./goreleaser", "build", "--snapshot", "--clean"]
22+
23+
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1)
24+
25+
for log in iter(process.stdout.readline, ""):
26+
print(log, end="")
27+
28+
process.stdout.close()
29+
exit_code = process.wait()
30+
31+
if exit_code != 0:
32+
print(f"GoReleaser command failed with exit code {exit_code}.")
33+
sys.exit(1)
34+
35+
print("GoReleaser build completed successfully!")
36+
37+
except FileNotFoundError:
38+
print("ERROR: 'goreleaser' command not found. Please ensure GoReleaser is installed and in your system's PATH.")
39+
sys.exit(1)
40+
except Exception as e:
41+
print(f"An unexpected error occurred while running `goreleaser build`: {e}")
42+
sys.exit(1)
43+
44+
45+
def upload_artifacts_to_s3():
46+
if not os.path.isdir(GORELEASER_DIST_DIR):
47+
print(f"ERROR: GoReleaser dist directory '{GORELEASER_DIST_DIR}' not found.")
48+
sys.exit(1)
49+
50+
try:
51+
s3_client = boto3.client("s3", region_name=AWS_REGION)
52+
except (NoCredentialsError, PartialCredentialsError):
53+
print("ERROR: Failed to create S3 client. AWS credentials not found.")
54+
sys.exit(1)
55+
except Exception as e:
56+
print(f"An error occurred connecting to S3: {e}")
57+
sys.exit(1)
58+
59+
uploaded_files = 0
60+
for root, _, files in os.walk(GORELEASER_DIST_DIR):
61+
for filename in files:
62+
local_path = os.path.join(root, filename)
63+
s3_key = s3_path(local_path)
64+
65+
print(f"Uploading artifact {local_path} to s3://{S3_BUCKET_NAME}/{s3_key}")
66+
67+
try:
68+
s3_client.upload_file(local_path, S3_BUCKET_NAME, s3_key)
69+
print(f"Successfully uploaded the artifact {filename}")
70+
uploaded_files += 1
71+
except Exception as e:
72+
print(f"ERROR: Failed to upload file {filename}: {e}")
73+
74+
if uploaded_files > 0:
75+
print(f"Successfully uploaded {uploaded_files} kubectl-mongodb plugin artifacts to S3.")
76+
77+
78+
def s3_path(local_path: str):
79+
commit_sha = os.environ.get(COMMIT_SHA_ENV_VAR, "").strip()
80+
if commit_sha == "":
81+
print(f"Error: The commit sha environment variable {COMMIT_SHA_ENV_VAR} is not set. It's required to form the S3 Path.")
82+
sys.exit(1)
83+
84+
return f"{S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/{local_path}"
85+
86+
87+
def download_plugin_for_tests_image():
88+
try:
89+
s3_client = boto3.client("s3", region_name=AWS_REGION)
90+
except Exception as e:
91+
print(f"An error occurred connecting to S3 to download kubectl plugin for tests image: {e}")
92+
return
93+
94+
95+
commit_sha = os.environ.get(COMMIT_SHA_ENV_VAR, "").strip()
96+
if commit_sha == "":
97+
print("Error: The commit sha environment variable is not set. It's required to form the S3 Path.")
98+
sys.exit(1)
99+
100+
local_file_path = "docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_linux"
101+
102+
bucket_path = f"{S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{commit_sha}/dist/kubectl-mongodb_linux_amd64_v1/kubectl-mongodb"
103+
print(f"Downloading s3://{S3_BUCKET_NAME}/{bucket_path} to {local_file_path}")
104+
105+
try:
106+
s3_client.download_file(S3_BUCKET_NAME, bucket_path, local_file_path)
107+
print(f"Successfully downloaded artifact to {local_file_path}")
108+
except ClientError as e:
109+
if e.response['Error']['Code'] == '404':
110+
print(f"ERROR: Artifact not found at s3://{S3_BUCKET_NAME}/{bucket_path} ")
111+
else:
112+
print(f"ERROR: Failed to download artifact. S3 Client Error: {e}")
113+
except Exception as e:
114+
print(f"An unexpected error occurred during download: {e}")
115+
116+
def main():
117+
run_goreleaser()
118+
upload_artifacts_to_s3()
119+
120+
download_plugin_for_tests_image()
121+
122+
123+
if __name__ == "__main__":
124+
main()

0 commit comments

Comments
 (0)