-
Notifications
You must be signed in to change notification settings - Fork 697
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
Elastic Agent: Set status.ObservedGeneration from metadata.Generation #5383
Merged
Merged
Changes from all commits
Commits
Show all changes
35 commits
Select commit
Hold shift + click to select a range
fd34d3d
Add agent observed generation.
naemono 5bc9db5
Run generation and update yaml for observedGeneration description cha…
naemono 8b7e14a
Properly order imports
naemono 9de32ab
capitalize Agent
naemono f77b470
remove erroneous debugging line
naemono 30f6c8f
renamed agent e2e test file.
naemono c6661bb
Ignore generation when checking if agent status is updated during k8s…
naemono 58ed712
remove unneeded nil in return from rebase.
naemono b6045d0
Add missing header to e2e test.
naemono 1374e88
Remove unused args struct
naemono 5e03689
move imports around properly
naemono b96cd85
Defer agent validation to reduce repetitive code.
naemono 2bc4fd9
Updating metav1.Object -> client.Object, and using type switch to get…
naemono a58c2da
Allow status to be updated even if associations are broken.
naemono 250fcb5
Allow error to be returned from doReconcile during defer.
naemono a78d66d
Merge branch 'main' into 3392-agent-observedGeneration
naemono 27027d0
Merge branch 'main' of https://github.com/elastic/cloud-on-k8s into 3…
naemono 8b549f8
ensure withmutatedfrom is used in e2e agent tests.
naemono 28731f7
Ensure we're not returning nil results
naemono ea5935e
Fixing comparison issues from rebase/merge
naemono c10da1b
Add nolint for the naked return, which is required for the defer to f…
naemono 3d9975d
Handle updating status properly when associations are broken.
naemono bc4f919
Debugging
naemono ecad6df
Debugging, and moving the defer
naemono 4c8d2ec
Go back to previous way of handling fetch association errors
naemono d7844d7
remove extraneous logging statement
naemono 205b1cf
Move status update to calling function.
naemono 5177545
Remove unused context
naemono b850b4f
Update the func's comments to be consistent
naemono 4e6f801
remove unneeded return
naemono f782303
Merge branch 'main' into 3392-agent-observedGeneration
naemono 0dea707
move common.LowestVersionFromPods closer to where pods are retrieved.
naemono 074933c
changes to return status instead of modifying a pointer.
naemono 2894c06
rename variable
naemono 052fb3f
Updating how status is handled for consistency
naemono 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 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 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 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 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 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 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,238 @@ | ||
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
// or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
// you may not use this file except in compliance with the Elastic License 2.0. | ||
|
||
package agent | ||
|
||
import ( | ||
"context" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
appsv1 "k8s.io/api/apps/v1" | ||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/runtime" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
agentv1alpha1 "github.com/elastic/cloud-on-k8s/pkg/apis/agent/v1alpha1" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/common" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/common/comparison" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/common/hash" | ||
"github.com/elastic/cloud-on-k8s/pkg/controller/common/watches" | ||
"github.com/elastic/cloud-on-k8s/pkg/utils/k8s" | ||
"github.com/elastic/cloud-on-k8s/pkg/utils/pointer" | ||
) | ||
|
||
func newReconcileAgent(objs ...runtime.Object) *ReconcileAgent { | ||
r := &ReconcileAgent{ | ||
Client: k8s.NewFakeClient(objs...), | ||
recorder: record.NewFakeRecorder(100), | ||
dynamicWatches: watches.NewDynamicWatches(), | ||
} | ||
return r | ||
} | ||
|
||
func TestReconcileAgent_Reconcile(t *testing.T) { | ||
defaultLabels := NewLabels(agentv1alpha1.Agent{ObjectMeta: metav1.ObjectMeta{Name: "testAgent"}}) | ||
tests := []struct { | ||
name string | ||
objs []runtime.Object | ||
request reconcile.Request | ||
want reconcile.Result | ||
expected agentv1alpha1.Agent | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid unmanaged agent does not increment observedGeneration", | ||
objs: []runtime.Object{ | ||
&agentv1alpha1.Agent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgent", | ||
Namespace: "test", | ||
Generation: 1, | ||
Annotations: map[string]string{ | ||
common.ManagedAnnotation: "false", | ||
}, | ||
}, | ||
Spec: agentv1alpha1.AgentSpec{ | ||
Version: "8.0.1", | ||
Deployment: &agentv1alpha1.DeploymentSpec{}, | ||
}, | ||
Status: agentv1alpha1.AgentStatus{ | ||
ObservedGeneration: 1, | ||
}, | ||
}, | ||
}, | ||
request: reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: "test", | ||
Name: "testAgent", | ||
}, | ||
}, | ||
want: reconcile.Result{}, | ||
expected: agentv1alpha1.Agent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgent", | ||
Namespace: "test", | ||
Generation: 1, | ||
Annotations: map[string]string{ | ||
common.ManagedAnnotation: "false", | ||
}, | ||
}, | ||
Spec: agentv1alpha1.AgentSpec{ | ||
Version: "8.0.1", | ||
Deployment: &agentv1alpha1.DeploymentSpec{}, | ||
}, | ||
Status: agentv1alpha1.AgentStatus{ | ||
ObservedGeneration: 1, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "too long name fails validation, and updates observedGeneration", | ||
objs: []runtime.Object{ | ||
&agentv1alpha1.Agent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgentwithtoolongofanamereallylongname", | ||
Namespace: "test", | ||
Generation: 2, | ||
}, | ||
Status: agentv1alpha1.AgentStatus{ | ||
ObservedGeneration: 1, | ||
}, | ||
}, | ||
}, | ||
request: reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: "test", | ||
Name: "testAgentwithtoolongofanamereallylongname", | ||
}, | ||
}, | ||
want: reconcile.Result{}, | ||
expected: agentv1alpha1.Agent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgentwithtoolongofanamereallylongname", | ||
Namespace: "test", | ||
Generation: 2, | ||
}, | ||
Status: agentv1alpha1.AgentStatus{ | ||
ObservedGeneration: 2, | ||
}, | ||
}, | ||
wantErr: true, | ||
}, | ||
{ | ||
name: "agent with ready deployment+pod updates status.health properly", | ||
objs: []runtime.Object{ | ||
&agentv1alpha1.Agent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgent", | ||
Namespace: "test", | ||
Generation: 2, | ||
}, | ||
Spec: agentv1alpha1.AgentSpec{ | ||
Version: "8.0.1", | ||
Deployment: &agentv1alpha1.DeploymentSpec{ | ||
Replicas: pointer.Int32(1), | ||
}, | ||
}, | ||
Status: agentv1alpha1.AgentStatus{ | ||
ObservedGeneration: 1, | ||
}, | ||
}, | ||
&appsv1.Deployment{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgent-agent", | ||
Namespace: "test", | ||
Labels: addLabel(defaultLabels, hash.TemplateHashLabelName, "2519944696"), | ||
}, | ||
Status: appsv1.DeploymentStatus{ | ||
AvailableReplicas: 1, | ||
Replicas: 1, | ||
ReadyReplicas: 1, | ||
Conditions: []appsv1.DeploymentCondition{ | ||
{ | ||
Type: appsv1.DeploymentAvailable, | ||
Status: corev1.ConditionTrue, | ||
}, | ||
}, | ||
}, | ||
}, | ||
&corev1.Pod{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgent", | ||
Namespace: "test", | ||
Generation: 2, | ||
Labels: map[string]string{NameLabelName: "testAgent", VersionLabelName: "8.0.1"}, | ||
}, | ||
Status: corev1.PodStatus{ | ||
Phase: corev1.PodRunning, | ||
}, | ||
}, | ||
}, | ||
request: reconcile.Request{ | ||
NamespacedName: types.NamespacedName{ | ||
Namespace: "test", | ||
Name: "testAgent", | ||
}, | ||
}, | ||
want: reconcile.Result{}, | ||
expected: agentv1alpha1.Agent{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "testAgent", | ||
Namespace: "test", | ||
Generation: 2, | ||
}, | ||
Spec: agentv1alpha1.AgentSpec{ | ||
Version: "8.0.1", | ||
Deployment: &agentv1alpha1.DeploymentSpec{ | ||
Replicas: pointer.Int32(1), | ||
}, | ||
}, | ||
Status: agentv1alpha1.AgentStatus{ | ||
Version: "8.0.1", | ||
ExpectedNodes: 1, | ||
AvailableNodes: 1, | ||
ObservedGeneration: 2, | ||
Health: agentv1alpha1.AgentGreenHealth, | ||
}, | ||
}, | ||
wantErr: false, | ||
}, | ||
} | ||
for _, tt := range tests { | ||
naemono marked this conversation as resolved.
Show resolved
Hide resolved
|
||
t.Run(tt.name, func(t *testing.T) { | ||
r := newReconcileAgent(tt.objs...) | ||
got, err := r.Reconcile(context.Background(), tt.request) | ||
if (err != nil) != tt.wantErr { | ||
t.Errorf("ReconcileAgent.Reconcile() error = %v, wantErr %v", err, tt.wantErr) | ||
return | ||
} | ||
if !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("ReconcileAgent.Reconcile() = %v, want %v", got, tt.want) | ||
} | ||
|
||
var agent agentv1alpha1.Agent | ||
if err := r.Client.Get(context.Background(), tt.request.NamespacedName, &agent); err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
// AllowUnexported required because of *AssocConf on the agent. | ||
comparison.AssertEqual(t, &agent, &tt.expected, cmp.AllowUnexported(agentv1alpha1.Agent{})) | ||
}) | ||
} | ||
} | ||
|
||
func addLabel(labels map[string]string, key, value string) map[string]string { | ||
newLabels := make(map[string]string, len(labels)) | ||
for k, v := range labels { | ||
newLabels[k] = v | ||
} | ||
newLabels[key] = value | ||
return newLabels | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are using this in only one place you might as well inline it. But its fine if you want to keep it.