-
Notifications
You must be signed in to change notification settings - Fork 14
[CLOUDP-338093] Use goreleaser
to build kubectl-mongodb
plugin for dev/staging workflows
#320
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
Open
viveksinghggits
wants to merge
3
commits into
master
Choose a base branch
from
kubectl-plugin-goreleaser
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+198
−10
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
141 changes: 141 additions & 0 deletions
141
scripts/release/kubectl-mongodb/python/build_kubectl_plugin.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import os | ||
import subprocess | ||
import sys | ||
|
||
import boto3 | ||
from botocore.exceptions import ClientError, NoCredentialsError, PartialCredentialsError | ||
|
||
from lib.base_logger import logger | ||
from scripts.release.build.build_info import ( | ||
load_build_info, | ||
) | ||
from scripts.release.build.build_scenario import ( | ||
BuildScenario, | ||
) | ||
|
||
AWS_REGION = "eu-north-1" | ||
KUBECTL_PLUGIN_BINARY_NAME = "kubectl-mongodb" | ||
S3_BUCKET_KUBECTL_PLUGIN_SUBPATH = KUBECTL_PLUGIN_BINARY_NAME | ||
|
||
GORELEASER_DIST_DIR = "dist" | ||
|
||
# LOCAL_KUBECTL_PLUGIN_PATH is the full filename where tests image expects the kuebctl-mongodb binary to be available | ||
LOCAL_KUBECTL_PLUGIN_PATH = "docker/mongodb-kubernetes-tests/multi-cluster-kube-config-creator_linux" | ||
|
||
|
||
def run_goreleaser(): | ||
try: | ||
command = ["./goreleaser", "build", "--snapshot", "--clean", "--skip", "post-hooks"] | ||
|
||
process = subprocess.Popen(command, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1) | ||
|
||
for log in iter(process.stdout.readline, ""): | ||
print(log, end="") | ||
|
||
process.stdout.close() | ||
exit_code = process.wait() | ||
mircea-cosbuc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if exit_code != 0: | ||
logger.debug(f"GoReleaser command failed with exit code {exit_code}.") | ||
sys.exit(1) | ||
|
||
logger.info("GoReleaser build completed successfully!") | ||
|
||
except FileNotFoundError: | ||
logger.debug( | ||
"ERROR: 'goreleaser' command not found. Please ensure goreleaser is installed and in your system's PATH." | ||
) | ||
sys.exit(1) | ||
except Exception as e: | ||
logger.debug(f"An unexpected error occurred while running `goreleaser build`: {e}") | ||
sys.exit(1) | ||
|
||
|
||
# upload_artifacts_to_s3 uploads the artifacts that are generated by goreleaser to S3 bucket at a specific path. | ||
# The S3 bucket and version are figured out and passed to this function based on BuildScenario. | ||
def upload_artifacts_to_s3(s3_bucket: str, version: str): | ||
if not os.path.isdir(GORELEASER_DIST_DIR): | ||
logger.info(f"ERROR: GoReleaser dist directory '{GORELEASER_DIST_DIR}' not found.") | ||
sys.exit(1) | ||
|
||
try: | ||
s3_client = boto3.client("s3", region_name=AWS_REGION) | ||
except (NoCredentialsError, PartialCredentialsError): | ||
logger.debug("ERROR: Failed to create S3 client. AWS credentials not found.") | ||
sys.exit(1) | ||
except Exception as e: | ||
logger.debug(f"An error occurred connecting to S3: {e}") | ||
sys.exit(1) | ||
|
||
uploaded_files = 0 | ||
# iterate over all the files generated by goreleaser in the dist directory and upload them to S3 | ||
for root, _, files in os.walk(GORELEASER_DIST_DIR): | ||
for filename in files: | ||
local_path = os.path.join(root, filename) | ||
s3_key = s3_path(local_path, version) | ||
|
||
logger.info(f"Uploading artifact {local_path} to s3://{s3_bucket}/{s3_key}") | ||
try: | ||
s3_client.upload_file(local_path, s3_bucket, s3_key) | ||
logger.info(f"Successfully uploaded the artifact {filename}") | ||
uploaded_files += 1 | ||
except Exception as e: | ||
logger.debug(f"ERROR: Failed to upload file {filename}: {e}") | ||
sys.exit(1) | ||
|
||
if uploaded_files > 0: | ||
logger.info(f"Successfully uploaded {uploaded_files} kubectl-mongodb plugin artifacts to S3.") | ||
|
||
|
||
# s3_path returns the path where the artifacts should be uploaded to in S3 object store. | ||
# For dev workflows it's going to be `kubectl-mongodb/{evg-patch-id}/{goreleaser-artifact}`, | ||
# for staging workflows it would be `kubectl-mongodb/{commit-sha}/{goreleaser-artifact}`. | ||
# The `version` string has the correct version (either patch id or commit sha), based on the BuildScenario. | ||
def s3_path(local_path: str, version: str): | ||
return f"{S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{version}/{local_path}" | ||
|
||
|
||
# download_plugin_for_tests_image downloads just the linux amd64 version of the binary and places it | ||
# at the location LOCAL_KUBECTL_PLUGIN_PATH. | ||
def download_plugin_for_tests_image(build_scenario: BuildScenario, s3_bucket: str, version: str): | ||
try: | ||
s3_client = boto3.client("s3", region_name=AWS_REGION) | ||
except Exception as e: | ||
logger.debug(f"An error occurred connecting to S3 to download kubectl plugin for tests image: {e}") | ||
sys.exit(1) | ||
|
||
plugin_path = f"{S3_BUCKET_KUBECTL_PLUGIN_SUBPATH}/{version}/dist/kubectl-mongodb_linux_amd64_v1/kubectl-mongodb" | ||
|
||
logger.info(f"Downloading s3://{s3_bucket}/{plugin_path} to {LOCAL_KUBECTL_PLUGIN_PATH}") | ||
try: | ||
s3_client.download_file(s3_bucket, plugin_path, LOCAL_KUBECTL_PLUGIN_PATH) | ||
# change the file's permissions to make file executable | ||
os.chmod(LOCAL_KUBECTL_PLUGIN_PATH, 0o755) | ||
|
||
logger.info(f"Successfully downloaded artifact to {LOCAL_KUBECTL_PLUGIN_PATH}") | ||
except ClientError as e: | ||
if e.response["Error"]["Code"] == "404": | ||
logger.debug(f"ERROR: Artifact not found at s3://{s3_bucket}/{plugin_path} ") | ||
else: | ||
logger.debug(f"ERROR: Failed to download artifact. S3 Client Error: {e}") | ||
sys.exit(1) | ||
except Exception as e: | ||
logger.debug(f"An unexpected error occurred during download: {e}") | ||
sys.exit(1) | ||
|
||
|
||
def main(): | ||
build_scenario = BuildScenario.infer_scenario_from_environment() | ||
kubectl_plugin_build_info = load_build_info(build_scenario).binaries[KUBECTL_PLUGIN_BINARY_NAME] | ||
|
||
run_goreleaser() | ||
|
||
upload_artifacts_to_s3(kubectl_plugin_build_info.s3_store, kubectl_plugin_build_info.version) | ||
|
||
download_plugin_for_tests_image( | ||
build_scenario, kubectl_plugin_build_info.s3_store, kubectl_plugin_build_info.version | ||
) | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.