-
Notifications
You must be signed in to change notification settings - Fork 12
/
envoydeployment_controller.go
204 lines (180 loc) · 8.2 KB
/
envoydeployment_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
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
198
199
200
201
202
203
204
/*
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 controllers
import (
"context"
"fmt"
"reflect"
"time"
"github.com/3scale-ops/basereconciler/mutators"
"github.com/3scale-ops/basereconciler/reconciler"
"github.com/3scale-ops/basereconciler/resource"
marin3rv1alpha1 "github.com/3scale-ops/marin3r/apis/marin3r/v1alpha1"
operatorv1alpha1 "github.com/3scale-ops/marin3r/apis/operator.marin3r/v1alpha1"
"github.com/3scale-ops/marin3r/pkg/envoy/container/defaults"
"github.com/3scale-ops/marin3r/pkg/reconcilers/operator/envoydeployment/generators"
"github.com/3scale-ops/marin3r/pkg/util/pointer"
"github.com/go-logr/logr"
appsv1 "k8s.io/api/apps/v1"
autoscalingv2 "k8s.io/api/autoscaling/v2"
policyv1 "k8s.io/api/policy/v1"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
)
// EnvoyDeploymentReconciler reconciles a EnvoyDeployment object
type EnvoyDeploymentReconciler struct {
*reconciler.Reconciler
}
//+kubebuilder:rbac:groups=operator.marin3r.3scale.net,namespace=placeholder,resources=envoydeployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=operator.marin3r.3scale.net,namespace=placeholder,resources=envoydeployments/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=operator.marin3r.3scale.net,namespace=placeholder,resources=envoydeployments/finalizers,verbs=update
//+kubebuilder:rbac:groups="apps",namespace=placeholder,resources=deployments,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="autoscaling",namespace=placeholder,resources=horizontalpodautoscalers,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups="policy",namespace=placeholder,resources=poddisruptionbudgets,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=operator.marin3r.3scale.net,namespace=placeholder,resources=discoveryservicecertificates,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=marin3r.3scale.net,namespace=placeholder,resources=envoyconfigs,verbs=get;list;watch
//+kubebuilder:rbac:groups=operator.marin3r.3scale.net,namespace=placeholder,resources=discoveryservices,verbs=get;list;watch
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
func (r *EnvoyDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
ctx, logger := r.Logger(ctx, "name", req.Name, "namespace", req.Namespace)
ed := &operatorv1alpha1.EnvoyDeployment{}
result := r.ManageResourceLifecycle(ctx, req, ed)
if result.ShouldReturn() {
return result.Values()
}
// Get the address of the DiscoveryService instance
ds := &operatorv1alpha1.DiscoveryService{}
dsKey := types.NamespacedName{Name: ed.Spec.DiscoveryServiceRef, Namespace: ed.GetNamespace()}
if err := r.Client.Get(ctx, dsKey, ds); err != nil {
if errors.IsNotFound(err) {
logger.Error(err, "DiscoveryService does not exist", "DiscoveryService", ed.Spec.DiscoveryServiceRef)
}
return ctrl.Result{Requeue: true, RequeueAfter: 10 * time.Second}, err
}
// Get the EnvoyConfig for additional data (like the envoy API version in use)
ec, err := r.getEnvoyConfig(ctx, types.NamespacedName{Name: ed.Spec.EnvoyConfigRef, Namespace: ed.GetNamespace()})
if err != nil {
logger.Error(err, "unable to get EnvoyConfig", "EnvoyConfig", ed.Spec.EnvoyConfigRef)
return ctrl.Result{}, err
}
gen := generators.GeneratorOptions{
InstanceName: ed.GetName(),
Namespace: ed.GetNamespace(),
DiscoveryServiceName: ed.Spec.DiscoveryServiceRef,
XdssAdress: fmt.Sprintf("%s.%s.%s", ds.GetServiceConfig().Name, ds.GetNamespace(), "svc"),
XdssPort: int(ds.GetXdsServerPort()),
EnvoyAPIVersion: ec.GetEnvoyAPIVersion(),
EnvoyNodeID: ec.Spec.NodeID,
EnvoyClusterID: func() string {
if ed.Spec.ClusterID != nil {
return *ed.Spec.ClusterID
}
return ec.Spec.NodeID
}(),
ClientCertificateName: fmt.Sprintf("%s-%s", defaults.DeploymentClientCertificate, ed.GetName()),
ClientCertificateDuration: ed.ClientCertificateDuration(),
SigningCertificateName: ds.GetRootCertificateAuthorityOptions().SecretName,
DeploymentImage: ed.Image(),
DeploymentResources: ed.Resources(),
ExposedPorts: ed.Spec.Ports,
ExtraArgs: ed.Spec.ExtraArgs,
AdminPort: int32(ed.AdminPort()),
AdminAccessLogPath: ed.AdminAccessLogPath(),
Replicas: ed.Replicas(),
LivenessProbe: ed.LivenessProbe(),
ReadinessProbe: ed.ReadinessProbe(),
Affinity: ed.Affinity(),
PodDisruptionBudget: ed.PodDisruptionBudget(),
ShutdownManager: ed.Spec.ShutdownManager,
InitManager: ed.Spec.InitManager,
}
resources := []resource.TemplateInterface{
resource.NewTemplateFromObjectFunction(gen.ClientCertificate).Apply(dscDefaulter),
resource.NewTemplateFromObjectFunction(gen.Deployment).
WithMutation(mutators.SetDeploymentReplicas(ed.Replicas().Dynamic == nil)),
resource.NewTemplateFromObjectFunction(gen.HPA).
WithEnabled(ed.Replicas().Dynamic != nil),
resource.NewTemplateFromObjectFunction(gen.PDB).
WithEnabled(!reflect.DeepEqual(ed.PodDisruptionBudget(), operatorv1alpha1.PodDisruptionBudgetSpec{})),
}
result = r.ReconcileOwnedResources(ctx, ed, resources)
if result.ShouldReturn() {
return result.Values()
}
// reconcile the status
result = r.ReconcileStatus(ctx, ed, []types.NamespacedName{gen.OwnedResourceKey()}, nil,
func() bool {
if ed.Status.DeploymentName == nil || *ed.Status.DeploymentName != gen.OwnedResourceKey().Name {
ed.Status.DeploymentName = pointer.New(gen.OwnedResourceKey().Name)
return true
}
return false
})
if result.ShouldReturn() {
return result.Values()
}
return ctrl.Result{}, nil
}
func (r *EnvoyDeploymentReconciler) getEnvoyConfig(ctx context.Context, key types.NamespacedName) (*marin3rv1alpha1.EnvoyConfig, error) {
ec := &marin3rv1alpha1.EnvoyConfig{}
err := r.Client.Get(ctx, key, ec)
if err != nil {
return nil, err
}
return ec, nil
}
// EnvoyConfigHandler returns an EventHandler to watch for EnvoyConfigs
func (r *EnvoyDeploymentReconciler) EnvoyConfigHandler() handler.EventHandler {
return r.FilteredEventHandler(
&operatorv1alpha1.EnvoyDeploymentList{},
func(event client.Object, o client.Object) bool {
ec := event.(*marin3rv1alpha1.EnvoyConfig)
ed := o.(*operatorv1alpha1.EnvoyDeployment)
if ed.Spec.EnvoyConfigRef == ec.GetName() {
return true
}
return false
},
logr.Discard(),
)
}
// EnvoyConfigHandler returns an EventHandler to watch for DiscoveryServices
func (r *EnvoyDeploymentReconciler) DiscoveryServiceHandler() handler.EventHandler {
return r.FilteredEventHandler(
&operatorv1alpha1.EnvoyDeploymentList{},
func(event client.Object, o client.Object) bool {
ds := event.(*operatorv1alpha1.DiscoveryService)
ed := o.(*operatorv1alpha1.EnvoyDeployment)
if ed.Spec.DiscoveryServiceRef == ds.GetName() {
return true
}
return false
},
logr.Discard(),
)
}
// SetupWithManager sets up the controller with the Manager.
func (r *EnvoyDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&operatorv1alpha1.EnvoyDeployment{}).
Owns(&appsv1.Deployment{}).
Owns(&operatorv1alpha1.DiscoveryServiceCertificate{}).
Owns(&policyv1.PodDisruptionBudget{}).
Owns(&autoscalingv2.HorizontalPodAutoscaler{}).
Watches(&marin3rv1alpha1.EnvoyConfig{}, r.EnvoyConfigHandler()).
Watches(&operatorv1alpha1.DiscoveryService{}, r.DiscoveryServiceHandler()).
Complete(r)
}