diff --git a/controllers/dcgmexporter_controller.go b/controllers/dcgmexporter_controller.go index 03ad4ef3f..b61c5ef1f 100644 --- a/controllers/dcgmexporter_controller.go +++ b/controllers/dcgmexporter_controller.go @@ -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. @@ -108,7 +107,7 @@ func (r *DcgmExporterReconciler) Reconcile(ctx context.Context, req ctrl.Request } err := reconcileDesiredObjects(ctx, r.Client, log, ¶ms.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. diff --git a/controllers/neuronmonitor_controller.go b/controllers/neuronmonitor_controller.go index 9728104b1..8ab91a0a7 100644 --- a/controllers/neuronmonitor_controller.go +++ b/controllers/neuronmonitor_controller.go @@ -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. @@ -107,7 +106,7 @@ func (r *NeuronMonitorReconciler) Reconcile(ctx context.Context, req ctrl.Reques return ctrl.Result{}, nil } err := reconcileDesiredObjects(ctx, r.Client, log, ¶ms.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. diff --git a/internal/status/dcgmexporter/dcgmexporter.go b/internal/status/dcgmexporter/dcgmexporter.go new file mode 100644 index 000000000..3dad83fba --- /dev/null +++ b/internal/status/dcgmexporter/dcgmexporter.go @@ -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 +} diff --git a/internal/status/dcgmexporter/handle.go b/internal/status/dcgmexporter/handle.go new file mode 100644 index 000000000..75d182b87 --- /dev/null +++ b/internal/status/dcgmexporter/handle.go @@ -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(¶ms.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(¶ms.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 +} diff --git a/internal/status/neuronmonitor/handle.go b/internal/status/neuronmonitor/handle.go new file mode 100644 index 000000000..0700c36bd --- /dev/null +++ b/internal/status/neuronmonitor/handle.go @@ -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(¶ms.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(¶ms.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 +} diff --git a/internal/status/neuronmonitor/neuronmonitor.go b/internal/status/neuronmonitor/neuronmonitor.go new file mode 100644 index 000000000..a23230caf --- /dev/null +++ b/internal/status/neuronmonitor/neuronmonitor.go @@ -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 +}