Skip to content

Commit feaa35f

Browse files
committed
chore: bump deps, implement unit tests
Implement unit-tests for the controller. Bump dependencies. Signed-off-by: Artem Chernyshev <[email protected]>
1 parent e9b6948 commit feaa35f

File tree

12 files changed

+1183
-460
lines changed

12 files changed

+1183
-460
lines changed

.drone.yml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,25 @@ steps:
7171
- name: docker
7272
path: /root/.docker/buildx
7373

74+
- name: unit-tests
75+
image: autonomy/build-container:latest
76+
pull: always
77+
environment:
78+
PLATFORM: linux/amd64,linux/arm64
79+
commands:
80+
- make unit-tests
81+
when:
82+
event:
83+
include:
84+
- pull_request
85+
volumes:
86+
- name: docker-socket
87+
path: /var/run
88+
- name: outerdockersock
89+
path: /var/outer-run
90+
- name: docker
91+
path: /root/.docker/buildx
92+
7493
- name: e2e-aws
7594
image: autonomy/build-container:latest
7695
pull: always

Dockerfile

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,10 @@ ENV GO111MODULE on
2424
ENV GOPROXY https://proxy.golang.org
2525
ENV GOCACHE /.cache/go-build
2626
ENV GOMODCACHE /.cache/mod
27-
RUN --mount=type=cache,target=/.cache go install sigs.k8s.io/controller-tools/cmd/[email protected]
28-
RUN --mount=type=cache,target=/.cache go install k8s.io/code-generator/cmd/[email protected]
27+
ARG CONTROLLER_GEN_VERSION
28+
ARG CONVERSION_GEN_VERSION
29+
RUN --mount=type=cache,target=/.cache go install sigs.k8s.io/controller-tools/cmd/controller-gen@${CONTROLLER_GEN_VERSION}
30+
RUN --mount=type=cache,target=/.cache go install k8s.io/code-generator/cmd/conversion-gen@${CONVERSION_GEN_VERSION}
2931
WORKDIR /src
3032
COPY ./go.mod ./
3133
COPY ./go.sum ./
@@ -47,6 +49,14 @@ RUN --mount=type=cache,target=/.cache controller-gen object:headerFile=./hack/bo
4749
FROM scratch AS generate
4850
COPY --from=generate-build /src/api /api
4951

52+
# runs unit-tests
53+
FROM build AS unit-tests-run
54+
ARG TESTPKGS
55+
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}
56+
57+
FROM scratch AS unit-tests
58+
COPY --from=unit-tests-run /src/coverage.txt /coverage.txt
59+
5060
FROM --platform=${BUILDPLATFORM} alpine:3.13 AS release-build
5161
ADD https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.1.0/kustomize_v4.1.0_linux_amd64.tar.gz .
5262
RUN tar -xf kustomize_v4.1.0_linux_amd64.tar.gz -C /usr/local/bin && rm kustomize_v4.1.0_linux_amd64.tar.gz
@@ -82,4 +92,4 @@ COPY --from=pkg-ca-certificates / /
8292
COPY --from=pkg-fhs / /
8393
COPY --from=binary /manager /manager
8494
LABEL org.opencontainers.image.source https://github.com/siderolabs/cluster-api-control-plane-provider-talos
85-
ENTRYPOINT [ "/manager" ]
95+
ENTRYPOINT [ "/manager" ]

Makefile

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ REGISTRY_AND_USERNAME := $(REGISTRY)/$(USERNAME)
77
NAME := cluster-api-control-plane-talos-controller
88
WITH_RACE ?= false
99
CGO_ENABLED = 0
10+
TESTPKGS ?= ./controllers/...
11+
CONTROLLER_GEN_VERSION ?= v0.11.3
12+
CONVERSION_GEN_VERSION ?= v0.26.0
1013

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

1922
ARTIFACTS := _out
2023

21-
TOOLS ?= ghcr.io/siderolabs/tools:v1.3.0-1-g712379c
22-
PKGS ?= v1.3.0
24+
TOOLS ?= ghcr.io/siderolabs/tools:v1.4.0-1-g955aabc
25+
PKGS ?= v1.4.1-5-ga333a84
2326

2427
BUILD := docker buildx build
2528
PLATFORM ?= linux/amd64
@@ -36,6 +39,9 @@ COMMON_ARGS += --build-arg=TOOLS=$(TOOLS)
3639
COMMON_ARGS += --build-arg=GO_BUILDFLAGS="$(GO_BUILDFLAGS)"
3740
COMMON_ARGS += --build-arg=GO_LDFLAGS="$(GO_LDFLAGS)"
3841
COMMON_ARGS += --build-arg=CGO_ENABLED="$(CGO_ENABLED)"
42+
COMMON_ARGS += --build-arg=TESTPKGS="$(TESTPKGS)"
43+
COMMON_ARGS += --build-arg=CONTROLLER_GEN_VERSION=$(CONTROLLER_GEN_VERSION)
44+
COMMON_ARGS += --build-arg=CONVERSION_GEN_VERSION=$(CONVERSION_GEN_VERSION)
3945

4046
all: manifests container
4147

@@ -131,3 +137,7 @@ integration-test-build:
131137
.PHONY: integration-test
132138
integration-test: integration-test-build
133139
@REGISTRY_AND_USERNAME=$(REGISTRY_AND_USERNAME) TAG=$(TAG) NAME=$(NAME) bash hack/test/e2e-aws.sh
140+
141+
.PHONY: unit-tests
142+
unit-tests: ## Performs unit tests
143+
@$(MAKE) local-$@ DEST=$(ARTIFACTS)

controllers/configs.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,14 @@ import (
1111
"reflect"
1212
"time"
1313

14+
"github.com/pkg/errors"
1415
cabptv1 "github.com/siderolabs/cluster-api-bootstrap-provider-talos/api/v1alpha3"
1516
controlplanev1 "github.com/siderolabs/cluster-api-control-plane-provider-talos/api/v1alpha3"
1617
talosclient "github.com/siderolabs/talos/pkg/machinery/client"
1718
talosconfig "github.com/siderolabs/talos/pkg/machinery/client/config"
1819
corev1 "k8s.io/api/core/v1"
1920
v1 "k8s.io/api/core/v1"
21+
"k8s.io/apimachinery/pkg/runtime/schema"
2022
"k8s.io/apimachinery/pkg/types"
2123
"k8s.io/client-go/kubernetes"
2224
"k8s.io/client-go/util/connrotation"
@@ -49,6 +51,27 @@ func (r *TalosControlPlaneReconciler) talosconfigForMachines(ctx context.Context
4951

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

54+
for _, ref := range tcp.GetOwnerReferences() {
55+
if ref.Kind != "Cluster" {
56+
continue
57+
}
58+
59+
gv, err := schema.ParseGroupVersion(ref.APIVersion)
60+
if err != nil {
61+
return nil, errors.WithStack(err)
62+
}
63+
64+
if gv.Group == clusterv1.GroupVersion.Group {
65+
clusterName = ref.Name
66+
67+
break
68+
}
69+
}
70+
71+
if clusterName == "" {
72+
return nil, fmt.Errorf("failed to determine the cluster name of the control plane")
73+
}
74+
5275
if !reflect.ValueOf(tcp.Spec.ControlPlaneConfig.InitConfig).IsZero() {
5376
return r.talosconfigFromWorkloadCluster(ctx, client.ObjectKey{Namespace: tcp.GetNamespace(), Name: clusterName}, machines...)
5477
}

0 commit comments

Comments
 (0)