Skip to content

🌱examples-builtin: Refactor controller setup to use builder pattern #3265

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 29, 2025

Conversation

s-z-z
Copy link
Contributor

@s-z-z s-z-z commented Jul 29, 2025

What

Refactors builtin example: main.go to use controller-runtime builder pattern (ctrl.NewControllerManagedBy) instead of controller.New for controller setup, consolidating ReplicaSet and Pod watches.

Why

The builder pattern is the recommended approach for Kubernetes controllers, improving code readability, maintainability, and alignment with community practices.

Impact

No functional changes. Enhances code clarity and consistency with other projects.

@k8s-ci-robot k8s-ci-robot added the cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. label Jul 29, 2025
@k8s-ci-robot k8s-ci-robot requested a review from joelanford July 29, 2025 09:35
@k8s-ci-robot
Copy link
Contributor

Welcome @s-z-z!

It looks like this is your first PR to kubernetes-sigs/controller-runtime 🎉. Please refer to our pull request process documentation to help your PR have a smooth ride to approval.

You will be prompted by a bot to use commands during the review process. Do not be afraid to follow the prompts! It is okay to experiment. Here is the bot commands documentation.

You can also check if kubernetes-sigs/controller-runtime has its own contribution guidelines.

You may want to refer to our testing guide if you run into trouble with your tests not passing.

If you are having difficulty getting your pull request seen, please follow the recommended escalation practices. Also, for tips and tricks in the contribution process you may want to read the Kubernetes contributor cheat sheet. We want to make sure your contribution gets all the attention it needs!

Thank you, and welcome to Kubernetes. 😃

@k8s-ci-robot k8s-ci-robot requested a review from JoelSpeed July 29, 2025 09:35
@k8s-ci-robot k8s-ci-robot added the needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. label Jul 29, 2025
@k8s-ci-robot
Copy link
Contributor

Hi @s-z-z. Thanks for your PR.

I'm waiting for a kubernetes-sigs member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work. Regular contributors should join the org to skip this step.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@k8s-ci-robot k8s-ci-robot added the size/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 29, 2025
@s-z-z s-z-z closed this Jul 29, 2025
@s-z-z s-z-z reopened this Jul 29, 2025
@s-z-z
Copy link
Contributor Author

s-z-z commented Jul 29, 2025

I test it, and it works correctly.

here is the result

command line operation

xdev➜  k --kubeconfig /tmp/k8s_test_framework_1613410604/4021304843.kubecfg get rs
No resources found in default namespace.

xdev➜  cat rs.yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  name: frigate-replicaset
  namespace: default
  labels:
    app: frigate
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frigate
  template:
    metadata:
      labels:
        app: frigate
    spec:
      containers:
        - name: frigate
          image: nginx:1.14.2#

xdev➜  k --kubeconfig /tmp/k8s_test_framework_1613410604/4021304843.kubecfg apply -f rs.yaml
replicaset.apps/frigate-replicaset created

xdev➜  k --kubeconfig /tmp/k8s_test_framework_1613410604/4021304843.kubecfg get rs frigate-replicaset -o yaml
apiVersion: apps/v1
kind: ReplicaSet
metadata:
  annotations:
    kubectl.kubernetes.io/last-applied-configuration: |
      {"apiVersion":"apps/v1","kind":"ReplicaSet","metadata":{"annotations":{},"labels":{"app":"frigate"},"name":"frigate-replicaset","namespace":"default"},"spec":{"replicas":3,"selector":{"matchLabels":{"app":"frigate"}},"template":{"metadata":{"labels":{"app":"frigate"}},"spec":{"containers":[{"image":"nginx:1.14.2","name":"frigate"}]}}}}
  creationTimestamp: "2025-07-29T10:53:09Z"
  generation: 1
  labels:
    app: frigate
    hello: world
  name: frigate-replicaset
  namespace: default
  resourceVersion: "212"
  uid: ba2d8168-ea52-48e4-88c5-3b062246377e
