Skip to content

Commit

Permalink
feat: Start implementing skaffold (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
ipv1337 committed Jun 19, 2023
1 parent e878ae7 commit 574ce57
Show file tree
Hide file tree
Showing 26 changed files with 601 additions and 51 deletions.
71 changes: 56 additions & 15 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,47 @@ bazel_dep(name = "rules_python", version = "0.21.0")
bazel_dep(name = "rules_pkg", version = "0.9.1")
bazel_dep(name = "rules_oci", version = "1.0.0")
bazel_dep(name = "platforms", version = "0.0.6")
# See: https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md
# bazel_dep(name = "rules_go", version = "0.39.1", repo_name = "io_bazel_rules_go")
bazel_dep(name = "rules_go", version = "0.39.1")
# bazel_dep(name = "gazelle", version = "0.31.0", repo_name = "bazel_gazelle")
bazel_dep(name = "gazelle", version = "0.31.0")

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.toolchains(crane_version = "v0.14.0")
use_repo(oci, "oci_crane_toolchains", "oci_crane_registry_toolchains", "oci_auth_config")

register_toolchains("@oci_crane_toolchains//:all", "@oci_crane_registry_toolchains//:all")

################################################################################
# rules_go
################################################################################

# go_deps = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps")
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")

go_deps.module(
path = "github.com/google/go-cmp",
sum = "h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=",
version = "v0.5.9",
)

use_repo(go_deps, "com_github_google_go_cmp")

# oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

oci.pull(
name = "distroless_base",
digest = "sha256:ccaef5ee2f1850270d453fdf700a5392534f8d1a8ca2acda391fbb6a06b81c86",
image = "gcr.io/distroless/base",
platforms = [
"linux/amd64",
"linux/arm64",
],
)

use_repo(oci, "distroless_base")

################################################################################
# rules_python
Expand All @@ -33,7 +74,7 @@ bazel_dep(name = "platforms", version = "0.0.6")
# "@python3_11_toolchains//:all",
# )

oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")
# oci = use_extension("@rules_oci//oci:extensions.bzl", "oci")

# See:
# - https://hub.docker.com/layers/library/python/3.10-bullseye/images/sha256-b48e216f7c4adcf24fecd7016f3b8ead76866a19571819f67f47c1ccaf899717?context=explore
Expand All @@ -53,26 +94,26 @@ use_repo(oci, "distroless_python")
# rules_js
################################################################################

npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)
# npm = use_extension("@aspect_rules_js//npm:extensions.bzl", "npm", dev_dependency = True)

npm.npm_translate_lock(
name = "npm",
pnpm_lock = "//:pnpm-lock.yaml",
npmrc = "//:.npmrc",
)
# npm.npm_translate_lock(
# name = "npm",
# pnpm_lock = "//:pnpm-lock.yaml",
# npmrc = "//:.npmrc",
# )

use_repo(npm, "npm")
# use_repo(npm, "npm")

################################################################################
# rules_ts
################################################################################

rules_ts_ext = use_extension(
"@aspect_rules_ts//ts:extensions.bzl",
"ext",
dev_dependency = True,
)
# rules_ts_ext = use_extension(
# "@aspect_rules_ts//ts:extensions.bzl",
# "ext",
# dev_dependency = True,
# )

rules_ts_ext.deps()
# rules_ts_ext.deps()

use_repo(rules_ts_ext, "npm_typescript")
# use_repo(rules_ts_ext, "npm_typescript")
14 changes: 14 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,20 @@ run_oci_py_helloworld_v2:
docker load --input `bazel cquery --output=files //projects/py_helloworld_v2_cli_app:tarball`
docker run --rm local/py_helloworld_v2_cli_app:latest

run_go_devops_cli_app:
skaffold dev -m go-devops-cli-app-config

run_py_devops_fastapi_app:
skaffold build -m py-devops-fastapi-app-config



dev_go_devops_cli_app:
skaffold dev -m go-devops-cli-app-config

dev_py_devops_fastapi_app:
skaffold dev -m py-devops-fastapi-app-config



git_new:
Expand Down
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

