Skip to content

Commit

Permalink
Switch from bazel define flags to skylib flags
Browse files Browse the repository at this point in the history
Summary:
We used --define to set the image versions and we explicitly created
targets for every version of the image push we needed.
Instead switch to skylib string_flags with TemplateVariableInfo and
BuildSettingInfo to parse the command line flags, with reasonable defaults and
use those to provide ctx vars that will be Make variable substituted by the
container_bundle rules.

Test Plan:
Build the container_push scripts with various flag settings and checked
the generated shell scripts. The image locations were as expected.
Also built the yamls to ensure that the kustomize replacements are working as
intended.

Reviewers: jamesbartlett, zasgar, michelle

Reviewed By: jamesbartlett

Signed-off-by: Vihang Mehta <vihang@pixielabs.ai>

Differential Revision: https://phab.corp.pixielabs.ai/D12883

GitOrigin-RevId: 7dad1d2d696d1ed603e68f7b14048c0a7bbc9efa
  • Loading branch information
vihangm authored and copybaranaut committed Jan 22, 2023
1 parent f9c6e19 commit 71f10f3
Show file tree
Hide file tree
Showing 9 changed files with 275 additions and 250 deletions.
6 changes: 0 additions & 6 deletions .bazelrc
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ coverage --define PL_COVERAGE=true
coverage --copt -DPL_COVERAGE
coverage --test_tag_filters=-requires_root,-requires_bpf,-no_coverage,-disabled,-no_gcc

# Use stable GIT build tag by default.
build --define BUNDLE_VERSION={STABLE_BUILD_TAG}

# pre-define as empty so that build all works. This might be overridden for dev vizier builds.
build --define DEV_VIZIER_IMAGE_PREFIX=""

# jenkins.bazelrc is copied from ci/jenkins.bazelrc by Jenkins workers during the build.
# The intention is to avoid polluting configurations of bazel for developers.
try-import %workspace%/jenkins.bazelrc
Expand Down
49 changes: 34 additions & 15 deletions bazel/images.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,45 @@
#
# SPDX-License-Identifier: Apache-2.0

def image_replacements(image_map, replace):
load("@bazel_skylib//rules:common_settings.bzl", "BuildSettingInfo")

PROPRIETARY_PREFIX = "gcr.io/pl-dev-infra/"
PUBLIC_PREFIX = "gcr.io/pixie-oss/pixie-prod/"
DEV_PREFIX = "gcr.io/pixie-oss/pixie-dev/"

def image_replacements(image_map, existing_prefix, new_prefix):
replacements = {}

for k in image_map.keys():
image_tag = k
for old, new in replace.items():
image_tag = image_tag.replace(old, new)
replacements[k] = image_tag
for image in image_map.keys():
image = image.removeprefix("$(IMAGE_PREFIX)").removesuffix(":$(BUNDLE_VERSION)")
replacements[existing_prefix + image] = new_prefix + image

return replacements

def image_map_with_bundle_version(image_map, replace):
with_version = {}
def _bundle_version_provider_impl(ctx):
return [
platform_common.TemplateVariableInfo({
"BUNDLE_VERSION": ctx.attr._bundle_version[BuildSettingInfo].value,
}),
]

for k, v in image_map.items():
image_tag = k
bundle_version_provider = rule(
implementation = _bundle_version_provider_impl,
attrs = {
"_bundle_version": attr.label(default = "//k8s:image_version"),
},
)

for old, new in replace.items():
image_tag = image_tag.replace(old, new)
k_with_version = "{0}:{1}".format(image_tag, "$(BUNDLE_VERSION)")
with_version[k_with_version] = v
def _image_prefix_provider_impl(ctx):
return [
platform_common.TemplateVariableInfo({
"IMAGE_PREFIX": ctx.attr.image_prefix,
}),
]

return with_version
image_prefix_provider = rule(
implementation = _image_prefix_provider_impl,
attrs = {
"image_prefix": attr.string(mandatory = True),
},
)
15 changes: 6 additions & 9 deletions ci/cloud_build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ parse_args() {
while test $# -gt 0; do
case "$1" in
-r) RELEASE=true
prod="True"
shift
;;
-p) PUBLIC=true
Expand All @@ -40,8 +39,6 @@ parse_args() {
done
}

