Query: Stable way to compare spec
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?
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.
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/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas 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
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/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas 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
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/staleis applied - After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied - After 30d of inactivity since
lifecycle/rottenwas 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: 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/staleis applied- After 30d of inactivity since
lifecycle/stalewas applied,lifecycle/rottenis applied- After 30d of inactivity since
lifecycle/rottenwas applied, the issue is closedYou 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.