@@ -22,12 +22,11 @@ import (
2222 "sync"
2323
2424 opmetrics "github.com/awslabs/operatorpkg/metrics"
25- "github.com/awslabs/operatorpkg/object"
2625 "github.com/awslabs/operatorpkg/serrors"
2726 "github.com/prometheus/client_golang/prometheus"
2827 "github.com/samber/lo"
2928 corev1 "k8s.io/api/core/v1"
30- metav1 "k8s.io/apimachinery/pkg/apis/meta/v1 "
29+ "k8s.io/apimachinery/pkg/api/errors "
3130 "k8s.io/apimachinery/pkg/types"
3231 "k8s.io/apimachinery/pkg/util/sets"
3332 "k8s.io/klog/v2"
@@ -216,19 +215,18 @@ func (cc *ClusterCost) UpdateNodeClaim(ctx context.Context, nodeClaim *v1.NodeCl
216215 }
217216
218217 np := & v1.NodePool {}
219- if err := cc .client .Get (ctx , types.NamespacedName {Name : nodeClaim .Labels [v1 .NodePoolLabelKey ]}, np ); err != nil {
218+ err := cc .client .Get (ctx , types.NamespacedName {Name : nodeClaim .Labels [v1 .NodePoolLabelKey ]}, np )
219+ if err != nil && ! errors .IsNotFound (err ) {
220220 failed = true
221221 return serrors .Wrap (err , "nodepool" , nodeClaim .Labels [v1 .NodePoolLabelKey ], "nodeclaim" , klog .KObj (nodeClaim ))
222222 }
223- if _ , found := lo .Find (nodeClaim .GetOwnerReferences (), func (o metav1.OwnerReference ) bool {
224- return o .Kind == object .GVK (np ).Kind && o .UID == np .UID
225- }); ! found {
226- failed = true
227- return serrors .Wrap (fmt .Errorf ("nodepool not found for nodeclaim" ), "nodepool" , nodeClaim .Labels [v1 .NodePoolLabelKey ], "nodeclaim" , klog .KObj (nodeClaim ))
223+ if errors .IsNotFound (err ) {
224+ np .UID = types .UID ("manual" )
228225 }
226+
229227 cc .Lock ()
230228 defer cc .Unlock ()
231- err : = cc .internalAddOffering (ctx , np , nodeClaim .Labels [corev1 .LabelInstanceTypeStable ], nodeClaim .Labels [v1 .CapacityTypeLabelKey ], nodeClaim .Labels [corev1 .LabelTopologyZone ], true )
229+ err = cc .internalAddOffering (ctx , np , nodeClaim .Labels [corev1 .LabelInstanceTypeStable ], nodeClaim .Labels [v1 .CapacityTypeLabelKey ], nodeClaim .Labels [corev1 .LabelTopologyZone ], true )
232230 if err != nil {
233231 failed = true
234232 return serrors .Wrap (err , "nodeclaim" , klog .KObj (nodeClaim ), "nodepool" , klog .KObj (np ))
@@ -267,15 +265,13 @@ func (cc *ClusterCost) DeleteNodeClaim(ctx context.Context, nodeClaim *v1.NodeCl
267265 nodePoolName := nodeClaim .Labels [v1 .NodePoolLabelKey ]
268266 np := & v1.NodePool {}
269267 err := cc .client .Get (ctx , client.ObjectKey {Name : nodePoolName }, np )
270- if err != nil {
268+ if err != nil && ! errors . IsNotFound ( err ) {
271269 failed = true
272270 return serrors .Wrap (err , "nodepool" , nodePoolName , "nodeclaim" , klog .KObj (nodeClaim ))
273271 }
274- if _ , found := lo .Find (nodeClaim .GetOwnerReferences (), func (o metav1.OwnerReference ) bool {
275- return o .Kind == object .GVK (np ).Kind && o .UID == np .UID
276- }); ! found {
277- failed = true
278- return serrors .Wrap (fmt .Errorf ("nodepool not found for nodeclaim" ), "nodepool" , nodeClaim .Labels [v1 .NodePoolLabelKey ], "nodeclaim" , klog .KObj (nodeClaim ))
272+ if errors .IsNotFound (err ) {
273+ // Technically not an error, as users can create NodeClaims manually without a NodePool.
274+ np .UID = types .UID ("manual" )
279275 }
280276 cc .Lock ()
281277 defer cc .Unlock ()
@@ -363,6 +359,13 @@ func (cc *ClusterCost) internalRemoveOffering(np *v1.NodePool, instanceName, cap
363359 return nil
364360}
365361
362+ func (cc * ClusterCost ) Reset () {
363+ cc .Lock ()
364+ defer cc .Unlock ()
365+ cc .npCostMap = make (map [types.UID ]* NodePoolCost )
366+ cc .nodeClaimSet = make (sets.Set [string ])
367+ }
368+
366369// GetClusterCost returns the total cost of all compute resources across
367370// all NodePools in the cluster.
368371func (cc * ClusterCost ) GetClusterCost () float64 {
0 commit comments