Skip to content

Commit 9c36fee

Browse files
set up kubebuilder boilerplate
1 parent f804737 commit 9c36fee

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2079
-0
lines changed

.dockerignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.gitignore

.gitignore

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
bin
8+
artifacts
9+
testbin/*
10+
.DS_Store
11+
# Test binary, build with `go test -c`
12+
*.test
13+
14+
# Output of the go coverage tool, specifically when used with LiteIDE
15+
*.out
16+
17+
18+
# editor and IDE paraphernalia
19+
.idea
20+
.vscode
21+
*.swp
22+
*.swo
23+
*~
24+
25+
# vendor files
26+
vendor
27+
28+
# Local Netlify folder
29+
.netlify
30+
31+
# Directory with CRDs for dependencies
32+
dep-crds

CHANGELOG/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
placeholder

Dockerfile

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Build the manager binary
2+
FROM golang:1.19 as builder
3+
ARG TARGETOS
4+
ARG TARGETARCH
5+
6+
WORKDIR /workspace
7+
# Copy the Go Modules manifests
8+
COPY go.mod go.mod
9+
COPY go.sum go.sum
10+
# cache deps before building and copying source so that we don't need to re-download as much
11+
# and so that source changes don't invalidate our downloaded layer
12+
RUN go mod download
13+
14+
# Copy the go source
15+
COPY main.go main.go
16+
COPY api/ api/
17+
COPY controllers/ controllers/
18+
19+
# Build
20+
# the GOARCH has not a default value to allow the binary be built according to the host where the command
21+
# was called. For example, if we call make docker-build in a local env which has the Apple Silicon M1 SO
22+
# the docker BUILDPLATFORM arg will be linux/arm64 when for Apple x86 it will be linux/amd64. Therefore,
23+
# by leaving it empty we can ensure that the container and binary shipped on it will have the same platform.
24+
RUN CGO_ENABLED=0 GOOS=${TARGETOS:-linux} GOARCH=${TARGETARCH} go build -a -o manager main.go
25+
26+
# Use distroless as minimal base image to package the manager binary
27+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
28+
FROM gcr.io/distroless/static:nonroot
29+
WORKDIR /
30+
COPY --from=builder /workspace/manager .
31+
USER 65532:65532
32+
33+
ENTRYPOINT ["/manager"]

Makefile

+157
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= controller:latest
4+
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
5+
ENVTEST_K8S_VERSION = 1.26.0
6+
7+
# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
8+
ifeq (,$(shell go env GOBIN))
9+
GOBIN=$(shell go env GOPATH)/bin
10+
else
11+
GOBIN=$(shell go env GOBIN)
12+
endif
13+
14+
# Setting SHELL to bash allows bash commands to be executed by recipes.
15+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
16+
SHELL = /usr/bin/env bash -o pipefail
17+
.SHELLFLAGS = -ec
18+
19+
.PHONY: all
20+
all: build
21+
22+
##@ General
23+
24+
# The help target prints out all targets with their descriptions organized
25+
# beneath their categories. The categories are represented by '##@' and the
26+
# target descriptions by '##'. The awk commands is responsible for reading the
27+
# entire set of makefiles included in this invocation, looking for lines of the
28+
# file as xyz: ## something, and then pretty-format the target and help. Then,
29+
# if there's a line with ##@ something, that gets pretty-printed as a category.
30+
# More info on the usage of ANSI control characters for terminal formatting:
31+
# https://en.wikipedia.org/wiki/ANSI_escape_code#SGR_parameters
32+
# More info on the awk command:
33+
# http://linuxcommand.org/lc3_adv_awk.php
34+
35+
.PHONY: help
36+
help: ## Display this help.
37+
@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)
38+
39+
##@ Development
40+
41+
.PHONY: manifests
42+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
43+
$(CONTROLLER_GEN) rbac:roleName=manager-role crd webhook paths="./..." output:crd:artifacts:config=config/crd/bases
44+
45+
.PHONY: generate
46+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
47+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
48+
49+
.PHONY: fmt
50+
fmt: ## Run go fmt against code.
51+
go fmt ./...
52+
53+
.PHONY: vet
54+
vet: ## Run go vet against code.
55+
go vet ./...
56+
57+
.PHONY: test
58+
test: manifests generate fmt vet envtest ## Run tests.
59+
KUBEBUILDER_ASSETS="$(shell $(ENVTEST) use $(ENVTEST_K8S_VERSION) --bin-dir $(LOCALBIN) -p path)" go test ./... -coverprofile cover.out
60+
61+
##@ Build
62+
63+
.PHONY: build
64+
build: manifests generate fmt vet ## Build manager binary.
65+
go build -o bin/manager main.go
66+
67+
.PHONY: run
68+
run: manifests generate fmt vet ## Run a controller from your host.
69+
go run ./main.go
70+
71+
# If you wish built the manager image targeting other platforms you can use the --platform flag.
72+
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
73+
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
74+
.PHONY: docker-build
75+
docker-build: test ## Build docker image with the manager.
76+
docker build -t ${IMG} .
77+
78+
.PHONY: docker-push
79+
docker-push: ## Push docker image with the manager.
80+
docker push ${IMG}
81+
82+
# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
83+
# architectures. (i.e. make docker-buildx IMG=myregistry/mypoperator:0.0.1). To use this option you need to:
84+
# - able to use docker buildx . More info: https://docs.docker.com/build/buildx/
85+
# - have enable BuildKit, More info: https://docs.docker.com/develop/develop-images/build_enhancements/
86+
# - be able to push the image for your registry (i.e. if you do not inform a valid value via IMG=<myregistry/image:<tag>> then the export will fail)
87+
# To properly provided solutions that supports more than one platform you should use this option.
88+
PLATFORMS ?= linux/arm64,linux/amd64,linux/s390x,linux/ppc64le
89+
.PHONY: docker-buildx
90+
docker-buildx: test ## Build and push docker image for the manager for cross-platform support
91+
# copy existing Dockerfile and insert --platform=${BUILDPLATFORM} into Dockerfile.cross, and preserve the original Dockerfile
92+
sed -e '1 s/\(^FROM\)/FROM --platform=\$$\{BUILDPLATFORM\}/; t' -e ' 1,// s//FROM --platform=\$$\{BUILDPLATFORM\}/' Dockerfile > Dockerfile.cross
93+
- docker buildx create --name project-v3-builder
94+
docker buildx use project-v3-builder
95+
- docker buildx build --push --platform=$(PLATFORMS) --tag ${IMG} -f Dockerfile.cross .
96+
- docker buildx rm project-v3-builder
97+
rm Dockerfile.cross
98+
99+
##@ Deployment
100+
101+
ifndef ignore-not-found
102+
ignore-not-found = false
103+
endif
104+
105+
.PHONY: install
106+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
107+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
108+
109+
.PHONY: uninstall
110+
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.
111+
$(KUSTOMIZE) build config/crd | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
112+
113+
.PHONY: deploy
114+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
115+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
116+
$(KUSTOMIZE) build config/default | kubectl apply -f -
117+
118+
.PHONY: undeploy
119+
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.
120+
$(KUSTOMIZE) build config/default | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
121+
122+
##@ Build Dependencies
123+
124+
## Location to install dependencies to
125+
LOCALBIN ?= $(shell pwd)/bin
126+
$(LOCALBIN):
127+
mkdir -p $(LOCALBIN)
128+
129+
## Tool Binaries
130+
KUSTOMIZE ?= $(LOCALBIN)/kustomize
131+
CONTROLLER_GEN ?= $(LOCALBIN)/controller-gen
132+
ENVTEST ?= $(LOCALBIN)/setup-envtest
133+
134+
## Tool Versions
135+
KUSTOMIZE_VERSION ?= v3.8.7
136+
CONTROLLER_TOOLS_VERSION ?= v0.11.1
137+
138+
KUSTOMIZE_INSTALL_SCRIPT ?= "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh"
139+
.PHONY: kustomize
140+
kustomize: $(KUSTOMIZE) ## Download kustomize locally if necessary. If wrong version is installed, it will be removed before downloading.
141+
$(KUSTOMIZE): $(LOCALBIN)
142+
@if test -x $(LOCALBIN)/kustomize && ! $(LOCALBIN)/kustomize version | grep -q $(KUSTOMIZE_VERSION); then \
143+
echo "$(LOCALBIN)/kustomize version is not expected $(KUSTOMIZE_VERSION). Removing it before installing."; \
144+
rm -rf $(LOCALBIN)/kustomize; \
145+
fi
146+
test -s $(LOCALBIN)/kustomize || { curl -Ss $(KUSTOMIZE_INSTALL_SCRIPT) | bash -s -- $(subst v,,$(KUSTOMIZE_VERSION)) $(LOCALBIN); }
147+
148+
.PHONY: controller-gen
149+
controller-gen: $(CONTROLLER_GEN) ## Download controller-gen locally if necessary. If wrong version is installed, it will be overwritten.
150+
$(CONTROLLER_GEN): $(LOCALBIN)
151+
test -s $(LOCALBIN)/controller-gen && $(LOCALBIN)/controller-gen --version | grep -q $(CONTROLLER_TOOLS_VERSION) || \
152+
GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-tools/cmd/controller-gen@$(CONTROLLER_TOOLS_VERSION)
153+
154+
.PHONY: envtest
155+
envtest: $(ENVTEST) ## Download envtest-setup locally if necessary.
156+
$(ENVTEST): $(LOCALBIN)
157+
test -s $(LOCALBIN)/setup-envtest || GOBIN=$(LOCALBIN) go install sigs.k8s.io/controller-runtime/tools/setup-envtest@latest

PROJECT

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# Code generated by tool. DO NOT EDIT.
2+
# This file is used to track the info used to scaffold your project
3+
# and allow the plugins properly work.
4+
# More info: https://book.kubebuilder.io/reference/project-config.html
5+
domain: x-k8s.io
6+
layout:
7+
- go.kubebuilder.io/v3
8+
projectName: jobset
9+
repo: sigs.k8s.io/jobset
10+
resources:
11+
- api:
12+
crdVersion: v1
13+
namespaced: true
14+
controller: true
15+
domain: x-k8s.io
16+
group: batch
17+
kind: JobSet
18+
path: sigs.k8s.io/jobset/api/v1alpha1
19+
version: v1alpha1
20+
version: "3"

api/v1alpha1/groupversion_info.go

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

api/v1alpha1/jobset_types.go

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
Copyright 2023 The Kubernetes Authors.
3+
Licensed under the Apache License, Version 2.0 (the "License");
4+
you may not use this file except in compliance with the License.
5+
You may obtain a copy of the License at
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
Unless required by applicable law or agreed to in writing, software
8+
distributed under the License is distributed on an "AS IS" BASIS,
9+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+
See the License for the specific language governing permissions and
11+
limitations under the License.
12+
*/
13+
14+
package v1alpha1
15+
16+
import (
17+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
18+
)
19+
20+
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
21+
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
22+
23+
// JobSetSpec defines the desired state of JobSet
24+
type JobSetSpec struct {
25+
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
26+
// Important: Run "make" to regenerate code after modifying this file
27+
28+
// Foo is an example field of JobSet. Edit jobset_types.go to remove/update
29+
Foo string `json:"foo,omitempty"`
30+
}
31+
32+
// JobSetStatus defines the observed state of JobSet
33+
type JobSetStatus struct {
34+
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
35+
// Important: Run "make" to regenerate code after modifying this file
36+
}
37+
38+
//+kubebuilder:object:root=true
39+
//+kubebuilder:subresource:status
40+
41+
// JobSet is the Schema for the jobsets API
42+
type JobSet struct {
43+
metav1.TypeMeta `json:",inline"`
44+
metav1.ObjectMeta `json:"metadata,omitempty"`
45+
46+
Spec JobSetSpec `json:"spec,omitempty"`
47+
Status JobSetStatus `json:"status,omitempty"`
48+
}
49+
50+
//+kubebuilder:object:root=true
51+
52+
// JobSetList contains a list of JobSet
53+
type JobSetList struct {
54+
metav1.TypeMeta `json:",inline"`
55+
metav1.ListMeta `json:"metadata,omitempty"`
56+
Items []JobSet `json:"items"`
57+
}
58+
59+
func init() {
60+
SchemeBuilder.Register(&JobSet{}, &JobSetList{})
61+
}

0 commit comments

Comments
 (0)