Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdeal committed Nov 21, 2024
1 parent 9b62022 commit fe329fc
Show file tree
Hide file tree
Showing 16 changed files with 71 additions and 37 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/node/health/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = BeforeSuite(func() {
env = test.NewEnvironment(
test.WithCRDs(apis.CRDs...),
test.WithCRDs(v1alpha1.CRDs...),
test.WithFieldIndexers(test.NodeClaimFieldIndexer(ctx), test.VolumeAttachmentFieldIndexer(ctx), test.NodeFieldIndexer(ctx)),
test.WithFieldIndexers(test.NodeClaimProviderIDFieldIndexer(ctx), test.VolumeAttachmentFieldIndexer(ctx), test.NodeProviderIDFieldIndexer(ctx)),
)
cloudProvider = fake.NewCloudProvider()
cloudProvider = fake.NewCloudProvider()
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/node/termination/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var _ = BeforeSuite(func() {
env = test.NewEnvironment(
test.WithCRDs(apis.CRDs...),
test.WithCRDs(v1alpha1.CRDs...),
test.WithFieldIndexers(test.NodeClaimFieldIndexer(ctx), test.VolumeAttachmentFieldIndexer(ctx)),
test.WithFieldIndexers(test.NodeClaimProviderIDFieldIndexer(ctx), test.VolumeAttachmentFieldIndexer(ctx)),
)

cloudProvider = fake.NewCloudProvider()
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/nodeclaim/consistency/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ var _ = BeforeSuite(func() {
env = test.NewEnvironment(
test.WithCRDs(apis.CRDs...),
test.WithCRDs(v1alpha1.CRDs...),
test.WithFieldIndexers(test.NodeClaimFieldIndexer(ctx), test.NodeFieldIndexer(ctx)),
test.WithFieldIndexers(test.NodeClaimProviderIDFieldIndexer(ctx), test.NodeProviderIDFieldIndexer(ctx)),
)
ctx = options.ToContext(ctx, test.Options())
cp = &fake.CloudProvider{}
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/nodeclaim/disruption/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func TestAPIs(t *testing.T) {

var _ = BeforeSuite(func() {
fakeClock = clock.NewFakeClock(time.Now())
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeFieldIndexer(ctx)))
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeProviderIDFieldIndexer(ctx)))
ctx = options.ToContext(ctx, test.Options())
cp = fake.NewCloudProvider()
nodeClaimDisruptionController = nodeclaimdisruption.NewController(fakeClock, env.Client, cp)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/nodeclaim/expiration/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ func TestAPIs(t *testing.T) {

var _ = BeforeSuite(func() {
fakeClock = clock.NewFakeClock(time.Now())
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeFieldIndexer(ctx)))
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeProviderIDFieldIndexer(ctx)))
ctx = options.ToContext(ctx, test.Options())
cp = fake.NewCloudProvider()
expirationController = expiration.NewController(fakeClock, env.Client, cp)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/nodeclaim/garbagecollection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func TestAPIs(t *testing.T) {

var _ = BeforeSuite(func() {
fakeClock = clock.NewFakeClock(time.Now())
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeFieldIndexer(ctx)))
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeProviderIDFieldIndexer(ctx)))
ctx = options.ToContext(ctx, test.Options())
cloudProvider = fake.NewCloudProvider()
garbageCollectionController = nodeclaimgarbagecollection.NewController(fakeClock, env.Client, cloudProvider)
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/nodeclaim/lifecycle/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestAPIs(t *testing.T) {

var _ = BeforeSuite(func() {
fakeClock = clock.NewFakeClock(time.Now())
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeFieldIndexer(ctx)))
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeProviderIDFieldIndexer(ctx)))
ctx = options.ToContext(ctx, test.Options())

cloudProvider = fake.NewCloudProvider()
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/nodeclaim/podevents/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ var _ = BeforeSuite(func() {
env = test.NewEnvironment(
test.WithCRDs(apis.CRDs...),
test.WithCRDs(v1alpha1.CRDs...),
test.WithFieldIndexers(test.NodeClaimFieldIndexer(ctx), test.NodeFieldIndexer(ctx)),
test.WithFieldIndexers(test.NodeClaimProviderIDFieldIndexer(ctx), test.NodeProviderIDFieldIndexer(ctx)),
)
ctx = options.ToContext(ctx, test.Options())
cp = fake.NewCloudProvider()
Expand Down
30 changes: 19 additions & 11 deletions pkg/test/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (

"github.com/awslabs/operatorpkg/option"
"github.com/samber/lo"
"go.uber.org/multierr"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
Expand Down Expand Up @@ -69,31 +70,38 @@ func WithFieldIndexers(fieldIndexers ...func(cache.Cache) error) option.Function
}
}

// NodeFieldIndexer provides indexes on the following fields:
//
// - spec.providerID
func NodeFieldIndexer(ctx context.Context) func(cache.Cache) error {
func NodeProviderIDFieldIndexer(ctx context.Context) func(cache.Cache) error {
return func(c cache.Cache) error {
return c.IndexField(ctx, &corev1.Node{}, "spec.providerID", func(obj client.Object) []string {
return []string{obj.(*corev1.Node).Spec.ProviderID}
})
}
}

// NodeClaimFieldIndexer provides indexes on the following fields:
//
// - status.providerID
func NodeClaimFieldIndexer(ctx context.Context) func(cache.Cache) error {
func NodeClaimProviderIDFieldIndexer(ctx context.Context) func(cache.Cache) error {
return func(c cache.Cache) error {
return c.IndexField(ctx, &v1.NodeClaim{}, "status.providerID", func(obj client.Object) []string {
return []string{obj.(*v1.NodeClaim).Status.ProviderID}
})
}
}

// VolumeAttachmentFieldIndexer provides indexes on the following fields:
//
// - status.nodeName
func NodeClaimNodeClassRefFieldIndexer(ctx context.Context) func(cache.Cache) error {
return func(c cache.Cache) error {
var err error
err = multierr.Append(err, c.IndexField(ctx, &v1.NodeClaim{}, "spec.nodeClassRef.group", func(obj client.Object) []string {
return []string{obj.(*v1.NodeClaim).Spec.NodeClassRef.Group}
}))
err = multierr.Append(err, c.IndexField(ctx, &v1.NodeClaim{}, "spec.nodeClassRef.kind", func(obj client.Object) []string {
return []string{obj.(*v1.NodeClaim).Spec.NodeClassRef.Kind}
}))
err = multierr.Append(err, c.IndexField(ctx, &v1.NodeClaim{}, "spec.nodeClassRef.name", func(obj client.Object) []string {
return []string{obj.(*v1.NodeClaim).Spec.NodeClassRef.Name}
}))
return err
}
}

func VolumeAttachmentFieldIndexer(ctx context.Context) func(cache.Cache) error {
return func(c cache.Cache) error {
return c.IndexField(ctx, &storagev1.VolumeAttachment{}, "spec.nodeName", func(obj client.Object) []string {
Expand Down
10 changes: 8 additions & 2 deletions pkg/test/nodeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,13 @@ package test
import (
"fmt"

"github.com/awslabs/operatorpkg/object"
"github.com/imdario/mergo"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"

v1 "sigs.k8s.io/karpenter/pkg/apis/v1"
"sigs.k8s.io/karpenter/pkg/utils/nodeclaim"
)

// NodeClaim creates a test NodeClaim with defaults that can be overridden by overrides.
Expand All @@ -42,11 +45,14 @@ func NodeClaim(overrides ...v1.NodeClaim) *v1.NodeClaim {
}
if override.Spec.NodeClassRef == nil {
override.Spec.NodeClassRef = &v1.NodeClassReference{
Group: object.GVK(defaultNodeClass).Group,
Kind: object.GVK(defaultNodeClass).Kind,
Name: "default",
Group: "karpenter.test.sh",
Kind: "TestNodeClass",
}
}
override.Labels = lo.Assign(map[string]string{
nodeclaim.NodeClassLabelKey(override.Spec.NodeClassRef): override.Spec.NodeClassRef.Name,
}, override.Labels)
if override.Spec.Requirements == nil {
override.Spec.Requirements = []v1.NodeSelectorRequirementWithMinValues{}
}
Expand Down
10 changes: 10 additions & 0 deletions pkg/test/nodeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,16 @@ import (
"sigs.k8s.io/karpenter/pkg/test/v1alpha1"
)

var (
// defaultNodeClass is the default NodeClass type used when creating NodeClassRefs for NodePools and NodeClaims
defaultNodeClass status.Object = &v1alpha1.TestNodeClass{}
)

// SetDefaultNodeClassType configures the default NodeClass type used when generating NodeClassRefs for test NodePools and NodeClaims.
func SetDefaultNodeClassType(nc status.Object) {
defaultNodeClass = nc
}

// NodeClass creates a test NodeClass with defaults that can be overridden by overrides.
// Overrides are applied in order, with a last write wins semantic.
func NodeClass(overrides ...v1alpha1.TestNodeClass) *v1alpha1.TestNodeClass {
Expand Down
5 changes: 3 additions & 2 deletions pkg/test/nodepool.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package test
import (
"fmt"

"github.com/awslabs/operatorpkg/object"
"github.com/imdario/mergo"
"github.com/samber/lo"
corev1 "k8s.io/api/core/v1"
Expand All @@ -45,9 +46,9 @@ func NodePool(overrides ...v1.NodePool) *v1.NodePool {
}
if override.Spec.Template.Spec.NodeClassRef == nil {
override.Spec.Template.Spec.NodeClassRef = &v1.NodeClassReference{
Group: object.GVK(defaultNodeClass).Group,
Kind: object.GVK(defaultNodeClass).Kind,
Name: "default",
Group: "karpenter.test.sh",
Kind: "TestNodeClass",
}
}
if override.Spec.Template.Spec.Requirements == nil {
Expand Down
15 changes: 12 additions & 3 deletions pkg/test/v1alpha1/testnodeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,17 @@ import (
_ "embed"

"github.com/awslabs/operatorpkg/object"
v1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
v1 "sigs.k8s.io/karpenter/pkg/apis/v1"
)

//go:generate controller-gen crd object:headerFile="../../../hack/boilerplate.go.txt" paths="./..." output:crd:artifacts:config=crds
var (
//go:embed crds/karpenter.test.sh_testnodeclasses.yaml
TestNodeClassCRD []byte
CRDs = []*v1.CustomResourceDefinition{
object.Unmarshal[v1.CustomResourceDefinition](TestNodeClassCRD),
CRDs = []*apiextensionsv1.CustomResourceDefinition{
object.Unmarshal[apiextensionsv1.CustomResourceDefinition](TestNodeClassCRD),
}
)

Expand All @@ -43,6 +44,14 @@ type TestNodeClass struct {
Status TestNodeClassStatus `json:"status,omitempty"`
}

func (in *TestNodeClass) NodeClassRef() *v1.NodeClassReference {
return &v1.NodeClassReference{
Group: object.GVK(in).Group,
Kind: object.GVK(in).Kind,
Name: in.Name,
}
}

// TestNodeClassList contains a list of TestNodeClass
// +kubebuilder:object:root=true
type TestNodeClassList struct {
Expand Down
12 changes: 2 additions & 10 deletions pkg/utils/node/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func TestAPIs(t *testing.T) {
}

var _ = BeforeSuite(func() {
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeClaimFieldIndexer(ctx)))
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeClaimProviderIDFieldIndexereldIndexer(ctx)))
})

var _ = AfterSuite(func() {
Expand All @@ -60,15 +60,7 @@ var _ = Describe("NodeUtils", func() {
var testNode *corev1.Node
var nodeClaim *v1.NodeClaim
BeforeEach(func() {
nodeClaim = test.NodeClaim(v1.NodeClaim{
Spec: v1.NodeClaimSpec{
NodeClassRef: &v1.NodeClassReference{
Kind: "NodeClassRef",
Group: "test.cloudprovider",
Name: "default",
},
},
})
nodeClaim = test.NodeClaim()
})
It("should return nodeClaim for node which has the same provider ID", func() {
testNode = test.NodeClaimLinkedNode(nodeClaim)
Expand Down
8 changes: 8 additions & 0 deletions pkg/utils/nodeclaim/nodeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ func WithNodePoolFilter(nodePoolName string) option.Function[ListOptions] {
return withClientListOptions(client.MatchingLabels(map[string]string{v1.NodePoolLabelKey: nodePoolName}))
}

func WithNodeClassFilter(nodeClass status.Object) option.Function[ListOptions] {
return withClientListOptions(client.MatchingFields{
"spec.nodeClassRef.group": object.GVK(nodeClass).Group,
"spec.nodeClassRef.kind": object.GVK(nodeClass).Kind,
"spec.nodeClassRef.name": nodeClass.GetName(),
})
}

func WithManagedFilter(cp cloudprovider.CloudProvider) option.Function[ListOptions] {
return func(opts *ListOptions) {
opts.filterPredicates = append(opts.filterPredicates, func(nc *v1.NodeClaim) bool { return IsManaged(nc, cp) })
Expand Down
2 changes: 1 addition & 1 deletion pkg/utils/nodeclaim/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func TestAPIs(t *testing.T) {
}

var _ = BeforeSuite(func() {
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeClaimFieldIndexer(ctx)))
env = test.NewEnvironment(test.WithCRDs(apis.CRDs...), test.WithCRDs(v1alpha1.CRDs...), test.WithFieldIndexers(test.NodeClaimProviderIDFieldIndexer(ctx)))
cloudProvider = fake.NewCloudProvider()
})

Expand Down

0 comments on commit fe329fc

Please sign in to comment.