generated from cloudoperators/repository-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(plugins): standardize and combine plugin controllers (#861)
* refactor(plugins): implement standard interface for HelmController * refactor(plugins): refactor initClientGetter * refactor(plugins): combine PluginWorkloadStatusController into HelmController, tests not passing * refactor(plugins): check only components of ready condition that are under test * feat(plugins): implement plugin option checksum calculation, skipping drift detection when checksum has not changed * refactor(plugins): merge HelmChartTest into HelmController * refactor(plugins): rename HelmController to PluginController, rename merged reconciler files * Automatic generation of CRD API Docs * (chore): resolves conflict for openapi * Automatic generation of CRD API Docs * Automatic application of license header * (chore): rename pluginHelm to plugin * refactor(plugins): improve plugin option checksum calculation --------- Co-authored-by: k.zagorski <k.zagorski@accenture.com> Co-authored-by: Cloud Operator <169066274+cloud-operator@users.noreply.github.com> Co-authored-by: Abhijith Ravindra <137736216+abhijith-darshan@users.noreply.github.com> Co-authored-by: abhijith-darshan <abhijith.ravindra@sap.com> Co-authored-by: License Bot <license_bot@github.com>
- Loading branch information
1 parent
2a93ec4
commit 6e00ff0
Showing
16 changed files
with
520 additions
and
428 deletions.
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
This file was deleted.
Oops, something went wrong.
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,95 @@ | ||
// SPDX-FileCopyrightText: 2024 SAP SE or an SAP affiliate company and Greenhouse contributors | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package plugin | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"reflect" | ||
"strings" | ||
|
||
"github.com/prometheus/client_golang/prometheus" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
|
||
greenhousev1alpha1 "github.com/cloudoperators/greenhouse/pkg/apis/greenhouse/v1alpha1" | ||
"github.com/cloudoperators/greenhouse/pkg/helm" | ||
) | ||
|
||
var ( | ||
chartTestRunsTotal = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "greenhouse_plugin_chart_test_runs_total", | ||
Help: "Total number of Helm Chart test runs with their results", | ||
}, | ||
[]string{"cluster", "plugin", "namespace", "result"}) | ||
) | ||
|
||
func init() { | ||
metrics.Registry.MustRegister(chartTestRunsTotal) | ||
} | ||
|
||
func (r *PluginReconciler) reconcileHelmChartTest(ctx context.Context, plugin *greenhousev1alpha1.Plugin) (*reconcileResult, error) { | ||
// Nothing to do when the status of the plugin is empty and when the plugin does not have a Helm Chart | ||
if reflect.DeepEqual(plugin.Status, greenhousev1alpha1.PluginStatus{}) || plugin.Status.HelmChart == nil { | ||
return nil, nil | ||
} | ||
|
||
if plugin.Spec.Disabled { | ||
return nil, nil | ||
} | ||
|
||
// Helm Chart Test cannot be done as the Helm Chart deployment is not successful | ||
if plugin.Status.HelmReleaseStatus.Status != "deployed" { | ||
return nil, nil | ||
} | ||
|
||
restClientGetter, err := initClientGetter(ctx, r.Client, r.kubeClientOpts, *plugin) | ||
if err != nil { | ||
return nil, fmt.Errorf("cannot access cluster: %s", err.Error()) | ||
} | ||
|
||
// Check if we should continue with reconciliation or requeue if cluster is scheduled for deletion | ||
result, err := shouldReconcileOrRequeue(ctx, r.Client, plugin) | ||
if err != nil { | ||
return nil, err | ||
} | ||
if result != nil { | ||
return &reconcileResult{requeueAfter: result.requeueAfter}, nil | ||
} | ||
|
||
hasHelmChartTest, err := helm.HelmChartTest(ctx, restClientGetter, plugin) | ||
prometheusLabels := prometheus.Labels{ | ||
"cluster": plugin.Spec.ClusterName, | ||
"plugin": plugin.Name, | ||
"namespace": plugin.Namespace, | ||
} | ||
if err != nil { | ||
errStr := fmt.Sprintf("Helm Chart testing failed: %s. To debug, please run `helm test %s`command in your remote cluster %s.", err.Error(), plugin.Name, plugin.Spec.ClusterName) | ||
errStr = strings.ReplaceAll(errStr, "\n", "") | ||
errStr = strings.ReplaceAll(errStr, "\t", " ") | ||
errStr = strings.ReplaceAll(errStr, "*", "") | ||
plugin.SetCondition(greenhousev1alpha1.FalseCondition(greenhousev1alpha1.HelmChartTestSucceededCondition, "", errStr)) | ||
|
||
prometheusLabels["result"] = "Error" | ||
chartTestRunsTotal.With(prometheusLabels).Inc() | ||
|
||
return nil, err | ||
} | ||
|
||
if !hasHelmChartTest { | ||
plugin.SetCondition(greenhousev1alpha1.TrueCondition(greenhousev1alpha1.HelmChartTestSucceededCondition, "", | ||
"No Helm Chart Tests defined by the PluginDefinition")) | ||
|
||
prometheusLabels["result"] = "NoTests" | ||
chartTestRunsTotal.With(prometheusLabels).Inc() | ||
} else { | ||
plugin.SetCondition(greenhousev1alpha1.TrueCondition(greenhousev1alpha1.HelmChartTestSucceededCondition, "", | ||
"Helm Chart Test is successful")) | ||
|
||
prometheusLabels["result"] = "Success" | ||
chartTestRunsTotal.With(prometheusLabels).Inc() | ||
} | ||
|
||
return nil, nil | ||
} |
Oops, something went wrong.