spec:
  replicas: 3
  selector:
    matchLabels:
      app: frigate
  template:
    metadata:
      creationTimestamp: null
      labels:
        app: frigate
    spec:
      containers:
      - image: nginx:1.14.2
        imagePullPolicy: IfNotPresent
        name: frigate
        resources: {}
        terminationMessagePath: /dev/termination-log
        terminationMessagePolicy: File
      dnsPolicy: ClusterFirst
      restartPolicy: Always
      schedulerName: default-scheduler
      securityContext: {}
      terminationGracePeriodSeconds: 30
status:
  replicas: 0

the rs got the label hello: world after create.

controller log

KUBEBUILDER_ASSETS="/root/envtest/k8s/1.33.0-linux-amd64" go run ./cmd/...
{"level":"info","ts":"2025-07-29T18:52:18+08:00","logger":"entrypoint","msg":"setting up manager"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"entrypoint","msg":"Setting up controller"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.builder","msg":"Registering a mutating webhook","GVK":"/v1, Kind=Pod","path":"/mutate--v1-pod"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.webhook","msg":"Registering webhook","path":"/mutate--v1-pod"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.builder","msg":"Registering a validating webhook","GVK":"/v1, Kind=Pod","path":"/validate--v1-pod"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.webhook","msg":"Registering webhook","path":"/validate--v1-pod"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"entrypoint","msg":"starting manager"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.metrics","msg":"Starting metrics server"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.webhook","msg":"Starting webhook server"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.metrics","msg":"Serving metrics server","bindAddress":":8080","secure":false}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.certwatcher","msg":"Updated current TLS certificate"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.webhook","msg":"Serving webhook server","host":"","port":9443}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","msg":"Starting EventSource","controller":"foo-controller","source":"kind source: *v1.Pod"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","logger":"controller-runtime.certwatcher","msg":"Starting certificate poll+watcher","interval":10}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","msg":"Starting EventSource","controller":"foo-controller","source":"kind source: *v1.ReplicaSet"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","msg":"Starting Controller","controller":"foo-controller"}
{"level":"info","ts":"2025-07-29T18:52:22+08:00","msg":"Starting workers","controller":"foo-controller","worker count":1}
{"level":"info","ts":"2025-07-29T18:53:09+08:00","msg":"Reconciling ReplicaSet","controller":"foo-controller","namespace":"default","name":"frigate-replicaset","reconcileID":"8cfd1326-b14b-4c51-8cd9-12fcdd3577f8","container name":"frigate"}
{"level":"info","ts":"2025-07-29T18:53:09+08:00","msg":"Reconciling ReplicaSet","controller":"foo-controller","namespace":"default","name":"frigate-replicaset","reconcileID":"b303c793-7791-4454-b42e-f1f9fca1b238","container name":"frigate"}

…By and move logging to ctrl.SetLogger

Signed-off-by: suzi1037 <[email protected]>
@alvaroaleman alvaroaleman added the tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges. label Jul 29, 2025
@k8s-ci-robot k8s-ci-robot added the lgtm "Looks good to me", indicates that a PR is ready to be merged. label Jul 29, 2025
@k8s-ci-robot
Copy link
Contributor

LGTM label has been added.

Git tree hash: d9614a9e1e60311c58ac2c07f5691c33c0914e5e

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: alvaroaleman, s-z-z

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@k8s-ci-robot k8s-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Jul 29, 2025
@k8s-ci-robot k8s-ci-robot merged commit ada8079 into kubernetes-sigs:main Jul 29, 2025
9 checks passed
@s-z-z s-z-z deleted the example-buitin branch July 30, 2025 02:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. lgtm "Looks good to me", indicates that a PR is ready to be merged. needs-ok-to-test Indicates a PR that requires an org member to verify it is safe to test. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. tide/merge-method-squash Denotes a PR that should be squashed by tide when it merges.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants