Skip to content

Commit

Permalink
desparately trying to figure out how to use protoc
Browse files Browse the repository at this point in the history
  • Loading branch information
BenHinthorne committed Jun 14, 2024
1 parent bf8049c commit 841717b
Show file tree
Hide file tree
Showing 8 changed files with 287 additions and 249 deletions.
2 changes: 2 additions & 0 deletions cluster-autoscaler/config/autoscaling_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ type AutoscalingOptions struct {
GRPCExpanderCert string
// GRPCExpanderURL is the url of the gRPC server when using the gRPC expander
GRPCExpanderURL string
// IncludeSimilarNodegroupsInExpanderOptions is whether CA will include similar nodegroups in it's request to the gRPC expander
IncludeSimilarNodegroupsInExpanderOptions bool
// IgnoreMirrorPodsUtilization is whether CA will ignore Mirror pods when calculating resource utilization for scaling down
IgnoreMirrorPodsUtilization bool
// MaxGracefulTerminationSec is maximum number of seconds scale down waits for pods to terminate before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func (o *ScaleUpOrchestrator) ScaleUp(
}

// Pick some expansion option.
// We have to be aware of the flag here?
bestOption := o.autoscalingContext.ExpanderStrategy.BestOption(options, nodeInfos)
if bestOption == nil || bestOption.NodeCount <= 0 {
return &status.ScaleUpStatus{
Expand Down
26 changes: 20 additions & 6 deletions cluster-autoscaler/expander/grpcplugin/grpc_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const (
)

type grpcclientstrategy struct {
grpcClient protos.ExpanderClient
grpcClient protos.ExpanderClient
includeSimilarNodegroups bool
}

// NewFilter returns an expansion filter that creates a gRPC client, and calls out to a gRPC server
Expand Down Expand Up @@ -79,7 +80,7 @@ func (g *grpcclientstrategy) BestOptions(expansionOptions []expander.Option, nod
}

// Transform inputs to gRPC inputs
grpcOptionsSlice, nodeGroupIDOptionMap := populateOptionsForGRPC(expansionOptions)
grpcOptionsSlice, nodeGroupIDOptionMap := populateOptionsForGRPC(expansionOptions, false)
grpcNodeMap := populateNodeInfoForGRPC(nodeInfo)

// call gRPC server to get BestOption
Expand All @@ -106,16 +107,29 @@ func (g *grpcclientstrategy) BestOptions(expansionOptions []expander.Option, nod
}

// populateOptionsForGRPC creates a map of nodegroup ID and options, as well as a slice of Options objects for the gRPC call
func populateOptionsForGRPC(expansionOptions []expander.Option) ([]*protos.Option, map[string]expander.Option) {
func populateOptionsForGRPC(expansionOptions []expander.Option, includeSimilarNodegroups bool) ([]*protos.Option, map[string]expander.Option) {
grpcOptionsSlice := []*protos.Option{}
nodeGroupIDOptionMap := make(map[string]expander.Option)
for _, option := range expansionOptions {
similarNodegroupIds := []string{}
nodeGroupIDOptionMap[option.NodeGroup.Id()] = option
grpcOptionsSlice = append(grpcOptionsSlice, newOptionMessage(option.NodeGroup.Id(), int32(option.NodeCount), option.Debug, option.Pods))
if includeSimilarNodegroups {
similarNodegroupIds = getSimilarNodegroupIds(option)
}

grpcOptionsSlice = append(grpcOptionsSlice, newOptionMessage(option.NodeGroup.Id(), int32(option.NodeCount), option.Debug, option.Pods, similarNodegroupIds))
}
return grpcOptionsSlice, nodeGroupIDOptionMap
}

func getSimilarNodegroupIds(option expander.Option) []string {
var similarNodegroupIds []string
for _, sng := range option.SimilarNodeGroups {
similarNodegroupIds = append(similarNodegroupIds, sng.Id())
}
return similarNodegroupIds
}

// populateNodeInfoForGRPC looks at the corresponding v1.Node object per NodeInfo object, and populates the grpcNodeInfoMap with these to pass over grpc
func populateNodeInfoForGRPC(nodeInfos map[string]*schedulerframework.NodeInfo) map[string]*v1.Node {
grpcNodeInfoMap := make(map[string]*v1.Node)
Expand All @@ -142,6 +156,6 @@ func transformAndSanitizeOptionsFromGRPC(bestOptionsResponseOptions []*protos.Op
return options
}

func newOptionMessage(nodeGroupId string, nodeCount int32, debug string, pods []*v1.Pod) *protos.Option {
return &protos.Option{NodeGroupId: nodeGroupId, NodeCount: nodeCount, Debug: debug, Pod: pods}
func newOptionMessage(nodeGroupId string, nodeCount int32, debug string, pods []*v1.Pod, similarNodegroupIds []string) *protos.Option {
return &protos.Option{NodeGroupId: nodeGroupId, NodeCount: nodeCount, Debug: debug, Pod: pods, SimilarNodegroupIds: similarNodegroupIds}
}
8 changes: 4 additions & 4 deletions cluster-autoscaler/expander/grpcplugin/grpc_client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ func TestPopulateOptionsForGrpc(t *testing.T) {
},
}
for _, tc := range testCases {
grpcOptionsSlice, nodeGroupIDOptionMap := populateOptionsForGRPC(tc.opts)
grpcOptionsSlice, nodeGroupIDOptionMap := populateOptionsForGRPC(tc.opts, false)
assert.Equal(t, tc.expectedOpts, grpcOptionsSlice)
assert.Equal(t, tc.expectedMap, nodeGroupIDOptionMap)
}
Expand Down Expand Up @@ -176,7 +176,7 @@ func TestBestOptionsValid(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockClient := mocks.NewMockExpanderClient(ctrl)
g := &grpcclientstrategy{mockClient}
g := &grpcclientstrategy{grpcClient: mockClient}

nodeInfos := makeFakeNodeInfos()
grpcNodeInfoMap := make(map[string]*v1.Node)
Expand All @@ -202,7 +202,7 @@ func TestBestOptionsErrors(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
mockClient := mocks.NewMockExpanderClient(ctrl)
g := grpcclientstrategy{mockClient}
g := grpcclientstrategy{grpcClient: mockClient}

badProtosOption := protos.Option{
NodeGroupId: "badID",
Expand All @@ -220,7 +220,7 @@ func TestBestOptionsErrors(t *testing.T) {
}{
{
desc: "Bad gRPC client config",
client: grpcclientstrategy{nil},
client: grpcclientstrategy{grpcClient: nil},
nodeInfo: makeFakeNodeInfos(),
mockResponse: protos.BestOptionsResponse{},
errResponse: nil,
Expand Down
Loading

0 comments on commit 841717b

Please sign in to comment.