Skip to content

Commit

Permalink
use embedded context for options
Browse files Browse the repository at this point in the history
  • Loading branch information
alec-rabold committed Nov 27, 2024
1 parent 36c0872 commit ae3cae5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 31 deletions.
31 changes: 8 additions & 23 deletions kwok/cloudprovider/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package kwok

import (
"context"
_ "embed"
"encoding/json"
"fmt"
Expand All @@ -28,6 +29,7 @@ import (
"k8s.io/apimachinery/pkg/api/resource"

"sigs.k8s.io/karpenter/kwok/apis/v1alpha1"
"sigs.k8s.io/karpenter/kwok/options"
v1 "sigs.k8s.io/karpenter/pkg/apis/v1"
"sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/scheduling"
Expand Down Expand Up @@ -64,30 +66,20 @@ type InstanceTypeOptions struct {
//go:embed instance_types.json
var defaultRawInstanceTypes []byte

type InstanceTypesOptions struct {
CustomInstanceTypesFilePath *string
}

type InstanceTypesOption func(*InstanceTypesOptions)

// ConstructInstanceTypes create many instance types based on the embedded instance type data
func ConstructInstanceTypes(opts ...InstanceTypesOption) ([]*cloudprovider.InstanceType, error) {
o := &InstanceTypesOptions{}
for _, opt := range opts {
opt(o)
}
func ConstructInstanceTypes(ctx context.Context) ([]*cloudprovider.InstanceType, error) {
var instanceTypes []*cloudprovider.InstanceType
var instanceTypeOptions []InstanceTypeOptions

rawInstanceTypes := defaultRawInstanceTypes
if o.CustomInstanceTypesFilePath != nil {
customRawInstanceTypes, err := os.ReadFile(*o.CustomInstanceTypesFilePath)
if customInstanceTypes := options.FromContext(ctx).InstanceTypesFilePath; customInstanceTypes != "" {
customRawInstanceTypes, err := os.ReadFile(customInstanceTypes)
if err != nil {
return nil, fmt.Errorf("could not read custom instance types file: %w", err)
}
rawInstanceTypes = customRawInstanceTypes
}

var instanceTypes []*cloudprovider.InstanceType
var instanceTypeOptions []InstanceTypeOptions

if err := json.Unmarshal(rawInstanceTypes, &instanceTypeOptions); err != nil {
return nil, fmt.Errorf("could not parse JSON data: %w", err)
}
Expand All @@ -99,13 +91,6 @@ func ConstructInstanceTypes(opts ...InstanceTypesOption) ([]*cloudprovider.Insta
return instanceTypes, nil
}

// WithInstanceTypesFromFile constructs instance types from a custom file.
func WithInstanceTypesFromFile(path string) InstanceTypesOption {
return func(o *InstanceTypesOptions) {
o.CustomInstanceTypesFilePath = &path
}
}

// parseSizeFromType will attempt to discover the instance size if it matches a special AWS format.
// If not, fall back to the cpu value. This works for both Azure and GCP (and the "generic" instances
// generated by tools/gen_instances.go)
Expand Down
9 changes: 1 addition & 8 deletions kwok/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,7 @@ import (

func main() {
ctx, op := operator.NewOperator()

options := kwok.GetOptionsOrDie()

var instanceTypesOptions []kwok.InstanceTypesOption
if options.InstanceTypesFilePath != "" {
instanceTypesOptions = append(instanceTypesOptions, kwok.WithInstanceTypesFromFile(options.InstanceTypesFilePath))
}
instanceTypes, err := kwok.ConstructInstanceTypes(instanceTypesOptions...)
instanceTypes, err := kwok.ConstructInstanceTypes(ctx)
if err != nil {
log.FromContext(ctx).Error(err, "failed constructing instance types")
}
Expand Down

0 comments on commit ae3cae5

Please sign in to comment.