controller-runtime icon indicating copy to clipboard operation
controller-runtime copied to clipboard

Query: Stable way to compare spec

Open Anuragch opened this issue 8 months ago • 1 comments

Hi all,

My CRD controller manages a variety of child resources. During reconciliation, I compare the current spec against the desired state. However, some fields are stored in a different format by Kubernetes – for example, CPU values. If the user specifies 0.2, Kubernetes stores it as 200m, causing unnecessary event churn.

I could preprocess these values in my spec before applying, but is this a sustainable and reliable approach? This issue isn't limited to CPU – memory limits and potentially other fields may also be affected.

Would using a predicate for spec comparison be more reliable in such cases? How do you handle these formatting discrepancies effectively?

Anuragch avatar May 16 '25 10:05 Anuragch

I think the typical approach (not just for controller-runtime) is the one you're leaning towards, e.g. for resource quantities:

package main

import (
	"fmt"
	"k8s.io/apimachinery/pkg/api/resource"
)

func main() {
	q1 := resource.MustParse("0.2")
	q2 := resource.MustParse("200m") // When fetching from k8s you'll probably use a resource.Quantity directly

	fmt.Println("q1:", q1.String())
	fmt.Println("q2:", q2.String())

	if q1.Equal(q2) {
		fmt.Println("The quantities are equal.")
	} else {
		fmt.Println("The quantities are NOT equal.")
	}
}
q1: 200m
q2: 200m
The quantities are equal.

You could also technically compare them as strings, but I'm not sure there's much point to it. If you need to compare nested structures, there's https://pkg.go.dev/k8s.io/apimachinery/pkg/api/equality, or a plain reflect.DeepEqual if you want to be strict about it.

smuth4 avatar May 31 '25 20:05 smuth4

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle stale
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale

k8s-triage-robot avatar Aug 29 '25 21:08 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Mark this issue as fresh with /remove-lifecycle rotten
  • Close this issue with /close
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle rotten

k8s-triage-robot avatar Sep 28 '25 21:09 k8s-triage-robot

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

k8s-triage-robot avatar Oct 28 '25 22:10 k8s-triage-robot

@k8s-triage-robot: Closing this issue, marking it as "Not Planned".

In response to this:

The Kubernetes project currently lacks enough active contributors to adequately respond to all issues and PRs.

This bot triages issues according to the following rules:

  • After 90d of inactivity, lifecycle/stale is applied
  • After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied
  • After 30d of inactivity since lifecycle/rotten was applied, the issue is closed

You can:

  • Reopen this issue with /reopen
  • Mark this issue as fresh with /remove-lifecycle rotten
  • Offer to help out with Issue Triage

Please send feedback to sig-contributor-experience at kubernetes/community.

/close not-planned

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 avatar Oct 28 '25 22:10 k8s-ci-robot