prod="False"

parse_args "$@"

repo_path=$(pwd)
Expand All @@ -56,8 +53,8 @@ echo "The image tag is: ${image_tag}"

# We are building the OSS images/YAMLs. In this case, we only want to push the images but not deploy the YAMLs.
if [[ "$PUBLIC" == "true" ]]; then
bazel run --stamp -c opt --action_env=GOOGLE_APPLICATION_CREDENTIALS --define BUNDLE_VERSION="${image_tag}" \
--define public=True //k8s/cloud:cloud_images_push
bazel run --stamp -c opt --action_env=GOOGLE_APPLICATION_CREDENTIALS --//k8s:image_version="${image_tag}" \
--//k8s:build_type=public //k8s/cloud:cloud_images_push

bazel build //tools/licenses:all_licenses --action_env=GOOGLE_APPLICATION_CREDENTIALS

Expand Down Expand Up @@ -88,16 +85,16 @@ if [[ "$PUBLIC" == "true" ]]; then
exit 0
fi

bazel run --stamp -c opt --action_env=GOOGLE_APPLICATION_CREDENTIALS --define BUNDLE_VERSION="${image_tag}" \
--define prod="${prod}" //k8s/cloud:cloud_images_push
bazel run --stamp -c opt --action_env=GOOGLE_APPLICATION_CREDENTIALS --//k8s:image_version="${image_tag}" \
--//k8s:build_type=proprietary //k8s/cloud:cloud_images_push

yaml_path="${repo_path}/bazel-bin/k8s/cloud/pixie_staging_cloud.yaml"
# Build prod YAMLs.
if [[ "$RELEASE" == "true" ]]; then
yaml_path="${repo_path}/bazel-bin/k8s/cloud/pixie_prod_cloud.yaml"
bazel build --stamp -c opt --define BUNDLE_VERSION="${image_tag}" //k8s/cloud:pixie_prod_cloud
bazel build --stamp -c opt --//k8s:image_version="${image_tag}" //k8s/cloud:pixie_prod_cloud
else # Build staging YAMLs.
bazel build --stamp -c opt --define BUNDLE_VERSION="${image_tag}" //k8s/cloud:pixie_staging_cloud
bazel build --stamp -c opt --//k8s:image_version="${image_tag}" //k8s/cloud:pixie_staging_cloud
fi

kubectl apply -f "$yaml_path"
13 changes: 7 additions & 6 deletions ci/operator_build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ bazel run -c opt //src/utils/artifacts/versions_gen:versions_gen -- \
tags=$(git for-each-ref --sort='-*authordate' --format '%(refname:short)' refs/tags \
| grep "release/operator" | grep -v "\-")

public="True"
build_type="--//k8s:build_type=public"
image_path="gcr.io/pixie-oss/pixie-prod/operator/operator_image:${release_tag}"
deleter_image_path="gcr.io/pixie-oss/pixie-prod/operator/vizier_deleter:${release_tag}"
channel="stable"
Expand All @@ -42,20 +42,21 @@ bucket="pixie-dev-public"
prev_tag=$(echo "$tags" | sed -n '2 p')

if [[ $release_tag == *"-"* ]]; then
public="False"
build_type="--//k8s:build_type=dev"
image_path="gcr.io/pixie-oss/pixie-dev/operator/operator_image:${release_tag}"
deleter_image_path="gcr.io/pixie-oss/pixie-dev/operator/vizier_deleter:${release_tag}"
channel="dev"
channels="dev"
# TODO(vihang/michelle): Revisit this bucket.
bucket="pixie-prod-artifacts"
# The previous version should be the 1st item in the tags. Since this is a non-release build,
# the first item in the tags is the previous release.
# The previous version should be the 1st item in the tags. Since this is a non-release build,
# the first item in the tags is the previous release.
prev_tag=$(echo "$tags" | sed -n '1 p')
fi

