Skip to content

Commit

Permalink
chore: add basic integration test
Browse files Browse the repository at this point in the history
It will be improved later.

Signed-off-by: Alexey Palazhchenko <[email protected]>
  • Loading branch information
AlekSi authored and smira committed Sep 1, 2021
1 parent 9fb0d07 commit ad592d1
Show file tree
Hide file tree
Showing 14 changed files with 513 additions and 154 deletions.
32 changes: 29 additions & 3 deletions .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,15 @@ kind: pipeline
type: kubernetes
name: default

services:
- name: docker
image: docker:20.10-dind
entrypoint: [dockerd]
privileged: true
volumes:
- name: docker-socket
path: /var/run

steps:
- name: setup-ci
image: autonomy/build-container:latest
Expand Down Expand Up @@ -36,6 +45,26 @@ steps:
- name: docker
path: /root/.docker/buildx

- name: test-pull-request
image: autonomy/build-container:latest
pull: always
environment:
PLATFORM: linux/amd64,linux/arm64
commands:
- make env-up
- make test
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: build-and-publish
image: autonomy/build-container:latest
pull: always
Expand Down Expand Up @@ -126,8 +155,5 @@ trigger:

depends_on:
- default
---
kind: signature
hmac: 2847e7d6100e958e0391d8c3822c13fca9fb50de86ec5ee648a9eacce0978e5b

...
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,7 @@ bin
*.swp
*.swo
*~
_out/
_out/

talosctl
kubeconfig
11 changes: 9 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,23 @@ FROM build AS manifests-build
ARG NAME
RUN --mount=type=cache,target=/.cache controller-gen crd:crdVersions=v1 paths="./api/..." output:crd:dir=config/crd/bases output:webhook:dir=config/webhook webhook
RUN --mount=type=cache,target=/.cache controller-gen rbac:roleName=manager-role paths="./controllers/..." output:rbac:dir=config/rbac

FROM scratch AS manifests
COPY --from=manifests-build /src/config /config

FROM build AS generate-build
RUN --mount=type=cache,target=/.cache controller-gen object:headerFile=./hack/boilerplate.go.txt paths="./..."
RUN --mount=type=cache,target=/.cache conversion-gen --input-dirs=./api/v1alpha2 --output-base ./ --output-file-base=zz_generated.conversion --go-header-file=./hack/boilerplate.go.txt
RUN --mount=type=cache,target=/.cache conversion-gen --input-dirs=./api/v1alpha2 --output-base ./ --output-file-base=zz_generated.conversion --go-header-file=./hack/boilerplate.go.txt

FROM scratch AS generate
COPY --from=generate-build /src/api /api

FROM build AS integration-test-build
RUN --mount=type=cache,target=/.cache go test -v -c ./internal/integration

FROM scratch AS integration-test
COPY --from=integration-test-build /src/integration.test /integration.test

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 All @@ -68,7 +75,7 @@ COPY --from=release-build /metadata.yaml /metadata.yaml

FROM build AS binary
ARG TARGETARCH
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /manager
RUN --mount=type=cache,target=/.cache GOOS=linux GOARCH=${TARGETARCH} go build -ldflags "-s -w" -o /manager
RUN chmod +x /manager

FROM scratch AS container
Expand Down
25 changes: 25 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ ARTIFACTS := _out

TOOLS ?= ghcr.io/talos-systems/tools:v0.5.0
PKGS ?= v0.5.0
TALOS_VERSION ?= v0.12.0
K8S_VERSION ?= 1.21.4

BUILD := docker buildx build
PLATFORM ?= linux/amd64
Expand Down Expand Up @@ -106,3 +108,26 @@ run: install ## Run the controller locally. This is for testing purposes only.
.PHONY: clean
clean:
@rm -rf $(ARTIFACTS)

# Make `make test` behave just like `go test` regarding relative paths.
test:
@$(MAKE) local-integration-test DEST=./internal/integration PLATFORM=linux/amd64
cd internal/integration && KUBECONFIG=../../kubeconfig ./integration.test -test.v

talosctl:
curl -Lo talosctl https://github.com/talos-systems/talos/releases/download/$(TALOS_VERSION)/talosctl-$(shell uname -s | tr "[:upper:]" "[:lower:]")-amd64
chmod +x ./talosctl

env-up: talosctl
./talosctl cluster create \
--name=cabpt-env \
--kubernetes-version=$(K8S_VERSION) \
--mtu=1450 \
--memory=2048 \
--cpus=2.0 \
--crashdump
./talosctl -n 10.5.0.2 kubeconfig -f kubeconfig

