Skip to content

Commit

Permalink
Build Krew archive and manifest
Browse files Browse the repository at this point in the history
Tested: `make krew-install` and verified that the plugin worked
correctly and the documentation link was correct. Built a test release
to my personal repo and verified that I could download the manifest and
install from it correctly.
  • Loading branch information
adrianludwin committed Sep 23, 2020
1 parent f990be3 commit 6dfcfdd
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 53 deletions.
63 changes: 33 additions & 30 deletions incubator/hnc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ endif
HNC_IMG_NAME ?= hnc-manager

# By default, the image tag is "latest" but you should override this when
# creating a release. If you're using Kind, the tag is 'kind-local' since K8s
# always attempts to re-pull an image the 'latest' tag, and this doesn't work
# when we're testing locally (we rely on the docker-push target, below, to push
# the image into Kind).
# creating a release in the form vX.Y.Z (e.g. v0.5.1).
#
# If you're using Kind, the tag is 'kind-local' since K8s always attempts to
# re-pull an image the 'latest' tag, and this doesn't work when we're testing
# locally (we rely on the docker-push target, below, to push the image into
# Kind).
ifneq ($(CONFIG),kind)
HNC_IMG_TAG ?= latest
else
Expand Down Expand Up @@ -64,8 +66,9 @@ CRD_OPTIONS ?= "crd:trivialVersions=true"
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
GOBIN ?= $(shell go env GOPATH)/bin

# Get check sum value of krew archive
KREW_CKSM=$(shell sha256sum bin/kubectl-hns.tar.gz | cut -d " " -f 1)
# Get check sum value of krew archive. Note that this value is only expanded
# when the var is used.
HNC_KREW_TAR_SHA256=$(shell sha256sum bin/kubectl-hns.tar.gz | cut -d " " -f 1)

# The directories to run Go command on (excludes /vendor)
DIRS=./api/... ./cmd/... ./internal/... ./third_party/...
Expand Down Expand Up @@ -104,29 +107,6 @@ kubectl: build
run: build
go run ./cmd/manager/main.go --novalidation

# Generate the Krew manifest and put it in manifests/ (creating that dir if it
# doesn't exist).
krew-build: krew-tar
-mkdir manifests
cp hack/krew-hierarchical-namespaces.yaml manifests/krew-hierarchical-namespaces.yaml
sed -i 's/^\(\s*sha256\s*:\s*\).*/\1"$(KREW_CKSM)"/' \
manifests/krew-hierarchical-namespaces.yaml
sed -i 's/^\(\s*version\s*:\s*\).*/\1"$(HNC_IMG_TAG)"/' \
manifests/krew-hierarchical-namespaces.yaml

# Make krew archive and put into /hack. IS THIS THE RIGHT WAY TO PACKAGE UP
# BINARIES FOR MULTIPLE OSES?
krew-tar: build
tar -zcvf bin/kubectl-hns.tar.gz bin/kubectl

# Install kubectl plugin locally using krew.
krew-install: krew-build
kubectl krew install --manifest=manifests/krew-hierarchical-namespaces.yaml --archive=bin/kubectl-hns.tar.gz

# Uninstall kubectl plugin locally using krew.
krew-uninstall:
-kubectl krew uninstall hierarchical-namespaces

# Generate manifests e.g. CRD, RBAC etc. This can both update the generated
# files in /config (which should be checked into Git) as well as the kustomized
# files in /manifest (which are not checked into Git).
Expand Down Expand Up @@ -338,7 +318,7 @@ endif
# Actual error checking:
check-release-env:
ifndef HNC_IMG_TAG
$(error HNC_IMG_TAG is undefined; ${ERR_MSG})
$(error HNC_IMG_TAG is undefined, should be vX.X.X; ${ERR_MSG})
endif
ifndef HNC_USER
$(error HNC_USER is undefined; ${ERR_MSG})
Expand All @@ -352,3 +332,26 @@ endif
ifeq ($(COULDNT_READ_RELEASE_IMG), 0)
$(error The image ${HNC_RELEASE_IMG} already exists. Force and overwrite this image by using HNC_FORCE_RELEASE=true)
endif

# Generate the Krew manifest and put it in manifests/. Note that 'manifests' must exist because
# krew-build calls krew-tar calls build calls manifests.
krew-build: krew-tar
cp hack/krew-kubectl-hns.yaml manifests/krew-kubectl-hns.yaml
sed -i 's/HNC_KREW_TAR_SHA256/${HNC_KREW_TAR_SHA256}/' manifests/krew-kubectl-hns.yaml
sed -i 's/HNC_IMG_TAG/${HNC_IMG_TAG}/' manifests/krew-kubectl-hns.yaml
sed -i 's/HNC_RELEASE_REPO_OWNER/${HNC_RELEASE_REPO_OWNER}/' manifests/krew-kubectl-hns.yaml

# This needs to be separate from krew-build so that the HNC_KREW_TAR_SHA256 env
# var can be evaluated before the recipe starts running.
krew-tar: build
cp LICENSE bin/kubectl
tar -zcvf bin/kubectl-hns.tar.gz bin/kubectl

