Skip to content
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 35 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
fd34d3d
Add agent observed generation.
naemono Feb 7, 2022
5bc9db5
Run generation and update yaml for observedGeneration description cha…
naemono Feb 15, 2022
8b7e14a
Properly order imports
naemono Feb 15, 2022
9de32ab
capitalize Agent
naemono Feb 15, 2022
f77b470
remove erroneous debugging line
naemono Feb 15, 2022
30f6c8f
renamed agent e2e test file.
naemono Feb 15, 2022
c6661bb
Ignore generation when checking if agent status is updated during k8s…
naemono Feb 16, 2022
58ed712
remove unneeded nil in return from rebase.
naemono Feb 16, 2022
b6045d0
Add missing header to e2e test.
naemono Feb 16, 2022
1374e88
Remove unused args struct
naemono Feb 16, 2022
5e03689
move imports around properly
naemono Feb 16, 2022
b96cd85
Defer agent validation to reduce repetitive code.
naemono Feb 21, 2022
2bc4fd9
Updating metav1.Object -> client.Object, and using type switch to get…
naemono Feb 22, 2022
a58c2da
Allow status to be updated even if associations are broken.
naemono Mar 9, 2022
250fcb5
Allow error to be returned from doReconcile during defer.
naemono Mar 9, 2022
a78d66d
Merge branch 'main' into 3392-agent-observedGeneration
naemono Mar 9, 2022
27027d0
Merge branch 'main' of https://github.com/elastic/cloud-on-k8s into 3…
naemono Mar 10, 2022
8b549f8
ensure withmutatedfrom is used in e2e agent tests.
naemono Mar 10, 2022
28731f7
Ensure we're not returning nil results
naemono Mar 10, 2022
ea5935e
Fixing comparison issues from rebase/merge
naemono Mar 10, 2022
c10da1b
Add nolint for the naked return, which is required for the defer to f…
naemono Mar 10, 2022
3d9975d
Handle updating status properly when associations are broken.
naemono Mar 10, 2022
bc4f919
Debugging
naemono Mar 10, 2022
ecad6df
Debugging, and moving the defer
naemono Mar 10, 2022
4c8d2ec
Go back to previous way of handling fetch association errors
naemono Mar 10, 2022
d7844d7
remove extraneous logging statement
naemono Mar 10, 2022
205b1cf
Move status update to calling function.
naemono Mar 17, 2022
5177545
Remove unused context
naemono Mar 18, 2022
b850b4f
Update the func's comments to be consistent
naemono Mar 21, 2022
4e6f801
remove unneeded return
naemono Mar 21, 2022
f782303
Merge branch 'main' into 3392-agent-observedGeneration
naemono Mar 24, 2022
0dea707
move common.LowestVersionFromPods closer to where pods are retrieved.
naemono Mar 31, 2022
074933c
changes to return status instead of modifying a pointer.
naemono Mar 31, 2022
2894c06
rename variable
naemono Mar 31, 2022
052fb3f
Updating how status is handled for consistency
naemono Apr 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 10 additions & 14 deletions pkg/controller/agent/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,14 +144,13 @@ func (r *ReconcileAgent) Reconcile(ctx context.Context, request reconcile.Reques
return reconcile.Result{}, nil
}

results, status := r.doReconcile(ctx, *agent)
status, results := r.doReconcile(ctx, *agent)

