-
Notifications
You must be signed in to change notification settings - Fork 239
/
Copy pathnodeclaim.go
201 lines (179 loc) · 7.03 KB
/
nodeclaim.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
/*
Copyright 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 nodeclaim
import (
"context"
"errors"
"fmt"
"github.com/samber/lo"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"knative.dev/pkg/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/karpenter/pkg/apis/v1beta1"
)
// PodEventHandler is a watcher on v1.Pods that maps Pods to NodeClaim based on the node names
// and enqueues reconcile.Requests for the NodeClaims
func PodEventHandler(c client.Client) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) (requests []reconcile.Request) {
if name := o.(*v1.Pod).Spec.NodeName; name != "" {
node := &v1.Node{}
if err := c.Get(ctx, types.NamespacedName{Name: name}, node); err != nil {
return []reconcile.Request{}
}
nodeClaimList := &v1beta1.NodeClaimList{}
if err := c.List(ctx, nodeClaimList, client.MatchingFields{"status.providerID": node.Spec.ProviderID}); err != nil {
return []reconcile.Request{}
}
return lo.Map(nodeClaimList.Items, func(n v1beta1.NodeClaim, _ int) reconcile.Request {
return reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&n),
}
})
}
return requests
})
}
// NodeEventHandler is a watcher on v1.Node that maps Nodes to NodeClaims based on provider ids
// and enqueues reconcile.Requests for the NodeClaims
func NodeEventHandler(c client.Client) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) []reconcile.Request {
node := o.(*v1.Node)
nodeClaimList := &v1beta1.NodeClaimList{}
if err := c.List(ctx, nodeClaimList, client.MatchingFields{"status.providerID": node.Spec.ProviderID}); err != nil {
return []reconcile.Request{}
}
return lo.Map(nodeClaimList.Items, func(n v1beta1.NodeClaim, _ int) reconcile.Request {
return reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&n),
}
})
})
}
// NodePoolEventHandler is a watcher on v1beta1.NodeClaim that maps NodePool to NodeClaims based
// on the v1beta1.NodePoolLabelKey and enqueues reconcile.Requests for the NodeClaim
func NodePoolEventHandler(c client.Client) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) (requests []reconcile.Request) {
nodeClaimList := &v1beta1.NodeClaimList{}
if err := c.List(ctx, nodeClaimList, client.MatchingLabels(map[string]string{v1beta1.NodePoolLabelKey: o.GetName()})); err != nil {
return requests
}
return lo.Map(nodeClaimList.Items, func(n v1beta1.NodeClaim, _ int) reconcile.Request {
return reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&n),
}
})
})
}
// NodeClassEventHandler is a watcher on v1beta1.NodeClaim that maps NodeClass to NodeClaims based
// on the nodeClassRef and enqueues reconcile.Requests for the NodeClaim
func NodeClassEventHandler(c client.Client) handler.EventHandler {
return handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, o client.Object) (requests []reconcile.Request) {
nodeClaimList := &v1beta1.NodeClaimList{}
if err := c.List(ctx, nodeClaimList, client.MatchingFields{
"spec.nodeClassRef.apiVersion": o.GetObjectKind().GroupVersionKind().GroupVersion().String(),
"spec.nodeClassRef.kind": o.GetObjectKind().GroupVersionKind().Kind,
"spec.nodeClassRef.name": o.GetName(),
}); err != nil {
return requests
}
return lo.Map(nodeClaimList.Items, func(n v1beta1.NodeClaim, _ int) reconcile.Request {
return reconcile.Request{
NamespacedName: client.ObjectKeyFromObject(&n),
}
})
})
}
// NodeNotFoundError is an error returned when no v1.Nodes are found matching the passed providerID
type NodeNotFoundError struct {
ProviderID string
}
func (e *NodeNotFoundError) Error() string {
return fmt.Sprintf("no nodes found for provider id '%s'", e.ProviderID)
}
func IsNodeNotFoundError(err error) bool {
if err == nil {
return false
}
nnfErr := &NodeNotFoundError{}
return errors.As(err, &nnfErr)
}
func IgnoreNodeNotFoundError(err error) error {
if !IsNodeNotFoundError(err) {
return err
}
return nil
}
// DuplicateNodeError is an error returned when multiple v1.Nodes are found matching the passed providerID
type DuplicateNodeError struct {
ProviderID string
}
func (e *DuplicateNodeError) Error() string {
return fmt.Sprintf("multiple found for provider id '%s'", e.ProviderID)
}
func IsDuplicateNodeError(err error) bool {
if err == nil {
return false
}
dnErr := &DuplicateNodeError{}
return errors.As(err, &dnErr)
}
func IgnoreDuplicateNodeError(err error) error {
if !IsDuplicateNodeError(err) {
return err
}
return nil
}
// NodeForNodeClaim is a helper function that takes a v1beta1.NodeClaim and attempts to find the matching v1.Node by its providerID
// This function will return errors if:
// 1. No v1.Nodes match the v1beta1.NodeClaim providerID
// 2. Multiple v1.Nodes match the v1beta1.NodeClaim providerID
func NodeForNodeClaim(ctx context.Context, c client.Client, nodeClaim *v1beta1.NodeClaim) (*v1.Node, error) {
nodes, err := AllNodesForNodeClaim(ctx, c, nodeClaim)
if err != nil {
return nil, err
}
if len(nodes) > 1 {
return nil, &DuplicateNodeError{ProviderID: nodeClaim.Status.ProviderID}
}
if len(nodes) == 0 {
return nil, &NodeNotFoundError{ProviderID: nodeClaim.Status.ProviderID}
}
return nodes[0], nil
}
// AllNodesForNodeClaim is a helper function that takes a v1beta1.NodeClaim and finds ALL matching v1.Nodes by their providerID
// If the providerID is not resolved for a NodeClaim, then no Nodes will map to it
func AllNodesForNodeClaim(ctx context.Context, c client.Client, nodeClaim *v1beta1.NodeClaim) ([]*v1.Node, error) {
// NodeClaims that have no resolved providerID have no nodes mapped to them
if nodeClaim.Status.ProviderID == "" {
return nil, nil
}
nodeList := v1.NodeList{}
if err := c.List(ctx, &nodeList, client.MatchingFields{"spec.providerID": nodeClaim.Status.ProviderID}); err != nil {
return nil, fmt.Errorf("listing nodes, %w", err)
}
return lo.ToSlicePtr(nodeList.Items), nil
}
func UpdateNodeOwnerReferences(nodeClaim *v1beta1.NodeClaim, node *v1.Node) *v1.Node {
node.OwnerReferences = append(node.OwnerReferences, metav1.OwnerReference{
APIVersion: v1beta1.SchemeGroupVersion.String(),
Kind: "NodeClaim",
Name: nodeClaim.Name,
UID: nodeClaim.UID,
BlockOwnerDeletion: ptr.Bool(true),
})
return node
}