/
region_types.go
123 lines (96 loc) · 3.57 KB
/
region_types.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
Copyright © 2023 PlusServer GmbH
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 v1alpha1
import (
"context"
"time"
"github.com/go-logr/logr"
"github.com/pluscloudopen/reseller-operator/internal/utils"
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
)
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.
// RegionSpec defines the desired state of Region
type RegionSpec struct {
Endpoint string `json:"endpoint,omitempty"`
Username string `json:"username,omitempty"`
Password string `json:"password,omitempty"`
}
// RegionStatus defines the observed state of Region
type RegionStatus struct {
Conditions []metav1.Condition `json:"conditions"`
}
func (v *Region) AwaitReady(ctx context.Context, timeout time.Duration, client client.Client, logger logr.Logger) error {
timeoutContext, cancel := context.WithTimeout(ctx, timeout)
defer cancel()
out := make(chan bool)
go v.awaitAllConditionsTrue(timeoutContext, client, logger, out)
select {
case <-out:
return nil
case <-timeoutContext.Done():
return timeoutContext.Err()
}
}
func (v *Region) awaitAllConditionsTrue(ctx context.Context, client client.Client, logger logr.Logger, out chan bool) {
for !utils.AllConditionsTrue(v.Status.Conditions) {
//Check if the context is still good
if ctx.Err() != nil {
return
}
//Wait before refreshing object
time.Sleep(3 * time.Second)
//Refresh object
if err := client.Get(ctx, types.NamespacedName{Namespace: v.Namespace, Name: v.Name}, v); err != nil {
logger.Error(err, "Couldn't refresh conditions")
}
}
out <- true
}
func (r *Region) UpdateRegionCondition(ctx context.Context, reconcileClient client.Client, reason RegionReadyReasons, message string) error {
return r.updateCondition(ctx, reconcileClient, string(RegionReady), reason.regionStatus(), string(reason), message)
}
func (r *Region) updateCondition(ctx context.Context, reconcileClient client.Client, typeString string, status v1.ConditionStatus, reason string, message string) error {
oldRegion := r.DeepCopy()
meta.SetStatusCondition(&r.Status.Conditions, v1.Condition{
Type: typeString,
Status: status,
Reason: reason,
Message: message,
})
return reconcileClient.Status().Patch(ctx, r, client.MergeFrom(oldRegion))
}
//+kubebuilder:object:root=true
//+kubebuilder:subresource:status
//+kubebuilder:resource:scope=Cluster
// Region is the Schema for the regions API
type Region struct {
metav1.TypeMeta `json:",inline"`
metav1.ObjectMeta `json:"metadata,omitempty"`
Spec RegionSpec `json:"spec,omitempty"`
Status RegionStatus `json:"status,omitempty"`
}
//+kubebuilder:object:root=true
// RegionList contains a list of Region
type RegionList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata,omitempty"`
Items []Region `json:"items"`
}
func init() {
SchemeBuilder.Register(&Region{}, &RegionList{})
}