Skip to content

Commit

Permalink
add options
Browse files Browse the repository at this point in the history
  • Loading branch information
jmdeal committed Oct 16, 2023
1 parent 3d16073 commit 2f7fedf
Show file tree
Hide file tree
Showing 69 changed files with 768 additions and 474 deletions.
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,6 @@ github.com/avast/retry-go v3.0.0+incompatible h1:4SOWQ7Qs+oroOTQOYnAHqelpCO0biHS
github.com/avast/retry-go v3.0.0+incompatible/go.mod h1:XtSnn+n/sHqQIpZ10K1qAevBhOOCWBLXXy3hyiqqBrY=
github.com/aws/aws-sdk-go v1.45.25 h1:c4fLlh5sLdK2DCRTY1z0hyuJZU4ygxX8m1FswL6/nF4=
github.com/aws/aws-sdk-go v1.45.25/go.mod h1:aVsgQcEevwlmQ7qHE9I3h+dtQgpqhFB+i8Phjh7fkwI=
github.com/aws/karpenter-core v0.31.1-0.20231013203304-4239902b18b9 h1:j0iZuhoAKHrt0oqfSiKDqvHMnV/t45wi0loG1lEqdUw=
github.com/aws/karpenter-core v0.31.1-0.20231013203304-4239902b18b9/go.mod h1:rb3kp/3cj38tACF6udfpmIvKoQMwirSVoHNlrd66LyE=
github.com/aws/karpenter-core v0.31.1-0.20231016061404-e22c2eb518be h1:EOrv6wPMVSck/XyHEBtfG2gaH5ovDHTRw1mcswB/7xs=
github.com/aws/karpenter-core v0.31.1-0.20231016061404-e22c2eb518be/go.mod h1:rb3kp/3cj38tACF6udfpmIvKoQMwirSVoHNlrd66LyE=
github.com/aws/karpenter/tools/kompat v0.0.0-20231010173459-62c25a3ea85c h1:oXWwIttmjYLbBKhLazG21aQvpJ3NOOr8IXhCJ/p6e/M=
Expand Down
4 changes: 2 additions & 2 deletions hack/code/prices_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/reconcile"

