forked from kubernetes-sigs/karpenter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontroller.go
147 lines (131 loc) · 5.18 KB
/
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
/*
Copyright 2023 The Kubernetes 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 disruption
import (
"context"
"fmt"
"github.com/samber/lo"
"go.uber.org/multierr"
v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/utils/clock"
controllerruntime "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/event"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"
"sigs.k8s.io/karpenter/pkg/apis/v1beta1"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/controllers/state"
operatorcontroller "sigs.k8s.io/karpenter/pkg/operator/controller"
nodeclaimutil "sigs.k8s.io/karpenter/pkg/utils/nodeclaim"
"sigs.k8s.io/karpenter/pkg/utils/result"
)
var _ operatorcontroller.TypedController[*v1beta1.NodeClaim] = (*Controller)(nil)
type nodeClaimReconciler interface {
Reconcile(context.Context, *v1beta1.NodePool, *v1beta1.NodeClaim) (reconcile.Result, error)
}
// Controller is a disruption controller that adds StatusConditions to nodeclaims when they meet certain disruption conditions
// e.g. When the NodeClaim has surpassed its owning provisioner's expirationTTL, then it is marked as "Expired" in the StatusConditions
type Controller struct {
kubeClient client.Client
watchChannel <-chan event.GenericEvent
drift *Drift
expiration *Expiration
emptiness *Emptiness
}
// NewController constructs a nodeclaim disruption controller
func NewController(clk clock.Clock, kubeClient client.Client, cluster *state.Cluster, cloudProvider cloudprovider.CloudProvider) operatorcontroller.Controller {
return operatorcontroller.Typed[*v1beta1.NodeClaim](kubeClient, &Controller{
kubeClient: kubeClient,
drift: &Drift{cloudProvider: cloudProvider},
expiration: &Expiration{kubeClient: kubeClient, clock: clk},
emptiness: &Emptiness{kubeClient: kubeClient, cluster: cluster, clock: clk},
})
}
// Reconcile executes a control loop for the resource
func (c *Controller) Reconcile(ctx context.Context, nodeClaim *v1beta1.NodeClaim) (reconcile.Result, error) {
if !nodeClaim.DeletionTimestamp.IsZero() {
return reconcile.Result{}, nil
}
stored := nodeClaim.DeepCopy()
nodePoolName, ok := nodeClaim.Labels[v1beta1.NodePoolLabelKey]
if !ok {
return reconcile.Result{}, nil
}
nodePool := &v1beta1.NodePool{}
if err := c.kubeClient.Get(ctx, types.NamespacedName{Name: nodePoolName}, nodePool); err != nil {
return reconcile.Result{}, client.IgnoreNotFound(err)
}
var results []reconcile.Result
var errs error
reconcilers := []nodeClaimReconciler{
c.expiration,
c.drift,
c.emptiness,
}
for _, reconciler := range reconcilers {
res, err := reconciler.Reconcile(ctx, nodePool, nodeClaim)
errs = multierr.Append(errs, err)
results = append(results, res)
}
if !equality.Semantic.DeepEqual(stored, nodeClaim) {
if err := nodeclaimutil.UpdateStatus(ctx, c.kubeClient, nodeClaim); err != nil {
if errors.IsConflict(err) {
return reconcile.Result{Requeue: true}, nil
}
return reconcile.Result{}, client.IgnoreNotFound(err)
}
}
if errs != nil {
return reconcile.Result{}, errs
}
return result.Min(results...), nil
}
func (c *Controller) Name() string {
return "nodeclaim.disruption"
}
func (c *Controller) Builder(_ context.Context, m manager.Manager) operatorcontroller.Builder {
return operatorcontroller.Adapt(controllerruntime.
NewControllerManagedBy(m).
For(&v1beta1.NodeClaim{}).
WithOptions(controller.Options{MaxConcurrentReconciles: 10}).
Watches(
&v1beta1.NodePool{},
nodeclaimutil.NodePoolEventHandler(c.kubeClient),
).
Watches(
&v1.Pod{},
nodeclaimutil.PodEventHandler(c.kubeClient),
).
// Raw sources coming from the watch channel should always have their GVK populated
WatchesRawSource(&source.Channel{Source: state.NodeClassEventChannel}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) (requests []reconcile.Request) {
gvk := o.GetObjectKind().GroupVersionKind()
nodeClaimList := &v1beta1.NodeClaimList{}
if err := c.kubeClient.List(ctx, nodeClaimList, client.MatchingFields{
// The string returned here will get of the following format "<group>/<kind>/<name>"
"spec.nodeClassRef": fmt.Sprintf("%s/%s/%s", gvk.Group, gvk.Kind, o.GetName()),
}); err != nil {
return []reconcile.Request{}
}
return lo.Map(nodeClaimList.Items, func(nc v1beta1.NodeClaim, _ int) reconcile.Request {
return reconcile.Request{NamespacedName: client.ObjectKeyFromObject(&nc)}
})
})),
)
}