Skip to content

Commit

Permalink
Merge pull request #56 from sttts/sttts-kcp-0.18
Browse files Browse the repository at this point in the history
✨ Rebase to 0.18
  • Loading branch information
kcp-ci-bot authored Jul 1, 2024
2 parents 12cc8d5 + 6c54815 commit a3df1ba
Show file tree
Hide file tree
Showing 56 changed files with 3,137 additions and 336 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,6 @@
hack/tools/bin

junit-report.xml
/artifacts
/artifacts

examples/kcp/.gitignore
34 changes: 34 additions & 0 deletions .prow.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
presubmits:
- name: pull-controller-runtime-everything
always_run: true
decorate: true
clone_uri: "ssh://git@github.com/kcp-dev/controller-runtime.git"
labels:
preset-goproxy: "true"
spec:
containers:
- image: ghcr.io/kcp-dev/infra/build:1.22.2-1
command:
- make
- test

- name: pull-controller-runtime-example-e2e
decorate: true
# only run e2e tests if code changed.
run_if_changed: "(pkg|examples|go.mod|go.sum|Makefile|.prow.yaml)"
clone_uri: "https://github.com/kcp-dev/controller-runtime"
labels:
preset-goproxy: "true"
spec:
containers:
- image: ghcr.io/kcp-dev/infra/build:1.22.2-1
env:
- name: KUBECONFIG
value: /home/prow/go/src/github.com/kcp-dev/controller-runtime/examples/kcp/.test/kcp.kubeconfig
command:
- make
- test-kcp-e2e
resources:
requests:
memory: 6Gi
cpu: 4
5 changes: 5 additions & 0 deletions DOWNSTREAM_OWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
approvers:
- sttts
- xrstf
- mjudeikis
- embik
Empty file added DOWNSTREAM_OWNERS_ALIASES
Empty file.
4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -142,4 +142,6 @@ APIDIFF_OLD_COMMIT ?= $(shell git rev-parse origin/main)
verify-apidiff: $(GO_APIDIFF) ## Check for API differences
$(GO_APIDIFF) $(APIDIFF_OLD_COMMIT) --print-compatible


