Skip to content

Commit

Permalink
add status handlers for dcgm and neuron (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
movence authored May 15, 2024
1 parent b1f2465 commit eb43162
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 8 deletions.
7 changes: 3 additions & 4 deletions controllers/dcgmexporter_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests/dcgmexporter"

"github.com/aws/amazon-cloudwatch-agent-operator/apis/v1alpha1"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/config"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests"
collectorStatus "github.com/aws/amazon-cloudwatch-agent-operator/internal/status/collector"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests/dcgmexporter"
dcgmexporterStatus "github.com/aws/amazon-cloudwatch-agent-operator/internal/status/dcgmexporter"
)

// DcgmExporterReconciler reconciles a DcgmExporter object.
Expand Down Expand Up @@ -108,7 +107,7 @@ func (r *DcgmExporterReconciler) Reconcile(ctx context.Context, req ctrl.Request
}

err := reconcileDesiredObjects(ctx, r.Client, log, &params.DcgmExp, params.Scheme, desiredObjects...)
return collectorStatus.HandleReconcileStatus(ctx, log, params, err)
return dcgmexporterStatus.HandleReconcileStatus(ctx, log, params, err)
}

// BuildDcgmExporter returns the generation and collected errors of all manifests for a given instance.
Expand Down
7 changes: 3 additions & 4 deletions controllers/neuronmonitor_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,11 @@ import (
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests/neuronmonitor"

"github.com/aws/amazon-cloudwatch-agent-operator/apis/v1alpha1"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/config"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests"
collectorStatus "github.com/aws/amazon-cloudwatch-agent-operator/internal/status/collector"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests/neuronmonitor"
neuronmonitorStatus "github.com/aws/amazon-cloudwatch-agent-operator/internal/status/neuronmonitor"
)

// NeuronMonitorReconciler reconciles a NeuronMonitor object.
Expand Down Expand Up @@ -107,7 +106,7 @@ func (r *NeuronMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Reques
return ctrl.Result{}, nil
}
err := reconcileDesiredObjects(ctx, r.Client, log, &params.NeuronExp, params.Scheme, desiredObjects...)
return collectorStatus.HandleReconcileStatus(ctx, log, params, err)
return neuronmonitorStatus.HandleReconcileStatus(ctx, log, params, err)
}

// BuildNeuronMonitor returns the generation and collected errors of all manifests for a given instance.
Expand Down
21 changes: 21 additions & 0 deletions internal/status/dcgmexporter/dcgmexporter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package dcgmexporter

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/amazon-cloudwatch-agent-operator/apis/v1alpha1"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/version"
)

func UpdateDcgmExporterStatus(ctx context.Context, cli client.Client, changed *v1alpha1.DcgmExporter) error {
if changed.Status.Version == "" {
changed.Status.Version = version.DcgmExporter()
}

return nil
}
44 changes: 44 additions & 0 deletions internal/status/dcgmexporter/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package dcgmexporter

import (
"context"
"fmt"

"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests"
)

const (
eventTypeNormal = "Normal"
eventTypeWarning = "Warning"

reasonError = "Error"
reasonStatusFailure = "StatusFailure"
reasonInfo = "Info"
)

func HandleReconcileStatus(ctx context.Context, log logr.Logger, params manifests.Params, err error) (ctrl.Result, error) {
log.V(2).Info("updating dcgmexporter status")
if err != nil {
params.Recorder.Event(&params.OtelCol, eventTypeWarning, reasonError, err.Error())
return ctrl.Result{}, err
}
changed := params.DcgmExp.DeepCopy()
statusErr := UpdateDcgmExporterStatus(ctx, params.Client, changed)
if statusErr != nil {
params.Recorder.Event(changed, eventTypeWarning, reasonStatusFailure, statusErr.Error())
return ctrl.Result{}, statusErr
}
statusPatch := client.MergeFrom(&params.DcgmExp)
if err := params.Client.Status().Patch(ctx, changed, statusPatch); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply status changes to the DcgmExporter CR: %w", err)
}
params.Recorder.Event(changed, eventTypeNormal, reasonInfo, "applied status changes")
return ctrl.Result{}, nil
}
44 changes: 44 additions & 0 deletions internal/status/neuronmonitor/handle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package neuronmonitor

import (
"context"
"fmt"

"github.com/go-logr/logr"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/amazon-cloudwatch-agent-operator/internal/manifests"
)

const (
eventTypeNormal = "Normal"
eventTypeWarning = "Warning"

reasonError = "Error"
reasonStatusFailure = "StatusFailure"
reasonInfo = "Info"
)

func HandleReconcileStatus(ctx context.Context, log logr.Logger, params manifests.Params, err error) (ctrl.Result, error) {
log.V(2).Info("updating neuronmonitor status")
if err != nil {
params.Recorder.Event(&params.NeuronExp, eventTypeWarning, reasonError, err.Error())
return ctrl.Result{}, err
}
changed := params.NeuronExp.DeepCopy()
statusErr := UpdateNeuronMonitorStatus(ctx, params.Client, changed)
if statusErr != nil {
params.Recorder.Event(changed, eventTypeWarning, reasonStatusFailure, statusErr.Error())
return ctrl.Result{}, statusErr
}
statusPatch := client.MergeFrom(&params.NeuronExp)
if err := params.Client.Status().Patch(ctx, changed, statusPatch); err != nil {
return ctrl.Result{}, fmt.Errorf("failed to apply status changes to the NeuronMonitor CR: %w", err)
}
params.Recorder.Event(changed, eventTypeNormal, reasonInfo, "applied status changes")
return ctrl.Result{}, nil
}
20 changes: 20 additions & 0 deletions internal/status/neuronmonitor/neuronmonitor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package neuronmonitor

import (
"context"

"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/amazon-cloudwatch-agent-operator/apis/v1alpha1"
"github.com/aws/amazon-cloudwatch-agent-operator/internal/version"
)

func UpdateNeuronMonitorStatus(ctx context.Context, cli client.Client, changed *v1alpha1.NeuronMonitor) error {
if changed.Status.Version == "" {
changed.Status.Version = version.NeuronMonitor()
}
return nil
}

0 comments on commit eb43162

Please sign in to comment.