Skip to content

Commit 24cb51f

Browse files
authored
Merge pull request #5 from elastx/node-group-udpate
Add support for nodegroup matching and update to newer deps
2 parents ac28a32 + 31909b0 commit 24cb51f

19 files changed

Lines changed: 674 additions & 847 deletions

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Build the manager binary
2-
FROM golang:1.16 as builder
2+
FROM golang:1.22 as builder
33

44
WORKDIR /workspace
55
# Copy the Go Modules manifests

Makefile

Lines changed: 70 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Image URL to use all building/pushing image targets
33
IMG ?= controller:latest
44
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5-
ENVTEST_K8S_VERSION = 1.22
5+
ENVTEST_K8S_VERSION = 1.28.0
66

77
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
88
ifeq (,$(shell go env GOBIN))
@@ -11,8 +11,13 @@ else
1111
GOBIN=$(shell go env GOBIN)
1212
endif
1313

14+
# CONTAINER_TOOL defines the container tool to be used for building images.
15+
# Be aware that the target commands are only tested with Docker which is
16+
# scaffolded by default. However, you might want to replace it to use other
17+
# tools. (i.e. podman)
18+
CONTAINER_TOOL ?= docker
19+
1420
# Setting SHELL to bash allows bash commands to be executed by recipes.
15-
# This is a requirement for 'setup-envtest.sh' in the test target.
1621
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
1722
SHELL = /usr/bin/env bash -o pipefail
1823
.SHELLFLAGS = -ec
@@ -24,7 +29,7 @@ all: build
2429

2530
# The help target prints out all targets with their descriptions organized
2631
# beneath their categories. The categories are represented by '##@' and the
27-
# target descriptions by '##'. The awk commands is responsible for reading the
32+
# target descriptions by '##'. The awk command is responsible for reading the
2833
# entire set of makefiles included in this invocation, looking for lines of the
2934
# file as xyz: ## something, and then pretty-format the target and help. Then,
3035
# if there's a line with ##@ something, that gets pretty-printed as a category.
@@ -57,29 +62,45 @@ vet: ## Run go vet against code.
5762

5863
.PHONY: test
5964
test: manifests generate fmt vet envtest ## Run tests.
60-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out
61-
62-
.PHONY: test-debug
63-
test-debug: manifests generate fmt vet envtest ## Run tests.
64-
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) -p path)" go test ./... -coverprofile cover.out -ginkgo.v -ginkgo.progress -test.v
65+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
6566

6667
##@ Build
6768

6869
.PHONY: build
69-
build: generate fmt vet ## Build manager binary.
70+
build: manifests generate fmt vet ## Build manager binary.
7071
go build -o bin/manager main.go
7172

7273
.PHONY: run
7374
run: manifests generate fmt vet ## Run a controller from your host.
74-
go run ./main.go
75+
ENABLE_WEBHOOKS="false" go run ./main.go
7576

77+
# If you wish to build the manager image targeting other platforms you can use the --platform flag.
78+
# (i.e. docker build --platform linux/arm64). However, you must enable docker buildKit for it.
79+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
7680
.PHONY: docker-build
77-
docker-build: test ## Build docker image with the manager.
78-
docker build -t ${IMG} .
81+
docker-build: ## Build docker image with the manager.
82+
$(CONTAINER_TOOL) build -t ${IMG} .
7983

8084
.PHONY: docker-push
8185
docker-push: ## Push docker image with the manager.
82-
docker push ${IMG}
86+
$(CONTAINER_TOOL) push ${IMG}
87+
88+
# PLATFORMS defines the target platforms for the manager image be built to provide support to multiple
89+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
90+
# - be able to use docker buildx. More info: https://docs.docker.com/build/buildx/
91+
# - have enabled BuildKit. More info: https://docs.docker.com/develop/develop-images/build_enhancements/
92+
# - be able to push the image to your registry (i.e. if you do not set a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
93+
# To adequately provide solutions that are compatible with multiple platforms, you should consider using this option.
94+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
95+
.PHONY: docker-buildx
96+
docker-buildx: ## Build and push docker image for the manager for cross-platform support
97+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
98+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
99+
- $(CONTAINER_TOOL) buildx create --name project-v3-builder
100+
$(CONTAINER_TOOL) buildx use project-v3-builder
101+
- $(CONTAINER_TOOL) buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
102+
- $(CONTAINER_TOOL) buildx rm project-v3-builder
103+
rm Dockerfile.cross
83104

