/
positive_condition_builder.go
87 lines (72 loc) · 3.04 KB
/
positive_condition_builder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/*
* Copyright (c) Microsoft Corporation.
* Licensed under the MIT license.
*/
package conditions
import (
"time"
"github.com/benbjohnson/clock"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
const ReasonSucceeded = "Succeeded"
// TODO: name?
type PositiveConditionBuilderInterface interface {
MakeTrueCondition(conditionType ConditionType, observedGeneration int64) Condition
MakeFalseCondition(conditionType ConditionType, severity ConditionSeverity, observedGeneration int64, reason string, message string) Condition
MakeUnknownCondition(conditionType ConditionType, observedGeneration int64, reason string, message string) Condition
}
var _ PositiveConditionBuilderInterface = &PositiveConditionBuilder{}
// PositiveConditionBuilder makes positive polarity conditions. A positive polarity condition
// is a condition where Status==True is the "happy path" state, and Status==False is a failure
// state.
type PositiveConditionBuilder struct {
clock clock.Clock
Ready *ReadyConditionBuilder
}
// NewPositiveConditionBuilder creates a new PositiveConditionBuilder for creating positive polarity conditions.
func NewPositiveConditionBuilder(clock clock.Clock) *PositiveConditionBuilder {
result := &PositiveConditionBuilder{
clock: clock,
}
result.Ready = NewReadyConditionBuilder(result)
return result
}
func (b *PositiveConditionBuilder) now() metav1.Time {
return metav1.NewTime(b.clock.Now().UTC().Truncate(time.Second))
}
// MakeTrueCondition makes a condition whose Status is True
func (b *PositiveConditionBuilder) MakeTrueCondition(conditionType ConditionType, observedGeneration int64) Condition {
return Condition{
Type: conditionType,
Status: metav1.ConditionTrue,
Severity: ConditionSeverityNone,
LastTransitionTime: b.now(),
ObservedGeneration: observedGeneration,
Reason: ReasonSucceeded,
}
}
// MakeFalseCondition makes a condition whose Status is False. A severity, reason, and message must be provided.
func (b *PositiveConditionBuilder) MakeFalseCondition(conditionType ConditionType, severity ConditionSeverity, observedGeneration int64, reason string, message string) Condition {
return Condition{
Type: conditionType,
Status: metav1.ConditionFalse,
Severity: severity,
LastTransitionTime: b.now(),
ObservedGeneration: observedGeneration,
Reason: reason,
Message: message,
}
}
// MakeUnknownCondition makes a condition whose Status is Unknown. A reason and message must be provided. No severity
// is required as conditions in Status Unknown do not have a known severity either.
func (b *PositiveConditionBuilder) MakeUnknownCondition(conditionType ConditionType, observedGeneration int64, reason string, message string) Condition {
return Condition{
Type: conditionType,
Status: metav1.ConditionUnknown,
Severity: ConditionSeverityNone,
LastTransitionTime: b.now(),
ObservedGeneration: observedGeneration,
Reason: reason,
Message: message,
}
}