Skip to content

Commit 3928004

Browse files
committed
kubebuilder v3 init
0 parents  commit 3928004

Some content is hidden

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

58 files changed

+2873
-0
lines changed

.dockerignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# More info: https://docs.docker.com/engine/reference/builder/#dockerignore-file
2+
# Ignore all files which are not go type
3+
!**/*.go
4+
!**/*.mod
5+
!**/*.sum

.gitignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
vendor/
2+
bin/
3+
testbin/
4+
5+
# Files generated by JetBrains IDEs, e.g. IntelliJ IDEA
6+
.idea/
7+
*.iml
8+
9+
# OSX trash
10+
.DS_Store

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# Build the manager binary
2+
FROM golang:1.15 as builder
3+
4+
WORKDIR /workspace
5+
# Copy the Go Modules manifests
6+
COPY go.mod go.mod
7+
COPY go.sum go.sum
8+
# cache deps before building and copying source so that we don't need to re-download as much
9+
# and so that source changes don't invalidate our downloaded layer
10+
RUN go mod download
11+
12+
# Copy the go source
13+
COPY main.go main.go
14+
COPY api/ api/
15+
COPY controllers/ controllers/
16+
17+
# Build
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
19+
20+
# Use distroless as minimal base image to package the manager binary
21+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
22+
FROM gcr.io/distroless/static:nonroot
23+
WORKDIR /
24+
COPY --from=builder /workspace/manager .
25+
USER 65532:65532
26+
27+
ENTRYPOINT ["/manager"]

Makefile

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
2+
# Image URL to use all building/pushing image targets
3+
IMG ?= controller:latest
4+
# Produce CRDs that work back to Kubernetes 1.11 (no version conversion)
5+
CRD_OPTIONS ?= "crd:trivialVersions=true,preserveUnknownFields=false"
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+
# This is a requirement for 'setup-envtest.sh' in the test target.
16+
# Options are set to exit when a recipe line exits non-zero or a piped command fails.
17+
SHELL = /usr/bin/env bash -o pipefail
18+
.SHELLFLAGS = -ec
19+
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+
help: ## Display this help.
36+
@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)
37+
38+
##@ Development
39+
40+
manifests: controller-gen ## Generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects.
41+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
42+
43+
generate: controller-gen ## Generate code containing DeepCopy, DeepCopyInto, and DeepCopyObject method implementations.
44+
$(CONTROLLER_GEN) object:headerFile="hack/boilerplate.go.txt" paths="./..."
45+
46+
fmt: ## Run go fmt against code.
47+
go fmt ./...
48+
49+
vet: ## Run go vet against code.
50+
go vet ./...
51+
52+
ENVTEST_ASSETS_DIR=$(shell pwd)/testbin
53+
test: manifests generate fmt vet ## Run tests.
54+
mkdir -p ${ENVTEST_ASSETS_DIR}
55+
test -f ${ENVTEST_ASSETS_DIR}/setup-envtest.sh || curl -sSLo ${ENVTEST_ASSETS_DIR}/setup-envtest.sh https://raw.githubusercontent.com/kubernetes-sigs/controller-runtime/v0.7.2/hack/setup-envtest.sh
56+
source ${ENVTEST_ASSETS_DIR}/setup-envtest.sh; fetch_envtest_tools $(ENVTEST_ASSETS_DIR); setup_envtest_env $(ENVTEST_ASSETS_DIR); go test ./... -coverprofile cover.out
57+
58+
##@ Build
59+
60+
build: generate fmt vet ## Build manager binary.
61+
go build -o bin/manager main.go
62+
63+
run: manifests generate fmt vet ## Run a controller from your host.
64+
go run ./main.go
65+
66+
docker-build: test ## Build docker image with the manager.
67+
docker build -t ${IMG} .
68+
69+
docker-push: ## Push docker image with the manager.
70+
docker push ${IMG}
71+
72+
##@ Deployment
73+
74+
install: manifests kustomize ## Install CRDs into the K8s cluster specified in ~/.kube/config.
75+
$(KUSTOMIZE) build config/crd | kubectl apply -f -
76+
77+
uninstall: manifests kustomize ## Uninstall CRDs from the K8s cluster specified in ~/.kube/config.
78+
$(KUSTOMIZE) build config/crd | kubectl delete -f -
79+
80+
deploy: manifests kustomize ## Deploy controller to the K8s cluster specified in ~/.kube/config.
81+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
82+
$(KUSTOMIZE) build config/default | kubectl apply -f -
83+
84+
undeploy: ## Undeploy controller from the K8s cluster specified in ~/.kube/config.
85+
$(KUSTOMIZE) build config/default | kubectl delete -f -
86+
87+
88+
CONTROLLER_GEN = $(shell pwd)/bin/controller-gen
89+
controller-gen: ## Download controller-gen locally if necessary.
90+
$(call go-get-tool,$(CONTROLLER_GEN),sigs.k8s.io/controller-tools/cmd/[email protected])
91+
92+
KUSTOMIZE = $(shell pwd)/bin/kustomize
93+
kustomize: ## Download kustomize locally if necessary.
94+
$(call go-get-tool,$(KUSTOMIZE),sigs.k8s.io/kustomize/kustomize/[email protected])
95+
96+
# go-get-tool will 'go get' any package $2 and install it to $1.
97+
PROJECT_DIR := $(shell dirname $(abspath $(lastword $(MAKEFILE_LIST))))
98+
define go-get-tool
99+
@[ -f $(1) ] || { \
100+
set -e ;\
101+
TMP_DIR=$$(mktemp -d) ;\
102+
cd $$TMP_DIR ;\
103+
go mod init tmp ;\
104+
echo "Downloading $(2)" ;\
105+
GOBIN=$(PROJECT_DIR)/bin go get $(2) ;\
106+
rm -rf $$TMP_DIR ;\
107+
}
108+
endef

PROJECT

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
domain: k8s.aws
2+
layout:
3+
- go.kubebuilder.io/v3
4+
projectName: migration
5+
repo: github.com/aws/aws-k8s-mcs-controller
6+
resources:
7+
- api:
8+
crdVersion: v1
9+
namespaced: true
10+
controller: true
11+
domain: k8s.aws
12+
group: multicluster
13+
kind: ServiceExport
14+
path: github.com/aws/aws-k8s-mcs-controller/api/v1alpha1
15+
version: v1alpha1
16+
- api:
17+
crdVersion: v1
18+
namespaced: true
19+
controller: true
20+
domain: k8s.aws
21+
group: multicluster
22+
kind: ServiceImport
23+
path: github.com/aws/aws-k8s-mcs-controller/api/v1alpha1
24+
version: v1alpha1
25+
version: "3"

README.md

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# How to build and run
2+
3+
Spin up a local Kubernetes cluster using `kind`
4+
5+
```
6+
kind create cluster
7+
kind export kubeconfig
8+
```
9+
10+
Install custom CRDs (`ServiceImport`, `ServiceExport`) to the cluster
11+
12+
```
13+
make install
14+
```
15+
16+
Run controller
17+
18+
```
19+
make run
20+
```
21+
22+
Create a testing ServiceExport resource
23+
24+
```
25+
# my-export.yaml
26+
27+
kind: ServiceExport
28+
apiVersion: multicluster.k8s.aws/v1alpha1
29+
metadata:
30+
namespace: demo
31+
name: my-service-name
32+
```
33+
34+
Apply config file
35+
36+
```
37+
kubectl create namespace demo
38+
kubectl apply -f my-export.yaml
39+
```
40+
41+
Check running controller if it correctly detects newly created resource
42+
43+
```
44+
2021-04-16T14:41:01.686-0700 INFO controllers.ServiceExport ServiceExport detected: demo/my-service-name
45+
```

api/v1alpha1/groupversion_info.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
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 v1alpha1 contains API Schema definitions for the multicluster v1alpha1 API group
18+
// +kubebuilder:object:generate=true
19+
// +groupName=multicluster.k8s.aws
20+
package v1alpha1
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: "multicluster.k8s.aws", Version: "v1alpha1"}
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/v1alpha1/serviceexport_types.go

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
Copyright 2020 The Kubernetes Authors.
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 v1alpha1
18+
19+
import (
20+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
21+
)
22+
23+
// +genclient
24+
// +kubebuilder:object:root=true
25+
26+
// ServiceExport declares that the Service with the same name and namespace
27+
// as this export should be consumable from other clusters.
28+
type ServiceExport struct {
29+
metav1.TypeMeta `json:",inline"`
30+
// +optional
31+
metav1.ObjectMeta `json:"metadata,omitempty"`
32+
// status describes the current state of an exported service.
33+
// Service configuration comes from the Service that had the same
34+
// name and namespace as this ServiceExport.
35+
// Populated by the multi-cluster service implementation's controller.
36+
// +optional
37+
Status ServiceExportStatus `json:"status,omitempty"`
38+
}
39+
40+
// ServiceExportStatus contains the current status of an export.
41+
type ServiceExportStatus struct {
42+
// +optional
43+
// +patchStrategy=merge
44+
// +patchMergeKey=type
45+
// +listType=map
46+
// +listMapKey=type
47+
Conditions []metav1.Condition `json:"conditions,omitempty" patchStrategy:"merge" patchMergeKey:"type"`
48+
}
49+
50+
// ServiceExportConditionType identifies a specific condition.
51+
type ServiceExportConditionType string
52+
53+
const (
54+
// ServiceExportValid means that the service referenced by this
55+
// service export has been recognized as valid by an mcs-controller.
56+
// This will be false if the service is found to be unexportable
57+
// (ExternalName, not found).
58+
ServiceExportValid ServiceExportConditionType = "Valid"
59+
// ServiceExportConflict means that there is a conflict between two
60+
// exports for the same Service. When "True", the condition message
61+
// should contain enough information to diagnose the conflict:
62+
// field(s) under contention, which cluster won, and why.
63+
// Users should not expect detailed per-cluster information in the
64+
// conflict message.
65+
ServiceExportConflict ServiceExportConditionType = "Conflict"
66+
)
67+
68+
// +kubebuilder:object:root=true
69+
70+
// ServiceExportList represents a list of endpoint slices
71+
type ServiceExportList struct {
72+
metav1.TypeMeta `json:",inline"`
73+
// Standard list metadata.
74+
// +optional
75+
metav1.ListMeta `json:"metadata,omitempty"`
76+
// List of endpoint slices
77+
// +listType=set
78+
Items []ServiceExport `json:"items"`
79+
}
80+
81+
func init() {
82+
SchemeBuilder.Register(&ServiceExport{}, &ServiceExportList{})
83+
}

0 commit comments

Comments
 (0)