84105
##@ Deployment
85106

@@ -89,46 +110,54 @@ endif
89110

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

94115
.PHONY: uninstall
95116
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.
96-
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
117+
$(KUSTOMIZE) build config/crd | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
97118

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

103124
.PHONY: undeploy
104125
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.
105-
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
126+
$(KUSTOMIZE) build config/default | $(KUBECTL) delete --ignore-not-found=$(ignore-not-found) -f -
106127

107-
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
108-
.PHONY: controller-gen
109-
controller-gen: ## Download controller-gen locally if necessary.
110-
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/controller-gen@v0.7.0)
128+
##@ Build Dependencies
129+
130+
## Location to install dependencies to
131+
LOCALBIN ?= $(shell pwd)/bin
132+
$(LOCALBIN):
133+
mkdir -p $(LOCALBIN)
134+
135+
## Tool Binaries
136+
KUBECTL ?= kubectl
137+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
138+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
139+
ENVTEST ?= $(LOCALBIN)/setup-envtest
140+
141+
## Tool Versions
142+
KUSTOMIZE_VERSION ?= v5.1.1
143+
CONTROLLER_TOOLS_VERSION ?= v0.14.0
111144

112-
KUSTOMIZE = $(shell pwd)/bin/kustomize
113145
.PHONY: kustomize
114-
kustomize: ## Download kustomize locally if necessary.
115-
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/v3@v3.8.7)
146+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
147+
$(KUSTOMIZE): $(LOCALBIN)
148+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
149+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
150+
rm -rf $(LOCALBIN)/kustomize; \
151+
fi
152+
test -s $(LOCALBIN)/kustomize || GOBIN=$(LOCALBIN) GO111MODULE=on go install sigs.k8s.io/kustomize/kustomize/v5@$(KUSTOMIZE_VERSION)
153+
154+
.PHONY: controller-gen
155+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
156+
$(CONTROLLER_GEN): $(LOCALBIN)
157+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
158+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
116159

117-
ENVTEST = $(shell pwd)/bin/setup-envtest
118160
.PHONY: envtest
119-
envtest: ## Download envtest-setup locally if necessary.
120-
$(call go-get-tool,$(ENVTEST),sigs.k8s.io/controller-runtime/tools/setup-envtest@latest)
121-
122-
# go-get-tool will 'go get' any package $2 and install it to $1.
123-
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
124-
define go-get-tool
125-
@[ -f $(1) ] || { \
126-
set -e ;\
127-
TMP_DIR=$$(mktemp -d) ;\
128-
cd $$TMP_DIR ;\
129-
go mod init tmp ;\
130-
echo "Downloading $(2)" ;\
131-
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
132-
rm -rf $$TMP_DIR ;\
133-
}
134-
endef
161+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
162+
$(ENVTEST): $(LOCALBIN)
163+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

PROJECT

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,23 @@
11
domain: elx.cloud
22
layout:
3-
- go.kubebuilder.io/v3
3+
- go.kubebuilder.io/v3
44
projectName: elx-nodegroup-controller
55
repo: github.com/elastx/elx-nodegroup-controller
66
resources:
7-
- api:
8-
crdVersion: v1
9-
controller: true
10-
domain: elx.cloud
11-
group: k8s
12-
kind: NodeGroup
13-
path: github.com/elastx/elx-nodegroup-controller/api/v1alpha1
14-
version: v1alpha1
7+
- api:
8+
crdVersion: v1
9+
controller: true
10+
domain: elx.cloud
11+
group: k8s
12+
kind: NodeGroup
13+
path: github.com/elastx/elx-nodegroup-controller/api/v1alpha1
14+
version: v1alpha1
15+
- api:
16+
crdVersion: v2
17+
controller: true
18+
domain: elx.cloud
19+
group: k8s
20+
kind: NodeGroup
21+
path: github.com/elastx/elx-nodegroup-controller/api/v1alpha2
22+
version: v1alpha2
1523
version: "3"

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@ kustomize build config/default | kubectl apply -f -
1212
## Sample nodegroup manifest
1313

