-
Notifications
You must be signed in to change notification settings - Fork 10
/
consul_controller.go
103 lines (90 loc) · 3.58 KB
/
consul_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
/*
* MIT License
*
* Copyright (c) since 2021, flomesh.io Authors.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package v1alpha1
import (
"context"
"k8s.io/apimachinery/pkg/api/errors"
"sigs.k8s.io/controller-runtime/pkg/builder"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/predicate"
ctrl "sigs.k8s.io/controller-runtime"
ctv1 "github.com/flomesh-io/fsm/pkg/apis/connector/v1alpha1"
connectorClientset "github.com/flomesh-io/fsm/pkg/gen/client/connector/clientset/versioned"
fctx "github.com/flomesh-io/fsm/pkg/context"
"github.com/flomesh-io/fsm/pkg/controllers"
)
type consulConnectorReconciler struct {
connectorReconciler
}
// NewConsulConnectorReconciler returns a new reconciler for consul connector resources
func NewConsulConnectorReconciler(ctx *fctx.ControllerContext) controllers.Reconciler {
return &consulConnectorReconciler{
connectorReconciler: connectorReconciler{
recorder: ctx.Manager.GetEventRecorderFor("consul-connector"),
fctx: ctx,
connectorAPIClient: connectorClientset.NewForConfigOrDie(ctx.KubeConfig),
},
}
}
// Reconcile reconciles a Gateway resource
func (r *consulConnectorReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
connector := &ctv1.ConsulConnector{}
if err := r.fctx.Get(
ctx,
req.NamespacedName,
connector,
); err != nil {
if errors.IsNotFound(err) {
// Request object not found, could have been deleted after reconcile request.
// Owned objects are automatically garbage collected. For additional cleanup logic use finalizers.
// Return and don't requeue
log.Info().Msgf("ConsulConnector resource not found. Ignoring since object must be deleted")
return ctrl.Result{}, nil
}
// Error reading the object - requeue the request.
log.Error().Msgf("Failed to get ConsulConnector, %v", err)
return ctrl.Result{}, err
}
if connector.DeletionTimestamp != nil {
return ctrl.Result{}, nil
}
mc := r.fctx.Config
result, err := r.deployConnector(connector, mc)
if err != nil || result.RequeueAfter > 0 || result.Requeue {
return result, err
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *consulConnectorReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&ctv1.ConsulConnector{}, builder.WithPredicates(predicate.NewPredicateFuncs(func(obj client.Object) bool {
_, ok := obj.(*ctv1.ConsulConnector)
if !ok {
log.Error().Msgf("unexpected object type %T", obj)
}
return ok
}))).
Complete(r)
}