forked from temporalio/temporal-worker-controller
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathworker_controller.go
More file actions
248 lines (218 loc) · 9.11 KB
/
worker_controller.go
File metadata and controls
248 lines (218 loc) · 9.11 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2024 Datadog, Inc.
package controller
import (
"context"
"fmt"
"time"
temporaliov1alpha1 "github.com/temporalio/temporal-worker-controller/api/v1alpha1"
"github.com/temporalio/temporal-worker-controller/internal/controller/clientpool"
"github.com/temporalio/temporal-worker-controller/internal/k8s"
"github.com/temporalio/temporal-worker-controller/internal/temporal"
appsv1 "k8s.io/api/apps/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
"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/log"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)
var (
apiGVStr = temporaliov1alpha1.GroupVersion.String()
)
const (
// TODO(jlegrone): add this everywhere
deployOwnerKey = ".metadata.controller"
buildIDLabel = "temporal.io/build-id"
)
// TemporalWorkerDeploymentReconciler reconciles a TemporalWorkerDeployment object
type TemporalWorkerDeploymentReconciler struct {
client.Client
Scheme *runtime.Scheme
TemporalClientPool *clientpool.ClientPool
// Disables panic recovery if true
DisableRecoverPanic bool
}
//+kubebuilder:rbac:groups=temporal.io,resources=temporalworkerdeployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=temporal.io,resources=temporalworkerdeployments/status,verbs=get;update;patch
//+kubebuilder:rbac:groups=temporal.io,resources=temporalworkerdeployments/finalizers,verbs=update
//+kubebuilder:rbac:groups=temporal.io,resources=temporalconnections,verbs=get;list;watch
//+kubebuilder:rbac:groups=core,resources=secrets,verbs=get;list;watch
//+kubebuilder:rbac:groups=apps,resources=deployments,verbs=get;list;watch;create;update;patch;delete
//+kubebuilder:rbac:groups=apps,resources=deployments/scale,verbs=update
// Reconcile is part of the main kubernetes reconciliation loop which aims to
// move the current state of the cluster closer to the desired state.
// The loop runs on a regular interval, or every time one of the watched resources listed above changes.
//
// For more details, check Reconcile and its Result here:
// - https://pkg.go.dev/sigs.k8s.io/controller-runtime@v0.15.0/pkg/reconcile
func (r *TemporalWorkerDeploymentReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
// TODO(Shivam): Monitor if the time taken for a successful reconciliation loop is closing in on 5 minutes. If so, we
// may need to increase the timeout value.
ctx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
l := log.FromContext(ctx)
l.Info("Running Reconcile loop")
// Fetch the worker deployment
var workerDeploy temporaliov1alpha1.TemporalWorkerDeployment
if err := r.Get(ctx, req.NamespacedName, &workerDeploy); err != nil {
l.Error(err, "unable to fetch TemporalWorker")
// we'll ignore not-found errors, since they can't be fixed by an immediate
// requeue (we'll need to wait for a new notification), and we can get them
// on deleted requests.
return ctrl.Result{}, client.IgnoreNotFound(err)
}
// TODO(jlegrone): Set defaults via webhook rather than manually
if err := workerDeploy.Default(ctx, &workerDeploy); err != nil {
l.Error(err, "TemporalWorkerDeployment defaulter failed")
return ctrl.Result{}, err
}
// TODO(carlydf): Handle warnings once we have some, handle ValidateUpdate once it is different from ValidateCreate
if _, err := workerDeploy.ValidateCreate(ctx, &workerDeploy); err != nil {
l.Error(err, "invalid TemporalWorkerDeployment")
return ctrl.Result{
Requeue: true,
RequeueAfter: 5 * time.Minute, // user needs time to fix this, if it changes, it will be re-queued immediately
}, nil
}
// Verify that a connection is configured
if workerDeploy.Spec.WorkerOptions.TemporalConnection == "" {
err := fmt.Errorf("TemporalConnection must be set")
l.Error(err, "")
return ctrl.Result{}, err
}
// Fetch the connection parameters
var temporalConnection temporaliov1alpha1.TemporalConnection
if err := r.Get(ctx, types.NamespacedName{
Name: workerDeploy.Spec.WorkerOptions.TemporalConnection,
Namespace: workerDeploy.Namespace,
}, &temporalConnection); err != nil {
l.Error(err, "unable to fetch TemporalConnection")
return ctrl.Result{}, err
}
// Get or update temporal client for connection
temporalClient, ok := r.TemporalClientPool.GetSDKClient(clientpool.ClientPoolKey{
HostPort: temporalConnection.Spec.HostPort,
Namespace: workerDeploy.Spec.WorkerOptions.TemporalNamespace,
MutualTLSSecret: temporalConnection.Spec.MutualTLSSecret,
}, temporalConnection.Spec.MutualTLSSecret != "")
if !ok {
c, err := r.TemporalClientPool.UpsertClient(ctx, clientpool.NewClientOptions{
K8sNamespace: workerDeploy.Namespace,
TemporalNamespace: workerDeploy.Spec.WorkerOptions.TemporalNamespace,
Spec: temporalConnection.Spec,
})
if err != nil {
l.Error(err, "unable to create TemporalClient")
return ctrl.Result{}, err
}
temporalClient = c
}
// Fetch Temporal worker deployment state
workerDeploymentName := k8s.ComputeWorkerDeploymentName(&workerDeploy)
temporalState, err := temporal.GetWorkerDeploymentState(
ctx,
temporalClient,
workerDeploymentName,
workerDeploy.Spec.WorkerOptions.TemporalNamespace,
)
if err != nil {
return ctrl.Result{}, fmt.Errorf("unable to get Temporal worker deployment state: %w", err)
}
// Compute a new status from k8s and temporal state
status, err := r.generateStatus(ctx, l, temporalClient, req, &workerDeploy, temporalState)
if err != nil {
return ctrl.Result{}, err
}
workerDeploy.Status = *status
if err := r.Status().Update(ctx, &workerDeploy); err != nil {
// Ignore "object has been modified" errors, since we'll just re-fetch
// on the next reconciliation loop.
if apierrors.IsConflict(err) {
return ctrl.Result{
Requeue: true,
RequeueAfter: time.Second,
}, nil
}
l.Error(err, "unable to update TemporalWorker status")
return ctrl.Result{}, err
}
// TODO(jlegrone): Set defaults via webhook rather than manually
// (defaults were already set above, but have to be set again after status update)
if err := workerDeploy.Default(ctx, &workerDeploy); err != nil {
l.Error(err, "TemporalWorkerDeployment defaulter failed")
return ctrl.Result{}, err
}
// Generate a plan to get to desired spec from current status
plan, err := r.generatePlan(ctx, l, &workerDeploy, temporalConnection.Spec, temporalState)
if err != nil {
return ctrl.Result{}, err
}
// Execute the plan, handling any errors
if err := r.executePlan(ctx, l, temporalClient, plan); err != nil {
return ctrl.Result{}, err
}
return ctrl.Result{
Requeue: true,
// TODO(jlegrone): Consider increasing this value if the only thing we need to check for is unreachable versions.
RequeueAfter: 10 * time.Second,
// For demo purposes only!
//RequeueAfter: 1 * time.Second,
}, nil
}
// SetupWithManager sets up the controller with the Manager.
func (r *TemporalWorkerDeploymentReconciler) SetupWithManager(mgr ctrl.Manager) error {
if err := mgr.GetFieldIndexer().IndexField(context.Background(), &appsv1.Deployment{}, deployOwnerKey, func(rawObj client.Object) []string {
// grab the job object, extract the owner...
deploy := rawObj.(*appsv1.Deployment)
owner := metav1.GetControllerOf(deploy)
if owner == nil {
return nil
}
// ...make sure it's a TemporalWorker...
// TODO(jlegrone): double check apiGVStr has the correct value
if owner.APIVersion != apiGVStr || owner.Kind != "TemporalWorkerDeployment" {
return nil
}
// ...and if so, return it
return []string{owner.Name}
}); err != nil {
return err
}
recoverPanic := !r.DisableRecoverPanic
return ctrl.NewControllerManagedBy(mgr).
For(&temporaliov1alpha1.TemporalWorkerDeployment{}).
Owns(&appsv1.Deployment{}).
Watches(&temporaliov1alpha1.TemporalConnection{}, handler.EnqueueRequestsFromMapFunc(r.findTWDsUsingConnection)).
WithOptions(controller.Options{
MaxConcurrentReconciles: 100,
RecoverPanic: &recoverPanic,
}).
Complete(r)
}
func (r *TemporalWorkerDeploymentReconciler) findTWDsUsingConnection(ctx context.Context, tc client.Object) []reconcile.Request {
var requests []reconcile.Request
// Find all TWDs in same namespace that reference this TC
var workers temporaliov1alpha1.TemporalWorkerDeploymentList
if err := r.List(ctx, &workers, client.InNamespace(tc.GetNamespace())); err != nil {
return requests
}
// Filter to ones using this connection
for _, worker := range workers.Items {
if worker.Spec.WorkerOptions.TemporalConnection == tc.GetName() {
// Add the TWD object as a reconcile request
requests = append(requests, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: worker.Name,
Namespace: worker.Namespace,
},
})
}
}
return requests
}