statusErr := updateStatus(*agent, r.Client, status)
if statusErr != nil {
if apierrors.IsConflict(statusErr) {
if err := updateStatus(*agent, r.Client, status); err != nil {
if apierrors.IsConflict(err) {
return results.WithResult(reconcile.Result{Requeue: true}).Aggregate()
}
results = results.WithError(statusErr)
results = results.WithError(err)
}

result, err := results.Aggregate()
Expand All @@ -160,36 +159,33 @@ func (r *ReconcileAgent) Reconcile(ctx context.Context, request reconcile.Reques
return result, err
}

func (r *ReconcileAgent) doReconcile(ctx context.Context, agent agentv1alpha1.Agent) (*reconciler.Results, agentv1alpha1.AgentStatus) {
func (r *ReconcileAgent) doReconcile(ctx context.Context, agent agentv1alpha1.Agent) (agentv1alpha1.AgentStatus, *reconciler.Results) {
naemono marked this conversation as resolved.
Show resolved Hide resolved
defer tracing.Span(&ctx)()
results := reconciler.NewResult(ctx)
status := newStatus(agent)

areAssocsConfigured, err := association.AreConfiguredIfSet(agent.GetAssociations(), r.recorder)
if err != nil {
return results.WithError(err), status
return status, results.WithError(err)
}
if !areAssocsConfigured {
return results, status
return status, results
}

// Run basic validations as a fallback in case webhook is disabled.
if err := r.validate(ctx, agent); err != nil {
results = results.WithError(err)
return results, status
return status, results
}

driverResults := internalReconcile(Params{
return internalReconcile(Params{
Context: ctx,
Client: r.Client,
EventRecorder: r.recorder,
Watches: r.dynamicWatches,
Agent: agent,
OperatorParams: r.Parameters,
}, &status)
naemono marked this conversation as resolved.
Show resolved Hide resolved

results = results.WithResults(driverResults)
return results, status
}, status)
}

func (r *ReconcileAgent) validate(ctx context.Context, agent agentv1alpha1.Agent) error {
Expand Down
20 changes: 10 additions & 10 deletions pkg/controller/agent/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ func newStatus(agent agentv1alpha1.Agent) agentv1alpha1.AgentStatus {
return status
}

func internalReconcile(params Params, status *agentv1alpha1.AgentStatus) *reconciler.Results {
func internalReconcile(params Params, status agentv1alpha1.AgentStatus) (agentv1alpha1.AgentStatus, *reconciler.Results) {
defer tracing.Span(&params.Context)()
results := reconciler.NewResult(params.Context)

agentVersion, err := version.Parse(params.Agent.Spec.Version)
if err != nil {
return results.WithError(err)
return status, results.WithError(err)
}
assocAllowed, err := association.AllowVersion(agentVersion, &params.Agent, params.Logger(), params.EventRecorder)
if err != nil {
return results.WithError(err)
return status, results.WithError(err)
}
if !assocAllowed {
return results // will eventually retry
return status, results // will eventually retry
}

svc, err := reconcileService(params)
if err != nil {
return results.WithError(err)
return status, results.WithError(err)
}

configHash := fnv.New32a()
Expand All @@ -122,24 +122,24 @@ func internalReconcile(params Params, status *agentv1alpha1.AgentStatus) *reconc
ExtraHTTPSANs: []commonv1.SubjectAlternativeName{{DNS: fmt.Sprintf("*.%s.%s.svc", HTTPServiceName(params.Agent.Name), params.Agent.Namespace)}},
}.ReconcileCAAndHTTPCerts(params.Context)
if caResults.HasError() {
return results.WithResults(caResults)
return status, results.WithResults(caResults)
}
_, _ = configHash.Write(fleetCerts.Data[certificates.CertFileName])
}
if res := reconcileConfig(params, configHash); res.HasError() {
return results.WithResults(res)
return status, results.WithResults(res)
}

// we need to deref the secret here (if any) to include it in the configHash otherwise Agent will not be rolled on content changes
if err := commonassociation.WriteAssocsToConfigHash(params.Client, params.Agent.GetAssociations(), configHash); err != nil {
return results.WithError(err)
return status, results.WithError(err)
}

podTemplate, err := buildPodTemplate(params, fleetCerts, configHash)
if err != nil {
return results.WithError(err)
return status, results.WithError(err)
}
return results.WithResults(reconcilePodVehicle(params, podTemplate, status))
return reconcilePodVehicle(params, podTemplate, status)
}

func reconcileService(params Params) (*corev1.Service, error) {
Expand Down
17 changes: 7 additions & 10 deletions pkg/controller/agent/reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

"github.com/pkg/errors"

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/daemonset"
Expand All @@ -27,7 +29,7 @@ import (
"github.com/elastic/cloud-on-k8s/pkg/utils/pointer"
)

func reconcilePodVehicle(params Params, podTemplate corev1.PodTemplateSpec, status *agentv1alpha1.AgentStatus) *reconciler.Results {
func reconcilePodVehicle(params Params, podTemplate corev1.PodTemplateSpec, status agentv1alpha1.AgentStatus) (agentv1alpha1.AgentStatus, *reconciler.Results) {
defer tracing.Span(&params.Context)()
results := reconciler.NewResult(params.Context)

Expand Down Expand Up @@ -62,7 +64,7 @@ func reconcilePodVehicle(params Params, podTemplate corev1.PodTemplateSpec, stat
})

if err != nil {
return results.WithError(err)
return status, results.WithError(err)
}

// clean up the other one
Expand All @@ -75,16 +77,11 @@ func reconcilePodVehicle(params Params, podTemplate corev1.PodTemplateSpec, stat
results.WithError(err)
}

err = calculateStatus(&params, ready, desired, status)
if err != nil {
params.Logger().Error(
err, "Error while calculating new status",
"namespace", params.Agent.Namespace,
"agent_name", params.Agent.Name,
)
if err = calculateStatus(&params, ready, desired, &status); err != nil {
err = errors.Wrap(err, "while calculating status")
}

return results.WithError(err)
return status, results.WithError(err)
}

func reconcileDeployment(rp ReconciliationParams) (int32, int32, error) {
Expand Down