-
Notifications
You must be signed in to change notification settings - Fork 21
/
envoyfleet_controller.go
141 lines (124 loc) · 6.15 KB
/
envoyfleet_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
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
/*
MIT License
Copyright (c) 2022 Kubeshop
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 controllers
import (
"context"
"fmt"
"time"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/predicate"
gatewayv1alpha1 "github.com/kubeshop/kusk-gateway/api/v1alpha1"
"github.com/kubeshop/kusk-gateway/pkg/analytics"
)
const (
// Used to set the State field in the Status
envoyFleetStateSuccess string = "Deployed"
envoyFleetStateFailure string = "Failed"
)
// EnvoyFleetReconciler reconciles a EnvoyFleet object
type EnvoyFleetReconciler struct {
client.Client
Scheme *runtime.Scheme
ConfigManager *KubeEnvoyConfigManager
}
// +kubebuilder:rbac:groups=gateway.kusk.io,resources=envoyfleet,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=gateway.kusk.io,resources=envoyfleet/status,verbs=get;update;patch
// +kubebuilder:rbac:groups=gateway.kusk.io,resources=envoyfleet/finalizers,verbs=update
// +kubebuilder:rbac:groups="";v1,resources=secrets,verbs=get;list;watch
//+kubebuilder:rbac:groups=core,resources=namespaces,verbs=get;list;watch;create;update;patch;delete
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.8.3/pkg/reconcile
func (r *EnvoyFleetReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
l := ctrl.LoggerFrom(ctx).WithName("envoy-fleet-controller")
l.Info("EnvoyFleet changed", "changed", req.NamespacedName)
analytics.SendAnonymousInfo(ctx, r.Client, "kusk", "reconciling EnvoyFleet")
ef := &gatewayv1alpha1.EnvoyFleet{}
err := r.Client.Get(ctx, req.NamespacedName, ef)
if err != nil {
if errors.IsNotFound(err) {
// EnvoyFleet was deleted - deployment and config deletion is handled by the API server itself
// thanks to OwnerReference
l.Info("No objects found, looks like EnvoyFleet was deleted")
return ctrl.Result{}, nil
}
l.Error(err, "Failed to retrieve EnvoyFleet object with cluster API")
return ctrl.Result{Requeue: true}, err
}
if err := controllerutil.SetControllerReference(ef, ef, r.Scheme); err != nil {
l.Error(err, "Failed setting controller owner reference")
return ctrl.Result{}, err
}
// Generate Envoy Fleet resources...
efResources, err := NewEnvoyFleetResources(ctx, r.Client, ef)
if err != nil {
l.Error(err, "Failed to create EnvoyFleet configuration")
ef.Status.State = envoyFleetStateFailure
if err := r.Client.Status().Update(ctx, ef); err != nil {
l.Error(err, "Unable to update Envoy Fleet status")
}
return ctrl.Result{}, fmt.Errorf("failed to create EnvoyFleet configuration: %w", err)
}
// and deploy them
if err = efResources.CreateOrUpdate(ctx); err != nil {
l.Error(err, fmt.Sprintf("Failed to reconcile EnvoyFleet, will retry in %d seconds", reconcilerDefaultRetrySeconds))
ef.Status.State = envoyFleetStateFailure
if err := r.Client.Status().Update(ctx, ef); err != nil {
l.Error(err, "Unable to update Envoy Fleet status")
}
return ctrl.Result{RequeueAfter: time.Duration(time.Duration(reconcilerDefaultRetrySeconds) * time.Second)},
fmt.Errorf("failed to create or update EnvoyFleet: %w", err)
}
// Call Envoy configuration manager to update Envoy fleet configuration when applicable
// This could be extended for any field that belongs to EnvoyFleet CRD but is used to configure Envoy proxy.
if efResources.fleet.Spec.AccessLog != nil {
l.Info("Calling Config Manager due to change in Envoy Fleet resource", "changed", req.NamespacedName)
if err := r.ConfigManager.UpdateConfiguration(ctx, gatewayv1alpha1.EnvoyFleetID{Name: req.Name, Namespace: req.Namespace}); err != nil {
ef.Status.State = envoyFleetStateFailure
if err := r.Client.Status().Update(ctx, ef); err != nil {
l.Error(err, "Unable to update Envoy Fleet status")
}
l.Error(err, fmt.Sprintf("Failed to reconcile Envoy Fleet, will retry in %d seconds", reconcilerDefaultRetrySeconds))
return ctrl.Result{RequeueAfter: time.Duration(time.Duration(reconcilerDefaultRetrySeconds) * time.Second)},
fmt.Errorf("failed to update Envoy Fleet %s configuration: %w", ef.Name, err)
}
}
l.Info(fmt.Sprintf("Reconciled EnvoyFleet '%s' resources", ef.Name))
ef.Status.State = envoyFleetStateSuccess
if err := r.Client.Status().Update(ctx, ef); err != nil {
l.Error(err, "Unable to update Envoy Fleet status")
return ctrl.Result{RequeueAfter: time.Duration(time.Duration(reconcilerDefaultRetrySeconds) * time.Second)}, fmt.Errorf("unable to update Envoy Fleet status")
}
return ctrl.Result{}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *EnvoyFleetReconciler) SetupWithManager(mgr ctrl.Manager) error {
// predicate will prevent triggering the Reconciler on resource Status field changes.
pred := predicate.GenerationChangedPredicate{}
return ctrl.NewControllerManagedBy(mgr).
For(&gatewayv1alpha1.EnvoyFleet{}).
WithEventFilter(pred).
Complete(r)
}