Skip to content
This repository was archived by the owner on Apr 24, 2024. It is now read-only.

Commit 19809d0

Browse files
committed
support non-kcp environment
1 parent 88d7475 commit 19809d0

File tree

13 files changed

+362
-22
lines changed

13 files changed

+362
-22
lines changed

Makefile

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
8080
go run ./main.go $(NAME_PREFIX)$(APIEXPORT_NAME)
8181

8282
.PHONY: docker-build
83-
docker-build: test ## Build docker image with the manager.
83+
docker-build: build ## Build docker image with the manager.
8484
docker build -t ${IMG} .
8585

8686
.PHONY: docker-push
@@ -101,6 +101,11 @@ install: manifests kustomize ## Install APIResourceSchemas and APIExport into kc
101101
uninstall: manifests kustomize ## Uninstall APIResourceSchemas and APIExport from kcp (using $KUBECONFIG or ~/.kube/config). Call with ignore-not-found=true to ignore resource not found errors during deletion.
102102
kustomize build config/kcp | kubectl delete --ignore-not-found=$(ignore-not-found) -f -
103103

104+
.PHONY: deploy-crd
105+
deploy-crd: manifests kustomize ## Deploy controller
106+
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
107+
$(KUSTOMIZE) build config/default-crd | kubectl apply -f - || true
108+
104109
.PHONY: deploy
105110
deploy: manifests kustomize ## Deploy controller
106111
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}

config/default-crd/kustomization.yaml

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
# Adds namespace to all resources.
2+
namespace: controller-runtime-example-system
3+
4+
# Value of this field is prepended to the
5+
# names of all resources, e.g. a deployment named
6+
# "wordpress" becomes "alices-wordpress".
7+
# Note that it should also match with the prefix (text before '-') of the namespace
8+
# field above.
9+
namePrefix: controller-runtime-example-
10+
11+
# Labels to add to all resources and selectors.
12+
#commonLabels:
13+
# someName: someValue
14+
15+
bases:
16+
- ../crd
17+
- ../rbac
18+
- ../manager
19+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
20+
# crd/kustomization.yaml
21+
#- ../webhook
22+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
23+
#- ../certmanager
24+
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
25+
#- ../prometheus
26+
27+
patchesStrategicMerge:
28+
# Protect the /metrics endpoint by putting it behind auth.
29+
# If you want your controller-manager to expose the /metrics
30+
# endpoint w/o any authn/z, please comment the following line.
31+
- manager_auth_proxy_patch.yaml
32+
33+
# Mount the controller config file for loading manager configurations
34+
# through a ComponentConfig type
35+
#- manager_config_patch.yaml
36+
37+
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
38+
# crd/kustomization.yaml
39+
#- manager_webhook_patch.yaml
40+
41+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
42+
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
43+
# 'CERTMANAGER' needs to be enabled to use ca injection
44+
#- webhookcainjection_patch.yaml
45+
46+
# the following config is for teaching kustomize how to do var substitution
47+
vars:
48+
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
49+
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
50+
# objref:
51+
# kind: Certificate
52+
# group: cert-manager.io
53+
# version: v1
54+
# name: serving-cert # this name should match the one in certificate.yaml
55+
# fieldref:
56+
# fieldpath: metadata.namespace
57+
#- name: CERTIFICATE_NAME
58+
# objref:
59+
# kind: Certificate
60+
# group: cert-manager.io
61+
# version: v1
62+
# name: serving-cert # this name should match the one in certificate.yaml
63+
#- name: SERVICE_NAMESPACE # namespace of the service
64+
# objref:
65+
# kind: Service
66+
# version: v1
67+
# name: webhook-service
68+
# fieldref:
69+
# fieldpath: metadata.namespace
70+
#- name: SERVICE_NAME
71+
# objref:
72+
# kind: Service
73+
# version: v1
74+
# name: webhook-service
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# This patch inject a sidecar container which is a HTTP proxy for the
2+
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
3+
apiVersion: apps/v1
4+
kind: Deployment
5+
metadata:
6+
name: controller-manager
7+
namespace: system
8+
spec:
9+
template:
10+
spec:
11+
containers:
12+
- name: kube-rbac-proxy
13+
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.11.0
14+
args:
15+
- "--secure-listen-address=0.0.0.0:8443"
16+
- "--upstream=http://127.0.0.1:8080/"
17+
- "--logtostderr=true"
18+
- "--v=0"
19+
ports:
20+
- containerPort: 8443
21+
protocol: TCP
22+
name: https
23+
resources:
24+
limits:
25+
cpu: 500m
26+
memory: 128Mi
27+
requests:
28+
cpu: 5m
29+
memory: 64Mi
30+
- name: manager
31+
args:
32+
- "--health-probe-bind-address=:8081"
33+
- "--metrics-bind-address=127.0.0.1:8080"
34+
- "--leader-elect"
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
apiVersion: apps/v1
2+
kind: Deployment
3+
metadata:
4+
name: controller-manager
5+
namespace: system
6+
spec:
7+
template:
8+
spec:
9+
containers:
10+
- name: manager
11+
args:
12+
- "--config=controller_manager_config.yaml"
13+
volumeMounts:
14+
- name: manager-config
15+
mountPath: /controller_manager_config.yaml
16+
subPath: controller_manager_config.yaml
17+
volumes:
18+
- name: manager-config
19+
configMap:
20+
name: manager-config

