Skip to content

Commit

Permalink
Adding nodeclass test for LicenseSelectors
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Cole committed Sep 21, 2023
1 parent 5d2baa9 commit 013cf14
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 44 deletions.
24 changes: 12 additions & 12 deletions pkg/apis/v1beta1/ec2nodeclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ type EC2NodeClassSpec struct {
// LicenseSelectorTerms is a list of LicenseSelectors. The terms are ORed.
// +optional
LicenseSelectorTerms []LicenseSelectorTerm `json:"licenseSelectorTerms,omitempty" hash:"ignore"`
// HostResourceGroupSelectorTerms is a list of HostResourceGroupSelectors. The terms are ORed.
// +optional
HostResourceGroupSelectorTerms []HostResourceGroupSelectorTerm `json:"hostResourceGroupSelectorTerms,omitempty" hash:"ignore"`
// PlacementGroupSelectorTerms is a list of PlacementGroupSelector. The terms are ORed.
// +optional
PlacementGroupSelectorTerms []PlacementGroupSelectorTerm `json:"placementGroupSelectorTerms,omitempty" hash:"ignore"`
// HostResourceGroupSelectorTerms is a list of HostResourceGroupSelectors. The terms are ORed.
// +optional
HostResourceGroupSelectorTerms []HostResourceGroupSelectorTerm `json:"hostResourceGroupSelectorTerms,omitempty" hash:"ignore"`
// PlacementGroupSelectorTerms is a list of PlacementGroupSelector. The terms are ORed.
// +optional
PlacementGroupSelectorTerms []PlacementGroupSelectorTerm `json:"placementGroupSelectorTerms,omitempty" hash:"ignore"`
// UserData to be applied to the provisioned nodes.
// It must be in the appropriate format based on the AMIFamily in use. Karpenter will merge certain fields into
// this UserData to ensure nodes are being provisioned with the correct configuration.
Expand Down Expand Up @@ -189,17 +189,17 @@ type LicenseSelectorTerm struct {
// HostResourceGroupSelectorTerm defines the selection logic for host Resource groups
// that are used to launch nodes. If multiple fields are used for selection, the requirements are ANDed
type HostResourceGroupSelectorTerm struct {
// Name of the hrg to be selected
// +optional
Name string `json:"name,omitempty"`
// Name of the hrg to be selected
// +optional
Name string `json:"name,omitempty"`
}

// PlacementGroupSelectorTerm defines the selection logic for ec2 placement groups
// that are used to launch nodes. If multiple fields are used for selection, the requirements are ANDed
type PlacementGroupSelectorTerm struct {
// Name of the placement group to be selected
// +optional
Name string `json:"name,omitempty"`
// Name of the placement group to be selected
// +optional
Name string `json:"name,omitempty"`
}

// MetadataOptions contains parameters for specifying the exposure of the
Expand Down
30 changes: 15 additions & 15 deletions pkg/apis/v1beta1/ec2nodeclass_status.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,12 @@ type AMI struct {

// HostResourceGroup contains the resolved host resource group name and arn for node launch
type HostResourceGroup struct {
// Name of the HRG
// +optional
Name string `json:"name,omitempty"`
// Arn of the HRG
// +optional
ARN string `json:"arn,omitempty"`
// Name of the HRG
// +optional
Name string `json:"name,omitempty"`
// Arn of the HRG
// +optional
ARN string `json:"arn,omitempty"`
}

// EC2NodeClassStatus contains the resolved state of the EC2NodeClass
Expand All @@ -73,13 +73,13 @@ type EC2NodeClassStatus struct {
// cluster under the AMI selectors.
// +optional
AMIs []AMI `json:"amis,omitempty"`
// Licenses contains the license arns
// +optional
Licenses []string `json:"licenses,omitempty"`
// HostResourceGroups contains the HRG arns
// +optional
HostResourceGroup *HostResourceGroup `json:"hostResourceGroup,omitempty"`
// PlacementGroups contains the ec2 placement group arns
// +optional
PlacementGroups []string `json:"placementGroups,omitempty"`
// Licenses contains the license arns
// +optional
Licenses []string `json:"licenses,omitempty"`
// HostResourceGroups contains the HRG arns
// +optional
HostResourceGroup *HostResourceGroup `json:"hostResourceGroup,omitempty"`
// PlacementGroups contains the ec2 placement group arns
// +optional
PlacementGroups []string `json:"placementGroups,omitempty"`
}
32 changes: 32 additions & 0 deletions pkg/apis/v1beta1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion pkg/fake/rgapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ import (
"github.com/aws/aws-sdk-go/service/resourcegroups/resourcegroupsiface"
)

var (
TEST_LICENSE_ARN = "arn:aws:license-manager:us-west-2:11111111111:license-configuration:lic-94ba36399bd98eaad808b0ffb1d1604b"
TEST_LICENSE_NAME = "test-license"
)

type ResourceGroupsAPI struct {
resourcegroupsiface.ResourceGroupsAPI
ResourceGroupsBehaviour
Expand All @@ -42,7 +47,7 @@ func (r *ResourceGroupsAPI) ListGroupsWithContext(_ aws.Context, input *resource
return r.ListGroupsBehavior.Invoke(input, func(input *resourcegroups.ListGroupsInput) (*resourcegroups.ListGroupsOutput, error) {
return &resourcegroups.ListGroupsOutput{
GroupIdentifiers: []*resourcegroups.GroupIdentifier{
{GroupArn: aws.String("arn:aws:license-manager:us-west-2:11111111111:license-configuration:lic-94ba36399bd98eaad808b0ffb1d1604b"), GroupName: aws.String("test-license")},
{GroupArn: aws.String(TEST_LICENSE_ARN), GroupName: aws.String(TEST_LICENSE_NAME)},
},
}, nil
})
Expand Down
25 changes: 13 additions & 12 deletions pkg/providers/hostresourcegroup/hostresourcegroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,18 @@ package hostresourcegroup

import (
"context"
"fmt"
"sync"
"fmt"

//"github.com/aws/aws-sdk-go/service/resourcegroups"
"github.com/aws/aws-sdk-go/service/resourcegroups"
"github.com/aws/aws-sdk-go/service/resourcegroups/resourcegroupsiface"
"github.com/aws/karpenter-core/pkg/utils/pretty"
"github.com/aws/karpenter/pkg/apis/v1beta1"
"github.com/mitchellh/hashstructure/v2"
"github.com/patrickmn/go-cache"
"knative.dev/pkg/logging"

"github.com/aws/karpenter-core/pkg/utils/pretty"
"github.com/aws/karpenter/pkg/apis/v1beta1"
)

type Provider struct {
Expand All @@ -50,10 +51,10 @@ func (p *Provider) Get(ctx context.Context, nodeClass *v1beta1.EC2NodeClass) (*v
p.Lock()
defer p.Unlock()

selectors := nodeClass.Spec.HostResourceGroupSelectorTerms
if selectors == nil {
return nil, nil
}
selectors := nodeClass.Spec.HostResourceGroupSelectorTerms
if selectors == nil {
return nil, nil
}

// Look for a cached result
hash, err := hashstructure.Hash(selectors, hashstructure.FormatV2, &hashstructure.HashOptions{SlicesAsSets: true})
Expand All @@ -64,14 +65,14 @@ func (p *Provider) Get(ctx context.Context, nodeClass *v1beta1.EC2NodeClass) (*v
return cached.(*v1beta1.HostResourceGroup), nil
}

var match *v1beta1.HostResourceGroup
var match *v1beta1.HostResourceGroup
err = p.resourcegroups.ListGroupsPagesWithContext(ctx, &resourcegroups.ListGroupsInput{}, func(page *resourcegroups.ListGroupsOutput, lastPage bool) bool {
for i := range page.GroupIdentifiers {
for x := range selectors {
if *page.GroupIdentifiers[i].GroupName == selectors[x].Name {
match = &v1beta1.HostResourceGroup{ ARN: *page.GroupIdentifiers[i].GroupArn, Name: *page.GroupIdentifiers[i].GroupName }
p.cache.SetDefault(fmt.Sprint(hash), match)
return false
match = &v1beta1.HostResourceGroup{ARN: *page.GroupIdentifiers[i].GroupArn, Name: *page.GroupIdentifiers[i].GroupName}
p.cache.SetDefault(fmt.Sprint(hash), match)
return false
}
}
}
Expand All @@ -84,7 +85,7 @@ func (p *Provider) Get(ctx context.Context, nodeClass *v1beta1.EC2NodeClass) (*v
}
if p.cm.HasChanged(fmt.Sprintf("hostresourcegroups/%t/%s", nodeClass.IsNodeTemplate, nodeClass.Name), match) {
logging.FromContext(ctx).
With("host resource group", match).
With("host resource group", match).
Debugf("discovered host resource group")
}

Expand Down
3 changes: 2 additions & 1 deletion pkg/providers/license/license.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (

"github.com/aws/karpenter/pkg/apis/v1beta1"

"github.com/aws/karpenter-core/pkg/utils/pretty"
"knative.dev/pkg/logging"

"github.com/aws/karpenter-core/pkg/utils/pretty"
)

type Provider struct {
Expand Down
7 changes: 4 additions & 3 deletions pkg/providers/placementgroup/placementgroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,9 @@ import (

"github.com/aws/karpenter/pkg/apis/v1beta1"

"github.com/aws/karpenter-core/pkg/utils/pretty"
"knative.dev/pkg/logging"

"github.com/aws/karpenter-core/pkg/utils/pretty"
)

type Provider struct {
Expand Down Expand Up @@ -78,14 +79,14 @@ func (p *Provider) Get(ctx context.Context, nodeClass *v1beta1.EC2NodeClass) (*e
for x := range selectors {
if *output.PlacementGroups[i].GroupName == selectors[x].Name {
match = output.PlacementGroups[i]
p.cache.SetDefault(fmt.Sprint(hash), match)
p.cache.SetDefault(fmt.Sprint(hash), match)
break
}
}
}
if p.cm.HasChanged(fmt.Sprintf("placementgroup/%t/%s", nodeClass.IsNodeTemplate, nodeClass.Name), match) {
logging.FromContext(ctx).
With("placement group", match).
With("placement group", match).
Debugf("discovered placement group")
}

Expand Down
7 changes: 7 additions & 0 deletions pkg/utils/nodeclass/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,13 @@ var _ = Describe("NodeClassUtils", func() {
Expect(convertedNodeTemplate.Status.Subnets).To(Equal(nodeTemplate.Status.Subnets))
Expect(convertedNodeTemplate.Status.AMIs).To(Equal(nodeTemplate.Status.AMIs))
})
It("should convert a AWSNodeTemplate with LicenseSelectors to a EC2NodeClass and back", func() {
nodeTemplate.Status.Licenses = []string{"test-license"}
nodeTemplate.Spec.LicenseSelector = map[string]string{"name": "test-license"}
convertedNodeTemplate := nodetemplateutil.New(nodeclassutil.New(nodeTemplate))
Expect(convertedNodeTemplate.Status.Licenses).To(Equal(nodeTemplate.Status.Licenses))
Expect(convertedNodeTemplate.Spec.LicenseSelector).To(Equal(nodeTemplate.Spec.LicenseSelector))
})
It("should retrieve a EC2NodeClass with a get call", func() {
nodeClass := test.EC2NodeClass()
ExpectApplied(ctx, env.Client, nodeClass)
Expand Down

0 comments on commit 013cf14

Please sign in to comment.