Skip to content

Commit

Permalink
Add status conditions 'Ready'
Browse files Browse the repository at this point in the history
  • Loading branch information
ChunyiLyu committed Mar 12, 2021
1 parent c7dc7d7 commit b7ee867
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 0 deletions.
44 changes: 44 additions & 0 deletions api/v1alpha1/conditions.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package v1alpha1

import (
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

const ready ConditionType = "Ready"

type ConditionType string

type Condition struct {
// Type indicates the scope of RabbitmqCluster status addressed by the condition.
Type ConditionType `json:"type"`
// True, False, or Unknown
Status corev1.ConditionStatus `json:"status"`
// The last time this Condition type changed.
LastTransitionTime metav1.Time `json:"lastTransitionTime,omitempty"`
// One word, camel-case reason for current status of the condition.
Reason string `json:"reason,omitempty"`
// Full text reason for current status of the condition.
Message string `json:"message,omitempty"`
}

// Ready indicates that the last Create/Update operator on the CR was successful.
func Ready() Condition {
return Condition{
Type: ready,
Status: corev1.ConditionTrue,
LastTransitionTime: metav1.Now(),
Reason: "SuccessfulCreateOrUpdate",
}
}

// NotReady indicates that the last Create/Update operator on the CR failed.
func NotReady(msg string) Condition {
return Condition{
Type: ready,
Status: corev1.ConditionFalse,
LastTransitionTime: metav1.Now(),
Reason: "FailedCreateOrUpdate",
Message: msg,
}
}
32 changes: 32 additions & 0 deletions api/v1alpha1/conditions_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package v1alpha1

import (
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("Conditions", func() {
Context("Ready", func() {
It("returns 'Ready' condition set to true", func() {
c := Ready()
Expect(string(c.Type)).To(Equal("Ready"))
Expect(c.Status).To(Equal(corev1.ConditionTrue))
Expect(c.Reason).To(Equal("SuccessfulCreateOrUpdate"))
Expect(c.LastTransitionTime).NotTo(Equal(metav1.Time{})) // has been set; not empty
})

})

Context("NotReady", func() {
It("returns 'Ready' condition set to false", func() {
c := NotReady("fail to declare queue")
Expect(string(c.Type)).To(Equal("Ready"))
Expect(c.Status).To(Equal(corev1.ConditionFalse))
Expect(c.Reason).To(Equal("FailedCreateOrUpdate"))
Expect(c.Message).To(Equal("fail to declare queue"))
Expect(c.LastTransitionTime).NotTo(Equal(metav1.Time{})) // has been set; not empty
})
})
})
16 changes: 16 additions & 0 deletions api/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b7ee867

Please sign in to comment.