config/kcp/clusterrole.yaml

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
---
2+
apiVersion: rbac.authorization.k8s.io/v1
3+
kind: ClusterRole
4+
metadata:
5+
creationTimestamp: null
6+
name: kcp-manager-role
7+
rules:
8+
- apiGroups:
9+
- apis.kcp.dev
10+
resources:
11+
- apiexports
12+
verbs:
13+
- get
14+
- list
15+
- watch
16+
- apiGroups:
17+
- apis.kcp.dev
18+
resources:
19+
- apiexports/content
20+
verbs:
21+
- '*'

config/kcp/clusterrolebinding.yaml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
apiVersion: rbac.authorization.k8s.io/v1
2+
kind: ClusterRoleBinding
3+
metadata:
4+
name: kcp-manager-rolebinding
5+
roleRef:
6+
apiGroup: rbac.authorization.k8s.io
7+
kind: ClusterRole
8+
name: kcp-manager-role
9+
subjects:
10+
- kind: ServiceAccount
11+
name: controller-manager
12+
namespace: system

config/kcp/kustomization.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
resources:
22
- today.apiresourceschemas.yaml
33
- apiexport.yaml
4+
- clusterrole.yaml
5+
- clusterrolebinding.yaml
46

57
configurations:
68
- kustomizeconfig.yaml

config/manager/manager.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ spec:
3030
- command:
3131
- /manager
3232
args:
33-
- data.my.domain
33+
- --api-export-name data.my.domain
3434
- --leader-elect
3535
image: controller:latest
3636
name: manager

config/rbac/role.yaml

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,98 @@ metadata:
55
creationTimestamp: null
66
name: manager-role
77
rules:
8+
- apiGroups:
9+
- ""
10+
resources:
11+
- configmaps
12+
verbs:
13+
- create
14+
- delete
15+
- get
16+
- list
17+
- patch
18+
- update
19+
- watch
20+
- apiGroups:
21+
- ""
22+
resources:
23+
- configmaps/finalizers
24+
verbs:
25+
- update
26+
- apiGroups:
27+
- ""
28+
resources:
29+
- configmaps/status
30+
verbs:
31+
- get
32+
- patch
33+
- update
34+
- apiGroups:
35+
- ""
36+
resources:
37+
- namespaces
38+
verbs:
39+
- create
40+
- delete
41+
- get
42+
- list
43+
- patch
44+
- update
45+
- watch
46+
- apiGroups:
47+
- ""
48+
resources:
49+
- namespaces/finalizers
50+
verbs:
51+
- update
52+
- apiGroups:
53+
- ""
54+
resources:
55+
- namespaces/status
56+
verbs:
57+
- get
58+
- patch
59+
- update
60+
- apiGroups:
61+
- ""
62+
resources:
63+
- secrets
64+
verbs:
65+
- create
66+
- delete
67+
- get
68+
- list
69+
- patch
70+
- update
71+
- watch
72+
- apiGroups:
73+
- ""
74+
resources:
75+
- secrets/finalizers
76+
verbs:
77+
- update
78+
- apiGroups:
79+
- ""
80+
resources:
81+
- secrets/status
82+
verbs:
83+
- get
84+
- patch
85+
- update
86+
- apiGroups:
87+
- apis.kcp.dev
88+
resources:
89+
- apiexports
90+
verbs:
91+
- get
92+
- list
93+
- watch
94+
- apiGroups:
95+
- apis.kcp.dev
96+
resources:
97+
- apiexports/status
98+
verbs:
99+
- get
8100
- apiGroups:
9101
- data.my.domain
10102
resources:

controllers/configmap_controller.go

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,18 @@ type ConfigMapReconciler struct {
4040
client.Client
4141
}
4242

43+
// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
44+
// +kubebuilder:rbac:groups="",resources=secrets/status,verbs=get;update;patch
45+
// +kubebuilder:rbac:groups="",resources=secrets/finalizers,verbs=update
46+
47+
// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
48+
// +kubebuilder:rbac:groups="",resources=configmaps/status,verbs=get;update;patch
49+
// +kubebuilder:rbac:groups="",resources=configmaps/finalizers,verbs=update
50+
51+
// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;create;update;patch;delete
52+
// +kubebuilder:rbac:groups="",resources=namespaces/status,verbs=get;update;patch
53+
// +kubebuilder:rbac:groups="",resources=namespaces/finalizers,verbs=update
54+
4355
func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
4456
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)
4557

@@ -77,8 +89,20 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
7789
log.Error(err, "unable to list configmaps")
7890
return ctrl.Result{}, nil
7991
}
92+
log.Info("List: got", "itemCount", len(configMapList.Items))
93+
found := false
8094
for _, cm := range configMapList.Items {
81-
log.Info("List: got", "clusterName", logicalcluster.From(&cm).String(), "namespace", cm.Namespace, "name", cm.Name)
95+
if !logicalcluster.From(&cm).Empty() {
96+
log.Info("List: got", "clusterName", logicalcluster.From(&cm).String(), "namespace", cm.Namespace, "name", cm.Name)
97+
} else {
98+
if cm.Name == configMap.Name && cm.Namespace == configMap.Namespace {
99+
if found {
100+
return ctrl.Result{}, fmt.Errorf("there should be listed only one configmap with the given name '%s' for the given namespace '%s' when the clusterName is not available", cm.Name, cm.Namespace)
101+
}
102+
found = true
103+
log.Info("Found in listed configmaps", "namespace", cm.Namespace, "name", cm.Name)
104+
}
105+
}
82106
}
83107

84108
// If the configmap has a namespace field, create the corresponding namespace

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module github.com/kcp-dev/controller-runtime-example
33
go 1.17
44

55
require (
6-
github.com/kcp-dev/apimachinery v0.0.0-20220621200107-3d03cbbc3770
6+
github.com/kcp-dev/apimachinery v0.0.0-20220627134323-8c44889e6e09
77
github.com/kcp-dev/kcp/pkg/apis v0.5.0-alpha.1
88
github.com/kcp-dev/logicalcluster v1.0.0
99
github.com/onsi/ginkgo v1.16.5

go.sum

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,9 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
302302
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
303303
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
304304
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
305-
github.com/kcp-dev/apimachinery v0.0.0-20220621200107-3d03cbbc3770 h1:vO2xIamfv7laTXwf8x+WZKugB1JTF62gHZgf+D0OY9E=
306305
github.com/kcp-dev/apimachinery v0.0.0-20220621200107-3d03cbbc3770/go.mod h1:FIzhTU6DM3HYZhGv8w/1S/mbmSv1HzulZpjr/1/6i/I=
306+
github.com/kcp-dev/apimachinery v0.0.0-20220627134323-8c44889e6e09 h1:glJkPAb39Ca6UjSQzN3IEQUhSwqMsQ1CyqDYIJEo7V0=
307+
github.com/kcp-dev/apimachinery v0.0.0-20220627134323-8c44889e6e09/go.mod h1:FIzhTU6DM3HYZhGv8w/1S/mbmSv1HzulZpjr/1/6i/I=
307308
github.com/kcp-dev/controller-runtime v0.11.3-0.20220624161137-f6e5a2f56683 h1:eev0JOSMdDsRfiTmmRPaYrTUXM/sTq+j2/4oNx78zFQ=
308309
github.com/kcp-dev/controller-runtime v0.11.3-0.20220624161137-f6e5a2f56683/go.mod h1:XP0cED2MCy2/reuXqTwLEENUkZlu6GE1UbQx55IUyA4=
309310
github.com/kcp-dev/kcp/pkg/apis v0.5.0-alpha.1 h1:Z8L4TOam02khARPvS0d3kdyFB1yfwYTCx9x+i09KRzY=

0 commit comments

Comments
 (0)