forked from k-orc/openstack-resource-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontroller.go
More file actions
197 lines (166 loc) · 6.76 KB
/
Copy pathcontroller.go
File metadata and controls
197 lines (166 loc) · 6.76 KB
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/*
Copyright 2024 The ORC Authors.
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 routerinterface
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/tools/record"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
orcv1alpha1 "github.com/k-orc/openstack-resource-controller/api/v1alpha1"
"github.com/k-orc/openstack-resource-controller/pkg/predicates"
ctrlexport "github.com/k-orc/openstack-resource-controller/internal/controllers/export"
"github.com/k-orc/openstack-resource-controller/internal/scope"
"github.com/k-orc/openstack-resource-controller/internal/util/dependency"
)
const (
Finalizer = "openstack.k-orc.cloud/routerinterface"
FieldOwner = "openstack.k-orc.cloud/routerinterfacecontroller"
// Field owner of the object finalizer.
SSAFinalizerTxn = "finalizer"
// Field owner of transient status.
SSAStatusTxn = "status"
)
// ssaFieldOwner returns the field owner for a specific named SSA transaction.
func ssaFieldOwner(txn string) client.FieldOwner {
return client.FieldOwner(FieldOwner + "/" + txn)
}
const (
// The time to wait between polling a port for ACTIVE status
portStatusPollingPeriod = 1 * time.Second
)
type routerInterfaceReconcilerConstructor struct {
scopeFactory scope.Factory
}
func New(scopeFactory scope.Factory) ctrlexport.Controller {
return routerInterfaceReconcilerConstructor{scopeFactory: scopeFactory}
}
func (routerInterfaceReconcilerConstructor) GetName() string {
return "router interface"
}
// orcRouterInterfaceReconciler reconciles an ORC Subnet.
type orcRouterInterfaceReconciler struct {
client client.Client
recorder record.EventRecorder
scopeFactory scope.Factory
}
// Index subnets by referenced network
const routerRefPath = "spec.routerRef"
func getRouterInterfacesForRouter(ctx context.Context, k8sClient client.Client, obj *orcv1alpha1.Router) ([]orcv1alpha1.RouterInterface, error) {
routerInterfaceList := &orcv1alpha1.RouterInterfaceList{}
if err := k8sClient.List(ctx, routerInterfaceList, client.InNamespace(obj.Namespace), client.MatchingFields{routerRefPath: obj.Name}); err != nil {
return nil, err
}
return routerInterfaceList.Items, nil
}
// SetupWithManager sets up the controller with the Manager.
func (c routerInterfaceReconcilerConstructor) SetupWithManager(ctx context.Context, mgr ctrl.Manager, options controller.Options) error {
log := mgr.GetLogger().WithValues("controller", "router interface")
reconciler := orcRouterInterfaceReconciler{
client: mgr.GetClient(),
recorder: mgr.GetEventRecorderFor("orc-router-interface-controller"),
scopeFactory: c.scopeFactory,
}
getRouterRefsForRouterInterface := func(obj client.Object) []string {
routerInterface, ok := obj.(*orcv1alpha1.RouterInterface)
if !ok {
return nil
}
return []string{string(routerInterface.Spec.RouterRef)}
}
if err := mgr.GetFieldIndexer().IndexField(ctx, &orcv1alpha1.RouterInterface{}, routerRefPath, func(obj client.Object) []string {
return getRouterRefsForRouterInterface(obj)
}); err != nil {
return fmt.Errorf("adding routers by routerinterface index: %w", err)
}
err := dependency.AddDeletionGuard(mgr, Finalizer, FieldOwner, getRouterRefsForRouterInterface, getRouterInterfacesForRouter)
if err != nil {
return err
}
const subnetRefPath = "spec.subnetRef"
if err := mgr.GetFieldIndexer().IndexField(ctx, &orcv1alpha1.RouterInterface{}, subnetRefPath, func(obj client.Object) []string {
routerInterface, ok := obj.(*orcv1alpha1.RouterInterface)
if !ok {
return nil
}
subnetRef := routerInterface.Spec.SubnetRef
if subnetRef == nil {
return nil
}
return []string{string(*subnetRef)}
}); err != nil {
return err
}
getRoutersForSubnet := func(ctx context.Context, k8sClient client.Client, subnet *orcv1alpha1.Subnet) ([]reconcile.Request, error) {
routerInterfaceList := &orcv1alpha1.RouterInterfaceList{}
err := k8sClient.List(ctx, routerInterfaceList, client.InNamespace(subnet.Namespace), client.MatchingFields{subnetRefPath: subnet.Name})
if err != nil {
return nil, fmt.Errorf("fetching router interfaces for %s: %w", client.ObjectKeyFromObject(subnet), err)
}
// Get a list of unique router names
routerSet := make(map[string]struct{})
for i := range routerInterfaceList.Items {
routerInterface := &routerInterfaceList.Items[i]
routerSet[string(routerInterface.Spec.RouterRef)] = struct{}{}
}
i := 0
routers := make([]reconcile.Request, len(routerSet))
for router := range routerSet {
routers[i].Name = router
routers[i].Namespace = subnet.Namespace
i++
}
return routers, nil
}
return ctrl.NewControllerManagedBy(mgr).
For(&orcv1alpha1.Router{}, builder.WithPredicates(predicates.NewBecameAvailable(log, &orcv1alpha1.Router{}))).
Named("router_interface").
Watches(&orcv1alpha1.RouterInterface{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
log := ctrl.LoggerFrom(ctx).WithValues("watch", "RouterInterface", "name", obj.GetName(), "namespace", obj.GetNamespace())
routerInterface, ok := obj.(*orcv1alpha1.RouterInterface)
if !ok {
log.Info("Watch got unexpected object type", "type", fmt.Sprintf("%T", obj))
return nil
}
return []reconcile.Request{
{NamespacedName: types.NamespacedName{Namespace: routerInterface.Namespace, Name: string(routerInterface.Spec.RouterRef)}},
}
}),
).
Watches(&orcv1alpha1.Subnet{},
handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, obj client.Object) []reconcile.Request {
log := ctrl.LoggerFrom(ctx).WithValues("watch", "Subnet", "name", obj.GetName(), "namespace", obj.GetNamespace())
subnet, ok := obj.(*orcv1alpha1.Subnet)
if !ok {
log.Info("Watch got unexpected object type", "type", fmt.Sprintf("%T", obj))
return nil
}
routers, err := getRoutersForSubnet(ctx, mgr.GetClient(), subnet)
if err != nil {
log.Error(err, "fetching routers for subnet")
return nil
}
return routers
}),
builder.WithPredicates(predicates.NewBecameAvailable(log, &orcv1alpha1.Subnet{})),
).
WithOptions(options).
Complete(&reconciler)
}