-
Notifications
You must be signed in to change notification settings - Fork 0
fix(metrics): resolve pods to their Deployment, not their ReplicaSet #298
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,200 @@ | ||
| package common | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/coroot/coroot-node-agent/flags" | ||
| "github.com/google/uuid" | ||
| "github.com/stretchr/testify/assert" | ||
| metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
| "k8s.io/apimachinery/pkg/types" | ||
| ) | ||
|
|
||
| // disableEphemeralAggregation pins AggregateEphemeralWorkloads off so these | ||
| // tests exercise the owner-resolution path only. | ||
| func disableEphemeralAggregation(t *testing.T) { | ||
| t.Helper() | ||
| orig := flags.AggregateEphemeralWorkloads | ||
| disabled := false | ||
| flags.AggregateEphemeralWorkloads = &disabled | ||
| t.Cleanup(func() { flags.AggregateEphemeralWorkloads = orig }) | ||
| } | ||
|
|
||
| func controllerRef(name string, uid types.UID, kind string) metav1.OwnerReference { | ||
| yes := true | ||
| return metav1.OwnerReference{Name: name, UID: uid, Kind: kind, Controller: &yes} | ||
| } | ||
|
|
||
| func TestStripPodTemplateHash(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| in string | ||
| wantName string | ||
| wantOk bool | ||
| }{ | ||
| // Deployment-generated ReplicaSet names — the case that caused the | ||
| // duplicate llm-server / llm-server-779f867dc9 series. | ||
| {"real replicaset name", "llm-server-779f867dc9", "llm-server", true}, | ||
| {"second revision", "llm-server-78c94f7dfd", "llm-server", true}, | ||
| {"shorter hash", "services-server-6f476f6f79", "services-server", true}, | ||
| {"hyphenated deployment", "my-long-app-name-5b8d9fcc7", "my-long-app-name", true}, | ||
|
|
||
| // Must NOT be rewritten: a bare ReplicaSet has no pod-template-hash, so | ||
| // stripping would invent a Deployment that does not exist. | ||
| {"bare replicaset, no suffix", "replicaset1", "replicaset1", false}, | ||
| {"no hyphen at all", "myapp", "myapp", false}, | ||
| {"suffix contains vowels", "my-app-frontend", "my-app-frontend", false}, | ||
| {"suffix too short", "my-app-abc", "my-app-abc", false}, | ||
| {"suffix too long", "my-app-bcdfghjklmnpqrstvwxz", "my-app-bcdfghjklmnpqrstvwxz", false}, | ||
| {"trailing hyphen", "my-app-", "my-app-", false}, | ||
| {"leading hyphen only", "-bcdfgh", "-bcdfgh", false}, | ||
| {"uppercase not a k8s hash", "my-app-ABCDEF", "my-app-ABCDEF", false}, | ||
| } | ||
| for _, tt := range tests { | ||
| t.Run(tt.name, func(t *testing.T) { | ||
| got, ok := stripPodTemplateHash(tt.in) | ||
| assert.Equal(t, tt.wantOk, ok) | ||
| assert.Equal(t, tt.wantName, got) | ||
| }) | ||
| } | ||
| } | ||
|
|
||
| // A pod whose ReplicaSet is already cached must resolve all the way to the | ||
| // Deployment, and that result is safe to memoize. | ||
| func TestResolvePodDescriptor_FullChainIsCached(t *testing.T) { | ||
| disableEphemeralAggregation(t) | ||
|
|
||
| rsUID := types.UID(uuid.NewString()) | ||
| deployUID := types.UID(uuid.NewString()) | ||
| podUID := types.UID(uuid.NewString()) | ||
|
|
||
| r := &K8sIPResolver{} | ||
| r.snapshot.ReplicaSets.Store(rsUID, MinimalOwnerInfo{ | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("llm-server", deployUID, "Deployment")}, | ||
| }) | ||
| r.snapshot.Deployments.Store(deployUID, MinimalOwnerInfo{}) | ||
|
|
||
| pod := &MinimalPod{ | ||
| UID: podUID, | ||
| Name: "llm-server-779f867dc9-abcde", | ||
| Namespace: "nudgebee", | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("llm-server-779f867dc9", rsUID, "ReplicaSet")}, | ||
| } | ||
|
|
||
| got := r.resolvePodDescriptor(pod) | ||
| assert.Equal(t, "llm-server", got.Name) | ||
| assert.Equal(t, "Deployment", got.Kind) | ||
| assert.Equal(t, "nudgebee", got.Namespace) | ||
|
|
||
| _, cached := r.snapshot.PodDescriptors.Load(podUID) | ||
| assert.True(t, cached, "a fully-resolved chain should be memoized") | ||
| } | ||
|
|
||
| // Regression for the primary bug: when the owning ReplicaSet is missing from | ||
| // the informer snapshot the climb cannot complete, so the result must NOT be | ||
| // memoized. Previously a shadowed `err` left the outer error nil, the partial | ||
| // (ReplicaSet) identity was cached, and the pod kept that identity forever. | ||
| func TestResolvePodDescriptor_TransientMissIsNotCached(t *testing.T) { | ||
| disableEphemeralAggregation(t) | ||
|
|
||
| rsUID := types.UID(uuid.NewString()) | ||
| podUID := types.UID(uuid.NewString()) | ||
|
|
||
| r := &K8sIPResolver{} // ReplicaSet deliberately absent from the snapshot | ||
|
|
||
| pod := &MinimalPod{ | ||
| UID: podUID, | ||
| Name: "llm-server-779f867dc9-abcde", | ||
| Namespace: "nudgebee", | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("llm-server-779f867dc9", rsUID, "ReplicaSet")}, | ||
| } | ||
|
|
||
| r.resolvePodDescriptor(pod) | ||
|
|
||
| _, cached := r.snapshot.PodDescriptors.Load(podUID) | ||
| assert.False(t, cached, "an unresolved owner chain must not be memoized") | ||
| } | ||
|
|
||
| // The whole point of not caching: once informers sync, the very next call must | ||
| // return the Deployment rather than a stale ReplicaSet identity. | ||
| func TestResolvePodDescriptor_RecoversAfterInformerSync(t *testing.T) { | ||
| disableEphemeralAggregation(t) | ||
|
|
||
| rsUID := types.UID(uuid.NewString()) | ||
| deployUID := types.UID(uuid.NewString()) | ||
| podUID := types.UID(uuid.NewString()) | ||
|
|
||
| r := &K8sIPResolver{} | ||
| pod := &MinimalPod{ | ||
| UID: podUID, | ||
| Name: "llm-server-779f867dc9-abcde", | ||
| Namespace: "nudgebee", | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("llm-server-779f867dc9", rsUID, "ReplicaSet")}, | ||
| } | ||
|
|
||
| // First call: ReplicaSet not cached yet. Falls back to the hash-stripped | ||
| // Deployment name and must not poison the cache. | ||
| first := r.resolvePodDescriptor(pod) | ||
| assert.Equal(t, "llm-server", first.Name, "fallback should strip the pod-template-hash") | ||
|
|
||
| // Informers catch up. | ||
| r.snapshot.ReplicaSets.Store(rsUID, MinimalOwnerInfo{ | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("llm-server", deployUID, "Deployment")}, | ||
| }) | ||
| r.snapshot.Deployments.Store(deployUID, MinimalOwnerInfo{}) | ||
|
|
||
| second := r.resolvePodDescriptor(pod) | ||
| assert.Equal(t, "llm-server", second.Name) | ||
| assert.Equal(t, "Deployment", second.Kind, "must resolve to Deployment once the RS is cached") | ||
|
|
||
| _, cached := r.snapshot.PodDescriptors.Load(podUID) | ||
| assert.True(t, cached, "resolved chain should now be memoized") | ||
| } | ||
|
|
||
| // An owner kind we do not track (e.g. Argo Rollouts) is a terminal condition, | ||
| // not a transient one: retrying can never help, so the identity we already have | ||
| // must still be cached to avoid re-walking the chain on every scrape. | ||
| func TestResolvePodDescriptor_UnsupportedOwnerKindIsCached(t *testing.T) { | ||
| disableEphemeralAggregation(t) | ||
|
|
||
| podUID := types.UID(uuid.NewString()) | ||
| r := &K8sIPResolver{} | ||
|
|
||
| pod := &MinimalPod{ | ||
| UID: podUID, | ||
| Name: "rollout-pod-1", | ||
| Namespace: "nudgebee", | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("my-rollout", types.UID(uuid.NewString()), "Rollout")}, | ||
| } | ||
|
|
||
| got := r.resolvePodDescriptor(pod) | ||
| assert.Equal(t, "my-rollout", got.Name) | ||
| assert.Equal(t, "Rollout", got.Kind) | ||
|
|
||
| _, cached := r.snapshot.PodDescriptors.Load(podUID) | ||
| assert.True(t, cached, "terminal (unsupported kind) resolution should be memoized") | ||
| } | ||
|
|
||
| // A bare ReplicaSet with no pod-template-hash must keep its own name; inventing | ||
| // a Deployment for it would be wrong. | ||
| func TestResolvePodDescriptor_BareReplicaSetKeepsItsName(t *testing.T) { | ||
| disableEphemeralAggregation(t) | ||
|
|
||
| rsUID := types.UID(uuid.NewString()) | ||
| podUID := types.UID(uuid.NewString()) | ||
|
|
||
| r := &K8sIPResolver{} | ||
| // Present in cache, but owned by nothing — the chain terminates here. | ||
| r.snapshot.ReplicaSets.Store(rsUID, MinimalOwnerInfo{}) | ||
|
|
||
| pod := &MinimalPod{ | ||
| UID: podUID, | ||
| Name: "standalone-rs-xyz", | ||
| Namespace: "nudgebee", | ||
| OwnerReferences: []metav1.OwnerReference{controllerRef("standalone-rs", rsUID, "ReplicaSet")}, | ||
| } | ||
|
|
||
| got := r.resolvePodDescriptor(pod) | ||
| assert.Equal(t, "standalone-rs", got.Name) | ||
| assert.Equal(t, "ReplicaSet", got.Kind) | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.