"github.com/aws/karpenter/pkg/apis/settings"
awsoptions "github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/providers/pricing"
"github.com/aws/karpenter/pkg/test"
)
Expand Down Expand Up @@ -93,7 +93,7 @@ func main() {
os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
os.Setenv("AWS_REGION", region)
ctx := context.Background()
ctx = settings.ToContext(ctx, test.Settings())
ctx = awsoptions.ToContext(ctx, test.Options())
sess := session.Must(session.NewSession())
ec2 := ec22.New(sess)
src := &bytes.Buffer{}
Expand Down
9 changes: 6 additions & 3 deletions hack/docs/configuration_gen_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ import (
"os"
"strings"

"github.com/aws/karpenter-core/pkg/operator/options"
coreoptions "github.com/aws/karpenter-core/pkg/operator/options"
"github.com/aws/karpenter/pkg/operator/options"
)

func main() {
Expand All @@ -48,11 +49,13 @@ func main() {
topDoc := fmt.Sprintf("%s%s\n\n", startDocSections[0], genStart)
bottomDoc := fmt.Sprintf("\n%s%s", genEnd, endDocSections[1])

opts := options.New()
fs := flag.NewFlagSet("karpenter", flag.ContinueOnError)
(&coreoptions.Options{}).AddFlags(fs)
(&options.Options{}).AddFlags(fs)

envVarsBlock := "| Environment Variable | CLI Flag | Description |\n"
envVarsBlock += "|--|--|--|\n"
opts.FlagSet.VisitAll(func(f *flag.Flag) {
fs.VisitAll(func(f *flag.Flag) {
if f.DefValue == "" {
envVarsBlock += fmt.Sprintf("| %s | %s | %s|\n", strings.ReplaceAll(strings.ToUpper(f.Name), "-", "_"), "\\-\\-"+f.Name, f.Usage)
} else {
Expand Down
8 changes: 4 additions & 4 deletions hack/docs/instancetypes_gen_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client/fake"
"sigs.k8s.io/controller-runtime/pkg/manager"

coresettings "github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
coreoperator "github.com/aws/karpenter-core/pkg/operator"
coreoptions "github.com/aws/karpenter-core/pkg/operator/options"
coretest "github.com/aws/karpenter-core/pkg/test"
nodepoolutil "github.com/aws/karpenter-core/pkg/utils/nodepool"
"github.com/aws/karpenter/pkg/apis/settings"
awscloudprovider "github.com/aws/karpenter/pkg/cloudprovider"
"github.com/aws/karpenter/pkg/operator"
awsoptions "github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/test"

"github.com/aws/karpenter-core/pkg/cloudprovider"
Expand Down Expand Up @@ -77,8 +77,8 @@ func main() {
lo.Must0(os.Setenv("AWS_SDK_LOAD_CONFIG", "true"))
lo.Must0(os.Setenv("AWS_REGION", "us-east-1"))

ctx := coresettings.ToContext(context.Background(), coretest.Settings())
ctx = settings.ToContext(ctx, test.Settings(test.SettingOptions{
ctx := coreoptions.ToContext(context.Background(), coretest.Options())
ctx = awsoptions.ToContext(ctx, test.Options(test.OptionsFields{
ClusterName: lo.ToPtr("docs-gen"),
ClusterEndpoint: lo.ToPtr("https://docs-gen.aws"),
IsolatedVPC: lo.ToPtr(true), // disable pricing lookup
Expand Down
9 changes: 7 additions & 2 deletions pkg/apis/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

v1 "k8s.io/api/core/v1"
"knative.dev/pkg/configmap"

coresettings "github.com/aws/karpenter-core/pkg/apis/settings"
)

type settingsKeyType struct{}
Expand Down Expand Up @@ -92,15 +94,18 @@ func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context,
return ToContext(ctx, s), nil
}

func (*Settings) FromContext(ctx context.Context) coresettings.Injectable {
return FromContext(ctx)
}

func ToContext(ctx context.Context, s *Settings) context.Context {
return context.WithValue(ctx, ContextKey, s)
}

func FromContext(ctx context.Context) *Settings {
data := ctx.Value(ContextKey)
if data == nil {
// This is developer error if this happens, so we should panic
panic("settings doesn't exist in context")
return nil
}
return data.(*Settings)
}
Expand Down
12 changes: 6 additions & 6 deletions pkg/cloudprovider/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,22 @@ import (

"github.com/aws/karpenter/pkg/apis"
"github.com/aws/karpenter/pkg/apis/settings"
"github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/test"

"github.com/aws/karpenter/pkg/cloudprovider"

coresettings "github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/controllers/provisioning"
"github.com/aws/karpenter-core/pkg/controllers/state"
"github.com/aws/karpenter-core/pkg/events"
"github.com/aws/karpenter-core/pkg/operator/options"
coreoptions "github.com/aws/karpenter-core/pkg/operator/options"
"github.com/aws/karpenter-core/pkg/operator/scheme"
coretest "github.com/aws/karpenter-core/pkg/test"
. "github.com/aws/karpenter-core/pkg/test/expectations"
)

var ctx context.Context
var stop context.CancelFunc
var opts *options.Options
var env *coretest.Environment
var awsEnv *test.Environment
var prov *provisioning.Provisioner
Expand All @@ -63,7 +62,8 @@ func TestAWS(t *testing.T) {

var _ = BeforeSuite(func() {
env = coretest.NewEnvironment(scheme.Scheme, coretest.WithCRDs(apis.CRDs...))
ctx = coresettings.ToContext(ctx, coretest.Settings())
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options())
ctx = settings.ToContext(ctx, test.Settings())
ctx, stop = context.WithCancel(ctx)
awsEnv = test.NewEnvironment(ctx, env)
Expand All @@ -81,8 +81,8 @@ var _ = AfterSuite(func() {
})

var _ = BeforeEach(func() {
ctx = options.ToContext(ctx, opts)
ctx = coresettings.ToContext(ctx, coretest.Settings())
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options())
ctx = settings.ToContext(ctx, test.Settings())

cluster.Reset()
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/events"
"github.com/aws/karpenter/pkg/apis/settings"
"github.com/aws/karpenter/pkg/cache"
"github.com/aws/karpenter/pkg/cloudprovider"
"github.com/aws/karpenter/pkg/controllers/interruption"
nodeclaimgarbagecollection "github.com/aws/karpenter/pkg/controllers/nodeclaim/garbagecollection"
nodeclaimlink "github.com/aws/karpenter/pkg/controllers/nodeclaim/link"
"github.com/aws/karpenter/pkg/controllers/nodeclass"
"github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/providers/amifamily"
"github.com/aws/karpenter/pkg/providers/instanceprofile"
"github.com/aws/karpenter/pkg/providers/pricing"
Expand All @@ -54,10 +54,10 @@ func NewControllers(ctx context.Context, sess *session.Session, clk clock.Clock,
linkController,
nodeclaimgarbagecollection.NewController(kubeClient, cloudProvider, linkController),
}
if settings.FromContext(ctx).InterruptionQueueName != "" {
if options.FromContext(ctx).InterruptionQueueName != "" {
controllers = append(controllers, interruption.NewController(kubeClient, clk, recorder, interruption.NewSQSProvider(sqs.New(sess)), unavailableOfferings))
}
if settings.FromContext(ctx).IsolatedVPC {
if options.FromContext(ctx).IsolatedVPC {
logging.FromContext(ctx).Infof("assuming isolated VPC, pricing information will not be updated")
} else {
controllers = append(controllers, pricing.NewController(pricingProvider))
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/interruption/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ import (

"github.com/aws/karpenter-core/pkg/apis/v1beta1"
"github.com/aws/karpenter-core/pkg/utils/pretty"
"github.com/aws/karpenter/pkg/apis/settings"
"github.com/aws/karpenter/pkg/apis/v1alpha1"
"github.com/aws/karpenter/pkg/cache"
interruptionevents "github.com/aws/karpenter/pkg/controllers/interruption/events"
"github.com/aws/karpenter/pkg/controllers/interruption/messages"
"github.com/aws/karpenter/pkg/controllers/interruption/messages/statechange"
"github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/utils"

"github.com/aws/karpenter-core/pkg/events"
Expand Down Expand Up @@ -80,8 +80,8 @@ func NewController(kubeClient client.Client, clk clock.Clock, recorder events.Re
}

func (c *Controller) Reconcile(ctx context.Context, _ reconcile.Request) (reconcile.Result, error) {
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).With("queue", settings.FromContext(ctx).InterruptionQueueName))
if c.cm.HasChanged(settings.FromContext(ctx).InterruptionQueueName, nil) {
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).With("queue", options.FromContext(ctx).InterruptionQueueName))
if c.cm.HasChanged(options.FromContext(ctx).InterruptionQueueName, nil) {
logging.FromContext(ctx).Debugf("watching interruption queue")
}
sqsMessages, err := c.sqsProvider.GetSQSMessages(ctx)
Expand Down
10 changes: 6 additions & 4 deletions pkg/controllers/interruption/interruption_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,15 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/aws/karpenter-core/pkg/operator/scheme"
"github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/apis/settings"
awscache "github.com/aws/karpenter/pkg/cache"
"github.com/aws/karpenter/pkg/controllers/interruption"
"github.com/aws/karpenter/pkg/controllers/interruption/events"
"github.com/aws/karpenter/pkg/fake"
"github.com/aws/karpenter/pkg/test"

coresettings "github.com/aws/karpenter-core/pkg/apis/settings"
coreoptions "github.com/aws/karpenter-core/pkg/operator/options"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
coretest "github.com/aws/karpenter-core/pkg/test"
)
Expand All @@ -77,12 +78,13 @@ func BenchmarkNotification100(b *testing.B) {
func benchmarkNotificationController(b *testing.B, messageCount int) {
ctx = logging.WithLogger(ctx, logging.FromContext(ctx).With("message-count", messageCount))
fakeClock = &clock.FakeClock{}
ctx = coresettings.ToContext(ctx, coretest.Settings())
ctx = settings.ToContext(ctx, test.Settings(test.SettingOptions{
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options(test.OptionsFields{
ClusterName: lo.ToPtr("karpenter-notification-benchmarking"),
IsolatedVPC: lo.ToPtr(true),
InterruptionQueueName: lo.ToPtr("test-cluster"),
}))
ctx = settings.ToContext(ctx, test.Settings())
env = coretest.NewEnvironment(scheme.Scheme)
// Stop the coretest environment after the coretest completes
defer func() {
Expand Down Expand Up @@ -181,7 +183,7 @@ func newProviders(kubeClient client.Client) providerSet {

func (p *providerSet) makeInfrastructure(ctx context.Context) error {
if _, err := p.sqsAPI.CreateQueueWithContext(ctx, &sqs.CreateQueueInput{
QueueName: lo.ToPtr(settings.FromContext(ctx).InterruptionQueueName),
QueueName: lo.ToPtr(options.FromContext(ctx).InterruptionQueueName),
Attributes: map[string]*string{
sqs.QueueAttributeNameMessageRetentionPeriod: aws.String("1200"), // 20 minutes for this test
},
Expand Down
8 changes: 4 additions & 4 deletions pkg/controllers/interruption/sqs.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ import (
"github.com/samber/lo"

"github.com/aws/karpenter-core/pkg/utils/atomic"
"github.com/aws/karpenter/pkg/apis/settings"
awserrors "github.com/aws/karpenter/pkg/errors"
"github.com/aws/karpenter/pkg/operator/options"
)

type SQSProvider struct {
Expand All @@ -43,7 +43,7 @@ func NewSQSProvider(client sqsiface.SQSAPI) *SQSProvider {
}
provider.queueURL.Resolve = func(ctx context.Context) (string, error) {
input := &sqs.GetQueueUrlInput{
QueueName: aws.String(settings.FromContext(ctx).InterruptionQueueName),
QueueName: aws.String(options.FromContext(ctx).InterruptionQueueName),
}
ret, err := provider.client.GetQueueUrlWithContext(ctx, input)
if err != nil {
Expand All @@ -66,12 +66,12 @@ func (s *SQSProvider) QueueExists(ctx context.Context) (bool, error) {
}

func (s *SQSProvider) DiscoverQueueURL(ctx context.Context) (string, error) {
if settings.FromContext(ctx).InterruptionQueueName != lo.FromPtr(s.queueName.Load()) {
if options.FromContext(ctx).InterruptionQueueName != lo.FromPtr(s.queueName.Load()) {
res, err := s.queueURL.TryGet(ctx, atomic.IgnoreCacheOption)
if err != nil {
return res, err
}
s.queueName.Store(lo.ToPtr(settings.FromContext(ctx).InterruptionQueueName))
s.queueName.Store(lo.ToPtr(options.FromContext(ctx).InterruptionQueueName))
return res, nil
}
return s.queueURL.TryGet(ctx)
Expand Down
8 changes: 5 additions & 3 deletions pkg/controllers/interruption/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@ import (
_ "knative.dev/pkg/system/testing"
"sigs.k8s.io/controller-runtime/pkg/client"

coresettings "github.com/aws/karpenter-core/pkg/apis/settings"
"github.com/aws/karpenter-core/pkg/apis/v1alpha5"
corev1beta1 "github.com/aws/karpenter-core/pkg/apis/v1beta1"
"github.com/aws/karpenter-core/pkg/events"
coreoptions "github.com/aws/karpenter-core/pkg/operator/options"
"github.com/aws/karpenter-core/pkg/operator/scheme"
coretest "github.com/aws/karpenter-core/pkg/test"
. "github.com/aws/karpenter-core/pkg/test/expectations"
Expand All @@ -53,6 +53,7 @@ import (
"github.com/aws/karpenter/pkg/controllers/interruption/messages/spotinterruption"
"github.com/aws/karpenter/pkg/controllers/interruption/messages/statechange"
"github.com/aws/karpenter/pkg/fake"
"github.com/aws/karpenter/pkg/operator/options"
"github.com/aws/karpenter/pkg/test"
"github.com/aws/karpenter/pkg/utils"
)
Expand Down Expand Up @@ -91,10 +92,11 @@ var _ = AfterSuite(func() {
})

var _ = BeforeEach(func() {
ctx = coresettings.ToContext(ctx, coretest.Settings())
ctx = settings.ToContext(ctx, test.Settings(test.SettingOptions{
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options(test.OptionsFields{
InterruptionQueueName: lo.ToPtr("test-cluster"),
}))
ctx = settings.ToContext(ctx, test.Settings())
unavailableOfferingsCache.Flush()
sqsapi.Reset()
sqsProvider.Reset()
Expand Down
Loading

0 comments on commit 2f7fedf

Please sign in to comment.