Skip to content

Commit 2d21859

Browse files
authored
Merge pull request #8 from att-cloudnative-labs/admission-webhook-and-kubebuilder-refactor
Refactor project to use kubebuilder framework and make configuration …
2 parents 9c641a1 + 176bf97 commit 2d21859

File tree

153 files changed

+2565
-12278
lines changed

Some content is hidden

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

153 files changed

+2565
-12278
lines changed

.gitignore

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,24 @@
1-
# kconfig binary
2-
/kconfig-controller
1+
2+
# Binaries for programs and plugins
3+
*.exe
4+
*.exe~
5+
*.dll
6+
*.so
7+
*.dylib
8+
bin
9+
10+
# Test binary, build with `go test -c`
11+
*.test
12+
13+
# Output of the go coverage tool, specifically when used with LiteIDE
14+
*.out
15+
16+
# Kubernetes Generated files - skip generated files, except for vendored files
17+
18+
!vendor/**/zz_generated.*
19+
20+
# editor and IDE paraphernalia
21+
.idea
22+
*.swp
23+
*.swo
24+
*~

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
## Development release
44

5+
0.8.0-BETA-1
6+
- Full refactor to kubebuilder framework
7+
- Environment variables not added to pods directly using admission-controller
8+
- DeployentBindings, StatefulSetBindings, and KnativeServiceBindings removed and replaced with a single KconfigBinding resource
9+
- The ability to specify refName and refKey has been removed. All external references (configmap, secrets) are placed in the same resource with the name, kc-(kconfig name)
10+
- EnvRefsVersion removed. Changes now tracked using KconfigBinding generation and its status' observedGeneration
11+
512
0.7.0-BETA-1
613

714
- KconfigBindings renamed to DeploymentBinding

Dockerfile

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Build the manager binary
2+
FROM golang:1.13 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+
COPY webhooks/ webhooks/
17+
18+
# Build
19+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o manager main.go
20+
21+
# Use distroless as minimal base image to package the manager binary
22+
# Refer to https://github.com/GoogleContainerTools/distroless for more details
23+
FROM gcr.io/distroless/static:nonroot
24+
WORKDIR /
25+
COPY --from=builder /workspace/manager .
26+
USER nonroot:nonroot
27+
28+
ENTRYPOINT ["/manager"]

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2020 AT&T Intellectual Property
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

Makefile

Lines changed: 66 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,51 +1,80 @@
1-
# Go parameters
2-
VERSION=v0.7.0-beta-1
3-
DOCKERIMAGE=docker-registry.aeg.cloud/kconfig-system/kconfig-controller
4-
GOCMD=go
5-
GOBUILD=$(GOCMD) build
6-
GOCLEAN=$(GOCMD) clean
7-
GOTEST=$(GOCMD) test
8-
GOGET=$(GOCMD) get
9-
KCONFIGPKG=github.com/att-cloudnative-labs/kconfig-controller/cmd
10-
CLIENTSET=pkg/client/clientset/versioned/clientset.go
11-
BINARY_NAME=kconfig-controller
12-
BINARY_UNIX=$(BINARY_NAME)_unix
131

14-
.PHONY: clientgen test build-docker build-local build-local-unix run push deploy clean
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"
156

16-
all: clientgen test build-docker
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
1713

18-
clientgen: $(CLIENTSET)
14+
all: manager
1915

20-
$(CLIENTSET): pkg/apis/kconfigcontroller/v1alpha1/types.go
21-
hack/update-codegen.sh
16+
# Run tests
17+
test: generate fmt vet manifests
18+
go test ./... -coverprofile cover.out
2219

23-
test:
24-
$(GOTEST) -v ./...
20+
# Build manager binary
21+
manager: generate fmt vet
22+
go build -o bin/manager main.go
2523

26-
build-docker: test
27-
docker build -f build/Dockerfile -t $(DOCKERIMAGE):$(VERSION) .
24+
# Run against the configured Kubernetes cluster in ~/.kube/config
25+
run: generate fmt vet manifests
26+
go run ./main.go
2827

29-
build-local: $(BINARY_NAME)
28+
# Install CRDs into a cluster
29+
install: manifests
30+
kustomize build config/crd | kubectl apply -f -
3031

31-
build-local-unix: $(BINARY_UNIX)
32+
# Uninstall CRDs from a cluster
33+
uninstall: manifests
34+
kustomize build config/crd | kubectl delete -f -
3235

