Skip to content

Commit

Permalink
chore: bump deps, implement unit tests
Browse files Browse the repository at this point in the history
Implement unit-tests for the controller.
Bump dependencies.

Signed-off-by: Artem Chernyshev <[email protected]>
  • Loading branch information
Unix4ever committed Apr 19, 2023
1 parent e9b6948 commit feaa35f
Show file tree
Hide file tree
Showing 12 changed files with 1,183 additions and 460 deletions.
19 changes: 19 additions & 0 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,25 @@ steps:
- name: docker
path: /root/.docker/buildx

- name: unit-tests
image: autonomy/build-container:latest
pull: always
environment:
PLATFORM: linux/amd64,linux/arm64
commands:
- make unit-tests
when:
event:
include:
- pull_request
volumes:
- name: docker-socket
path: /var/run
- name: outerdockersock
path: /var/outer-run
- name: docker
path: /root/.docker/buildx

- name: e2e-aws
image: autonomy/build-container:latest
pull: always
Expand Down
16 changes: 13 additions & 3 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,10 @@ ENV GO111MODULE on
ENV GOPROXY https://proxy.golang.org
ENV GOCACHE /.cache/go-build
ENV GOMODCACHE /.cache/mod
RUN --mount=type=cache,target=/.cache go install sigs.k8s.io/controller-tools/cmd/[email protected]
RUN --mount=type=cache,target=/.cache go install k8s.io/code-generator/cmd/[email protected]
ARG CONTROLLER_GEN_VERSION
ARG CONVERSION_GEN_VERSION
RUN --mount=type=cache,target=/.cache go install sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_GEN_VERSION}
RUN --mount=type=cache,target=/.cache go install k8s.io/code-generator/cmd/conversion-gen@${CONVERSION_GEN_VERSION}
WORKDIR /src
COPY ./go.mod ./
COPY ./go.sum ./
Expand All @@ -47,6 +49,14 @@ RUN --mount=type=cache,target=/.cache controller-gen object:headerFile=./hack/bo
FROM scratch AS generate
COPY --from=generate-build /src/api /api

# runs unit-tests
FROM build AS unit-tests-run
ARG TESTPKGS
RUN --mount=type=cache,target=/root/.cache/go-build --mount=type=cache,target=/go/pkg --mount=type=cache,target=/tmp go test -v -covermode=atomic -coverprofile=coverage.txt -coverpkg=${TESTPKGS} -count 1 ${TESTPKGS}

FROM scratch AS unit-tests
COPY --from=unit-tests-run /src/coverage.txt /coverage.txt

FROM --platform=${BUILDPLATFORM} alpine:3.13 AS release-build
ADD https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.1.0/kustomize_v4.1.0_linux_amd64.tar.gz .
RUN tar -xf kustomize_v4.1.0_linux_amd64.tar.gz -C /usr/local/bin && rm kustomize_v4.1.0_linux_amd64.tar.gz
Expand Down Expand Up @@ -82,4 +92,4 @@ COPY --from=pkg-ca-certificates / /
COPY --from=pkg-fhs / /
COPY --from=binary /manager /manager
LABEL org.opencontainers.image.source https://github.com/siderolabs/cluster-api-control-plane-provider-talos
ENTRYPOINT [ "/manager" ]
ENTRYPOINT [ "/manager" ]
14 changes: 12 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ REGISTRY_AND_USERNAME := $(REGISTRY)/$(USERNAME)
NAME := cluster-api-control-plane-talos-controller
WITH_RACE ?= false
CGO_ENABLED = 0
TESTPKGS ?= ./controllers/...
CONTROLLER_GEN_VERSION ?= v0.11.3
CONVERSION_GEN_VERSION ?= v0.26.0

ifneq (, $(filter $(WITH_RACE), t true TRUE y yes 1))
GO_BUILDFLAGS += -race
Expand All @@ -18,8 +21,8 @@ GO_LDFLAGS += -s -w

ARTIFACTS := _out

TOOLS ?= ghcr.io/siderolabs/tools:v1.3.0-1-g712379c
PKGS ?= v1.3.0
TOOLS ?= ghcr.io/siderolabs/tools:v1.4.0-1-g955aabc
PKGS ?= v1.4.1-5-ga333a84

BUILD := docker buildx build
PLATFORM ?= linux/amd64
Expand All @@ -36,6 +39,9 @@ COMMON_ARGS += --build-arg=TOOLS=$(TOOLS)
COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"
COMMON_ARGS += --build-arg=GO_LDFLAGS="$(GO_LDFLAGS)"
COMMON_ARGS += --build-arg=CGO_ENABLED="$(CGO_ENABLED)"
COMMON_ARGS += --build-arg=TESTPKGS="$(TESTPKGS)"
COMMON_ARGS += --build-arg=CONTROLLER_GEN_VERSION=$(CONTROLLER_GEN_VERSION)
COMMON_ARGS += --build-arg=CONVERSION_GEN_VERSION=$(CONVERSION_GEN_VERSION)

all: manifests container

Expand Down Expand Up @@ -131,3 +137,7 @@ integration-test-build:
.PHONY: integration-test
integration-test: integration-test-build
@REGISTRY_AND_USERNAME=$(REGISTRY_AND_USERNAME) TAG=$(TAG) NAME=$(NAME) bash hack/test/e2e-aws.sh

.PHONY: unit-tests
unit-tests: ## Performs unit tests
@$(MAKE) local-$@ DEST=$(ARTIFACTS)
23 changes: 23 additions & 0 deletions controllers/configs.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,14 @@ import (
"reflect"
"time"

"github.com/pkg/errors"
cabptv1 "github.com/siderolabs/cluster-api-bootstrap-provider-talos/api/v1alpha3"
controlplanev1 "github.com/siderolabs/cluster-api-control-plane-provider-talos/api/v1alpha3"
talosclient "github.com/siderolabs/talos/pkg/machinery/client"
talosconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/util/connrotation"
Expand Down Expand Up @@ -49,6 +51,27 @@ func (r *TalosControlPlaneReconciler) talosconfigForMachines(ctx context.Context

clusterName := tcp.GetLabels()["cluster.x-k8s.io/cluster-name"]

for _, ref := range tcp.GetOwnerReferences() {
if ref.Kind != "Cluster" {
continue
}

gv, err := schema.ParseGroupVersion(ref.APIVersion)
if err != nil {
return nil, errors.WithStack(err)
}

if gv.Group == clusterv1.GroupVersion.Group {
clusterName = ref.Name

break
}
}

if clusterName == "" {
return nil, fmt.Errorf("failed to determine the cluster name of the control plane")
}

if !reflect.ValueOf(tcp.Spec.ControlPlaneConfig.InitConfig).IsZero() {
return r.talosconfigFromWorkloadCluster(ctx, client.ObjectKey{Namespace: tcp.GetNamespace(), Name: clusterName}, machines...)
}
Expand Down
Loading

0 comments on commit feaa35f

Please sign in to comment.