Skip to content

Commit

Permalink
Refactor snapcraft build pipeline (canonical#3324)
Browse files Browse the repository at this point in the history
  • Loading branch information
neoaggelos authored Jul 21, 2022
1 parent 9f2d030 commit 2cd93a8
Show file tree
Hide file tree
Showing 82 changed files with 703 additions and 587 deletions.
2 changes: 0 additions & 2 deletions CODEOWNERS

This file was deleted.

2 changes: 2 additions & 0 deletions build-scripts/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
.build
.install
7 changes: 7 additions & 0 deletions build-scripts/addon-repositories.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

ADDONS_REPOS="
core,${CORE_ADDONS_REPO:-https://github.com/canonical/microk8s-core-addons},${CORE_ADDONS_REPO_BRANCH:-main}
community,${COMMUNITY_ADDONS_REPO:-https://github.com/canonical/microk8s-community-addons},${COMMUNITY_ADDONS_REPO_BRANCH:-main}
"
ADDONS_REPOS_ENABLED="core"
63 changes: 63 additions & 0 deletions build-scripts/build-component.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/bin/bash

set -ex

DIR=`realpath $(dirname "${0}")`

BUILD_DIRECTORY="${SNAPCRAFT_PART_BUILD:-${DIR}/.build}"
INSTALL_DIRECTORY="${SNAPCRAFT_PART_INSTALL:-${DIR}/.install}"

STRICT="${STRICT:-false}"
if [ "x${STRICT}" == "xfalse" ]; then
PROJECT_DIR="${SNAPCRAFT_PROJECT_DIR:-${DIR}/..}"
if cat "${PROJECT_DIR}/snap/snapcraft.yaml" | grep "confinement:" | grep strict > /dev/null; then
STRICT="true"
fi
fi

mkdir -p "${BUILD_DIRECTORY}" "${INSTALL_DIRECTORY}"

COMPONENT_NAME="${1}"
COMPONENT_DIRECTORY="${DIR}/components/${COMPONENT_NAME}"

GIT_REPOSITORY="$(cat "${COMPONENT_DIRECTORY}/repository")"
GIT_TAG="$("${COMPONENT_DIRECTORY}/version.sh")"

COMPONENT_BUILD_DIRECTORY="${BUILD_DIRECTORY}/${COMPONENT_NAME}"

# cleanup git repository if we cannot git checkout to the build tag
if [ -d "${COMPONENT_BUILD_DIRECTORY}" ]; then
cd "${COMPONENT_BUILD_DIRECTORY}"
if ! git checkout "${GIT_TAG}"; then
cd "${BUILD_DIRECTORY}"
rm -rf "${COMPONENT_BUILD_DIRECTORY}"
fi
fi

if [ ! -d "${COMPONENT_BUILD_DIRECTORY}" ]; then
git clone "${GIT_REPOSITORY}" --depth 1 -b "${GIT_TAG}" "${COMPONENT_BUILD_DIRECTORY}"
fi

cd "${COMPONENT_BUILD_DIRECTORY}"
git config user.name "MicroK8s builder bot"
git config user.email "[email protected]"
if echo "${GIT_TAG}" | grep -e rc -e alpha -e beta; then
if [ -d "${COMPONENT_DIRECTORY}/pre-patches" ]; then
for patch in "${COMPONENT_DIRECTORY}"/pre-patches/*; do
git am < "${patch}"
done
fi
else
if [ -d "${COMPONENT_DIRECTORY}/patches" ]; then
for patch in "${COMPONENT_DIRECTORY}"/patches/*; do
git am < "${patch}"
done
fi
fi
if [ "x${STRICT}" == "xtrue" ] && [ -d "${COMPONENT_DIRECTORY}/strict-patches" ]; then
for patch in "${COMPONENT_DIRECTORY}"/strict-patches/*; do
git am < "${patch}"
done
fi

bash -xe "${COMPONENT_DIRECTORY}/build.sh" "${INSTALL_DIRECTORY}"
45 changes: 0 additions & 45 deletions build-scripts/build-k8s-binaries.sh

This file was deleted.

25 changes: 25 additions & 0 deletions build-scripts/components/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Parts directory

This directory contains the build scripts for Go components built into MicroK8s.

The directory structure looks like this:

```
build-scripts/
build-component.sh <-- runs as `build-component.sh $component_name`
checks out the git repository, applies the specified
patches, then runs the `build.sh` step for the component
component/
$component_name/
repository <-- git repository to clone
version.sh <-- prints the repository tag or commit to checkout
build.sh <-- runs as `build.sh $output`
first argument is the output directory where
binaries should be placed.
patches/
... <-- list of patches to apply after checkout (for stable versions)
pre-patches/
... <-- list of patches to apply after checkout (for pre-releases)
strict-patches/
... <-- list of patches to apply when building strictly confined snap
```
7 changes: 7 additions & 0 deletions build-scripts/components/cluster-agent/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

export INSTALL="${1}/bin"
mkdir -p "${INSTALL}"

make cluster-agent
cp cluster-agent "${INSTALL}"
1 change: 1 addition & 0 deletions build-scripts/components/cluster-agent/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/canonical/microk8s-cluster-agent
3 changes: 3 additions & 0 deletions build-scripts/components/cluster-agent/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "main"
8 changes: 8 additions & 0 deletions build-scripts/components/cni/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

INSTALL="${1}/opt/cni/bin"
mkdir -p "${INSTALL}"

./build_linux.sh

cp bin/* "${INSTALL}/"
1 change: 1 addition & 0 deletions build-scripts/components/cni/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/containernetworking/plugins
4 changes: 4 additions & 0 deletions build-scripts/components/cni/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/bash

# Match https://github.com/kubernetes/kubernetes/blob/master/build/dependencies.yaml#L20
echo "v0.9.1"
9 changes: 9 additions & 0 deletions build-scripts/components/containerd/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/bash

INSTALL="${1}/bin"
mkdir -p "${INSTALL}"

for bin in ctr containerd containerd-shim containerd-shim-runc-v1 containerd-shim-runc-v2; do
make "bin/${bin}"
cp "bin/${bin}" "${INSTALL}/${bin}"
done
1 change: 1 addition & 0 deletions build-scripts/components/containerd/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/containerd/containerd
3 changes: 3 additions & 0 deletions build-scripts/components/containerd/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v1.6.6"
7 changes: 7 additions & 0 deletions build-scripts/components/crictl/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#!/bin/bash

export INSTALL="${1}/bin"
mkdir -p "${INSTALL}"

make crictl
cp build/bin/crictl "${INSTALL}/crictl"
25 changes: 25 additions & 0 deletions build-scripts/components/crictl/patches/0001-Add-s-w-ldflags.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
From facd1c26a641d2e6664bda7b5e1aaf9ff2c3309a Mon Sep 17 00:00:00 2001
From: Angelos Kolaitis <[email protected]>
Date: Fri, 15 Jul 2022 10:40:10 +0300
Subject: [PATCH] Add -s -w ldflags

---
Makefile | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/Makefile b/Makefile
index f5a1160..49b314f 100644
--- a/Makefile
+++ b/Makefile
@@ -34,7 +34,7 @@ BINDIR ?= /usr/local/bin

VERSION := $(shell git describe --tags --dirty --always)
VERSION := $(VERSION:v%=%)
-GO_LDFLAGS := -X $(PROJECT)/pkg/version.Version=$(VERSION)
+GO_LDFLAGS := -X $(PROJECT)/pkg/version.Version=$(VERSION) -s -w

BUILD_PATH := $(shell pwd)/build
BUILD_BIN_PATH := $(BUILD_PATH)/bin
--
2.25.1

1 change: 1 addition & 0 deletions build-scripts/components/crictl/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/kubernetes-sigs/cri-tools
3 changes: 3 additions & 0 deletions build-scripts/components/crictl/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v1.24.2"
11 changes: 11 additions & 0 deletions build-scripts/components/dqlite-client/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

INSTALL="${1}/bin"
mkdir -p "${INSTALL}"

export CGO_LDFLAGS_ALLOW="-Wl,-z,now"
export CGO_CFLAGS="-I${SNAPCRAFT_STAGE}/usr/include/"
export CGO_LDFLAGS="-L${SNAPCRAFT_STAGE}/lib"
go build -ldflags "-s -w" -tags libsqlite3 ./cmd/dqlite

cp dqlite "${INSTALL}/dqlite"
1 change: 1 addition & 0 deletions build-scripts/components/dqlite-client/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/canonical/go-dqlite
3 changes: 3 additions & 0 deletions build-scripts/components/dqlite-client/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v1.11.1"
24 changes: 24 additions & 0 deletions build-scripts/components/dqlite/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash

INSTALL="${1}"

[ ! -f ./configure ] && [ -f ./autogen.sh ] && env NOCONFIGURE=1 ./autogen.sh
[ ! -f ./configure ] && [ -f ./bootstrap ] && env NOCONFIGURE=1 ./bootstrap
[ ! -f ./configure ] && autoreconf --install

export SQLITE_CFLAGS="-I${SNAPCRAFT_STAGE}/usr/include"
export SQLITE_LIBS="-L${SNAPCRAFT_STAGE}/lib -lsqlite3"
export RAFT_CFLAGS="-I${SNAPCRAFT_STAGE}/usr/include"
export RAFT_LIBS="-L${SNAPCRAFT_STAGE}/lib -lraft"

./configure

mkdir -p build

make -j"${SNAPCRAFT_PARALLEL_BUILD_COUNT}"
make install DESTDIR="${PWD}/build"

mkdir -p "${INSTALL}/lib" "${INSTALL}/usr/include"

cp -r "build/usr/local/lib/libdqlite"*"so"* "${INSTALL}/lib/"
cp -r "build/usr/local/include/"* "${INSTALL}/usr/include/"
1 change: 1 addition & 0 deletions build-scripts/components/dqlite/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/canonical/dqlite
3 changes: 3 additions & 0 deletions build-scripts/components/dqlite/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v1.11.1"
10 changes: 10 additions & 0 deletions build-scripts/components/etcd/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#!/bin/bash

export INSTALL="${1}"
mkdir -p "${INSTALL}"

GO_LDFLAGS="-s -w" make

for bin in etcd etcdctl; do
cp "bin/${bin}" "${INSTALL}/${bin}"
done
1 change: 1 addition & 0 deletions build-scripts/components/etcd/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/etcd-io/etcd
3 changes: 3 additions & 0 deletions build-scripts/components/etcd/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v3.4.3"
19 changes: 19 additions & 0 deletions build-scripts/components/flanneld/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

INSTALL="${1}/opt/cni/bin"
mkdir -p "${INSTALL}"

# TODO(neoaggelos): update to a non-ancient version of flanneld that we can actually build
FLANNELD_VERSION="v0.11.0"
ARCH="${ARCH:-`dpkg --print-architecture`}"
if [ "$ARCH" = "ppc64el" ]; then
ARCH="ppc64le"
elif [ "$ARCH" = "armhf" ]; then
ARCH="arm"
fi
mkdir -p download
cd download
curl -LO https://github.com/coreos/flannel/releases/download/${FLANNELD_VERSION}/flannel-${FLANNELD_VERSION}-linux-${ARCH}.tar.gz
tar xvzf flannel-*.tar.gz

cp flanneld "${INSTALL}/flanneld"
1 change: 1 addition & 0 deletions build-scripts/components/flanneld/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/flannel-io/flannel
3 changes: 3 additions & 0 deletions build-scripts/components/flanneld/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v0.11.0"
20 changes: 20 additions & 0 deletions build-scripts/components/helm/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

INSTALL="${1}"
mkdir -p "${INSTALL}/bin"

make
cp bin/helm "${INSTALL}/bin/helm"

./bin/helm completion bash \
| sed "s/complete -o default -F __start_helm helm/complete -o default -F __start_helm microk8s.helm/g" \
| sed "s/complete -o default -o nospace -F __start_helm helm/complete -o default -o nospace -F __start_helm microk8s.helm/g" \
> bin/helm.bash

./bin/helm completion bash \
| sed "s/complete -o default -F __start_helm helm/complete -o default -F __start_helm microk8s.helm3/g" \
| sed "s/complete -o default -o nospace -F __start_helm helm/complete -o default -o nospace -F __start_helm microk8s.helm3/g" \
> bin/helm3.bash

cp bin/helm.bash "${INSTALL}/helm.bash"
cp bin/helm3.bash "${INSTALL}/helm3.bash"
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
From d08d2604a9f0f925b93b87d74ee0a2c26c785467 Mon Sep 17 00:00:00 2001
From: Angelos Kolaitis <[email protected]>
Date: Thu, 14 Jul 2022 18:18:09 +0300
Subject: [PATCH] disable warnings for kubeconfig permissions

---
cmd/helm/root.go | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cmd/helm/root.go b/cmd/helm/root.go
index ef92fea9..512f823f 100644
--- a/cmd/helm/root.go
+++ b/cmd/helm/root.go
@@ -206,7 +206,7 @@ func newRootCmd(actionConfig *action.Configuration, out io.Writer, args []string
loadPlugins(cmd, out)

// Check permissions on critical files
- checkPerms()
+ // checkPerms()

// Check for expired repositories
checkForExpiredRepos(settings.RepositoryConfig)
--
2.25.1
1 change: 1 addition & 0 deletions build-scripts/components/helm/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/helm/helm
3 changes: 3 additions & 0 deletions build-scripts/components/helm/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v3.9.1"
11 changes: 11 additions & 0 deletions build-scripts/components/k8s-dqlite/build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/bin/bash

INSTALL="${1}/bin"
mkdir -p "${INSTALL}"

export CGO_LDFLAGS_ALLOW="-Wl,-z,now"
export CGO_CFLAGS="-I${SNAPCRAFT_STAGE}/usr/include/"
export CGO_LDFLAGS="-L${SNAPCRAFT_STAGE}/lib"
go build -ldflags "-s -w" -tags libsqlite3,dqlite .

cp k8s-dqlite "${INSTALL}/k8s-dqlite"
1 change: 1 addition & 0 deletions build-scripts/components/k8s-dqlite/repository
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
https://github.com/canonical/k8s-dqlite
3 changes: 3 additions & 0 deletions build-scripts/components/k8s-dqlite/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash

echo "v1.0.4"
Loading

0 comments on commit 2cd93a8

Please sign in to comment.