33-
$(BINARY_NAME): clientgen test **/*.go
34-
$(GOBUILD) -o $(BINARY_NAME) -v $(KCONFIGPKG)
36+
# Deploy controller in the configured Kubernetes cluster in ~/.kube/config
37+
deploy: manifests
38+
cd config/manager && kustomize edit set image controller=${IMG}
39+
kustomize build config/default | kubectl apply -f -
3540

36-
$(BINARY_UNIX): clientgen test **/*.go
37-
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 $(GOBUILD) -o $(BINARY_NAME) -v $(KCONFIGPKG)
41+
# Generate manifests e.g. CRD, RBAC etc.
42+
manifests: controller-gen
43+
$(CONTROLLER_GEN) $(CRD_OPTIONS) rbac:roleName=manager-role webhook paths="./..." output:crd:artifacts:config=config/crd/bases
3844

39-
run: build-local
40-
./$(BINARY_NAME) -v 5 --kubeconfig ~/.kube/config --logtostderr
45+
# Run go fmt against code
46+
fmt:
47+
go fmt ./...
4148

42-
push:
43-
docker push $(DOCKERIMAGE):$(VERSION)
49+
# Run go vet against code
50+
vet:
51+
go vet ./...
4452

45-
deploy:
46-
kubectl -n common-system replace -f install/
53+
# Generate code
54+
generate: controller-gen
55+
$(CONTROLLER_GEN) object:headerFile=./hack/boilerplate.go.txt paths="./..."
4756

48-
clean:
49-
$(GOCLEAN)
50-
rm -f $(BINARY_NAME)
51-
rm -f $(BINARY_UNIX)
57+
# Build the docker image
58+
docker-build: test
59+
docker build . -t ${IMG}
60+
61+
# Push the docker image
62+
docker-push:
63+
docker push ${IMG}
64+
65+
# find or download controller-gen
66+
# download controller-gen if necessary
67+
controller-gen:
68+
ifeq (, $(shell which controller-gen))
69+
@{ \
70+
set -e ;\
71+
CONTROLLER_GEN_TMP_DIR=$$(mktemp -d) ;\
72+
cd $$CONTROLLER_GEN_TMP_DIR ;\
73+
go mod init tmp ;\
74+
go get sigs.k8s.io/controller-tools/cmd/[email protected] ;\
75+
rm -rf $$CONTROLLER_GEN_TMP_DIR ;\
76+
}
77+
CONTROLLER_GEN=$(GOBIN)/controller-gen
78+
else
79+
CONTROLLER_GEN=$(shell which controller-gen)
80+
endif

PROJECT

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
domain: atteg.com
2+
repo: github.com/att-cloudnative-labs/kconfig-controller
3+
resources:
4+
- group: kconfigcontroller
5+
kind: Kconfig
6+
version: v1beta1
7+
- group: kconfigcontroller
8+
kind: KconfigBinding
9+
version: v1beta1
10+
version: "2"

README.md

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,19 @@
3838

3939
----
4040

41-
Kconfig is a Kubernetes Custom-controller and CRD for externalizing configuration of Kubernetes deployments, statefulsets, and knative services. Kconfig allows environment variables to be defined in a single resource that selects the target workload resource based on labels, and inserts the specified environment variables into the target workload resource.
41+
Kconfig is a Kubernetes custom-controller, admission-webhook, and custom resource definition for externalizing configuration of Kubernetes Pods. Kconfig allows environment variables to be defined in a single resource that selects the target pods based on labels, and inserts the specified environment variables into the target pods.
4242

43-
Multiple Kconfig resources can select a single target resource and the target will have the aggregation of each of those Kconfigs. In addition, Kconfigs have a level field which determines the order, in relation to other Kconfigs that select the same target resource, in which environment variables from multiple Kconfigs are defined in the container environment.
43+
Multiple Kconfig resources can select the same target labels and the target pods will have the aggregation of each of those Kconfigs. In addition, Kconfigs have a level field which determines the order, in relation to other Kconfigs that select common pods, in which environment variables from multiple Kconfigs are defined in the container environment.
4444

4545
Aside from defining simple key/value pairs, Kconfigs can also define and reference environment variables to be stored in configmaps and/or secrets.
4646

47-
For a target to have its environment variables controlled by Kconfigs, it needs the annotation ```kconfigcontroller.atteg.com/env=true```.
47+
For a target to have its environment variables controlled by Kconfigs, it needs the annotation ```kconfigcontroller.atteg.com/inject=true```.
4848

