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

fix(plugins): rename NoHelmChartTestFailuresCondition to HelmChartTestSucceededCondition #748

Merged
merged 16 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 1 addition & 1 deletion docs/reference/api/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
openapi: 3.0.0
info:
title: Greenhouse
version: 0f86140
version: ca963d1
description: PlusOne operations platform
paths:
/TeamMembership:
Expand Down
14 changes: 14 additions & 0 deletions pkg/apis/greenhouse/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package v1alpha1

import (
"slices"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

Expand Down Expand Up @@ -132,6 +134,11 @@ func (sc *StatusConditions) SetConditions(conditionsToSet ...Condition) {
}
break
}

// Remove the deprecated NoHelmChartTestFailures condition if it exists
if currentCondition.Type == NoHelmChartTestFailuresCondition {
sc.Conditions = removeCondition(sc.Conditions, NoHelmChartTestFailuresCondition)
}
}
// if the condition does not exist, append it
if !exists {
Expand All @@ -140,6 +147,13 @@ func (sc *StatusConditions) SetConditions(conditionsToSet ...Condition) {
}
}

// removeCondition removes the condition with the given type from the list of conditions.
func removeCondition(conditions []Condition, conditionType ConditionType) []Condition {
return slices.DeleteFunc(conditions, func(c Condition) bool {
return c.Type == conditionType
})
}