# Push operator image.
bazel run --stamp -c opt --define BUNDLE_VERSION="${release_tag}" \
--stamp --define public="${public}" //k8s/operator:operator_images_push
bazel run --stamp -c opt --//k8s:image_version="${release_tag}" \
--stamp "${build_type}" //k8s/operator:operator_images_push

# Build operator bundle for OLM.
tmp_dir="$(mktemp -d)"
Expand Down
23 changes: 7 additions & 16 deletions ci/vizier_build_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -29,31 +29,22 @@ echo "The release tag is: ${release_tag}"
bazel run -c opt //src/utils/artifacts/versions_gen:versions_gen -- \
--repo_path "${repo_path}" --artifact_name vizier --versions_file "${versions_file}"

public="True"
build_type="--//k8s:build_type=public"
bucket="pixie-dev-public"
extra_bazel_args=()
if [[ $release_tag == *"-"* ]]; then
public="False"
build_type="--//k8s:build_type=dev"
# TODO(vihang/michelle): Revisit this bucket.
bucket="pixie-prod-artifacts"
fi
if [[ -n $DEV_ARTIFACT_BUCKET ]]; then
public="False"
bucket="${DEV_ARTIFACT_BUCKET}"
if [[ -z $DEV_IMAGE_PREFIX ]]; then
echo "Must specify DEV_IMAGE_PREFIX, when specifying a dev release with DEV_ARTIFACT_BUCKET"
exit 1
fi
extra_bazel_args+=("--define" "DEV_VIZIER_IMAGE_PREFIX=${DEV_IMAGE_PREFIX}")
extra_bazel_args+=("--//k8s/vizier:use_dev_vizier_images")
fi

output_path="gs://${bucket}/vizier/${release_tag}"
latest_output_path="gs://${bucket}/vizier/latest"

bazel run --stamp -c opt --define BUNDLE_VERSION="${release_tag}" \
--stamp --define public="${public}" //k8s/vizier:vizier_images_push "${extra_bazel_args[@]}"
bazel build --stamp -c opt --define BUNDLE_VERSION="${release_tag}" \
--stamp --define public="${public}" //k8s/vizier:vizier_yamls "${extra_bazel_args[@]}"
bazel run --stamp -c opt --//k8s:image_version="${release_tag}" \
--stamp "${build_type}" //k8s/vizier:vizier_images_push "${extra_bazel_args[@]}"
bazel build --stamp -c opt --//k8s:image_version="${release_tag}" \
--stamp "${build_type}" //k8s/vizier:vizier_yamls "${extra_bazel_args[@]}"

output_path="gs://${bucket}/vizier/${release_tag}"
yamls_tar="${repo_path}/bazel-bin/k8s/vizier/vizier_yamls.tar"
Expand Down
70 changes: 70 additions & 0 deletions k8s/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Copyright 2018- The Pixie Authors.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# SPDX-License-Identifier: Apache-2.0

load("@bazel_skylib//rules:common_settings.bzl", "string_flag")
load("//bazel:images.bzl", "DEV_PREFIX", "PROPRIETARY_PREFIX", "PUBLIC_PREFIX", "bundle_version_provider", "image_prefix_provider")

package(default_visibility = ["//visibility:public"])

string_flag(
name = "image_version",
build_setting_default = "latest",
)

string_flag(
name = "build_type",
build_setting_default = "dev",
values = [
"public",
"proprietary",
"dev",
],
)

config_setting(
name = "dev",
flag_values = {
":build_type": "dev",
},
)

config_setting(
name = "public",
flag_values = {
":build_type": "public",
},
)

config_setting(
name = "proprietary",
flag_values = {
":build_type": "proprietary",
},
)

bundle_version_provider(
name = "bundle_version",
)

image_prefix_provider(
name = "image_prefix",
image_prefix = select({
":dev": DEV_PREFIX,
":proprietary": PROPRIETARY_PREFIX,
":public": PUBLIC_PREFIX,
"//conditions:default": DEV_PREFIX,
}),
)
Loading

0 comments on commit 71f10f3

Please sign in to comment.