Skip to content

Commit

Permalink
update to latest capi; minor clean up; fix docker build (k3s-io#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
zawachte authored Oct 6, 2022
1 parent bf7f69e commit 1138354
Show file tree
Hide file tree
Showing 29 changed files with 869 additions and 370 deletions.
56 changes: 53 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,55 @@
FROM gcr.io/distroless/static:nonroot
# Build the manager binary
# Run this with docker build --build-arg builder_image=<golang:x.y.z>
ARG builder_image

# Build architecture
ARG ARCH

# Ignore Hadolint rule "Always tag the version of an image explicitly."
# It's an invalid finding since the image is explicitly set in the Makefile.
# https://github.com/hadolint/hadolint/wiki/DL3006
# hadolint ignore=DL3006
FROM ${builder_image} as builder
WORKDIR /workspace

# Run this with docker build --build-arg goproxy=$(go env GOPROXY) to override the goproxy
ARG goproxy=https://proxy.golang.org
# Run this with docker build --build-arg package=./controlplane/kubeadm or --build-arg package=./bootstrap/kubeadm
ENV GOPROXY=$goproxy

# Copy the Go Modules manifests
COPY go.mod go.mod
COPY go.sum go.sum

# Cache deps before building and copying source so that we don't need to re-download as much
# and so that source changes don't invalidate our downloaded layer
RUN --mount=type=cache,target=/go/pkg/mod \
go mod download

# Copy the sources
COPY ./ ./

# Cache the go build into the Go’s compiler cache folder so we take benefits of compiler caching across docker build calls
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
go build ./bootstrap/main.go

# Build
ARG package=.
ARG ARCH
ARG ldflags

# Do not force rebuild of up-to-date packages (do not use -a) and use the compiler cache folder
RUN --mount=type=cache,target=/root/.cache/go-build \
--mount=type=cache,target=/go/pkg/mod \
CGO_ENABLED=0 GOOS=linux GOARCH=${ARCH} \
go build -trimpath -ldflags "${ldflags} -extldflags '-static'" \
-o manager ${package}

# Production image
FROM gcr.io/distroless/static:nonroot-${ARCH}
WORKDIR /
COPY bin/manager ./
USER nonroot:nonroot
COPY --from=builder /workspace/manager .
# Use uid of nonroot user (65532) because kubernetes expects numeric user when applying pod security policies
USER 65532
ENTRYPOINT ["/manager"]
50 changes: 30 additions & 20 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,29 @@
GO_VERSION ?= 1.19.0
GO_CONTAINER_IMAGE ?= docker.io/library/golang:$(GO_VERSION)

ARCH ?= $(shell go env GOARCH)

# Use GOPROXY environment variable if set
GOPROXY := $(shell go env GOPROXY)
ifeq ($(GOPROXY),)
GOPROXY := https://proxy.golang.org
endif
export GOPROXY

# Active module mode, as we use go modules to manage dependencies
export GO111MODULE=on

GO_INSTALL := ./hack/go_install.sh

BIN_DIR := bin
TOOLS_BIN_DIR := $(abspath $(BIN_DIR))


# Image URL to use all building/pushing image targets
BOOTSTRAP_IMG ?= ghcr.io/zawachte/cluster-api-k3s/bootstrap-controller:v0.1.3
BOOTSTRAP_IMG ?= ghcr.io/zawachte/cluster-api-k3s/bootstrap-controller:v0.1.4

# Image URL to use all building/pushing image targets
CONTROLPLANE_IMG ?= ghcr.io/zawachte/cluster-api-k3s/controlplane-controller:v0.1.3
CONTROLPLANE_IMG ?= ghcr.io/zawachte/cluster-api-k3s/controlplane-controller:v0.1.4


# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
Expand All @@ -16,10 +36,14 @@ else
GOBIN=$(shell go env GOBIN)
endif

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
CONTROLLER_GEN_BIN = controller-gen
CONTROLLER_GEN_PKG = "sigs.k8s.io/controller-tools/cmd/controller-gen"
CONTROLLER_GEN_VER = "v0.8.0"
CONTROLLER_GEN := $(abspath $(TOOLS_BIN_DIR)/$(CONTROLLER_GEN_BIN)-$(CONTROLLER_GEN_VER))

.PHONY: controller-gen
controller-gen: ## Download controller-gen locally if necessary.
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) $(CONTROLLER_GEN_PKG) $(CONTROLLER_GEN_BIN) $(CONTROLLER_GEN_VER)

