Skip to content

Commit

Permalink
Add missing provider
Browse files Browse the repository at this point in the history
  • Loading branch information
Sebastian Cole committed Sep 15, 2023
1 parent b495155 commit d033501
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
77 changes: 77 additions & 0 deletions pkg/providers/hostresourcegroup/hostresourcegroup.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package hostresourcegroup

import (
"context"
"sync"

//"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/patrickmn/go-cache"
"knative.dev/pkg/logging"
)

type Provider struct {
sync.RWMutex
resourcegroups resourcegroupsiface.ResourceGroupsAPI
cache *cache.Cache
cm *pretty.ChangeMonitor
}

func NewProvider(rgapi resourcegroupsiface.ResourceGroupsAPI, cache *cache.Cache) *Provider {
return &Provider{
resourcegroups: rgapi,
cm: pretty.NewChangeMonitor(),
// TODO: Remove cache for v1beta1, utilize resolved subnet from the AWSNodeTemplate.status
// Subnets are sorted on AvailableIpAddressCount, descending order
cache: cache,
}
}

func (p *Provider) Get(ctx context.Context, nodeClass *v1beta1.NodeClass) (*v1beta1.HostResourceGroup, error) {
p.Lock()
defer p.Unlock()
var nextToken *string = nil
var groups []*resourcegroups.GroupIdentifier
for {
resp, err := p.resourcegroups.ListGroupsWithContext(ctx, &resourcegroups.ListGroupsInput{ NextToken: nextToken})
if err != nil {
return nil, err
}
nextToken = resp.NextToken
for i := range resp.GroupIdentifiers {
groups = append(groups, resp.GroupIdentifiers[i])
}
if nextToken == nil {
break
}
}

// filter resp to only include those that match the hostResourceGroupSelector Name
for i := range groups {
group := groups[i]
for x := range nodeClass.Spec.HostResourceGroupSelectorTerms {
selector := nodeClass.Spec.HostResourceGroupSelectorTerms[x]
logging.FromContext(ctx).
With("group", group).
With("selector", selector).
Debugf("checking for host resource group match")
if *group.GroupName == selector.Name {
match := &v1beta1.HostResourceGroup{ARN: *group.GroupArn, Name: *group.GroupName}
logging.FromContext(ctx).
With("Matched hrg", match).
Debugf("discovered host resource group configuration")

return match, nil
}
}
}
logging.FromContext(ctx).
With("groups", groups).
Debugf("No hrg matched")

// No matching groups
return nil, nil
}
3 changes: 3 additions & 0 deletions pkg/utils/nodetemplate/nodetemplate.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ func New(nodeClass *v1beta1.EC2NodeClass) *v1alpha1.AWSNodeTemplate {
}

func NewHostResourceGroups(hrg *v1beta1.HostResourceGroup) []string {
if hrg == nil {
return nil
}
return []string{hrg.ARN}
}

Expand Down

0 comments on commit d033501

Please sign in to comment.