Skip to content

Commit

Permalink
refactor: remove the controller code and use the webhook code in the …
Browse files Browse the repository at this point in the history
…runtime manager.
  • Loading branch information
Peefy committed Aug 31, 2023
1 parent 9f34c3c commit 31cacf5
Show file tree
Hide file tree
Showing 41 changed files with 449 additions and 1,047 deletions.
24 changes: 16 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
FROM registry.access.redhat.com/ubi8/ubi-minimal
FROM golang:1.19 as builder

ENV TZ="Europe/Zurich" \
LANG="en_US.UTF-8" \
WEBHOOK=/usr/local/bin/webhook \
UIDGID=1001:1001
ENV GO111MODULE=on \
GOPROXY=https://goproxy.cn,direct

COPY bin/webhook ${WEBHOOK}
WORKDIR /

USER ${UIDGID}
COPY . .

CMD ["${WEBHOOK}"]
RUN GOOS=linux GOARCH=amd64 go build -o manager

FROM kcllang/kcl

WORKDIR /
COPY --from=builder /manager .

ENV KCL_GO_DISABLE_ARTIFACT=on
ENV LANG="en_US.UTF-8"

ENTRYPOINT ["/manager"]
26 changes: 15 additions & 11 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ ifeq ($(USE_IMAGE_DIGESTS), true)
endif

# Image URL to use all building/pushing image targets
IMG ?= controller:latest
IMG ?= kcllang/webhookserver
# ENVTEST_K8S_VERSION refers to the version of kubebuilder assets to be downloaded by envtest binary.
ENVTEST_K8S_VERSION = 1.26.0
ENVTEST_K8S_VERSION = 1.28.0

# Get the currently used golang install path (in GOPATH/bin, unless GOBIN is set)
ifeq (,$(shell go env GOBIN))
Expand Down Expand Up @@ -134,15 +134,19 @@ test: manifests generate fmt vet envtest ## Run tests.

.PHONY: build
build: ## Build binaries.
make webhook
make manager

.PHONY: build
build-linux: ## Build binaries.
make manager-linux

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

.PHONY: webhook-linux
webhook-linux: generate fmt vet
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o build/bin/webhook main.go
.PHONY: manager-linux
manager-linux: generate fmt vet
CGO_ENABLED=0 GOOS=linux GOARCH=amd64 GO111MODULE=on go build -a -o build/bin/manager main.go

.PHONY: run
run: manifests generate fmt vet ## Run a controller from your host.
Expand All @@ -152,11 +156,11 @@ run: manifests generate fmt vet ## Run a controller from your host.
# (i.e. docker build --platform linux/arm64 ). However, you must enable docker buildKit for it.
# More info: https://docs.docker.com/develop/develop-images/build_enhancements/
.PHONY: docker-build
docker-build: webhook-linux ## Build docker image with the webhook.
docker-build: ## Build docker image with the manager.
docker build -t $(IMG) .

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

# PLATFORMS defines the target platforms for the manager image be build to provide support to multiple
Expand Down
69 changes: 58 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,26 +15,74 @@ KCL Operator provides cluster integration, allowing you to use Access Webhook to

![architecture](./images/arch.png)

## CR Example
## Developing

+ Install Go 1.20+
+ Install Kubectl and Kustomize
+ Install [Operator SDK](https://sdk.operatorframework.io/)
+ Prepare a Kubernetes Cluster e.g., K3d

Run `make help` to get the help.

## Quick Start

1. Deploy the KCL Operator.

```shell
make deploy
```

Use the following command to watch and wait the pod status is Running.

```shell
kubectl get po
```

```yaml
2. Deploy the KCL source

```shell
kubectl apply -f- << EOF
apiVersion: krm.kcl.dev/v1alpha1
kind: KCLRun
metadata:
name: set-annotation
spec:
params:
annotations:
config.kubernetes.io/local-config: "true"
source: oci://ghcr.io/kcl-lang/set-annotation
source: |
items = [item | {
metadata.annotations: {
"managed-by" = "kcl-operator"
}
} for item in option("items")]
EOF
```

## Developing
3. Validate the mutation result by creating a nginx Pod YAML.

+ Install Go 1.20+
+ Install [Operator SDK](https://sdk.operatorframework.io/)
```shell
kubectl apply -f- << EOF
apiVersion: v1
kind: Pod
metadata:
name: nginx
annotations:
app: nginx
spec:
containers:
- name: nginx
image: nginx:1.14.2
ports:
- containerPort: 80
EOF
kubectl get po nginx -o yaml | grep kcl-operator
```

Run `make help` to get the help.
The output is

```shell
managed-by: kcl-operator
```

We can find the annotation `managed-by=kcl-operator` is added on the pod.

## Guides for Developing KCL

Expand All @@ -57,4 +105,3 @@ See [here](https://kcl-lang.io/docs/reference/lang/tour) to study more features
## Examples

See [here](https://github.com/kcl-lang/krm-kcl/tree/main/examples) for more examples.

Loading

0 comments on commit 31cacf5

Please sign in to comment.