Bazel GitOps Rules provides tooling to bridge the gap between Bazel (for hermetic, reproducible, container builds) and continuous, git-operation driven, deployments. Users author standard kubernetes manifests and kustomize overlays for their services. Bazel GitOps Rules handles image push and substitution, applies necessary kustomizations, and handles content addressed substitutions of all object references (configmaps, secrets, etc). Bazel targets are exposed for applying the rendered manifest directly to a Kubernetes cluster, or into version control facilitating deployment via Git operations.
Bazel GitOps Rules is an alternative to rules_k8s. The main differences are:
<a name="installation"></a>
From the release you wish to use:
https://github.com/adobe/rules_gitops/releases
copy the WORKSPACE snippet into your WORKSPACE file.
<a name="k8s_deploy"></a>
The k8s_deploy creates rules that produce the .apply and .gitops targets k8s_deploy is defined in k8s.bzl. k8s_deploy takes the files listed in the manifests, patches, and configmaps_srcs attributes and combines (renders) them into one YAML file. This happens when you bazel build or bazel run a target created by the k8s_deploy. The file is created at bazel-bin/path/to/package/name.yaml. When you run a .apply target, it runs kubectl apply on this file. When you run a .gitops target, it copies this file to
the appropriate location in the same os separate repository.
For example, let's look at the example's k8s_deploy. We can peek at the file containing the rendered K8s manifests:
cd examples bazel run //helloworld:mynamespace.show
When you run bazel run ///helloworld:mynamespace.apply, it applies this file into your personal ({BUILD_USER}) namespace. Viewing the rendered files with .show can be useful for debugging issues with invalid or misconfigured manifests.
| Parameter | Default | Description |
|---|---|---|
| cluster | None | The name of the cluster in which these manifests will be applied. |
| namespace | None | The target namespace to assign to all manifests. Any namespace value in the source manifests will be replaced or added if not specified. |
| user | {BUILD_USER} | The user passed to kubectl in .apply rule. Must exist in users ~/.kube/config |
| configmaps_srcs | None | A list of files (of any type) that will be combined into configmaps. See Generating Configmaps. |
| configmaps_renaming | None | Configmaps/Secrets renaming policy. Could be None or 'hash'. 'hash' renaming policy is used to add a unique suffix to the generated configmap or secret name. All references to the configmap or secret in other manifests will be replaced with the generated name. |
| secrets_srcs | None | A list of files (of any type) that will be combined into a secret similar to configmaps. |
| manifests | glob(['*.yaml','*.yaml.tpl']) | A list of base manifests. See Base Manifests and Overlays. |
| name_prefix | None | Adds prefix to the names of all resources defined in manifests. |
| name_suffix | None | Adds suffix to the names of all resources defined in manifests. |
| patches | None | A list of patch files to overlay the base manifests. See Base Manifests and Overlays. |
| image_name_patches | None | A dict of image names that will be replaced with new ones. See kustomization images. |
| image_tag_patches | None | A dict of image names which tags be replaced with new ones. See kustomization images. |
| substitutions | None | Does parameter substitution in all the manifests (including configmaps). This should generally be limited to "CLUSTER" and "NAMESPACE" only. Any other replacements should be done with overlays. |
| configurations | [] | A list of files with kustomize configurations. |
| prefix_suffix_app_labels | False | Add the bundled configuration file allowing adding suffix and prefix to labels app and app.kubernetes.io/name and respective selector in Deployment. |
| common_labels | {} | A map of labels that should be added to all objects and object templates. |
| common_annotations | {} | A map of annotations that should be added to all objects and object templates. |
| start_tag | "{{" | The character start sequence used for substitutions. |
| end_tag | "}}" | The character end sequence used for substitutions. |
| deps | [] | A list of dependencies used to drive k8s_deploy functionality (i.e. deps_aliases). |
| deps_aliases | {} | A dict of labels of file dependencies. File dependency contents are available for template expansion in manifests as {{imports.<label>}}. Each dependency in this dictionary should be present in the deps attribute. |
| objects | [] | A list of other instances of k8s_deploy that this one depends on. See Adding Dependencies. |
| images | {} | A dict of labels of Docker images. See Injecting Docker Images. |
| image_digest_tag | False | A flag for whether or not to tag the image with the container digest. |
| image_registry | docker.io | The registry to push images to. |
| image_repository | None | The repository to push images to. By default, this is generated from the current package path. |
| image_repository_prefix | None | Add a prefix to the image_repository. Can be used to upload the images in |
| image_pushes | [] | A list of labels implementing K8sPushInfo referring image uploaded into registry. See Injecting Docker Images. |
| release_branch_prefix | master | A git branch name/prefix. Automatically run GitOps while building this branch. See GitOps and Deployment. |
| deployment_branch | None | Automatic GitOps output will appear in a branch and PR with this name. See GitOps and Deployment. |
| gitops_path | cloud | Path within the git repo where gitops files get generated into |
| tags | [] | See Bazel docs on tags. |
| visibility | Default_visibility | Changes the visibility of all rules generated by this macro. See Bazel docs on visibility. |
<a name="base-manifests-and-overlays"></a>
The manifests listed in the manifests attribute are the base manifests used by the deployment. This is where the important manifests like Deployments, Services, etc. are listed.
The base manifests will be modified by most of the other k8s_deploy attributes like substitutions and images. Additionally, they can be modified to configure them different clusters/namespaces/etc. using overlays.
To demonstrate, let's go over hypothetical multi cluster deployment.
Here is the fragment of the k8s_deploy rule that is responsible for generating manifest variants per CLOUD, CLUSTER, and NAMESPACE :
k8s_deploy( ... manifests = glob([ # (1) "manifests/*.yaml", "manifests/%s/*.yaml" % (CLOUD), ]), patches = glob([ # (2) "overlays/*.yaml", "overlays/%s/*.yaml" % (CLOUD), "overlays/%s/%s/*.yaml" % (CLOUD, NAMESPACE), "overlays/%s/%s/%s/*.yaml" % (CLOUD, NAMESPACE, CLUSTER), ]), ... )
The manifests list (1) combines common base manifests and CLOUD specific manifests.
manifests
├── aws
│ └── pvc.yaml
├── onprem
│ ├── pv.yaml
│ └── pvc.yaml
├── deployment.yaml
├── ingress.yaml
└── service.yaml
Here we see that aws and onprem clouds have different persistence configurations aws/pvc.yaml and onprem/pvc.yaml.
The patches list (2) requires more granular configuration that introduces 3 levels of customization: CLOUD, NAMESPACE, and CLUSTER. Each manifest fragment in the overlays subtree applied as strategic merge patch update operation.
overlays
├── aws
│ ├── deployment.yaml
│ ├── prod
│ │ ├── deployment.yaml
│ │ └── us-east-1
│ │ └── deployment.yaml
│ └── uat
│ └── deployment.yaml
└── onprem
├── prod
│ ├── deployment.yaml
│ └── us-east
│ └── deployment.yaml
└── uat
└── deployment.yaml
That looks like a lot. But lets try to decode what is happening here:
aws/deployment.yaml adds persistent volume reference specific to all AWS deployments.aws/prod/deployment.yaml modifies main container CPU and memory requirements in production configurations.aws/prod/us-east-1/deployment.yaml adds monitoring sidecar.<a name="generating-configmaps"></a>
Configmaps are a special case of manifests. They can be rendered from a collection of files of any kind (.yaml, .properties, .xml, .sh, whatever). Let's use hypothetical Grafana deployment as an example:
[ k8s_deploy( name = NAME, cluster = CLUSTER, configmaps_srcs = glob([ # (1) "configmaps/%s/**/*" % CLUSTER ]), configmaps_renaming = 'hash', # (2) ... ) for NAME, CLUSTER, NAMESPACE in [ ("mynamespace", "dev", "{BUILD_USER}"), # (3) ("prod-grafana", "prod", "prod"), # (4) ] ]
Here we generate two k8s_deploy targets, one for mynamespace (3), another for production deployment (4).
The directory structure of configmaps looks like this:
grafana
└── configmaps
├── dev
│ └── grafana
│ └── ldap.toml
└── prod
└── grafana
└── ldap.toml
The configmaps_srcs parameter (1) will get resolved into the patterns configmaps/dev/**/* and configmaps/prod/**/*. The result of rendering the manifests bazel run //grafana:prod-grafana.show will have following manifest fragment:
apiVersion: v1 data: ldap.toml: | [[servers]] ... kind: ConfigMap metadata: name: grafana-k75h878g4f namespace: ops-prod
The name of directory on the first level of glob patten grafana become the configmap name. The ldap.toml file on the next level were embedded into the configmap.
In this example, the configmap renaming policy (2) is set to hash, so the configmap's name appears as grafana-k75h878g4f. (If the renaming policy was None, the configmap's name would remain as grafana.) All the references to the grafana configmap in other manifests are replaced with the generated name:
apiVersion: apps/v1 kind: Deployment spec: template: spec: containers: volumes: ... - configMap: items: - key: ldap.toml path: ldap.toml name: grafana-k75h878g4f name: grafana-ldap
<a name="injecting-docker-images"></a>
Third-party Docker images can be referenced directly in K8s manifests, but for most apps, we need to run our own images. The images are built in the Bazel build pipeline using rules_docker. For example, the java_image rule creates an image of a Java application from Java source code, dependencies, and configuration.
Here's a (very contrived) example of how this ties in with k8s_deploy. Here's the BUILD file located in the package //examples:
java_image( name = "helloworld_image", srcs = glob(["*.java"]), ... ) k8s_deploy( name = "helloworld", manifests = ["helloworld.yaml"], images = { "helloworld_image": ":helloworld_image", # (1) } )
And here's helloworld.yaml:
apiVersion: v1 kind: Pod metadata: name: helloworld spec: containers: - image: //examples:helloworld_image # (2)
There images attribute dictionary (1) defines the images available for the substitution. The manifest file references the fully qualified image target path //examples:helloworld_image (2).
The image key value in the dictionary is used as an image push identifier. The best practice (as provided in the example) is to use image key that matches the label name of the image target.
When we bazel build the example, the rendered manifest will look something like this:
apiVersion: v1 kind: Pod metadata: name: helloworld spec: containers: - image: registry.example.com/examples/helloworld_image@sha256:c94d75d68f4c1b436f545729bbce82774fda07
The image substitution using an images key is supported, but not recommended (this functionality might be removed in the future). For example, helloworld.yaml can reference helloworld_image:
apiVersion: v1 kind: Pod metadata: name: helloworld spec: containers: - image: helloworld_image
Image substitutions for Custom Resource Definitions (CRD) resources could also use target references directly. Their digests are available through string substitution. For example,
apiVersion: v1 kind: MyCrd metadata: name: my_crd labels: app_label_image_digest: "{{//examples:helloworld_image.digest}}" app_label_image_short_digest: "{{//examples:helloworld_image.short-digest}}" spec: image: "{{//examples:helloworld_image}}"
would become
apiVersion: v1 kind: MyCrd metadata: name: my_crd labels: app_label_image_digest: "e6d465223da74519ba3e2b38179d1268b71a72f" app_label_image_short_digest: "e6d465223d" spec: image: registry.example.com/examples/helloworld_image@sha256:e6d465223da74519ba3e2b38179d1268b71a72f
An all examples above the image: URL points to the helloworld_image in the private Docker registry. The image is uploaded to the registry before any .apply or .gitops target is executed. See


AI一键生成PPT,就用博思AIPPT!
博思AIPPT,新一代的AI生成PPT平台,支持智能生成PPT、AI美化PPT、文本&链接生成PPT、导入Word/PDF/Markdown文档生成PPT等,内置海量精美PPT模板,涵盖商务、教育、科技等不同风格,同时针对每个页面提供多种版式,一键自适应切换,完美适配各种办公场景。


AI赋能电商视觉革命,一站式智能商拍平台
潮际好麦深耕服装行业,是国内AI试衣效果最好的软件。使用先进AIGC能力为电商卖家批量提供优质的、低成本的商拍图。合作品牌有Shein、Lazada、安踏、百丽等65个国内外头部品牌,以及国内10万+淘宝、天猫、京东等主流平台的品牌商家,为卖家节省将近85%的出图成本,提升约3倍出图效率,让品牌能够快速上架。


企业专属的AI法律顾问
iTerms是法大大集团旗下法律子品牌,基于最先进的大语言模型(LLM)、专业的法律知识库和强大的智能体架构,帮助企业扫清合规障碍,筑牢风控防线,成为您企业专属的AI法律顾问。


稳定高效的流量提升解决方案,助力品牌曝光
稳定高效的流量提升解决方案,助力品牌曝光


最新版Sora2模型免费使用,一键生成无水印视频
最新版Sora2模型免费使用,一键生成无水印视频


实时语音翻译/同声传译工具
Transly是一个多场景的AI大语言模型驱动的同声传译、专业翻译助手,它拥有超精准的音频识别翻译能力,几乎零延迟的使用体验和支持多国语言可以让你带它走遍全球,无论你是留学生、商务人士、韩剧美剧爱好者,还是出国游玩、多国会议、跨国追星等等,都可以满足你所有需要同传的场景需求,线上线下通用,扫除语言障碍,让全世界的语言交流不再有国界。


选题、配图、成文,一站式创作,让内容运营更高效
讯飞绘文,一个AI集成平台,支持写作、选题、配图、排版和发布。高效生成适用于各类媒体的定制内容,加速品牌传播,提升内容营销效果。


AI辅助编程,代码自动修复
Trae是一种自适应的集成开发环境(IDE),通过自动化和多元协作改变开发流程。利用Trae,团队能够 更快速、精确地编写和部署代码,从而提高编程效率和项目交付速度。Trae具备上下文感知和代码自动完成功能,是提升开发效率的理想工具。


最强AI数据分析助手
小浣熊家族Raccoon,您的AI智能助手,致力于通过先进的人工智能技术,为用户提供高效、便捷的智能服务。无论是日常咨询还是专业问题解答,小浣熊都能以快速、准确的响应满足您的需求,让您的生活更加智能便捷。


像人一样思考的AI智能体
imini 是一款超级AI智能体,能根据人类指令,自主思考、自主完成、并且交付结果的AI智能体。
最新AI工具、AI资讯
独家AI资源、AI项目落地

微信扫一扫关注公众号