1414
```yml
15-
apiVersion: k8s.elx.cloud/v1alpha1
15+
apiVersion: k8s.elx.cloud/v1alpha2
1616
kind: NodeGroup
1717
metadata:
1818
name: nodegroup-sample
1919
spec:
20-
members:
20+
members:
2121
- node1 # Kubernetes node name
22+
nodeGroupNames:
23+
- node0 # Kubernetes nodegroup name, used for clusters with dynamic node naming
2224
labels:
2325
name: value
2426
taints:

api/v1alpha1/groupversion_info.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ limitations under the License.
1515
*/
1616

1717
// Package v1alpha1 contains API Schema definitions for the k8s v1alpha1 API group
18-
//+kubebuilder:object:generate=true
19-
//+groupName=k8s.elx.cloud
18+
// +kubebuilder:object:generate=true
19+
// +groupName=k8s.elx.cloud
2020
package v1alpha1
2121

2222
import (

api/v1alpha1/zz_generated.deepcopy.go

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/v1alpha2/groupversion_info.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
// Package v1alpha2 contains API Schema definitions for the k8s v1alpha1 API group
18+
// +kubebuilder:object:generate=true
19+
// +groupName=k8s.elx.cloud
20+
package v1alpha2
21+
22+
import (
23+
"k8s.io/apimachinery/pkg/runtime/schema"
24+
"sigs.k8s.io/controller-runtime/pkg/scheme"
25+
)
26+
27+
var (
28+
// GroupVersion is group version used to register these objects
29+
GroupVersion = schema.GroupVersion{Group: "k8s.elx.cloud", Version: "v1alpha2"}
30+
31+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
32+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
33+
34+
// AddToScheme adds the types in this group-version to the given scheme.
35+
AddToScheme = SchemeBuilder.AddToScheme
36+
)

api/v1alpha2/nodegroup_types.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
Copyright 2022.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package v1alpha2
18+
19+
import (
20+
corev1 "k8s.io/api/core/v1"
21+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22+
)
23+
24+
type NodeGroupSpec struct {
25+
//+optional
26+
Members []string `json:"members"`
27+
//+optional
28+
NodeGroupNames []string `json:"nodeGroupNames"`
29+
//+optional
30+
Labels map[string]string `json:"labels,omitEmpty"`
31+
//+optional
32+
Taints []corev1.Taint `json:"taints,omitEmpty"`
33+
}
34+
35+
type NodeGroupStatus struct {
36+
}
37+
38+
//+kubebuilder:object:root=true
39+
//+kubebuilder:storageversion
40+
//+kubebuilder:subresource:status
41+
//+kubebuilder:resource:scope=Cluster
42+
43+
// NodeGroup is the Schema for the nodegroups API
44+
type NodeGroup struct {
45+
metav1.TypeMeta `json:",inline"`
46+
metav1.ObjectMeta `json:"metadata,omitempty"`
47+
48+
Spec NodeGroupSpec `json:"spec,omitempty"`
49+
Status NodeGroupStatus `json:"status,omitempty"`
50+
}
51+
52+
//+kubebuilder:object:root=true
53+
54+
// NodeGroupList contains a list of NodeGroup
55+
type NodeGroupList struct {
56+
metav1.TypeMeta `json:",inline"`
57+
metav1.ListMeta `json:"metadata,omitempty"`
58+
Items []NodeGroup `json:"items"`
59+
}
60+
61+
func init() {
62+
SchemeBuilder.Register(&NodeGroup{}, &NodeGroupList{})
63+
}

0 commit comments

Comments
 (0)