-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: Integrate AWS Application Recovery Controller Zonal Shift with Karpenter #9042
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,6 +25,7 @@ import ( | |
| awsmiddleware "github.com/aws/aws-sdk-go-v2/aws/middleware" | ||
| "github.com/aws/aws-sdk-go-v2/config" | ||
| "github.com/aws/aws-sdk-go-v2/feature/ec2/imds" | ||
| "github.com/aws/aws-sdk-go-v2/service/arczonalshift" | ||
| "github.com/aws/aws-sdk-go-v2/service/ec2" | ||
| "github.com/aws/aws-sdk-go-v2/service/eks" | ||
| "github.com/aws/aws-sdk-go-v2/service/iam" | ||
|
|
@@ -33,6 +34,8 @@ import ( | |
| "github.com/awslabs/operatorpkg/option" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
|
|
||
| arczonalshiftProvider "github.com/aws/karpenter-provider-aws/pkg/providers/arczonalshift" | ||
|
|
||
| "github.com/aws/smithy-go" | ||
| "github.com/patrickmn/go-cache" | ||
| "github.com/samber/lo" | ||
|
|
@@ -94,6 +97,7 @@ type Operator struct { | |
| SSMProvider ssmp.Provider | ||
| CapacityReservationProvider capacityreservation.Provider | ||
| EC2API *kwokec2.Client | ||
| ZonalShiftProvider arczonalshiftProvider.Provider | ||
| } | ||
|
|
||
| func NewOperator(ctx context.Context, operator *operator.Operator) (context.Context, *Operator) { | ||
|
|
@@ -118,6 +122,25 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont | |
| } else { | ||
| log.FromContext(ctx).WithValues("kube-dns-ip", kubeDNSIP).V(1).Info("discovered kube dns") | ||
| } | ||
| var zsProvider arczonalshiftProvider.Provider | ||
| if options.FromContext(ctx).EnableZonalShift { | ||
| input := eks.DescribeClusterInput{Name: &options.FromContext(ctx).ClusterName} | ||
| output, _ := eksapi.DescribeCluster(ctx, &input) | ||
| clusterArn := output.Cluster.Arn | ||
| arczonalshiftAPI := arczonalshift.NewFromConfig(cfg) | ||
| inputGMR := arczonalshift.GetManagedResourceInput{ResourceIdentifier: clusterArn} | ||
| _, getManagedResourceErr := arczonalshiftAPI.GetManagedResource(ctx, &inputGMR) | ||
| if getManagedResourceErr != nil { | ||
| // Resource is not found/registered in Zonal Shift. Log a message and use the NoopProvider so we don't block starting up. | ||
| log.FromContext(ctx).WithValues("Cluster", clusterArn).V(1).Info("Cluster not found in Zonal Shift") | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Error not Info |
||
| zsProvider = arczonalshiftProvider.NewNoopProvider() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should probably panic, I don't think we should fail with just a log |
||
| } else { | ||
| azMap := GetAvailablityZoneMapping(ctx, ec2api) | ||
| zsProvider = arczonalshiftProvider.NewProvider(arczonalshiftAPI, operator.Clock, *clusterArn, azMap) | ||
| } | ||
| } else { | ||
| zsProvider = arczonalshiftProvider.NewNoopProvider() | ||
| } | ||
| unavailableOfferingsCache := awscache.NewUnavailableOfferings() | ||
| ssmCache := cache.New(awscache.SSMCacheTTL, awscache.DefaultCleanupInterval) | ||
| validationCache := cache.New(awscache.ValidationTTL, awscache.DefaultCleanupInterval) | ||
|
|
@@ -175,6 +198,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont | |
| capacityReservationProvider, | ||
| unavailableOfferingsCache, | ||
| instancetype.NewDefaultResolver(cfg.Region), | ||
| zsProvider, | ||
| ) | ||
| // Ensure we're able to hydrate instance types before starting any reliant controllers. | ||
| // Instance type updates are hydrated asynchronously after this by controllers. | ||
|
|
@@ -216,6 +240,7 @@ func NewOperator(ctx context.Context, operator *operator.Operator) (context.Cont | |
| SSMProvider: ssmProvider, | ||
| CapacityReservationProvider: capacityReservationProvider, | ||
| EC2API: ec2api, | ||
| ZonalShiftProvider: zsProvider, | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -311,3 +336,12 @@ func SetupIndexers(ctx context.Context, mgr manager.Manager) { | |
| return []string{id} | ||
| }), "failed to setup node instanceID indexer") | ||
| } | ||
|
|
||
| func GetAvailablityZoneMapping(ctx context.Context, ec2Api sdk.EC2API) map[string]string { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't need to exist, we have the mapping from subnets already, more below |
||
| azMap := make(map[string]string) | ||
| output, _ := ec2Api.DescribeAvailabilityZones(ctx, &ec2.DescribeAvailabilityZonesInput{}) | ||
| for _, az := range output.AvailabilityZones { | ||
| azMap[*az.ZoneName] = *az.ZoneId | ||
| } | ||
| return azMap | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| /* | ||
| Licensed under the Apache License, Version 2.0 (the "License"); | ||
| you may not use this file except in compliance with the License. | ||
| You may obtain a copy of the License at | ||
|
|
||
| http://www.apache.org/licenses/LICENSE-2.0 | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software | ||
| distributed under the License is distributed on an "AS IS" BASIS, | ||
| WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
| */ | ||
|
|
||
| package arczonalshift | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/awslabs/operatorpkg/reconciler" | ||
| "github.com/awslabs/operatorpkg/singleton" | ||
| controllerruntime "sigs.k8s.io/controller-runtime" | ||
| "sigs.k8s.io/controller-runtime/pkg/manager" | ||
| "sigs.k8s.io/karpenter/pkg/operator/injection" | ||
|
|
||
| "github.com/aws/karpenter-provider-aws/pkg/providers/arczonalshift" | ||
| ) | ||
|
|
||
| type Controller struct { | ||
| arczonalshiftProvider arczonalshift.Provider | ||
| } | ||
|
|
||
| func NewController( | ||
| arczonalshiftProvider arczonalshift.Provider, | ||
| ) *Controller { | ||
| return &Controller{ | ||
| arczonalshiftProvider: arczonalshiftProvider, | ||
| } | ||
| } | ||
|
|
||
| func (c *Controller) Reconcile(ctx context.Context) (reconciler.Result, error) { | ||
| ctx = injection.WithControllerName(ctx, "zonalshift") | ||
| err := c.arczonalshiftProvider.UpdateZonalShifts(ctx) | ||
| if err != nil { | ||
| return reconciler.Result{}, fmt.Errorf("updating zonal shifts: %w", err) | ||
| } | ||
|
|
||
| return reconciler.Result{RequeueAfter: 30 * time.Second}, nil | ||
| } | ||
|
|
||
| func (c *Controller) Register(_ context.Context, m manager.Manager) error { | ||
| return controllerruntime.NewControllerManagedBy(m).Named("zonalshift").WatchesRawSource(singleton.Source()).Complete(singleton.AsReconciler(c)) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: you can put these . calls on new lines |
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't use camel case in imports