-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
146 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -49,6 +49,7 @@ func main() { | |
ctx, | ||
op.Manager, | ||
op.Config, | ||
op.EC2API, | ||
op.Clock, | ||
op.GetClient(), | ||
op.EventRecorder, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* | ||
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 status | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"fmt" | ||
"time" | ||
|
||
"sigs.k8s.io/controller-runtime/pkg/reconcile" | ||
|
||
"github.com/aws/aws-sdk-go-v2/aws" | ||
"github.com/aws/aws-sdk-go-v2/service/ec2" | ||
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types" | ||
"github.com/aws/smithy-go" | ||
|
||
v1 "github.com/aws/karpenter-provider-aws/pkg/apis/v1" | ||
sdk "github.com/aws/karpenter-provider-aws/pkg/aws" | ||
"github.com/aws/karpenter-provider-aws/pkg/utils" | ||
) | ||
|
||
type Tags struct { | ||
ec2API sdk.EC2API | ||
} | ||
|
||
func (ip *Tags) Reconcile(ctx context.Context, nodeClass *v1.EC2NodeClass) (reconcile.Result, error) { | ||
if nodeClass.Spec.Role != "" { | ||
createLaunchTemplateInput := &ec2.CreateLaunchTemplateInput{ | ||
DryRun: aws.Bool(true), | ||
TagSpecifications: []ec2types.TagSpecification{ | ||
{ResourceType: ec2types.ResourceTypeInstance, Tags: utils.MergeTags(nodeClass.Spec.Tags)}, | ||
{ResourceType: ec2types.ResourceTypeVolume, Tags: utils.MergeTags(nodeClass.Spec.Tags)}, | ||
{ResourceType: ec2types.ResourceTypeFleet, Tags: utils.MergeTags(nodeClass.Spec.Tags)}, | ||
}, | ||
} | ||
_, err := ip.ec2API.CreateLaunchTemplate(ctx, createLaunchTemplateInput) | ||
var APIErr smithy.APIError | ||
if err != nil && errors.As(err, &APIErr) && APIErr.ErrorCode() == "UnauthorizedOperation" { | ||
nodeClass.StatusConditions().SetFalse(v1.ConditionTypeTagsReady, "UnauthorizedOperation", fmt.Sprintf("role does not contain required permissions: %v", err.Error())) | ||
return reconcile.Result{}, fmt.Errorf("reconciling Tags, %w", err) | ||
} | ||
} | ||
nodeClass.StatusConditions().SetTrue(v1.ConditionTypeTagsReady) | ||
return reconcile.Result{RequeueAfter: time.Minute}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
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 status_test | ||
|
||
import ( | ||
"github.com/aws/smithy-go" | ||
|
||
v1 "github.com/aws/karpenter-provider-aws/pkg/apis/v1" | ||
"github.com/aws/karpenter-provider-aws/pkg/controllers/nodeclass/status" | ||
"github.com/aws/karpenter-provider-aws/pkg/fake" | ||
"github.com/aws/karpenter-provider-aws/pkg/test" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
. "sigs.k8s.io/karpenter/pkg/test/expectations" | ||
) | ||
|
||
var _ = Describe("NodeClass Tags Status Controller", func() { | ||
BeforeEach(func() { | ||
nodeClass = test.EC2NodeClass(v1.EC2NodeClass{ | ||
Spec: v1.EC2NodeClassSpec{ | ||
Tags: map[string]string{ | ||
"fakeKey": "fakeValue", | ||
}, | ||
}, | ||
}) | ||
}) | ||
BeforeEach(func() { | ||
awsEnv.Reset() | ||
}) | ||
It("Should update EC2NodeClass status for Tags", func() { | ||
ExpectApplied(ctx, env.Client, nodeClass) | ||
ExpectObjectReconciled(ctx, env.Client, statusController, nodeClass) | ||
nodeClass = ExpectExists(ctx, env.Client, nodeClass) | ||
Expect(nodeClass.StatusConditions().IsTrue(v1.ConditionTypeTagsReady)).To(BeTrue()) | ||
}) | ||
It("Should not update EC2NodeClass status for Tags without appropriate permissions", func() { | ||
ec2api := fake.NewEC2API() | ||
ec2api.NextError.Set(&smithy.GenericAPIError{ | ||
Code: "UnauthorizedOperation", | ||
}) | ||
ExpectApplied(ctx, env.Client, nodeClass) | ||
statusController := status.NewController(env.Client, awsEnv.SubnetProvider, awsEnv.SecurityGroupProvider, awsEnv.AMIProvider, awsEnv.InstanceProfileProvider, awsEnv.LaunchTemplateProvider, ec2api) | ||
_, err := statusController.Reconcile(ctx, nodeClass) | ||
Expect(err).To(HaveOccurred()) | ||
Expect(err.Error()).To(ContainSubstring("UnauthorizedOperation")) | ||
nodeClass = ExpectExists(ctx, env.Client, nodeClass) | ||
Expect(nodeClass.StatusConditions().IsTrue(v1.ConditionTypeTagsReady)).To(BeFalse()) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters