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

support non-kcp environment #8

Merged
merged 1 commit into from
Jul 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ run: manifests generate fmt vet ## Run a controller from your host.
go run ./main.go $(NAME_PREFIX)$(APIEXPORT_NAME)

.PHONY: docker-build
docker-build: test ## Build docker image with the manager.
docker-build: build ## Build docker image with the manager.
docker build -t ${IMG} .

.PHONY: docker-push
Expand All @@ -101,6 +101,11 @@ install: manifests kustomize ## Install APIResourceSchemas and APIExport into kc
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.
kustomize build config/kcp | kubectl delete --ignore-not-found=$(ignore-not-found) -f -

.PHONY: deploy-crd
deploy-crd: manifests kustomize ## Deploy controller
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
$(KUSTOMIZE) build config/default-crd | kubectl apply -f - || true

.PHONY: deploy
deploy: manifests kustomize ## Deploy controller
cd config/manager && $(KUSTOMIZE) edit set image controller=${IMG}
Expand Down
74 changes: 74 additions & 0 deletions config/default-crd/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
# Adds namespace to all resources.
namespace: controller-runtime-example-system

# Value of this field is prepended to the
# names of all resources, e.g. a deployment named
# "wordpress" becomes "alices-wordpress".
# Note that it should also match with the prefix (text before '-') of the namespace
# field above.
namePrefix: controller-runtime-example-

# Labels to add to all resources and selectors.
#commonLabels:
# someName: someValue

bases:
- ../crd
- ../rbac
- ../manager
# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- ../webhook
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'. 'WEBHOOK' components are required.
#- ../certmanager
# [PROMETHEUS] To enable prometheus monitor, uncomment all sections with 'PROMETHEUS'.
#- ../prometheus

patchesStrategicMerge:
# Protect the /metrics endpoint by putting it behind auth.
# If you want your controller-manager to expose the /metrics
# endpoint w/o any authn/z, please comment the following line.
- manager_auth_proxy_patch.yaml

# Mount the controller config file for loading manager configurations
# through a ComponentConfig type
#- manager_config_patch.yaml

# [WEBHOOK] To enable webhook, uncomment all the sections with [WEBHOOK] prefix including the one in
# crd/kustomization.yaml
#- manager_webhook_patch.yaml

# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER'.
# Uncomment 'CERTMANAGER' sections in crd/kustomization.yaml to enable the CA injection in the admission webhooks.
# 'CERTMANAGER' needs to be enabled to use ca injection
#- webhookcainjection_patch.yaml

# the following config is for teaching kustomize how to do var substitution
vars:
# [CERTMANAGER] To enable cert-manager, uncomment all sections with 'CERTMANAGER' prefix.
#- name: CERTIFICATE_NAMESPACE # namespace of the certificate CR
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
# fieldref:
# fieldpath: metadata.namespace
#- name: CERTIFICATE_NAME
# objref:
# kind: Certificate
# group: cert-manager.io
# version: v1
# name: serving-cert # this name should match the one in certificate.yaml
#- name: SERVICE_NAMESPACE # namespace of the service
# objref:
# kind: Service
# version: v1
# name: webhook-service
# fieldref:
# fieldpath: metadata.namespace
#- name: SERVICE_NAME
# objref:
# kind: Service
# version: v1
# name: webhook-service
34 changes: 34 additions & 0 deletions config/default-crd/manager_auth_proxy_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# This patch inject a sidecar container which is a HTTP proxy for the
# controller manager, it performs RBAC authorization against the Kubernetes API using SubjectAccessReviews.
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: kube-rbac-proxy
image: gcr.io/kubebuilder/kube-rbac-proxy:v0.11.0
args:
- "--secure-listen-address=0.0.0.0:8443"
- "--upstream=http://127.0.0.1:8080/"
- "--logtostderr=true"
- "--v=0"
ports:
- containerPort: 8443
protocol: TCP
name: https
resources:
limits:
cpu: 500m
memory: 128Mi
requests:
cpu: 5m
memory: 64Mi
- name: manager
args:
- "--health-probe-bind-address=:8081"
- "--metrics-bind-address=127.0.0.1:8080"
- "--leader-elect"
20 changes: 20 additions & 0 deletions config/default-crd/manager_config_patch.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: controller-manager
namespace: system
spec:
template:
spec:
containers:
- name: manager
args:
- "--config=controller_manager_config.yaml"
volumeMounts:
- name: manager-config
mountPath: /controller_manager_config.yaml
subPath: controller_manager_config.yaml
volumes:
- name: manager-config
configMap:
name: manager-config
21 changes: 21 additions & 0 deletions config/kcp/clusterrole.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: kcp-manager-role
rules:
- apiGroups:
- apis.kcp.dev
resources:
- apiexports
verbs:
- get
- list
- watch
- apiGroups:
- apis.kcp.dev
resources:
- apiexports/content
verbs:
- '*'
12 changes: 12 additions & 0 deletions config/kcp/clusterrolebinding.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: kcp-manager-rolebinding
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: kcp-manager-role
subjects:
- kind: ServiceAccount
name: controller-manager
namespace: system
2 changes: 2 additions & 0 deletions config/kcp/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
resources:
- today.apiresourceschemas.yaml
- apiexport.yaml
- clusterrole.yaml
- clusterrolebinding.yaml

