-
Notifications
You must be signed in to change notification settings - Fork 40
/
addon_instance.go
93 lines (73 loc) · 3.06 KB
/
addon_instance.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
88
89
90
91
92
93
package client
import (
"context"
"fmt"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"sigs.k8s.io/controller-runtime/pkg/client"
av1alpha1 "github.com/openshift/addon-operator/apis/addons/v1alpha1"
)
// AddonInstanceClient provides methods for interacting with
// AddonInstance resources.
type AddonInstanceClient interface {
// SendPulse updates the LastHeartbeatTime for the AddonInstance
// and applies optional conditions if provided.
SendPulse(ctx context.Context, instance av1alpha1.AddonInstance, opts ...SendPulseOption) error
}
// NewAddonInstanceClient returns a configured AddonInstanceClient
// using the given client instance as a base.
func NewAddonInstanceClient(client client.Client) AddonInstanceClient {
return &AddonInstanceClientImpl{
client: client,
}
}
type AddonInstanceClientImpl struct {
client client.Client
}
func (c *AddonInstanceClientImpl) SendPulse(ctx context.Context, instance av1alpha1.AddonInstance, opts ...SendPulseOption) error {
var cfg sendPulseConfig
cfg.Option(opts...)
instance.Status.LastHeartbeatTime = metav1.Now()
for _, c := range cfg.Conditions {
c.ObservedGeneration = instance.Generation
meta.SetStatusCondition(&instance.Status.Conditions, c)
}
if err := c.client.Status().Update(ctx, &instance); err != nil {
return fmt.Errorf("setting status for AddonInstance %s/%s: %w", instance.Namespace, instance.Name, err)
}
return nil
}
type sendPulseConfig struct {
Conditions []metav1.Condition
}
func (c *sendPulseConfig) Option(opts ...SendPulseOption) {
for _, opt := range opts {
opt.ConfigureSendPulse(c)
}
}
type SendPulseOption interface {
ConfigureSendPulse(*sendPulseConfig)
}
// NewAddonInstanceConditionDegraded returns an AddonInstanceDegraded status condition
// with the given status, reason, and message.
func NewAddonInstanceConditionDegraded(status metav1.ConditionStatus, reason, msg string) metav1.Condition {
return newAddonInstanceCondition(av1alpha1.AddonInstanceConditionDegraded, status, reason, msg)
}
// NewAddonInstanceConditionInstalled returns an AddonInstanceInstalled status condition
// with the given status, reason, and message.
func NewAddonInstanceConditionInstalled(status metav1.ConditionStatus, reason av1alpha1.AddonInstanceInstalledReason, msg string) metav1.Condition {
return newAddonInstanceCondition(av1alpha1.AddonInstanceConditionInstalled, status, reason.String(), msg)
}
// NewAddonInstanceConditionReadyToBeDeleted returns an AddonInstanceReadyToBeDeleted status condition
// with the given status, reason, and message.
func NewAddonInstanceConditionReadyToBeDeleted(status metav1.ConditionStatus, reason av1alpha1.AddonInstanceReadyToBeDeleted, msg string) metav1.Condition {
return newAddonInstanceCondition(av1alpha1.AddonInstanceConditionReadyToBeDeleted, status, reason.String(), msg)
}
func newAddonInstanceCondition(cond av1alpha1.AddonInstanceCondition, status metav1.ConditionStatus, reason, msg string) metav1.Condition {
return metav1.Condition{
Type: cond.String(),
Status: status,
Reason: reason,
Message: msg,
}
}