Skip to content
This repository has been archived by the owner on Feb 6, 2024. It is now read-only.

Commit

Permalink
Merge pull request #44 from bazelbuild/apply
Browse files Browse the repository at this point in the history
Add an apply action.
  • Loading branch information
mattmoor authored Sep 29, 2017
2 parents 2175cc8 + 3275b82 commit 9c83c62
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 13 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,20 @@ Like `.create` this deploys the **resolved** template, which includes
republishing images. **This action is intended to be the workhorse
of fast-iteration development** (rebuilding / republishing / redeploying).

### Apply

Users can "apply" a configuration by running:
```shell
bazel run :dev.apply
```

`:dev.apply` maps to `kubectl apply`, which will create or replace an existing
configuration. For more information see the `kubectl` documentation.

This applies the **resolved** template, which includes republishing images.
**This action is intended to be the workhorse of fast-iteration development**
(rebuilding / republishing / redeploying).

### Delete

Users can tear down their environment by running:
Expand Down
13 changes: 4 additions & 9 deletions examples/hellohttp/e2e-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ function get_lb_ip() {
-o jsonpath='{.status.loadBalancer.ingress[0].ip}'
}

function create() {
bazel run examples/hellohttp/${LANGUAGE}:staging.create
}

function check_msg() {
OUTPUT=$(curl http://$(get_lb_ip):8080)
echo Checking response from service: "${OUTPUT}" matches: "DEMO$1<space>"
Expand All @@ -35,24 +31,23 @@ function edit() {
./examples/hellohttp/${LANGUAGE}/edit.sh "$1"
}

function update() {
bazel run examples/hellohttp/${LANGUAGE}:staging.replace
function apply() {
bazel run examples/hellohttp/${LANGUAGE}:staging.apply
}

function delete() {
bazel run examples/hellohttp/${LANGUAGE}:staging.describe
bazel run examples/hellohttp/${LANGUAGE}:staging.delete
}


create
apply
trap "delete" EXIT
sleep 25
check_msg

for i in $RANDOM $RANDOM; do
edit "$i"
update
apply
# Don't let k8s slowness cause flakes.
sleep 25
check_msg "$i"
Expand Down
1 change: 1 addition & 0 deletions k8s/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package(default_visibility = ["//visibility:public"])
licenses(["notice"]) # Apache 2.0

exports_files([
"apply.sh.tpl",
"create.sh.tpl",
"replace.sh.tpl",
"resolve.sh.tpl",
Expand Down
27 changes: 27 additions & 0 deletions k8s/apply.sh.tpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/bin/bash

# Copyright 2017 The Bazel Authors. All rights reserved.
#
# 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.
set -euo pipefail

function guess_runfiles() {
pushd ${BASH_SOURCE[0]}.runfiles > /dev/null 2>&1
pwd
popd > /dev/null 2>&1
}

RUNFILES="${PYTHON_RUNFILES:-$(guess_runfiles)}"

PYTHON_RUNFILES=${RUNFILES} %{resolve_script} | \
kubectl --cluster="%{cluster}" --namespace="%{namespace}" apply -f -
30 changes: 26 additions & 4 deletions k8s/object.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,23 @@ _k8s_object = rule(
implementation = _impl,
)

_k8s_object_apply = rule(
attrs = {
"resolved": attr.label(
cfg = "target",
executable = True,
allow_files = True,
),
"_template": attr.label(
default = Label("//k8s:apply.sh.tpl"),
single_file = True,
allow_files = True,
),
} + _common_attrs,
executable = True,
implementation = _common_impl,
)

_k8s_object_create = rule(
attrs = {
"resolved": attr.label(
Expand Down Expand Up @@ -295,16 +312,21 @@ def k8s_object(name, **kwargs):

_k8s_object(name=name, **kwargs)

if "cluster" in kwargs and "kind" in kwargs:
if "cluster" in kwargs:
_k8s_object_create(name=name + ".create", resolved=name,
kind=kwargs.get("kind"), cluster=kwargs.get("cluster"),
namespace=kwargs.get("namespace"))
_k8s_object_delete(name=name + ".delete", unresolved=name + ".yaml",
kind=kwargs.get("kind"), cluster=kwargs.get("cluster"),
namespace=kwargs.get("namespace"))
_k8s_object_describe(name=name + ".describe", unresolved=name + ".yaml",
kind=kwargs.get("kind"), cluster=kwargs.get("cluster"),
namespace=kwargs.get("namespace"))
_k8s_object_replace(name=name + ".replace", resolved=name,
kind=kwargs.get("kind"), cluster=kwargs.get("cluster"),
namespace=kwargs.get("namespace"))
_k8s_object_apply(name=name + ".apply", resolved=name,
kind=kwargs.get("kind"), cluster=kwargs.get("cluster"),
namespace=kwargs.get("namespace"))
if "kind" in kwargs:
_k8s_object_describe(name=name + ".describe", unresolved=name + ".yaml",
kind=kwargs.get("kind"),
cluster=kwargs.get("cluster"),
namespace=kwargs.get("namespace"))

0 comments on commit 9c83c62

Please sign in to comment.