// GetConditionByType returns the condition of the given type, if it exists.
func (sc *StatusConditions) GetConditionByType(conditionType ConditionType) *Condition {
if len(sc.Conditions) == 0 {
Expand Down
55 changes: 55 additions & 0 deletions pkg/apis/greenhouse/v1alpha1/conditions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,4 +353,59 @@ var _ = Describe("Test conditions util functions", func() {
Expect(condition1.Equal(condition2)).To(BeTrue())

})

It("should remove the deprecated NoHelmChartTestFailures condition", func() {
By("removing the deprecated NoHelmChartTestFailures condition")
noHelmChartTestFailuresCondition := greenhousev1alpha1.Condition{
Type: greenhousev1alpha1.NoHelmChartTestFailuresCondition,
Status: metav1.ConditionTrue,
LastTransitionTime: timeNow,
Message: "test",
}

existingCondition1 := greenhousev1alpha1.Condition{
Type: greenhousev1alpha1.HelmReconcileFailedCondition,
Status: metav1.ConditionFalse,
LastTransitionTime: timeNow,
Message: "test",
}

existingCondition2 := greenhousev1alpha1.Condition{
Type: greenhousev1alpha1.HelmDriftDetectedCondition,
Status: metav1.ConditionTrue,
LastTransitionTime: timeNow,
Message: "test",
}

statusConditions := greenhousev1alpha1.StatusConditions{
Conditions: []greenhousev1alpha1.Condition{noHelmChartTestFailuresCondition, existingCondition1, existingCondition2},
}

newCondition1 := greenhousev1alpha1.Condition{
Type: greenhousev1alpha1.HelmChartTestSucceededCondition,
Status: metav1.ConditionTrue,
LastTransitionTime: timeNow,
Message: "test",
}

newCondition2 := greenhousev1alpha1.Condition{
Type: greenhousev1alpha1.ReadyCondition,
Status: metav1.ConditionTrue,
LastTransitionTime: metav1.NewTime(metav1.Now().AddDate(0, 0, -1)),
Message: "test",
}

// Set new condition
statusConditions.SetConditions(newCondition1)
statusConditions.SetConditions(newCondition2)

// Check if the deprecated condition is removed
Expect(statusConditions.GetConditionByType(greenhousev1alpha1.NoHelmChartTestFailuresCondition)).To(BeNil())

// Check if the new condition is added
condition := statusConditions.GetConditionByType(greenhousev1alpha1.HelmChartTestSucceededCondition)
Expect(condition).NotTo(BeNil())
Expect(condition.Status).To(Equal(metav1.ConditionTrue))
Expect(condition.LastTransitionTime).To(Equal(timeNow))
})
})
5 changes: 4 additions & 1 deletion pkg/apis/greenhouse/v1alpha1/plugin_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,12 @@ const (
// StatusUpToDateCondition reflects the failed reconciliation of the Plugin.
StatusUpToDateCondition ConditionType = "StatusUpToDate"

// NoHelmChartTestFailuresCondition reflects the status of the HelmChart tests.
// Deprecated: NoHelmChartTestFailuresCondition reflects the status of the HelmChart tests.
NoHelmChartTestFailuresCondition ConditionType = "NoHelmChartTestFailures"

// HelmChartTestSucceededCondition reflects the status of the HelmChart tests.
HelmChartTestSucceededCondition ConditionType = "HelmChartTestSucceeded"

// PluginDefinitionNotFoundReason is set when the pluginDefinition is not found.
PluginDefinitionNotFoundReason ConditionReason = "PluginDefinitionNotFound"

Expand Down
16 changes: 8 additions & 8 deletions pkg/controllers/plugin/helm_chart_test_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,11 @@ func (r *HelmChartTestReconciler) Reconcile(ctx context.Context, req ctrl.Reques

pluginStatus := initPluginStatus(plugin)

noHelmChartTestFailuresCondition := *pluginStatus.GetConditionByType(greenhousev1alpha1.NoHelmChartTestFailuresCondition)
helmChartTestSucceededCondition := *pluginStatus.GetConditionByType(greenhousev1alpha1.HelmChartTestSucceededCondition)

defer func() {
_, err := clientutil.PatchStatus(ctx, r.Client, plugin, func() error {
plugin.Status.StatusConditions.SetConditions(noHelmChartTestFailuresCondition)
plugin.Status.StatusConditions.SetConditions(helmChartTestSucceededCondition)
return nil
})
if err != nil {
Expand Down Expand Up @@ -119,12 +119,12 @@ func (r *HelmChartTestReconciler) Reconcile(ctx context.Context, req ctrl.Reques
"namespace": plugin.Namespace,
}
if err != nil {
noHelmChartTestFailuresCondition.Status = metav1.ConditionFalse
helmChartTestSucceededCondition.Status = metav1.ConditionFalse
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, "*", "")
noHelmChartTestFailuresCondition.Message = errStr
helmChartTestSucceededCondition.Message = errStr

prometheusLabels["result"] = "Error"
chartTestRunsTotal.With(prometheusLabels).Inc()
Expand All @@ -133,14 +133,14 @@ func (r *HelmChartTestReconciler) Reconcile(ctx context.Context, req ctrl.Reques
}

if !hasHelmChartTest {
noHelmChartTestFailuresCondition.Status = metav1.ConditionTrue
noHelmChartTestFailuresCondition.Message = "No Helm Chart Tests defined by the PluginDefinition"
helmChartTestSucceededCondition.Status = metav1.ConditionTrue
helmChartTestSucceededCondition.Message = "No Helm Chart Tests defined by the PluginDefinition"

prometheusLabels["result"] = "NoTests"
chartTestRunsTotal.With(prometheusLabels).Inc()
} else {
noHelmChartTestFailuresCondition.Status = metav1.ConditionTrue
noHelmChartTestFailuresCondition.Message = "Helm Chart Test is successful"
helmChartTestSucceededCondition.Status = metav1.ConditionTrue
helmChartTestSucceededCondition.Message = "Helm Chart Test is successful"

prometheusLabels["result"] = "Success"
chartTestRunsTotal.With(prometheusLabels).Inc()
Expand Down
4 changes: 2 additions & 2 deletions pkg/controllers/plugin/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ var exposedConditions = []greenhousev1alpha1.ConditionType{
greenhousev1alpha1.HelmDriftDetectedCondition,
greenhousev1alpha1.HelmReconcileFailedCondition,
greenhousev1alpha1.StatusUpToDateCondition,
greenhousev1alpha1.NoHelmChartTestFailuresCondition,
greenhousev1alpha1.HelmChartTestSucceededCondition,
greenhousev1alpha1.WorkloadReadyCondition,
}

Expand Down Expand Up @@ -272,7 +272,7 @@ func computeReadyCondition(
readyCondition.Message = "Helm reconcile failed"
return readyCondition
}
if conditions.GetConditionByType(greenhousev1alpha1.NoHelmChartTestFailuresCondition).IsFalse() {
if conditions.GetConditionByType(greenhousev1alpha1.HelmChartTestSucceededCondition).IsFalse() {
readyCondition.Status = metav1.ConditionFalse
readyCondition.Message = "Helm Chart Test failed"
return readyCondition
Expand Down