env-down: talosctl
./talosctl cluster destroy \
--name=cabpt-env
37 changes: 37 additions & 0 deletions controllers/scheme.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/*
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package controllers

import (
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/kubernetes/scheme"
capiv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
expv1 "sigs.k8s.io/cluster-api/exp/api/v1alpha3"

bootstrapv1alpha2 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha2"
bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
// +kubebuilder:scaffold:imports
)

// Use shared scheme for all calls.
func init() {
utilruntime.Must(capiv1.AddToScheme(scheme.Scheme))
utilruntime.Must(expv1.AddToScheme(scheme.Scheme))
utilruntime.Must(bootstrapv1alpha2.AddToScheme(scheme.Scheme))
utilruntime.Must(bootstrapv1alpha3.AddToScheme(scheme.Scheme))

// +kubebuilder:scaffold:scheme
}
15 changes: 8 additions & 7 deletions controllers/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,16 @@ package controllers
import (
"context"

bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
"github.com/talos-systems/crypto/x509"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1/generate"
"gopkg.in/yaml.v2"
corev1 "k8s.io/api/core/v1"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
capiv1 "sigs.k8s.io/cluster-api/api/v1alpha3"
"sigs.k8s.io/controller-runtime/pkg/client"

bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
)

func (r *TalosConfigReconciler) fetchSecret(ctx context.Context, config *bootstrapv1alpha3.TalosConfig, secretName string) (*corev1.Secret, error) {
Expand Down Expand Up @@ -65,10 +66,10 @@ func (r *TalosConfigReconciler) writeInputSecret(ctx context.Context, scope *Tal
Namespace: scope.Config.Namespace,
Name: scope.Cluster.Name + "-talos",
Labels: map[string]string{
clusterv1.ClusterLabelName: scope.Cluster.Name,
capiv1.ClusterLabelName: scope.Cluster.Name,
},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(scope.Cluster, clusterv1.GroupVersion.WithKind("Cluster")),
*metav1.NewControllerRef(scope.Cluster, capiv1.GroupVersion.WithKind("Cluster")),
},
},
Data: map[string][]byte{
Expand All @@ -94,10 +95,10 @@ func (r *TalosConfigReconciler) writeK8sCASecret(ctx context.Context, scope *Tal
Namespace: scope.Config.Namespace,
Name: scope.Cluster.Name + "-ca",
Labels: map[string]string{
clusterv1.ClusterLabelName: scope.Cluster.Name,
capiv1.ClusterLabelName: scope.Cluster.Name,
},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(scope.Cluster, clusterv1.GroupVersion.WithKind("Cluster")),
*metav1.NewControllerRef(scope.Cluster, capiv1.GroupVersion.WithKind("Cluster")),
},
},
Data: map[string][]byte{
Expand Down Expand Up @@ -131,7 +132,7 @@ func (r *TalosConfigReconciler) writeBootstrapData(ctx context.Context, scope *T
Namespace: scope.Config.Namespace,
Name: ownerName + "-bootstrap-data",
Labels: map[string]string{
clusterv1.ClusterLabelName: scope.Cluster.Name,
capiv1.ClusterLabelName: scope.Cluster.Name,
},
OwnerReferences: []metav1.OwnerReference{
*metav1.NewControllerRef(scope.Config, bootstrapv1alpha3.GroupVersion.WithKind("TalosConfig")),
Expand Down
82 changes: 0 additions & 82 deletions controllers/suite_test.go

This file was deleted.

3 changes: 2 additions & 1 deletion controllers/talosconfig_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (

jsonpatch "github.com/evanphx/json-patch"
"github.com/go-logr/logr"
bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
"github.com/talos-systems/talos/pkg/machinery/config"
"github.com/talos-systems/talos/pkg/machinery/config/configpatcher"
"github.com/talos-systems/talos/pkg/machinery/config/types/v1alpha1"
Expand All @@ -51,6 +50,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/source"

bootstrapv1alpha3 "github.com/talos-systems/cluster-api-bootstrap-provider-talos/api/v1alpha3"
)

const (
Expand Down
6 changes: 3 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ go 1.16
require (
github.com/evanphx/json-patch v4.11.0+incompatible
github.com/go-logr/logr v0.1.0
github.com/onsi/ginkgo v1.16.2
github.com/onsi/gomega v1.13.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/talos-systems/crypto v0.3.1
github.com/talos-systems/talos/pkg/machinery v0.11.3
golang.org/x/sys v0.0.0-20210816074244-15123e1e1f71
gopkg.in/yaml.v2 v2.4.0
k8s.io/api v0.17.9
k8s.io/apiextensions-apiserver v0.17.9
k8s.io/apimachinery v0.17.9
k8s.io/client-go v0.17.9
k8s.io/utils v0.0.0-20200619165400-6e3d28b6ed19
sigs.k8s.io/cluster-api v0.3.12
sigs.k8s.io/cluster-api v0.3.22
sigs.k8s.io/controller-runtime v0.5.14
)
Loading

0 comments on commit ad592d1

Please sign in to comment.