# Install kubectl plugin locally using krew.
krew-install: krew-build
kubectl krew install --manifest=manifests/krew-kubectl-hns.yaml --archive=bin/kubectl-hns.tar.gz

# Uninstall kubectl plugin locally using krew.
krew-uninstall:
-kubectl krew uninstall hns

26 changes: 26 additions & 0 deletions incubator/hnc/cloudbuild.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,10 @@ steps:
export HNC_REGISTRY=$_HNC_REGISTRY
export HNC_IMG_NAME=$_HNC_IMG_NAME
export HNC_IMG_TAG=$_HNC_IMG_TAG
export HNC_RELEASE_REPO_OWNER=$_HNC_REPO_OWNER
echo "Building HNC manifests and plugin for $$HNC_REGISTRY/$$HNC_IMG_NAME:$$HNC_IMG_TAG"
make build
make krew-build
# Upload manifest
- name: gcr.io/cloud-builders/curl
args:
Expand Down Expand Up @@ -61,6 +63,30 @@ steps:
- '-u'
- '$_HNC_USER:$_HNC_PERSONAL_ACCESS_TOKEN'
- 'https://uploads.github.com/repos/$_HNC_REPO_OWNER/multi-tenancy/releases/$_HNC_RELEASE_ID/assets?name=kubectl-hns_darwin_amd64'
# Upload plugin (Krew tar file)
- name: gcr.io/cloud-builders/curl
args:
- '-X'
- 'POST'
- '-H'
- 'Content-Type: application/x-application'
- '--data-binary'
- '@multi-tenancy/incubator/hnc/bin/kubectl-hns.tar.gz'
- '-u'
- '$_HNC_USER:$_HNC_PERSONAL_ACCESS_TOKEN'
- 'https://uploads.github.com/repos/$_HNC_REPO_OWNER/multi-tenancy/releases/$_HNC_RELEASE_ID/assets?name=kubectl-hns.tar.gz'
# Upload plugin (Krew manifest file - to add to the Krew index)
- name: gcr.io/cloud-builders/curl
args:
- '-X'
- 'POST'
- '-H'
- 'Content-Type: application/x-application'
- '--data-binary'
- '@multi-tenancy/incubator/hnc/manifests/krew-kubectl-hns.yaml'
- '-u'
- '$_HNC_USER:$_HNC_PERSONAL_ACCESS_TOKEN'
- 'https://uploads.github.com/repos/$_HNC_REPO_OWNER/multi-tenancy/releases/$_HNC_RELEASE_ID/assets?name=krew-kubectl-hns.yaml'
# Build Docker image
- name: gcr.io/cloud-builders/docker
args: ['build', '-t', 'gcr.io/$PROJECT_ID/$_HNC_IMG_NAME:$_HNC_IMG_TAG', 'multi-tenancy/incubator/hnc']
Expand Down
11 changes: 11 additions & 0 deletions incubator/hnc/docs/releasing.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,17 @@ branch.

We may revise this guidance as HNC matures.

## Update Krew

Starting with HNC v0.6.x, the build process also generates a Krew tarball and
manifest. This manifest should be downloaded and checked into the Krew index,
*if* it's for the latest branch. E.g. if you've already released HNC v0.7.0 and
have to release HNC v0.6.1, do *not* update the Krew index; Krew can only
support one version of a plugin at a time so we should only support the most
recent branch.

More details on updating Krew TBD.

## Track usage

After the release, you can run the same command you used to find the release ID
Expand Down
23 changes: 0 additions & 23 deletions incubator/hnc/hack/krew-hierarchical-namespaces.yaml

This file was deleted.

35 changes: 35 additions & 0 deletions incubator/hnc/hack/krew-kubectl-hns.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
apiVersion: krew.googlecontainertools.github.com/v1alpha2
kind: Plugin
metadata:
name: hns
spec:
shortDescription: Manipulate hierarchical namespaces provided by HNC.
description: |
Manipulates hierarchical namespaces provided by the Hierarchical Namespace Controller (HNC).
version: HNC_IMG_TAG
homepage: https://github.com/HNC_RELEASE_REPO_OWNER/multi-tenancy/tree/master/incubator/hnc/docs/user-guide
platforms:
- uri: https://github.com/HNC_RELEASE_REPO_OWNER/multi-tenancy/releases/download/hnc-HNC_IMG_TAG/kubectl-hns.tar.gz
selector:
matchLabels:
os: linux
arch: amd64
sha256: HNC_KREW_TAR_SHA256
files:
- from: "bin/kubectl/kubectl-hns_linux_amd64"
to: "."
- from: "bin/kubectl/LICENSE"
to: "."
bin: "./kubectl-hns_linux_amd64"
- uri: https://github.com/HNC_RELEASE_REPO_OWNER/multi-tenancy/releases/download/hnc-HNC_IMG_TAG/kubectl-hns.tar.gz
selector:
matchLabels:
os: darwin
arch: amd64
sha256: HNC_KREW_TAR_SHA256
files:
- from: "bin/kubectl/kubectl-hns_darwin_amd64"
to: "."
- from: "bin/kubectl/LICENSE"
to: "."
bin: "./kubectl-hns_darwin_amd64"

0 comments on commit 6dfcfdd

Please sign in to comment.