-
Notifications
You must be signed in to change notification settings - Fork 165
/
aro_controller.go
105 lines (83 loc) · 2.91 KB
/
aro_controller.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
94
95
96
97
98
99
100
101
102
103
104
105
package base
// Copyright (c) Microsoft Corporation.
// Licensed under the Apache License 2.0.
import (
"context"
operatorv1 "github.com/openshift/api/operator/v1"
"github.com/openshift/library-go/pkg/operator/v1helpers"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
arov1alpha1 "github.com/Azure/ARO-RP/pkg/operator/apis/aro.openshift.io/v1alpha1"
)
type AROController struct {
Log *logrus.Entry
Client client.Client
Name string
}
func (c *AROController) SetConditions(ctx context.Context, cnds ...*operatorv1.OperatorCondition) {
cluster, err := c.GetCluster(ctx)
if err != nil {
c.Log.Warn("Failed to retrieve ARO cluster resource")
return
}
newConditions := cluster.Status.DeepCopy().Conditions
for _, cnd := range cnds {
v1helpers.SetOperatorCondition(&newConditions, *cnd)
}
if equality.Semantic.DeepEqual(cluster.Status.Conditions, newConditions) {
return
}
cluster.Status.Conditions = newConditions
if err := c.Client.Status().Update(ctx, cluster); err != nil {
c.Log.Error("error updating controller conditions", err)
}
}
func (c *AROController) SetProgressing(ctx context.Context, message string) {
cnd := c.defaultProgressing()
cnd.Status = operatorv1.ConditionTrue
cnd.Message = message
c.SetConditions(ctx, cnd)
}
func (c *AROController) ClearProgressing(ctx context.Context) {
c.SetConditions(ctx, c.defaultProgressing())
}
func (c *AROController) SetDegraded(ctx context.Context, err error) {
cnd := c.defaultDegraded()
cnd.Status = operatorv1.ConditionTrue
cnd.Message = err.Error()
c.SetConditions(ctx, cnd)
}
func (c *AROController) ClearDegraded(ctx context.Context) {
c.SetConditions(ctx, c.defaultDegraded())
}
func (c *AROController) ClearConditions(ctx context.Context) {
c.SetConditions(ctx, c.defaultAvailable(), c.defaultProgressing(), c.defaultDegraded())
}
func (c *AROController) GetCluster(ctx context.Context) (*arov1alpha1.Cluster, error) {
cluster := &arov1alpha1.Cluster{}
err := c.Client.Get(ctx, types.NamespacedName{Name: arov1alpha1.SingletonClusterName}, cluster)
return cluster, err
}
func (c *AROController) defaultAvailable() *operatorv1.OperatorCondition {
return &operatorv1.OperatorCondition{
Type: c.conditionName(operatorv1.OperatorStatusTypeAvailable),
Status: operatorv1.ConditionTrue,
}
}
func (c *AROController) defaultProgressing() *operatorv1.OperatorCondition {
return &operatorv1.OperatorCondition{
Type: c.conditionName(operatorv1.OperatorStatusTypeProgressing),
Status: operatorv1.ConditionFalse,
}
}
func (c *AROController) defaultDegraded() *operatorv1.OperatorCondition {
return &operatorv1.OperatorCondition{
Type: c.conditionName(operatorv1.OperatorStatusTypeDegraded),
Status: operatorv1.ConditionFalse,
}
}
func (c *AROController) conditionName(conditionType string) string {
return c.Name + "Controller" + conditionType
}