Skip to content

Commit

Permalink
Add global config parsing instead of context injection
Browse files Browse the repository at this point in the history
  • Loading branch information
jonathan-innis committed Apr 9, 2024
1 parent aee00fd commit 98effff
Show file tree
Hide file tree
Showing 37 changed files with 358 additions and 551 deletions.
2 changes: 2 additions & 0 deletions cmd/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (

"github.com/aws/karpenter-provider-aws/pkg/cloudprovider"
"github.com/aws/karpenter-provider-aws/pkg/controllers"
"github.com/aws/karpenter-provider-aws/pkg/global"
"github.com/aws/karpenter-provider-aws/pkg/operator"
"github.com/aws/karpenter-provider-aws/pkg/webhooks"

Expand All @@ -30,6 +31,7 @@ import (
)

func main() {
lo.Must0(global.Initialize())
ctx, op := operator.NewOperator(coreoperator.NewOperator())
awsCloudProvider := cloudprovider.New(
op.InstanceTypesProvider,
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ require (
sigs.k8s.io/yaml v1.4.0
)

replace sigs.k8s.io/karpenter => github.com/jonathan-innis/karpenter v0.0.4-0.20240409062722-d59d53db306d

require (
contrib.go.opencensus.io/exporter/ocagent v0.7.1-0.20200907061046-05415f1de66d // indirect
contrib.go.opencensus.io/exporter/prometheus v0.4.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9Y
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/jonathan-innis/karpenter v0.0.4-0.20240409062722-d59d53db306d h1:iHQZtrsr3PxEdOHReLaMmH4rFyjzNweuHx+3ksCAdTc=
github.com/jonathan-innis/karpenter v0.0.4-0.20240409062722-d59d53db306d/go.mod h1:fieFojxOec/l0tDmFT7R+g/Y+SGQbL9VlcYO8xb3sLo=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
Expand Down Expand Up @@ -759,8 +761,6 @@ sigs.k8s.io/controller-runtime v0.17.2 h1:FwHwD1CTUemg0pW2otk7/U5/i5m2ymzvOXdbeG
sigs.k8s.io/controller-runtime v0.17.2/go.mod h1:+MngTvIQQQhfXtwfdGw/UOQ/aIaqsYywfCINOtwMO/s=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd h1:EDPBXCAspyGV4jQlpZSudPeMmr1bNJefnuqLsRAsHZo=
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd/go.mod h1:B8JuhiUyNFVKdsE8h686QcCxMaH6HrOAZj4vswFpcB0=
sigs.k8s.io/karpenter v0.35.1-0.20240404200702-545d88a836d4 h1:eb3ubw9tFiV62Gq1Ci5r5BCJ8OlKu2Mqf7h3tJIRe0U=
sigs.k8s.io/karpenter v0.35.1-0.20240404200702-545d88a836d4/go.mod h1:fieFojxOec/l0tDmFT7R+g/Y+SGQbL9VlcYO8xb3sLo=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1 h1:150L+0vs/8DA78h1u02ooW1/fFq/Lwr+sGiqlzvrtq4=
sigs.k8s.io/structured-merge-diff/v4 v4.4.1/go.mod h1:N8hJocpFajUSSeSJ9bOZ77VzejKZaXsTtZo4/u7Io08=
sigs.k8s.io/yaml v1.4.0 h1:Mk1wCc2gy/F0THH0TAp1QYyJNzRm2KCLy3o5ASXVI5E=
Expand Down
5 changes: 1 addition & 4 deletions hack/code/prices_gen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@ import (
"sigs.k8s.io/controller-runtime/pkg/reconcile"

controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/test"
)

func getAWSRegions(partition string) []string {
Expand Down Expand Up @@ -94,7 +92,6 @@ func main() {
os.Setenv("AWS_SDK_LOAD_CONFIG", "true")
os.Setenv("AWS_REGION", region)
ctx := context.Background()
ctx = options.ToContext(ctx, test.Options())
sess := session.Must(session.NewSession())
ec2 := ec22.New(sess)
src := &bytes.Buffer{}
Expand All @@ -108,7 +105,7 @@ func main() {
// record prices for each region we are interested in
for _, region := range getAWSRegions(opts.partition) {
log.Println("fetching for", region)
pricingProvider := pricing.NewDefaultProvider(ctx, pricing.NewAPI(sess, region), ec2, region)
pricingProvider := pricing.NewDefaultProvider(pricing.NewAPI(sess, region), ec2, region)
controller := controllerspricing.NewController(pricingProvider)
_, err := controller.Reconcile(ctx, reconcile.Request{NamespacedName: types.NamespacedName{}})
if err != nil {
Expand Down
10 changes: 5 additions & 5 deletions hack/docs/configuration_gen_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import (
"os"
"strings"

coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
coreglobal "sigs.k8s.io/karpenter/pkg/global"

"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/global"
)

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

fs := &coreoptions.FlagSet{
fs := &coreglobal.FlagSet{
FlagSet: flag.NewFlagSet("karpenter", flag.ContinueOnError),
}
(&coreoptions.Options{}).AddFlags(fs)
(&options.Options{}).AddFlags(fs)
coreglobal.AddFlags(fs)
global.AddFlags(fs)

envVarsBlock := "| Environment Variable | CLI Flag | Description |\n"
envVarsBlock += "|--|--|--|\n"
Expand Down
21 changes: 7 additions & 14 deletions hack/docs/instancetypes_gen_docs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"
"sigs.k8s.io/karpenter/pkg/apis/v1beta1"

"sigs.k8s.io/karpenter/pkg/cloudprovider"
coreoperator "sigs.k8s.io/karpenter/pkg/operator"
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
coretest "sigs.k8s.io/karpenter/pkg/test"
"sigs.k8s.io/karpenter/pkg/utils/resources"

awscloudprovider "github.com/aws/karpenter-provider-aws/pkg/cloudprovider"
"github.com/aws/karpenter-provider-aws/pkg/global"
"github.com/aws/karpenter-provider-aws/pkg/operator"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/test"

"sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/utils/resources"
)

// FakeManager is a manager that takes all the utilized calls from the operator setup
Expand Down Expand Up @@ -84,14 +80,11 @@ func main() {
lo.Must0(os.Setenv("AWS_SDK_LOAD_CONFIG", "true"))
lo.Must0(os.Setenv("AWS_REGION", "us-east-1"))

ctx := coreoptions.ToContext(context.Background(), coretest.Options())
ctx = options.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
}))
global.Initialize("--cluster-name", "docs-gen",
"--cluster-endpoint", "https://docs-gen.aws",
"--isolated-vpc")

ctx, op := operator.NewOperator(ctx, &coreoperator.Operator{
ctx, op := operator.NewOperator(context.Background(), &coreoperator.Operator{
Manager: &FakeManager{},
KubernetesInterface: kubernetes.NewForConfigOrDie(&rest.Config{}),
})
Expand Down
12 changes: 2 additions & 10 deletions pkg/cloudprovider/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,13 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/apis/v1beta1"
"github.com/aws/karpenter-provider-aws/pkg/cloudprovider"
"github.com/aws/karpenter-provider-aws/pkg/fake"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/test"

corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
corecloudproivder "sigs.k8s.io/karpenter/pkg/cloudprovider"
"sigs.k8s.io/karpenter/pkg/controllers/provisioning"
"sigs.k8s.io/karpenter/pkg/controllers/state"
"sigs.k8s.io/karpenter/pkg/events"
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
"sigs.k8s.io/karpenter/pkg/operator/scheme"
coretest "sigs.k8s.io/karpenter/pkg/test"

Expand Down Expand Up @@ -76,8 +74,6 @@ func TestAWS(t *testing.T) {

var _ = BeforeSuite(func() {
env = coretest.NewEnvironment(scheme.Scheme, coretest.WithCRDs(apis.CRDs...))
ctx = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options())
ctx, stop = context.WithCancel(ctx)
awsEnv = test.NewEnvironment(ctx, env)
fakeClock = clock.NewFakeClock(time.Now())
Expand All @@ -94,18 +90,14 @@ var _ = AfterSuite(func() {
})

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

cluster.Reset()
awsEnv.Reset()

awsEnv.LaunchTemplateProvider.KubeDNSIP = net.ParseIP("10.0.100.10")
awsEnv.LaunchTemplateProvider.ClusterEndpoint = "https://test-cluster"
})

var _ = AfterEach(func() {
ExpectCleanedUp(ctx, env.Client)
cluster.Reset()
awsEnv.Reset()
})

var _ = Describe("CloudProvider", func() {
Expand Down
6 changes: 3 additions & 3 deletions pkg/controllers/controllers.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"sigs.k8s.io/karpenter/pkg/cloudprovider"

controllerspricing "github.com/aws/karpenter-provider-aws/pkg/controllers/pricing"
"github.com/aws/karpenter-provider-aws/pkg/global"
"github.com/aws/karpenter-provider-aws/pkg/providers/launchtemplate"

"github.com/aws/aws-sdk-go/aws/session"
Expand All @@ -36,7 +37,6 @@ import (
nodeclaimgarbagecollection "github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclaim/garbagecollection"
nodeclaimtagging "github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclaim/tagging"
"github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclass"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/amifamily"
"github.com/aws/karpenter-provider-aws/pkg/providers/instance"
"github.com/aws/karpenter-provider-aws/pkg/providers/instanceprofile"
Expand All @@ -57,8 +57,8 @@ func NewControllers(ctx context.Context, sess *session.Session, clk clock.Clock,
nodeclaimtagging.NewController(kubeClient, instanceProvider),
controllerspricing.NewController(pricingProvider),
}
if options.FromContext(ctx).InterruptionQueue != "" {
controllers = append(controllers, interruption.NewController(kubeClient, clk, recorder, lo.Must(sqs.NewProvider(ctx, servicesqs.New(sess), options.FromContext(ctx).InterruptionQueue)), unavailableOfferings))
if global.Config.InterruptionQueue != "" {
controllers = append(controllers, interruption.NewController(kubeClient, clk, recorder, lo.Must(sqs.NewProvider(ctx, servicesqs.New(sess), global.Config.InterruptionQueue)), unavailableOfferings))
}
return controllers
}
18 changes: 6 additions & 12 deletions pkg/controllers/interruption/interruption_benchmark_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,13 @@ import (

"sigs.k8s.io/karpenter/pkg/operator/scheme"

coretest "sigs.k8s.io/karpenter/pkg/test"

awscache "github.com/aws/karpenter-provider-aws/pkg/cache"
"github.com/aws/karpenter-provider-aws/pkg/controllers/interruption"
"github.com/aws/karpenter-provider-aws/pkg/controllers/interruption/events"
"github.com/aws/karpenter-provider-aws/pkg/fake"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/providers/sqs"
"github.com/aws/karpenter-provider-aws/pkg/test"

coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
coretest "sigs.k8s.io/karpenter/pkg/test"
)

var r = rand.New(rand.NewSource(time.Now().Unix()))
Expand All @@ -79,12 +76,9 @@ 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 = coreoptions.ToContext(ctx, coretest.Options())
ctx = options.ToContext(ctx, test.Options(test.OptionsFields{
ClusterName: lo.ToPtr("karpenter-notification-benchmarking"),
IsolatedVPC: lo.ToPtr(true),
InterruptionQueue: lo.ToPtr("test-cluster"),
}))
global.Config.ClusterName = "karpenter-notification-benchmarking"
global.Config.IsolatedVPC = true
global.Config.InterruptionQueue = "test-cluster"
env = coretest.NewEnvironment(scheme.Scheme)
// Stop the coretest environment after the coretest completes
defer func() {
Expand Down Expand Up @@ -183,7 +177,7 @@ func newProviders(ctx context.Context, kubeClient client.Client) providerSet {

func (p *providerSet) makeInfrastructure(ctx context.Context) error {
if _, err := p.sqsAPI.CreateQueueWithContext(ctx, &servicesqs.CreateQueueInput{
QueueName: lo.ToPtr(options.FromContext(ctx).InterruptionQueueName),
QueueName: lo.ToPtr(global.Config.InterruptionQueueName),
Attributes: map[string]*string{
servicesqs.QueueAttributeNameMessageRetentionPeriod: aws.String("1200"), // 20 minutes for this test
},
Expand Down
2 changes: 0 additions & 2 deletions pkg/controllers/interruption/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (

corev1beta1 "sigs.k8s.io/karpenter/pkg/apis/v1beta1"
"sigs.k8s.io/karpenter/pkg/events"
coreoptions "sigs.k8s.io/karpenter/pkg/operator/options"
"sigs.k8s.io/karpenter/pkg/operator/scheme"
coretest "sigs.k8s.io/karpenter/pkg/test"

Expand Down Expand Up @@ -91,7 +90,6 @@ var _ = AfterSuite(func() {
})

var _ = BeforeEach(func() {
ctx = coreoptions.ToContext(ctx, coretest.Options())
unavailableOfferingsCache.Flush()
sqsapi.Reset()
})
Expand Down
21 changes: 10 additions & 11 deletions pkg/controllers/nodeclaim/garbagecollection/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import (
"github.com/aws/karpenter-provider-aws/pkg/cloudprovider"
"github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclaim/garbagecollection"
"github.com/aws/karpenter-provider-aws/pkg/fake"
"github.com/aws/karpenter-provider-aws/pkg/operator/options"
"github.com/aws/karpenter-provider-aws/pkg/global"
"github.com/aws/karpenter-provider-aws/pkg/test"

. "github.com/onsi/ginkgo/v2"
Expand All @@ -62,7 +62,6 @@ func TestAPIs(t *testing.T) {
}

var _ = BeforeSuite(func() {
ctx = options.ToContext(ctx, test.Options())
env = coretest.NewEnvironment(scheme.Scheme, coretest.WithCRDs(apis.CRDs...))
awsEnv = test.NewEnvironment(ctx, env)
cloudProvider = cloudprovider.New(awsEnv.InstanceTypesProvider, awsEnv.InstanceProvider, events.NewRecorder(&record.FakeRecorder{}),
Expand All @@ -74,7 +73,7 @@ var _ = AfterSuite(func() {
Expect(env.Stop()).To(Succeed(), "Failed to stop environment")
})

var _ = BeforeEach(func() {
var _ = AfterEach(func() {
awsEnv.Reset()
})

Expand Down Expand Up @@ -104,7 +103,7 @@ var _ = Describe("GarbageCollection", func() {
},
Tags: []*ec2.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName)),
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", global.Config.ClusterName)),
Value: aws.String("owned"),
},
{
Expand All @@ -117,7 +116,7 @@ var _ = Describe("GarbageCollection", func() {
},
{
Key: aws.String(corev1beta1.ManagedByAnnotationKey),
Value: aws.String(options.FromContext(ctx).ClusterName),
Value: aws.String(global.Config.ClusterName),
},
},
PrivateDnsName: aws.String(fake.PrivateDNSName()),
Expand All @@ -130,8 +129,8 @@ var _ = Describe("GarbageCollection", func() {
})
AfterEach(func() {
ExpectCleanedUp(ctx, env.Client)
awsEnv.Reset()
})

It("should delete an instance if there is no NodeClaim owner", func() {
// Launch time was 1m ago
instance.LaunchTime = aws.Time(time.Now().Add(-time.Minute))
Expand Down Expand Up @@ -172,7 +171,7 @@ var _ = Describe("GarbageCollection", func() {
},
Tags: []*ec2.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName)),
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", global.Config.ClusterName)),
Value: aws.String("owned"),
},
{
Expand All @@ -185,7 +184,7 @@ var _ = Describe("GarbageCollection", func() {
},
{
Key: aws.String(corev1beta1.ManagedByAnnotationKey),
Value: aws.String(options.FromContext(ctx).ClusterName),
Value: aws.String(global.Config.ClusterName),
},
},
PrivateDnsName: aws.String(fake.PrivateDNSName()),
Expand Down Expand Up @@ -230,7 +229,7 @@ var _ = Describe("GarbageCollection", func() {
},
Tags: []*ec2.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName)),
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", global.Config.ClusterName)),
Value: aws.String("owned"),
},
},
Expand Down Expand Up @@ -339,7 +338,7 @@ var _ = Describe("GarbageCollection", func() {
},
Tags: []*ec2.Tag{
{
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", options.FromContext(ctx).ClusterName)),
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", global.Config.ClusterName)),
Value: aws.String("owned"),
},
{
Expand All @@ -348,7 +347,7 @@ var _ = Describe("GarbageCollection", func() {
},
{
Key: aws.String(corev1beta1.ManagedByAnnotationKey),
Value: aws.String(options.FromContext(ctx).ClusterName),
Value: aws.String(global.Config.ClusterName),
},
},
PrivateDnsName: aws.String(fake.PrivateDNSName()),
Expand Down
Loading

0 comments on commit 98effff

Please sign in to comment.