KUSTOMIZE = $(shell pwd)/bin/kustomize
.PHONY: kustomize
Expand Down Expand Up @@ -82,7 +106,7 @@ generate-bootstrap: controller-gen

# Build the docker image
docker-build-bootstrap: manager-bootstrap
docker build . -t ${BOOTSTRAP_IMG}
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./bootstrap/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${BOOTSTRAP_IMG}

# Push the docker image
docker-push-bootstrap:
Expand Down Expand Up @@ -130,22 +154,8 @@ generate-controlplane: controller-gen

# Build the docker image
docker-build-controlplane: manager-controlplane
docker build . -t ${CONTROLPLANE_IMG}
DOCKER_BUILDKIT=1 docker build --build-arg builder_image=$(GO_CONTAINER_IMAGE) --build-arg goproxy=$(GOPROXY) --build-arg ARCH=$(ARCH) --build-arg package=./controlplane/main.go --build-arg ldflags="$(LDFLAGS)" . -t ${CONTROLPLANE_IMG}

# Push the docker image
docker-push-controlplane:
docker push ${CONTROLPLANE_IMG}

# go-get-tool will 'go get' any package $2 and install it to $1.
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
define go-get-tool
@[ -f $(1) ] || { \
set -e ;\
TMP_DIR=$$(mktemp -d) ;\
cd $$TMP_DIR ;\
go mod init tmp ;\
echo "Downloading $(2)" ;\
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: kthreesconfigs.bootstrap.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: kthreesconfigtemplates.bootstrap.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: kthreescontrolplanes.controlplane.cluster.x-k8s.io
spec:
Expand All @@ -17,8 +16,8 @@ spec:
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: This denotes whether or not the control plane has the uploaded
kubeadm-config configmap
- description: This denotes whether or not the control plane has completed the
k3s server initialization
jsonPath: .status.initialized
name: Initialized
type: boolean
Expand Down Expand Up @@ -302,7 +301,6 @@ spec:
type: string
required:
- infrastructureTemplate
- kthreesConfigSpec
- version
type: object
status:
Expand Down Expand Up @@ -363,8 +361,8 @@ spec:
for programmatic interpretation.
type: string
initialized:
description: Initialized denotes whether or not the control plane
has the uploaded kubeadm-config configmap.
description: Initialized denotes whether or not the k3s server is
up.
type: boolean
observedGeneration:
description: ObservedGeneration is the latest generation observed
Expand Down
2 changes: 1 addition & 1 deletion bootstrap/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: controller
newName: ghcr.io/zawachte/cluster-api-k3s/bootstrap-controller
newTag: v0.1.3
newTag: v0.1.4
2 changes: 1 addition & 1 deletion bootstrap/config/rbac/auth_proxy_client_clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: rbac.authorization.k8s.io/v1beta1
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: metrics-reader
Expand Down
1 change: 0 additions & 1 deletion bootstrap/config/rbac/role.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
Expand Down
8 changes: 3 additions & 5 deletions controlplane/api/v1beta1/condition_consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ const (
)

const (
// AvailableCondition documents that the first control plane instance has completed the kubeadm init operation
// AvailableCondition documents that the first control plane instance has completed the server init operation
// and so the control plane is available and an API server instance is ready for processing requests.
AvailableCondition clusterv1.ConditionType = "Available"

// WaitingForKthreesServerReason (Severity=Info) documents a KThreesControlPlane object waiting for the first
// control plane instance to complete the kubeadm init operation.
// control plane instance to complete the k3s server operation.
WaitingForKthreesServerReason = "WaitingForKthreesServer"
)

Expand All @@ -68,9 +68,7 @@ const (
)

const (
// ControlPlaneComponentsHealthyCondition reports the overall status of control plane components
// implemented as static pods generated by kubeadm including kube-api-server, kube-controller manager,
// kube-scheduler and etcd if managed.
// ControlPlaneComponentsHealthyCondition reports the overall status of the k3s server.
ControlPlaneComponentsHealthyCondition clusterv1.ConditionType = "ControlPlaneComponentsHealthy"

// ControlPlaneComponentsUnhealthyReason (Severity=Error) documents a control plane component not healthy.
Expand Down
5 changes: 2 additions & 3 deletions controlplane/api/v1beta1/kthreescontrolplane_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,8 +101,7 @@ type KThreesControlPlaneStatus struct {
// +optional
UnavailableReplicas int32 `json:"unavailableReplicas,omitempty"`

// Initialized denotes whether or not the control plane has the
// uploaded kubeadm-config configmap.
// Initialized denotes whether or not the k3s server is initialized.
// +optional
Initialized bool `json:"initialized"`

Expand Down Expand Up @@ -134,7 +133,7 @@ type KThreesControlPlaneStatus struct {
// +kubebuilder:object:root=true
// +kubebuilder:subresource:status
// +kubebuilder:subresource:scale:specpath=.spec.replicas,statuspath=.status.replicas,selectorpath=.status.selector
// +kubebuilder:printcolumn:name="Initialized",type=boolean,JSONPath=".status.initialized",description="This denotes whether or not the control plane has the uploaded kubeadm-config configmap"
// +kubebuilder:printcolumn:name="Initialized",type=boolean,JSONPath=".status.initialized",description="This denotes whether or not the control plane has completed the k3s server initialization"
// +kubebuilder:printcolumn:name="API Server Available",type=boolean,JSONPath=".status.ready",description="KThreesControlPlane API Server is ready to receive requests"
// +kubebuilder:printcolumn:name="Version",type=string,JSONPath=".spec.version",description="Kubernetes version associated with this control plane"
// +kubebuilder:printcolumn:name="Replicas",type=integer,JSONPath=".status.replicas",description="Total number of non-terminated machines targeted by this control plane"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: kthreesconfigs.bootstrap.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: kthreesconfigtemplates.bootstrap.cluster.x-k8s.io
spec:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@

---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.7.0
controller-gen.kubebuilder.io/version: v0.8.0
creationTimestamp: null
name: kthreescontrolplanes.controlplane.cluster.x-k8s.io
spec:
Expand All @@ -17,8 +16,8 @@ spec:
scope: Namespaced
versions:
- additionalPrinterColumns:
- description: This denotes whether or not the control plane has the uploaded
kubeadm-config configmap
- description: This denotes whether or not the control plane has completed the
k3s server initialization
jsonPath: .status.initialized
name: Initialized
type: boolean
Expand Down Expand Up @@ -302,7 +301,6 @@ spec:
type: string
required:
- infrastructureTemplate
- kthreesConfigSpec
- version
type: object
status:
Expand Down Expand Up @@ -363,8 +361,8 @@ spec:
for programmatic interpretation.
type: string
initialized:
description: Initialized denotes whether or not the control plane
has the uploaded kubeadm-config configmap.
description: Initialized denotes whether or not the k3s server is
up.
type: boolean
observedGeneration:
description: ObservedGeneration is the latest generation observed
Expand Down
2 changes: 1 addition & 1 deletion controlplane/config/manager/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ kind: Kustomization
images:
- name: controller
newName: ghcr.io/zawachte/cluster-api-k3s/controlplane-controller
newTag: v0.1.3
newTag: v0.1.4
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
apiVersion: rbac.authorization.k8s.io/v1beta1
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: metrics-reader
Expand Down
12 changes: 0 additions & 12 deletions controlplane/controllers/kthreescontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -514,12 +514,6 @@ func (r *KThreesControlPlaneReconciler) reconcile(ctx context.Context, cluster *
return ctrl.Result{Requeue: true}, nil
}
// Ensure kubeadm role bindings for v1.18+
if err := workloadCluster.AllowBootstrapTokensToGetNodes(ctx); err != nil {
return ctrl.Result{}, errors.Wrap(err, "failed to set role and role binding for kubeadm")
}
// Update kube-proxy daemonset.
if err := workloadCluster.UpdateKubeProxyImageInfo(ctx, kcp); err != nil {
logger.Error(err, "failed to update kube-proxy daemonset")
Expand Down Expand Up @@ -661,12 +655,6 @@ func (r *KThreesControlPlaneReconciler) upgradeControlPlane(
return ctrl.Result{}, errors.Wrapf(err, "failed to parse kubernetes version %q", kcp.Spec.Version)
}
// Ensure kubeadm cluster role & bindings for v1.18+
// as per https://github.com/kubernetes/kubernetes/commit/b117a928a6c3f650931bdac02a41fca6680548c4
// if err := workloadCluster.AllowBootstrapTokensToGetNodes(ctx); err != nil {
// return ctrl.Result{}, errors.Wrap(err, "failed to set role and role binding for kubeadm")
// }
if kcp.Spec.KThreesConfigSpec.ClusterConfiguration != nil {
imageRepository := kcp.Spec.KThreesConfigSpec.ClusterConfiguration.ImageRepository
Expand Down
Loading

0 comments on commit 1138354

Please sign in to comment.