49-
Kconfig-controller also has secondary resources, DeploymentBindings, StatefulSetBindings, and KnativeServiceBindings. These resources should not be created/manipulated directly by users and are used by the control loops. These resources serve as a target for Kconfigs to update their changes whereafter, the controller can re-processed the contained environment variables for all Kconfigs that target a particular deployment, statefulset, or knative service. Note that there will always be one of these 'binding resources for each workload resource that contains the kconfig enabled annotation shown above.
49+
Add the annotation, ```kconfigcontroller.atteg.com/refresh-template=true``` to have updates to a kconfig to trigger a rolling update for deployments, statefulsets of the selected pods.
50+
51+
Kconfig-controller also has a secondary custom resource, KconfigBinding, that is used by the controllers and should not be created/manipulated directly by users. This resources serve as a target for Kconfigs to update their changes whereafter, the admission-controller can import the contained environment variables directly into pods. Note that there is a one-to-one mapping for each kconfig and kconfigbinding.
52+
53+
Build requires Kustomize (https://github.com/kubernetes-sigs/kustomize) locally and cert-manager (https://github.com/jetstack/cert-manager) installed in the kubernetes cluser for the admission-controller's TLS certificates.
5054

5155
----
5256

@@ -66,7 +70,6 @@ spec:
6670
- type: Secret
6771
key: PLEASECREATETHIS
6872
value: shhhhh
69-
refName: samplesecret
7073
- type: Secret
7174
key: MYSECRETVAR
7275
secretKeyRef:
@@ -92,23 +95,27 @@ spec:
9295
app: myapp
9396
```
9497
95-
The first envConfig is a 'Value' type. An empty type field implies a 'Value' type envConfig. This definition would apply a simple key and value field to the target deployment's container enviroment variables. The second envConfig is a 'Secret' type. Notice that this envConfig has a value and a refName field. The refName field indicates the name of the secret that this envConfig should be stored in. If the secret does not exist, the kconfig-controller will create it and store the contents of the value field in it. After such an action takes place, the Kconfig is automatically updated with the secretKeyRef to the secret and with the value field removed. The same is true with a 'ConfigMap' type. Notice the final two envConfigs that show how the envConfig appears after a Kconfig is created/updated with a ConfigMap or Secret type envConfig that contains a value. Whenever a get Kconfig is performed, you will never see a value field, as the action is performed immediately on update and the field is automatically removed.
98+
The first envConfig is a 'Value' type. An empty type field implies a 'Value' type envConfig. This definition would apply a simple key and value field to the target deployment's container environment variables. The second envConfig is a 'Secret' type. Notice that this envConfig has a value and a refName field. The refName field indicates the name of the secret that this envConfig should be stored in. If the secret does not exist, the kconfig-controller will create it and store the contents of the value field in it. After such an action takes place, the Kconfig is automatically updated with the secretKeyRef to the secret and with the value field removed. The same is true with a 'ConfigMap' type. Notice the final two envConfigs that show how the envConfig appears after a Kconfig is created/updated with a ConfigMap or Secret type envConfig that contains a value. Whenever a get Kconfig is performed, you will never see a value field, as the action is performed immediately on update and the field is automatically removed.
9699
97-
## Build
100+
## Build and Push
98101
99102
```bash
100-
docker build -f build/Dockerfile -t docker-registry.aeg.cloud/kconfig-system/kconfig-controller:v0.7.0-beta-1 .
103+
make docker-build IMG=your-registry.com/kconfig-controller-system/kconfig-controller:v1beta1
104+
make docker-push IMG=your-registry.com/kconfig-controller-system/kconfig-controller:v1beta1
101105
```
102106

103107
## Installation
104108

105109
```bash
106-
kubectl apply -f install/
110+
make deploy IMG=your-registry.com/kconfig-controller-system/kconfig-controller:v1beta1
107111
```
108112

109113
## Roadmap
110114

111-
* Ability to select the container configs apply to. Currently the configs are only placed in the first container in a pod template
115+
* Ability to select the container configs apply to. Currently the configs are only placed in the first container in a pod spec
112116
* Validate that all existing configmap/secret references in a Kconfig exists and if not, removed them from the Kconfig
113-
* Support for files form and mount locations for files through Kconfigs
114-
* Possible move to injecting the environment variables directly to pods through a custom admission controller
117+
* Support for creating files and mount locations for files through Kconfigs
118+
119+
---
120+
121+
*Developed using the Kubebuilder Framework, https://github.com/kubernetes-sigs/kubebuilder

api/v1beta1/groupversion_info.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
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+
7+
http://www.apache.org/licenses/LICENSE-2.0
8+
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
*/
15+
16+
// Package v1beta1 contains API Schema definitions for the kconfigcontroller v1beta1 API group
17+
// +kubebuilder:object:generate=true
18+
// +groupName=kconfigcontroller.atteg.com
19+
package v1beta1
20+
21+
import (
22+
"k8s.io/apimachinery/pkg/runtime/schema"
23+
"sigs.k8s.io/controller-runtime/pkg/scheme"
24+
)
25+
26+
var (
27+
// GroupVersion is group version used to register these objects
28+
GroupVersion = schema.GroupVersion{Group: "kconfigcontroller.atteg.com", Version: "v1beta1"}
29+
30+
// SchemeBuilder is used to add go types to the GroupVersionKind scheme
31+
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}
32+
33+
// AddToScheme adds the types in this group-version to the given scheme.
34+
AddToScheme = SchemeBuilder.AddToScheme
35+
)

0 commit comments

Comments
 (0)