Skip to content

Commit

Permalink
feat(plugins) More precise metrics (#439)
Browse files Browse the repository at this point in the history
  • Loading branch information
gciezkowski-acc committed Dec 10, 2024
1 parent 4ab40f4 commit f9ddc52
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/controllers/plugin/helm_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,11 @@ func (r *HelmReconciler) reconcileHelmRelease(
if err := helm.InstallOrUpgradeHelmChartFromPlugin(ctx, r.Client, restClientGetter, pluginDefinition, plugin); err != nil {
reconcileFailedCondition.Status = metav1.ConditionTrue
reconcileFailedCondition.Message = "Helm install/upgrade failed: " + err.Error()
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonUninstallHelmFailed)
return driftDetectedCondition, reconcileFailedCondition
}
reconcileFailedCondition.Status = metav1.ConditionFalse
reconcileFailedCondition.Message = "Helm install/upgrade successful"
metrics.UpdateMetrics(plugin, metrics.MetricResultSuccess, metrics.MetricReasonEmpty)
return driftDetectedCondition, reconcileFailedCondition
}

Expand Down
6 changes: 6 additions & 0 deletions pkg/helm/helm.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ const driftDetectionInterval = 60 * time.Minute
func InstallOrUpgradeHelmChartFromPlugin(ctx context.Context, local client.Client, restClientGetter genericclioptions.RESTClientGetter, pluginDefinition *greenhousev1alpha1.PluginDefinition, plugin *greenhousev1alpha1.Plugin) error {
// Early return if the pluginDefinition is not helm based
if pluginDefinition.Spec.HelmChart == nil {
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonHelmChartIsNotDefined)
return fmt.Errorf("no helm chart defined in pluginDefinition.Spec.HelmChart for pluginDefinition %s", plugin.Spec.PluginDefinition)
}
latestRelease, isReleaseExists, err := isReleaseExistsForPlugin(ctx, restClientGetter, plugin)
Expand All @@ -71,27 +72,32 @@ func InstallOrUpgradeHelmChartFromPlugin(ctx context.Context, local client.Clien
}
helmChart, err := locateChartForPlugin(restClientGetter, pluginDefinition)
if err != nil {
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonUpgradeFailed)
return err
}
// Avoid attempts to upgrade a failed release and attempt to resurrect it.
if latestRelease.Info != nil && latestRelease.Info.Status == release.StatusFailed {
log.FromContext(ctx).Info("attempting to reset release status", "current status", latestRelease.Info.Status.String())
if err := ResetHelmReleaseStatusToDeployed(ctx, restClientGetter, plugin); err != nil {
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonUpgradeFailed)
return err
}
}
// Avoid upgrading a currently pending release.
if releaseStatus, ok := isCanReleaseBeUpgraded(latestRelease); !ok {
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonUpgradeFailed)
return fmt.Errorf("cannot upgrade release %s/%s in status %s", latestRelease.Namespace, latestRelease.Name, releaseStatus.String())
}
log.FromContext(ctx).Info("upgrading release", "namespace", plugin.Spec.ReleaseNamespace, "name", plugin.Name)

c, err := clientutil.NewK8sClientFromRestClientGetter(restClientGetter)
if err != nil {
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonUpgradeFailed)
return err
}

if err := replaceCustomResourceDefinitions(ctx, c, helmChart.CRDObjects(), true); err != nil {
metrics.UpdateMetrics(plugin, metrics.MetricResultError, metrics.MetricReasonUpgradeFailed)
return err
}

Expand Down
1 change: 1 addition & 0 deletions pkg/metrics/plugin_metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const (
MetricReasonPluginDefinitionNotFound MetricReason = "plugin_definition_not_found"
MetricReasonTemplateFailed MetricReason = "template_failed"
MetricReasonDiffFailed MetricReason = "diff_failed"
MetricReasonHelmChartIsNotDefined MetricReason = "helm_chart_is_not_defined"
)

var (
Expand Down

0 comments on commit f9ddc52

Please sign in to comment.