-
Notifications
You must be signed in to change notification settings - Fork 6
/
cluster.go
140 lines (112 loc) · 3.35 KB
/
cluster.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// Copyright 2021 Weaveworks or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MPL-2.0
package scope
import (
"context"
"fmt"
"github.com/go-logr/logr"
"k8s.io/klog/v2/klogr"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/conditions"
"sigs.k8s.io/cluster-api/util/patch"
"sigs.k8s.io/controller-runtime/pkg/client"
infrav1 "github.com/weaveworks-liquidmetal/cluster-api-provider-microvm/api/v1alpha1"
"github.com/weaveworks-liquidmetal/cluster-api-provider-microvm/internal/defaults"
)
var _ Scoper = &ClusterScope{}
func NewClusterScope(cluster *clusterv1.Cluster,
microvmCluster *infrav1.MicrovmCluster,
client client.Client, opts ...ClusterScopeOption,
) (*ClusterScope, error) {
if cluster == nil {
return nil, errClusterRequired
}
if microvmCluster == nil {
return nil, errMicrovmClusterRequired
}
if client == nil {
return nil, errClientRequired
}
patchHelper, err := patch.NewHelper(microvmCluster, client)
if err != nil {
return nil, fmt.Errorf("creating patch helper for microvm cluster: %w", err)
}
scope := &ClusterScope{
Cluster: cluster,
MvmCluster: microvmCluster,
client: client,
controllerName: defaults.ManagerName,
Logger: klogr.New(),
patchHelper: patchHelper,
}
for _, opt := range opts {
opt(scope)
}
return scope, nil
}
type ClusterScopeOption func(*ClusterScope)
func WithClusterLogger(logger logr.Logger) ClusterScopeOption {
return func(s *ClusterScope) {
s.Logger = logger
}
}
func WithClusterControllerName(name string) ClusterScopeOption {
return func(s *ClusterScope) {
s.controllerName = name
}
}
// ClusterScope is the scope for reconciling a cluster.
type ClusterScope struct {
logr.Logger
Cluster *clusterv1.Cluster
MvmCluster *infrav1.MicrovmCluster
client client.Client
patchHelper *patch.Helper
controllerName string
}
// Name returns the name of the resource.
func (cs *ClusterScope) Name() string {
return cs.MvmCluster.Name
}
// Namespace returns the resources namespace.
func (cs *ClusterScope) Namespace() string {
return cs.MvmCluster.Namespace
}
// ClusterName returns the name of the cluster.
func (cs *ClusterScope) ClusterName() string {
return cs.Cluster.Name
}
// ControllerName returns the name of the controller that created the scope.
func (cs *ClusterScope) ControllerName() string {
return cs.controllerName
}
// Patch persists the resource and status.
func (cs *ClusterScope) Patch() error {
applicableConditions := []clusterv1.ConditionType{
infrav1.LoadBalancerAvailableCondition,
}
conditions.SetSummary(cs.MvmCluster,
conditions.WithConditions(applicableConditions...),
conditions.WithStepCounterIf(cs.MvmCluster.DeletionTimestamp.IsZero()),
conditions.WithStepCounter(),
)
err := cs.patchHelper.Patch(
context.TODO(),
cs.MvmCluster,
patch.WithOwnedConditions{Conditions: []clusterv1.ConditionType{
clusterv1.ReadyCondition,
infrav1.LoadBalancerAvailableCondition,
}})
if err != nil {
return fmt.Errorf("unable to patch cluster: %w", err)
}
return nil
}
// Close closes the current scope persisting the resource and status.
func (cs *ClusterScope) Close() error {
return cs.Patch()
}
// Placement is used to get the placement configuration for the cluster.
func (cs *ClusterScope) Placement() infrav1.Placement {
return cs.MvmCluster.Spec.Placement
}