configurations:
- kustomizeconfig.yaml
2 changes: 1 addition & 1 deletion config/manager/manager.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ spec:
- command:
- /manager
args:
- data.my.domain
- --api-export-name data.my.domain
- --leader-elect
image: controller:latest
name: manager
Expand Down
92 changes: 92 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,98 @@ metadata:
creationTimestamp: null
name: manager-role
rules:
- apiGroups:
- ""
resources:
- configmaps
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- configmaps/finalizers
verbs:
- update
- apiGroups:
- ""
resources:
- configmaps/status
verbs:
- get
- patch
- update
- apiGroups:
- ""
resources:
- namespaces
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- namespaces/finalizers
verbs:
- update
- apiGroups:
- ""
resources:
- namespaces/status
verbs:
- get
- patch
- update
- apiGroups:
- ""
resources:
- secrets
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- ""
resources:
- secrets/finalizers
verbs:
- update
- apiGroups:
- ""
resources:
- secrets/status
verbs:
- get
- patch
- update
- apiGroups:
- apis.kcp.dev
resources:
- apiexports
verbs:
- get
- list
- watch
- apiGroups:
- apis.kcp.dev
resources:
- apiexports/status
verbs:
- get
- apiGroups:
- data.my.domain
resources:
Expand Down
26 changes: 25 additions & 1 deletion controllers/configmap_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ type ConfigMapReconciler struct {
client.Client
}

// +kubebuilder:rbac:groups="",resources=secrets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=secrets/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=secrets/finalizers,verbs=update

// +kubebuilder:rbac:groups="",resources=configmaps,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=configmaps/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=configmaps/finalizers,verbs=update

// +kubebuilder:rbac:groups="",resources=namespaces,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="",resources=namespaces/status,verbs=get;update;patch
// +kubebuilder:rbac:groups="",resources=namespaces/finalizers,verbs=update

func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
log := log.FromContext(ctx).WithValues("cluster", req.ClusterName)

Expand Down Expand Up @@ -77,8 +89,20 @@ func (r *ConfigMapReconciler) Reconcile(ctx context.Context, req ctrl.Request) (
log.Error(err, "unable to list configmaps")
return ctrl.Result{}, nil
}
log.Info("List: got", "itemCount", len(configMapList.Items))
found := false
for _, cm := range configMapList.Items {
log.Info("List: got", "clusterName", logicalcluster.From(&cm).String(), "namespace", cm.Namespace, "name", cm.Name)
if !logicalcluster.From(&cm).Empty() {
log.Info("List: got", "clusterName", logicalcluster.From(&cm).String(), "namespace", cm.Namespace, "name", cm.Name)
} else {
if cm.Name == configMap.Name && cm.Namespace == configMap.Namespace {
if found {
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)
}
found = true
log.Info("Found in listed configmaps", "namespace", cm.Namespace, "name", cm.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd move this back out (just inside the else block) and change it back to the List: got... log message. There can be multiple configmaps in a cluster across all namespaces, and this is supposed to show all of them.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, but imagine what it looks like when it's "connected" to a normal OpenShift cluster - there are approx. 550 ConfigMaps in the cluster and all of them are printed every time when the leader election updates its own configmap. This makes the whole log unreadable.
That's why I change it so it prints the count of all CMs found either in the cluster or in the workspace, plus:

  • when it's running against kcp cluster (clusterName is set) then it shows all of them (as you requested)
  • when it's running against a normal cluster, then it only finds the right ConfigMap which triggered the reconcile, and prints only that one

}
}
}

// If the configmap has a namespace field, create the corresponding namespace
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/kcp-dev/controller-runtime-example
go 1.17

require (
github.com/kcp-dev/apimachinery v0.0.0-20220621200107-3d03cbbc3770
github.com/kcp-dev/apimachinery v0.0.0-20220627134323-8c44889e6e09
github.com/kcp-dev/kcp/pkg/apis v0.5.0-alpha.1
github.com/kcp-dev/logicalcluster v1.0.0
github.com/onsi/ginkgo v1.16.5
Expand Down
3 changes: 2 additions & 1 deletion go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -302,8 +302,9 @@ github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/X
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
github.com/julienschmidt/httprouter v1.3.0/go.mod h1:JR6WtHb+2LUe8TCKY3cZOxFyyO8IZAc4RVcycCCAKdM=
github.com/kcp-dev/apimachinery v0.0.0-20220621200107-3d03cbbc3770 h1:vO2xIamfv7laTXwf8x+WZKugB1JTF62gHZgf+D0OY9E=
github.com/kcp-dev/apimachinery v0.0.0-20220621200107-3d03cbbc3770/go.mod h1:FIzhTU6DM3HYZhGv8w/1S/mbmSv1HzulZpjr/1/6i/I=
github.com/kcp-dev/apimachinery v0.0.0-20220627134323-8c44889e6e09 h1:glJkPAb39Ca6UjSQzN3IEQUhSwqMsQ1CyqDYIJEo7V0=
github.com/kcp-dev/apimachinery v0.0.0-20220627134323-8c44889e6e09/go.mod h1:FIzhTU6DM3HYZhGv8w/1S/mbmSv1HzulZpjr/1/6i/I=
github.com/kcp-dev/controller-runtime v0.11.3-0.20220624161137-f6e5a2f56683 h1:eev0JOSMdDsRfiTmmRPaYrTUXM/sTq+j2/4oNx78zFQ=
github.com/kcp-dev/controller-runtime v0.11.3-0.20220624161137-f6e5a2f56683/go.mod h1:XP0cED2MCy2/reuXqTwLEENUkZlu6GE1UbQx55IUyA4=
github.com/kcp-dev/kcp/pkg/apis v0.5.0-alpha.1 h1:Z8L4TOam02khARPvS0d3kdyFB1yfwYTCx9x+i09KRzY=
Expand Down
Loading