-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
improve EtcdCluster conditions processing (#92)
Fixes: #79 --------- Signed-off-by: Artem Bortnikov <artem.bortnikov@telekom.com>
- Loading branch information
Showing
7 changed files
with
230 additions
and
67 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/* | ||
Copyright 2024 The etcd-operator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package factory | ||
|
||
import ( | ||
"slices" | ||
|
||
etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// Condition is a wrapper over metav1.Condition type. | ||
type Condition struct { | ||
metav1.Condition | ||
} | ||
|
||
// NewCondition returns Condition object with provided condition type. | ||
func NewCondition(conditionType etcdaenixiov1alpha1.EtcdCondType) Condition { | ||
return Condition{metav1.Condition{Type: string(conditionType)}} | ||
} | ||
|
||
// WithStatus converts boolean value passed as an argument to metav1.ConditionStatus type and sets condition status. | ||
func (c Condition) WithStatus(status bool) Condition { | ||
c.Status = metav1.ConditionFalse | ||
if status { | ||
c.Status = metav1.ConditionTrue | ||
} | ||
return c | ||
} | ||
|
||
// WithReason sets condition reason. | ||
func (c Condition) WithReason(reason string) Condition { | ||
c.Reason = reason | ||
return c | ||
} | ||
|
||
// WithMessage sets condition message. | ||
func (c Condition) WithMessage(message string) Condition { | ||
c.Message = message | ||
return c | ||
} | ||
|
||
// Complete finalizes condition building by setting transition timestamp and returns wrapped metav1.Condition object. | ||
func (c Condition) Complete() metav1.Condition { | ||
c.LastTransitionTime = metav1.Now() | ||
return c.Condition | ||
} | ||
|
||
// FillConditions fills EtcdCluster .status.Conditions list with all available conditions in "False" status. | ||
func FillConditions(cluster *etcdaenixiov1alpha1.EtcdCluster) { | ||
SetCondition(cluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized). | ||
WithStatus(false). | ||
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)). | ||
WithMessage(string(etcdaenixiov1alpha1.EtcdInitCondNegMessage)). | ||
Complete()) | ||
SetCondition(cluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionReady). | ||
WithStatus(false). | ||
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeStatefulSetNotReady)). | ||
WithMessage(string(etcdaenixiov1alpha1.EtcdReadyCondNegMessage)). | ||
Complete()) | ||
} | ||
|
||
// SetCondition sets either replaces corresponding existing condition in the .status.Conditions list or appends | ||
// one passed as an argument. In case operation will not result into condition status change, return. | ||
func SetCondition( | ||
cluster *etcdaenixiov1alpha1.EtcdCluster, | ||
condition metav1.Condition, | ||
) { | ||
condition.ObservedGeneration = cluster.GetGeneration() | ||
idx := slices.IndexFunc(cluster.Status.Conditions, func(c metav1.Condition) bool { | ||
return c.Type == condition.Type | ||
}) | ||
|
||
if idx == -1 { | ||
cluster.Status.Conditions = append(cluster.Status.Conditions, condition) | ||
return | ||
} | ||
statusNotChanged := cluster.Status.Conditions[idx].Status == condition.Status | ||
reasonNotChanged := cluster.Status.Conditions[idx].Reason == condition.Reason | ||
if statusNotChanged && reasonNotChanged { | ||
return | ||
} | ||
cluster.Status.Conditions[idx] = condition | ||
} |
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,74 @@ | ||
/* | ||
Copyright 2024 The etcd-operator Authors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package factory | ||
|
||
import ( | ||
"slices" | ||
|
||
etcdaenixiov1alpha1 "github.com/aenix-io/etcd-operator/api/v1alpha1" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var _ = Describe("Condition builder", func() { | ||
Context("When updating condition", func() { | ||
etcdCluster := &etcdaenixiov1alpha1.EtcdCluster{} | ||
|
||
It("should fill conditions", func() { | ||
FillConditions(etcdCluster) | ||
Expect(etcdCluster.Status.Conditions).NotTo(BeEmpty()) | ||
for _, c := range etcdCluster.Status.Conditions { | ||
Expect(c.Status).To(Equal(metav1.ConditionFalse)) | ||
Expect(c.LastTransitionTime).NotTo(BeZero()) | ||
} | ||
}) | ||
|
||
It("should update existing condition", func() { | ||
conditionsLength := len(etcdCluster.Status.Conditions) | ||
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized). | ||
WithStatus(false). | ||
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)). | ||
WithMessage("test"). | ||
Complete()) | ||
Expect(len(etcdCluster.Status.Conditions)).To(Equal(conditionsLength)) | ||
}) | ||
|
||
It("should keep last transition timestamp without status change, otherwise update", func() { | ||
idx := slices.IndexFunc(etcdCluster.Status.Conditions, func(condition metav1.Condition) bool { | ||
return condition.Type == etcdaenixiov1alpha1.EtcdConditionInitialized | ||
}) | ||
timestamp := etcdCluster.Status.Conditions[idx].LastTransitionTime | ||
|
||
By("setting condition without status change") | ||
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized). | ||
WithStatus(false). | ||
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)). | ||
WithMessage("test"). | ||
Complete()) | ||
Expect(etcdCluster.Status.Conditions[idx].LastTransitionTime).To(Equal(timestamp)) | ||
|
||
By("setting condition with status changed") | ||
SetCondition(etcdCluster, NewCondition(etcdaenixiov1alpha1.EtcdConditionInitialized). | ||
WithStatus(true). | ||
WithReason(string(etcdaenixiov1alpha1.EtcdCondTypeInitStarted)). | ||
WithMessage("test"). | ||
Complete()) | ||
Expect(etcdCluster.Status.Conditions[idx].LastTransitionTime).NotTo(Equal(timestamp)) | ||
}) | ||
}) | ||
}) |
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
Oops, something went wrong.