Skip to content

Commit

Permalink
minor fixes (#4)
Browse files Browse the repository at this point in the history
* fix failed cases

* move to dapper and fixed integration tests

* fixed test cases

* v1.0.x and v1.1.x hardware generation

* Additional hardware test cases

* added port 443 to join url
  • Loading branch information
ibrokethecloud authored Oct 26, 2022
1 parent 9e27078 commit 3dc335a
Show file tree
Hide file tree
Showing 20 changed files with 320 additions and 157 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
bin/*
25 changes: 25 additions & 0 deletions Dockerfile.dapper
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM golang:1.18

ARG DAPPER_HOST_ARCH
ENV ARCH $DAPPER_HOST_ARCH

RUN export K8S_VERSION=1.24.2 && \
curl -sSLo envtest-bins.tar.gz "https://go.kubebuilder.io/test-tools/${K8S_VERSION}/$(go env GOOS)/$(go env GOARCH)" && \
mkdir /usr/local/kubebuilder && \
tar -C /usr/local/kubebuilder --strip-components=1 -zvxf envtest-bins.tar.gz && \
curl -sSLo kustomize.tar.gz "https://github.com/kubernetes-sigs/kustomize/releases/download/kustomize%2Fv4.5.7/kustomize_v4.5.7_linux_amd64.tar.gz" && \
tar -C /usr/bin --strip-components=1 -zxvf kustomize.tar.gz && \
go install sigs.k8s.io/controller-tools/cmd/[email protected]

RUN apt update && \
apt install -y bash git gcc docker.io vim less file curl wget ca-certificates qemu-utils

ENV DAPPER_ENV REPO TAG DRONE_TAG CROSS
ENV DAPPER_SOURCE /go/src/github.com/harvester/vm-import-controller
ENV DAPPER_OUTPUT ./bin ./pkg/api ./config
ENV DAPPER_DOCKER_SOCKET true
ENV DAPPER_RUN_ARGS "--network=host"
WORKDIR ${DAPPER_SOURCE}

ENTRYPOINT ["./scripts/entry"]
CMD ["ci"]
137 changes: 11 additions & 126 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,130 +1,15 @@
TARGETS := $(shell ls scripts)

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.23
.dapper:
@echo Downloading dapper
@curl -sL https://releases.rancher.com/dapper/latest/dapper-$$(uname -s)-$$(uname -m) > .dapper.tmp
@@chmod +x .dapper.tmp
@./.dapper.tmp -v
@mv .dapper.tmp .dapper

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif
$(TARGETS): .dapper
./.dapper $@

# Setting SHELL to bash allows bash commands to be executed by recipes.
# This is a requirement for 'setup-envtest.sh' in the test target.
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
SHELL = /usr/bin/env bash -o pipefail
.SHELLFLAGS = -ec
.DEFAULT_GOAL := ci

.PHONY: all
all: build

##@ General

# The help target prints out all targets with their descriptions organized
# beneath their categories. The categories are represented by '##@' and the
# target descriptions by '##'. The awk commands is responsible for reading the
# entire set of makefiles included in this invocation, looking for lines of the
# file as xyz: ## something, and then pretty-format the target and help. Then,
# if there's a line with ##@ something, that gets pretty-printed as a category.
# More info on the usage of ANSI control characters for terminal formatting:
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
# More info on the awk command:
# http://linuxcommand.org/lc3_adv_awk.php

.PHONY: help
help: ## Display this help.
@awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m<target>\033[0m\n"} /^[a-zA-Z_0-9-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[1m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST)

##@ Development

.PHONY: manifests
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases

.PHONY: generate
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."

.PHONY: fmt
fmt: ## Run go fmt against code.
go fmt ./...

.PHONY: vet
vet: ## Run go vet against code.
go vet ./...

.PHONY: test
test: manifests generate fmt vet envtest ## Run tests.
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test -v -p 1 ./... -coverprofile cover.out

##@ Build

.PHONY: build
build: generate fmt vet ## Build manager binary.
go build -o bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker build -t ${IMG} .

.PHONY: docker-push
docker-push: ## Push docker image with the manager.
docker push ${IMG}

##@ Deployment

ifndef ignore-not-found
ignore-not-found = false
endif

.PHONY: install
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
$(KUSTOMIZE) build config/crd | kubectl apply -f -

.PHONY: uninstall
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default | kubectl apply -f -

.PHONY: undeploy
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config. Call with ignore-not-found=true to ignore resource not found errors during deletion.
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
.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])

KUSTOMIZE = $(shell pwd)/bin/kustomize
.PHONY: kustomize
kustomize: ## Download kustomize locally if necessary.
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])

ENVTEST = $(shell pwd)/bin/setup-envtest
.PHONY: envtest
envtest: ## Download envtest-setup locally if necessary.
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)

# 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 install $(2) ;\
rm -rf $$TMP_DIR ;\
}
endef
.PHONY: $(TARGETS)
21 changes: 21 additions & 0 deletions cover.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
mode: set
github.com/harvester/seeder/pkg/events/events.go:16.103,25.16 3 1
github.com/harvester/seeder/pkg/events/events.go:29.2,29.56 1 1
github.com/harvester/seeder/pkg/events/events.go:25.16,27.3 1 0
github.com/harvester/seeder/pkg/events/events.go:32.72,34.16 2 1
github.com/harvester/seeder/pkg/events/events.go:38.2,40.28 3 1
github.com/harvester/seeder/pkg/events/events.go:73.2,81.28 8 1
github.com/harvester/seeder/pkg/events/events.go:34.16,36.3 1 0
github.com/harvester/seeder/pkg/events/events.go:40.28,41.27 1 1
github.com/harvester/seeder/pkg/events/events.go:44.3,49.17 6 1
github.com/harvester/seeder/pkg/events/events.go:52.3,52.24 1 1
github.com/harvester/seeder/pkg/events/events.go:41.27,42.12 1 1
github.com/harvester/seeder/pkg/events/events.go:49.17,51.4 1 0
github.com/harvester/seeder/pkg/events/events.go:52.24,54.18 2 1
github.com/harvester/seeder/pkg/events/events.go:58.4,58.29 1 1
github.com/harvester/seeder/pkg/events/events.go:62.4,63.18 2 1
github.com/harvester/seeder/pkg/events/events.go:66.4,66.33 1 1
github.com/harvester/seeder/pkg/events/events.go:54.18,56.5 1 0
github.com/harvester/seeder/pkg/events/events.go:58.29,60.5 1 1
github.com/harvester/seeder/pkg/events/events.go:63.18,65.5 1 0
github.com/harvester/seeder/pkg/events/events.go:66.33,69.5 2 1
26 changes: 26 additions & 0 deletions package/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Build the manager binary
FROM golang:1.18 as builder

WORKDIR /workspace
# 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 go mod download

# Copy the go source
COPY main.go main.go
COPY pkg/ pkg/

# Build
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 go build -a -o manager main.go

# Use distroless as minimal base image to package the manager binary
# Refer to https://github.com/GoogleContainerTools/distroless for more details
FROM gcr.io/distroless/static:nonroot
WORKDIR /
COPY --from=builder /workspace/manager .
USER 65532:65532

ENTRYPOINT ["/manager"]
9 changes: 5 additions & 4 deletions pkg/api/v1alpha1/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package v1alpha1

// common constants not associated with a particular type
const (
DefaultNS = "metal-system"
TinkConfig = "tinkerbell"
DefaultAPIPort = "9345"
OverrideAPIPortLabel = "clusterPort.harvesterhci.io"
DefaultNS = "metal-system"
TinkConfig = "tinkerbell"
DefaultAPIPort = "9345"
OverrideAPIPortLabel = "clusterPort.harvesterhci.io"
OverrideRedfishPortLabel = "redfishPort.harvesterhci.io"
)

var (
Expand Down
6 changes: 5 additions & 1 deletion pkg/controllers/cluster_events_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,11 @@ func (r *ClusterEventReconciler) updateNodes(ctx context.Context, c *seederv1alp
}
username := s.Data["username"]
password := s.Data["password"]
e, err := events.NewEventFetcher(ctx, string(username), string(password), fmt.Sprintf("https://%s", i.Spec.Connection.Host))
bmcendpoint := fmt.Sprintf("https://%s", i.Spec.BaseboardManagementSpec.Connection.Host)
if port, ok := i.Labels[seederv1alpha1.OverrideRedfishPortLabel]; ok {
bmcendpoint = fmt.Sprintf("https://%s:%s", i.Spec.BaseboardManagementSpec.Connection.Host, port)
}
e, err := events.NewEventFetcher(ctx, string(username), string(password), bmcendpoint)
if err != nil {
return err
}
Expand Down
24 changes: 18 additions & 6 deletions pkg/controllers/cluster_events_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var _ = Describe("cluster events test", func() {

i = &seederv1alpha1.Inventory{
ObjectMeta: metav1.ObjectMeta{
Name: "event-test",
Name: "cluster-event-test",
Namespace: "default",
},
Spec: seederv1alpha1.InventorySpec{
Expand All @@ -61,7 +61,7 @@ var _ = Describe("cluster events test", func() {
Port: 623,
InsecureTLS: true,
AuthSecretRef: corev1.SecretReference{
Name: "event-test",
Name: "cluster-event-test",
Namespace: "default",
},
},
Expand All @@ -74,7 +74,7 @@ var _ = Describe("cluster events test", func() {

s = &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "event-test",
Name: "cluster-event-test",
Namespace: "default",
},
StringData: map[string]string{
Expand Down Expand Up @@ -213,6 +213,18 @@ var _ = Describe("cluster events test", func() {

It("check for cluster event reconcilliation", func() {
// poll for cluster to be ready for and for nodes to be patched with inventory info
Eventually(func() error {
iObj := &seederv1alpha1.Inventory{}
if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: i.Namespace, Name: i.Name}, iObj); err != nil {
return err
}
iObj.Labels = map[string]string{
seederv1alpha1.OverrideRedfishPortLabel: redfishPort,
}
return k8sClient.Update(ctx, iObj)

}, "30s", "5s").ShouldNot(HaveOccurred())

Eventually(func() error {
cObj := &seederv1alpha1.Cluster{}
fmt.Println(cObj.Spec.Nodes)
Expand Down Expand Up @@ -255,8 +267,8 @@ var _ = Describe("cluster events test", func() {
for _, a := range v.Status.Addresses {
if a.Address == iObj.Status.Address {
found = true
if _, ok := v.Labels["totalCpuCores"]; !ok {
return fmt.Errorf("waiting for cpu cores to be populated")
if _, ok := v.Labels["manufacturer"]; !ok {
return fmt.Errorf("waiting for manufacturer to be populated")
}
}
}
Expand All @@ -266,7 +278,7 @@ var _ = Describe("cluster events test", func() {
return fmt.Errorf("waiting to find node matching ip address allocated to inventory %s", iObj.Status.Address)
}
return nil
}, "60s", "5s").ShouldNot(HaveOccurred())
}, "120s", "5s").ShouldNot(HaveOccurred())
})

AfterEach(func() {
Expand Down
7 changes: 5 additions & 2 deletions pkg/controllers/inventory_event_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,11 @@ func (r *InventoryEventReconciller) getInventoryInfo(ctx context.Context, i *see
return fmt.Errorf("secret %s has no key password", s.Name)
}

bmcendpoint := i.Spec.BaseboardManagementSpec.Connection.Host
rc, err := events.NewEventFetcher(ctx, string(username), string(password), fmt.Sprintf("https://%s", bmcendpoint))
bmcendpoint := fmt.Sprintf("https://%s", i.Spec.BaseboardManagementSpec.Connection.Host)
if port, ok := i.Labels[seederv1alpha1.OverrideRedfishPortLabel]; ok {
bmcendpoint = fmt.Sprintf("https://%s:%s", i.Spec.BaseboardManagementSpec.Connection.Host, port)
}
rc, err := events.NewEventFetcher(ctx, string(username), string(password), bmcendpoint)
if err != nil {
return err
}
Expand Down
16 changes: 14 additions & 2 deletions pkg/controllers/inventory_event_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ var _ = Describe("Inventory event controller tests", func() {
})

It("check inventory and baremetal reconcile", func() {
Eventually(func() error {
iObj := &seederv1alpha1.Inventory{}
if err := k8sClient.Get(ctx, types.NamespacedName{Namespace: i.Namespace, Name: i.Name}, iObj); err != nil {
return err
}
iObj.Labels = map[string]string{
seederv1alpha1.OverrideRedfishPortLabel: redfishPort,
}
return k8sClient.Update(ctx, iObj)

}, "30s", "5s").ShouldNot(HaveOccurred())

Eventually(func() error {
iObj := &seederv1alpha1.Inventory{}
err := k8sClient.Get(ctx, types.NamespacedName{Namespace: i.Namespace, Name: i.Name}, iObj)
Expand All @@ -96,9 +108,9 @@ var _ = Describe("Inventory event controller tests", func() {
return err
}

_, ok := iObj.Labels["totalCpuCores"]
_, ok := iObj.Labels["manufacturer"]
if !ok {
return fmt.Errorf("waiting for totalCpuCores to be populated")
return fmt.Errorf("waiting for manufacturer to be populated")
}
return nil
}, "120s", "5s").ShouldNot(HaveOccurred())
Expand Down
9 changes: 3 additions & 6 deletions pkg/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import (
"github.com/harvester/seeder/pkg/mock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/ory/dockertest/v3/docker"
rufio "github.com/tinkerbell/rufio/api/v1alpha1"
"k8s.io/apimachinery/pkg/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
Expand All @@ -54,8 +53,8 @@ var (
//eg *errgroup.Group
//egctx context.Context
//setupLog = ctrl.Log.WithName("setup")
pool *dockertest.Pool
//redfishPort string
pool *dockertest.Pool
redfishPort string
redfishMock *dockertest.Resource
)

Expand Down Expand Up @@ -178,14 +177,12 @@ var _ = BeforeSuite(func() {
"--key",
"/mockup/localhost.key",
},
PortBindings: map[docker.Port][]docker.PortBinding{
"8000/tcp": {{HostPort: "443"}},
},
}

redfishMock, err = pool.BuildAndRunWithBuildOptions(redfishBuildOpts, redfishRunOpts)
Expect(err).NotTo(HaveOccurred())
time.Sleep(30 * time.Second)
redfishPort = redfishMock.GetPort("8000/tcp")
})

var _ = AfterSuite(func() {
Expand Down
Loading

0 comments on commit 3dc335a

Please sign in to comment.