.PHONY: test-kcp-e2e
test-kcp-e2e:
cd examples/kcp && make kcp-server kcp-controller test
1 change: 1 addition & 0 deletions examples/builtins/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ func main() {

// Setup a Manager
entryLog.Info("setting up manager")

mgr, err := manager.New(config.GetConfigOrDie(), manager.Options{})
if err != nil {
entryLog.Error(err, "unable to set up overall controller manager")
Expand Down
91 changes: 91 additions & 0 deletions examples/kcp/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
SHELL := /bin/bash

.PHONY: help
help: ## Display this help.
@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)

GO_INSTALL = ./hack/go-install.sh

LOCALBIN ?= $(shell pwd)/bin
TOOLS_DIR=hack/tools
TOOLS_BIN_DIR := $(abspath $(TOOLS_DIR)/bin)
ARTIFACT_DIR ?= .test

KCP ?= $(LOCALBIN)/kcp
KUBECTL_KCP ?= $(LOCALBIN)/kubectl-kcp

KCP_VERSION ?= 0.23.0
CONTROLLER_GEN := $(TOOLS_BIN_DIR)/controller-gen
export CONTROLLER_GEN # so hack scripts can use it

KCP_APIGEN_VER := v0.21.0
KCP_APIGEN_BIN := apigen
KCP_APIGEN_GEN := $(TOOLS_BIN_DIR)/$(KCP_APIGEN_BIN)-$(KCP_APIGEN_VER)
export KCP_APIGEN_GEN # so hack scripts can use it

OS ?= $(shell go env GOOS )
ARCH ?= $(shell go env GOARCH )

$(KCP): ## Download kcp locally if necessary.
mkdir -p $(LOCALBIN)
curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION)/kcp_$(KCP_VERSION)_$(OS)_$(ARCH).tar.gz | tar --directory $(LOCALBIN)/../ -xvzf - bin/kcp
touch $(KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir

$(KUBECTL_KCP): ## Download kcp kubectl plugins locally if necessary.
curl -L -s -o - https://github.com/kcp-dev/kcp/releases/download/v$(KCP_VERSION)/kubectl-kcp-plugin_$(KCP_VERSION)_$(OS)_$(ARCH).tar.gz | tar --directory $(LOCALBIN)/../ -xvzf - bin
touch $(KUBECTL_KCP) # we download an "old" file, so make will re-download to refresh it unless we make it newer than the owning dir

$(KCP_APIGEN_GEN):
GOBIN=$(TOOLS_BIN_DIR) $(GO_INSTALL) github.com/kcp-dev/kcp/sdk/cmd/apigen $(KCP_APIGEN_BIN) $(KCP_APIGEN_VER)

$(CONTROLLER_GEN): $(TOOLS_DIR)/go.mod # Build controller-gen from tools folder.
cd $(TOOLS_DIR) && go build -tags=tools -o bin/controller-gen sigs.k8s.io/controller-tools/cmd/controller-gen

build: $(KCP) $(KUBECTL_KCP) build-controller

ifeq (,$(shell go env GOBIN))
GOBIN=$(shell go env GOPATH)/bin
else
GOBIN=$(shell go env GOBIN)
endif

build-controller: ## Build the controller binary.
go build -o $(LOCALBIN)/kcp-controller ./main.go

.PHONY: kcp-server
kcp-server: $(KCP) $(ARTIFACT_DIR)/kcp ## Run the kcp server.
@if [[ ! -s $(ARTIFACT_DIR)/kcp.log ]]; then ( $(KCP) start -v 5 --root-directory $(ARTIFACT_DIR)/kcp --kubeconfig-path $(ARTIFACT_DIR)/kcp.kubeconfig --audit-log-maxsize 1024 --audit-log-mode=batch --audit-log-batch-max-wait=1s --audit-log-batch-max-size=1000 --audit-log-batch-buffer-size=10000 --audit-log-batch-throttle-burst=15 --audit-log-batch-throttle-enable=true --audit-log-batch-throttle-qps=10 --audit-policy-file ./test/e2e/audit-policy.yaml --audit-log-path $(ARTIFACT_DIR)/audit.log >$(ARTIFACT_DIR)/kcp.log 2>&1 & ); fi
@echo "Waiting for kcp server to generate kubeconfig..."
@while true; do if [[ ! -s $(ARTIFACT_DIR)/kcp.kubeconfig ]]; then sleep 0.2; else break; fi; done
@echo "Waiting for kcp server to be ready..."
@while true; do if ! kubectl --kubeconfig $(ARTIFACT_DIR)/kcp.kubeconfig get --raw /readyz >$(ARTIFACT_DIR)/kcp.probe.log 2>&1; then sleep 0.2; else break; fi; done
@echo "kcp server is ready and running in the background. To stop run 'make test-cleanup'"

.PHONY: kcp-bootstrap
kcp-bootstrap: ## Bootstrap the kcp server.
@go run ./config/main.go

.PHONY: kcp-controller
kcp-controller: build kcp-bootstrap ## Run the kcp-controller.
@echo "Starting kcp-controller in the background. To stop run 'make test-cleanup'"
@if [[ ! -s $(ARTIFACT_DIR)/controller.log ]]; then ( ./bin/kcp-controller >$(ARTIFACT_DIR)/controller.log 2>&1 & ); fi

.PHONY: test-e2e-cleanup
test-cleanup: ## Clean up processes and directories from an end-to-end test run.
rm -rf $(ARTIFACT_DIR) || true
pkill -sigterm kcp || true
pkill -sigterm kubectl || true
pkill -sigterm kcp-controller || true

$(ARTIFACT_DIR)/kcp: ## Create a directory for the kcp server data.
mkdir -p $(ARTIFACT_DIR)/kcp

generate: build $(CONTROLLER_GEN) $(KCP_APIGEN_GEN) # Generate code
./hack/update-codegen-crds.sh

run-local: build-controller kcp-bootstrap
./bin/kcp-controller

.PHONY: test # Run tests
test:
go test ./... --workspace=root --kubeconfig=$(CURDIR)/$(ARTIFACT_DIR)/kcp.kubeconfig
85 changes: 85 additions & 0 deletions examples/kcp/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# controller-runtime-example
An example project that is multi-cluster aware and works with [kcp](https://github.com/kcp-dev/kcp)

## Description

In this example, we intentionally not using advanced kubebuilder patterns to keep the example simple and easy to understand.
In the future, we will add more advanced examples. Example covers 3 parts:
1. KCP bootstrapping - creating APIExport & consumer workspaces
1. Creating WorkspaceType for particular exports
2. Running controller with APIExport aware configuration and reconciling multiple consumer workspaces


This example contains an example project that works with APIExports and multiple kcp workspaces. It demonstrates
two reconcilers:

1. ConfigMap
1. Get a ConfigMap for the key from the queue, from the correct logical cluster
2. If the ConfigMap has labels["name"], set labels["response"] = "hello-$name" and save the changes
3. List all ConfigMaps in the logical cluster and log each one's namespace and name
4. If the ConfigMap from step 1 has data["namespace"] set, create a namespace whose name is the data value.
5. If the ConfigMap from step 1 has data["secretData"] set, create a secret in the same namespace as the ConfigMap,
with an owner reference to the ConfigMap, and data["dataFromCM"] set to the data value.

2. Widget
1. Show how to list all Widget instances across all logical clusters
2. Get a Widget for the key from the queue, from the correct logical cluster
3. List all Widgets in the same logical cluster
4. Count the number of Widgets (list length)
5. Make sure `.status.total` matches the current count (via a `patch`)

## Getting Started

### Running on kcp

1. Run KCP with the following command:

```sh
make kcp-server
```

From this point onwards you can inspect kcp configuration using kubeconfig:

```sh
export KUBECONFIG=.test/kcp.kubeconfig
```

1. Bootstrap the KCP server with the following command:

```sh
export KUBECONFIG=./.test/kcp.kubeconfig
make kcp-bootstrap
```

1. Run controller:

```sh
export KUBECONFIG=./.test/kcp.kubeconfig
make run-local
```

1. In separate shell you can run tests to exercise the controller:

```sh
export KUBECONFIG=./.test/kcp.kubeconfig
make test
```

### Uninstall resources
To delete the resources from kcp:

```sh
make test-clean
```



### How it works
This project aims to follow the Kubernetes [Operator pattern](https://kubernetes.io/docs/concepts/extend-kubernetes/operator/)

It uses [Controllers](https://kubernetes.io/docs/concepts/architecture/controller/)
which provides a reconcile function responsible for synchronizing resources until the desired state is reached.




36 changes: 36 additions & 0 deletions examples/kcp/apis/v1alpha1/groupversion_info.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// Package v1alpha1 contains API Schema definitions for the data v1alpha1 API group
// +kubebuilder:object:generate=true
// +groupName=data.my.domain
package v1alpha1

import (
"k8s.io/apimachinery/pkg/runtime/schema"
"sigs.k8s.io/controller-runtime/pkg/scheme"
)

var (
// GroupVersion is group version used to register these objects
GroupVersion = schema.GroupVersion{Group: "data.my.domain", Version: "v1alpha1"}

// SchemeBuilder is used to add go types to the GroupVersionKind scheme
SchemeBuilder = &scheme.Builder{GroupVersion: GroupVersion}

// AddToScheme adds the types in this group-version to the given scheme.
AddToScheme = SchemeBuilder.AddToScheme
)
56 changes: 56 additions & 0 deletions examples/kcp/apis/v1alpha1/widget_types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
Copyright 2024.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// WidgetSpec defines the desired state of Widget
type WidgetSpec struct {
Foo string `json:"foo,omitempty"`
}

// WidgetStatus defines the observed state of Widget
type WidgetStatus struct {
Total int `json:"total,omitempty"`
}

// +kubebuilder:object:root=true
// +kubebuilder:subresource:status

// Widget is the Schema for the widgets API
type Widget struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec WidgetSpec `json:"spec,omitempty"`
Status WidgetStatus `json:"status,omitempty"`
}

// +kubebuilder:object:root=true

// WidgetList contains a list of Widget
type WidgetList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Widget `json:"items"`
}

func init() {
SchemeBuilder.Register(&Widget{}, &WidgetList{})
}
Loading

0 comments on commit a3df1ba

Please sign in to comment.