From 8677963cf38b22e1a2dd43b2e46c6b7d929cf60d Mon Sep 17 00:00:00 2001 From: Jonathan Innis Date: Thu, 12 Oct 2023 12:36:13 -0700 Subject: [PATCH] Add the NodeClaim indexer --- go.mod | 2 +- go.sum | 4 ++-- pkg/controllers/nodeclass/controller.go | 2 +- pkg/operator/operator.go | 10 ++++++++++ pkg/test/nodeclass.go | 2 +- 5 files changed, 15 insertions(+), 5 deletions(-) diff --git a/go.mod b/go.mod index 72ebfd4405a8..76a1361dd608 100644 --- a/go.mod +++ b/go.mod @@ -30,7 +30,7 @@ require ( sigs.k8s.io/controller-runtime v0.16.2 ) -replace github.com/aws/karpenter-core => github.com/jonathan-innis/karpenter-core v0.0.0-20231012191332-1e368cfd9c25 +replace github.com/aws/karpenter-core => github.com/jonathan-innis/karpenter-core v0.0.0-20231012204809-85c20f5ab91f require ( contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect diff --git a/go.sum b/go.sum index b845778d4ff1..ab55bfc2957a 100644 --- a/go.sum +++ b/go.sum @@ -224,8 +224,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8= github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= -github.com/jonathan-innis/karpenter-core v0.0.0-20231012191332-1e368cfd9c25 h1:ouaAwe6U5+LIgvgnAfwa7gMnFKtpvgrRMc6ATJlCiP4= -github.com/jonathan-innis/karpenter-core v0.0.0-20231012191332-1e368cfd9c25/go.mod h1:rb3kp/3cj38tACF6udfpmIvKoQMwirSVoHNlrd66LyE= +github.com/jonathan-innis/karpenter-core v0.0.0-20231012204809-85c20f5ab91f h1:KHHkgIt6RPCEDezhXrr/Y+F9MI2TGi6A5+46zkdQU1I= +github.com/jonathan-innis/karpenter-core v0.0.0-20231012204809-85c20f5ab91f/go.mod h1:rb3kp/3cj38tACF6udfpmIvKoQMwirSVoHNlrd66LyE= github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY= github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y= github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4= diff --git a/pkg/controllers/nodeclass/controller.go b/pkg/controllers/nodeclass/controller.go index 9fb9796bd597..112294f3626f 100644 --- a/pkg/controllers/nodeclass/controller.go +++ b/pkg/controllers/nodeclass/controller.go @@ -107,7 +107,7 @@ func (c *Controller) Finalize(ctx context.Context, nodeClass *v1beta1.EC2NodeCla return reconcile.Result{}, nil } nodeClaimList := &corev1beta1.NodeClaimList{} - if err := c.kubeClient.List(ctx, nodeClaimList, client.MatchingFields{"spec.nodeClass.name": nodeClass.Name}); err != nil { + if err := c.kubeClient.List(ctx, nodeClaimList, client.MatchingFields{"spec.nodeClassRef.name": nodeClass.Name}); err != nil { return reconcile.Result{}, fmt.Errorf("listing nodeclaims that are using nodeclass, %w", err) } if len(nodeClaimList.Items) > 0 { diff --git a/pkg/operator/operator.go b/pkg/operator/operator.go index d07f0dd98f7a..3e430aafa32c 100644 --- a/pkg/operator/operator.go +++ b/pkg/operator/operator.go @@ -37,6 +37,7 @@ import ( "github.com/aws/aws-sdk-go/service/iam" "github.com/aws/aws-sdk-go/service/ssm" "github.com/patrickmn/go-cache" + "sigs.k8s.io/controller-runtime/pkg/client" "github.com/samber/lo" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -46,6 +47,7 @@ import ( "knative.dev/pkg/logging" "knative.dev/pkg/ptr" + corev1beta1 "github.com/aws/karpenter-core/pkg/apis/v1beta1" "github.com/aws/karpenter-core/pkg/operator" "github.com/aws/karpenter/pkg/apis/settings" awscache "github.com/aws/karpenter/pkg/cache" @@ -167,6 +169,14 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont launchTemplateProvider, ) + lo.Must0(operator.Manager.GetFieldIndexer().IndexField(ctx, &corev1beta1.NodeClaim{}, "spec.nodeClassRef.name", func(o client.Object) []string { + nc := o.(*corev1beta1.NodeClaim) + if nc.Spec.NodeClassRef == nil { + return []string{} + } + return []string{nc.Spec.NodeClassRef.Name} + }), "failed to setup nodeclaim indexer") + return ctx, &Operator{ Operator: operator, Session: sess, diff --git a/pkg/test/nodeclass.go b/pkg/test/nodeclass.go index 8c780f497a8c..54b7f49b1e49 100644 --- a/pkg/test/nodeclass.go +++ b/pkg/test/nodeclass.go @@ -68,7 +68,7 @@ func EC2NodeClass(overrides ...v1beta1.EC2NodeClass) *v1beta1.EC2NodeClass { func EC2NodeClassFieldIndexer(ctx context.Context) func(cache.Cache) error { return func(c cache.Cache) error { - return c.IndexField(ctx, &corev1beta1.NodeClaim{}, "spec.nodeClass.name", func(obj client.Object) []string { + return c.IndexField(ctx, &corev1beta1.NodeClaim{}, "spec.nodeClassRef.name", func(obj client.Object) []string { nc := obj.(*corev1beta1.NodeClaim) if nc.Spec.NodeClassRef == nil { return []string{""}