Unlike other examples and demos out in the wild, we have a loftier goals. If you don't see the following objectives being met, I wouldn't bother digging much further since technologies and versions change so much that anything here would almost be useless similar to what my experience was while looking for best practices and more indepth examples that can be used as blueprints rather than a one time presentation.

## Quickstart

1. Download bazelisk
1. Download skaffold

## Motivation

[Salesforce](https://www.youtube.com/watch?v=KZIYdxsRp4w)
Expand Down
1 change: 1 addition & 0 deletions WORKSPACE
48 changes: 47 additions & 1 deletion WORKSPACE.bazel
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Declares that this directory is the root of a Bazel workspace.
# See https://docs.bazel.build/versions/main/build-ref.html#workspace
# See: https://docs.bazel.build/versions/main/build-ref.html#workspace
# ISSUE: This needs to be symlinked to WORKSPACE for Skaffold
workspace(
# How this workspace would be referenced with absolute labels from another workspace
name = "monorepo",
Expand Down Expand Up @@ -65,3 +66,48 @@ multi_pip_parse(
load("@pip_deps//:requirements.bzl", "install_deps")

install_deps()



#
# rules_docker
#

# http_archive(
# name = "io_bazel_rules_docker",
# sha256 = "b1e80761a8a8243d03ebca8845e9cc1ba6c82ce7c5179ce2b295cd36f7e394bf",
# urls = ["https://github.com/bazelbuild/rules_docker/releases/download/v0.25.0/rules_docker-v0.25.0.tar.gz"],
# )

#
# Golang (rules_docker - go_image)
#

# load("@rules_go//go:deps.bzl", "go_register_toolchains", "go_rules_dependencies")

# go_rules_dependencies()

# go_register_toolchains(
# go_version = "1.20.2",
# )

# load(
# "@io_bazel_rules_docker//repositories:repositories.bzl",
# container_repositories = "repositories",
# )

# container_repositories()

# load(
# "@io_bazel_rules_docker//repositories:deps.bzl",
# container_deps = "deps",
# )

# container_deps()

# load(
# "@io_bazel_rules_docker//go:image.bzl",
# _go_image_repos = "repositories",
# )

# _go_image_repos()
5 changes: 5 additions & 0 deletions k8s/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
DO NOT USE THIS CONVENTION

Use kubernetes manifests per application instead!

This is only a placeholder to warn anyone attempting to use the approach.
2 changes: 2 additions & 0 deletions libs/devops/models/devops.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# See:
# - https://www.w3schools.com/python/python_inheritance.asp
# - https://github.com/faif/python-patterns/blob/master/patterns/creational/abstract_factory.py
# Example:
# - https://github.com/Netflix/dispatch/blob/master/tests/factories.py

import random
from typing import Type
Expand Down
1 change: 1 addition & 0 deletions projects/go_devops_cli_app/BUILD.bazel
22 changes: 22 additions & 0 deletions projects/go_devops_cli_app/BUILD.bazel-go_image
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# See: https://bazel.build/concepts/build-files
# TODO:
# - Migrate to rules_oci (https://github.com/bazel-contrib/rules_oci/blob/main/docs/go.md)
# - https://github.com/aspect-build/bazel-examples/tree/main/oci_go_image

#
# Golang (rules_docker - go_image)
#

load("@io_bazel_rules_docker//go:image.bzl", "go_image")

go_image(
name = "go_devops_cli_app_image",
srcs = [
"src/main.go",
"src/go.mod",
"src/go.sum",
],
goos = "linux",
goarch = "amd64",
static = "on",
)
86 changes: 86 additions & 0 deletions projects/go_devops_cli_app/BUILD.bazel-oci_image
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
# See: https://bazel.build/concepts/build-files
# TODO:
# - Migrate to rules_oci (https://github.com/bazel-contrib/rules_oci/blob/main/docs/go.md)
# - https://github.com/aspect-build/bazel-examples/tree/main/oci_go_image

#
# Golang (rules_oci - oci_go_image)
#

load("@aspect_bazel_lib//lib:transitions.bzl", "platform_transition_filegroup")
load("@rules_go//go:def.bzl", "go_binary", "go_library")
load("@rules_oci//oci:defs.bzl", "oci_image", "oci_tarball")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@container_structure_test//:defs.bzl", "container_structure_test")

go_library(
name = "app_lib",
srcs = ["src/main.go"],
importpath = "example.com/custom_registry/app",
visibility = ["//visibility:private"],
deps = ["@com_github_google_go_cmp//cmp"],
)

go_binary(
name = "app",
embed = [":app_lib"],
visibility = ["//visibility:public"],
)

# Put app go_binary into a tar layer.
pkg_tar(
name = "app_layer",
srcs = [":app"],
# If the binary depends on RUNFILES, uncomment the attribute below.
# include_runfiles = True
)

oci_image(
name = "image",
base = "@distroless_base",
entrypoint = ["/app"],
tars = [":app_layer"],
)

# This is the target that should be released to the target platform
platform_transition_filegroup(
name = "transitioned_image",
srcs = [":image"],
target_platform = select({
"@platforms//cpu:arm64": "@rules_go//go/toolchain:linux_arm64",
"@platforms//cpu:x86_64": "@rules_go//go/toolchain:linux_amd64",
}),
)

# $ bazel build projects/go_devops_cli_app:tarball
# $ docker load --input $(bazel cquery --output=files projects/go_devops_cli_app:tarball)
# $ docker run --rm flyr.io/go-devops-cli-app:latest
# string(
# - "Hello World",
# + "Hello Go",
# )
oci_tarball(
name = "tarball",
# Use the image built for the exec platform rather than the target platform
image = ":image",
repo_tags = [
"flyr.io/go-devops-cli-app:latest",
],
)

# Skaffold needs a target ending with .tar
oci_tarball(
name = "tarball.tar",
# Use the image built for the exec platform rather than the target platform
image = ":image",
repo_tags = ["go-devops-cli-app"],
)

# See: https://github.com/GoogleContainerTools/container-structure-test#running-structure-tests-through-bazel
container_structure_test(
name = "test",
configs = ["tests/test.yaml"],
# Use the image built for the exec platform rather than the target platform
image = ":image",
timeout = "short"
)
15 changes: 15 additions & 0 deletions projects/go_devops_cli_app/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Overview

Template for creating FastAPI projects.

## Roadmap

- [ ] Setup basic templating
- [ ] Research Cookiecutter
- [ ] Bake-in everything including best practices
- [ ] Test and validate framework
- [ ] Gather feedback from beta testers

## Known issues

- [ ] None yet
8 changes: 8 additions & 0 deletions projects/go_devops_cli_app/kubernetes/k8s-pod.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
apiVersion: v1
kind: Pod
metadata:
name: bazel
spec:
containers:
- name: bazel
image: go-devops-cli-app
19 changes: 19 additions & 0 deletions projects/go_devops_cli_app/skaffold.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# See:
# - https://github.com/GoogleContainerTools/skaffold/blob/main/examples/multi-config-microservices/skaffold.yaml
# - https://github.com/GoogleContainerTools/skaffold/blob/main/examples/structure-tests/skaffold.yaml

apiVersion: skaffold/v4beta5
kind: Config
metadata:
# skaffold dev -m go-devops-cli-app-config
name: go-devops-cli-app-config
build:
artifacts:
- image: go-devops-cli-app
context: .
bazel:
# target: //projects/go_devops_cli_app:go_devops_cli_app_image.tar
target: //projects/go_devops_cli_app:tarball.tar
manifests:
rawYaml:
- kubernetes/*
5 changes: 5 additions & 0 deletions projects/go_devops_cli_app/src/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module example.com/mod

go 1.17

require github.com/google/go-cmp v0.5.9
2 changes: 2 additions & 0 deletions projects/go_devops_cli_app/src/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
10 changes: 10 additions & 0 deletions projects/go_devops_cli_app/src/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
"fmt"
"github.com/google/go-cmp/cmp"
)

func main() {
fmt.Println(cmp.Diff("Hello World", "Hello Go"))
}
5 changes: 5 additions & 0 deletions projects/go_devops_cli_app/tests/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
schemaVersion: 2.0.0
commandTests:
- name: 'test'
command: '/app'
expectedOutput: ['"Hello World"']
Loading

0 comments on commit 574ce57

Please sign in to comment.