-
Notifications
You must be signed in to change notification settings - Fork 964
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
4 changed files
with
220 additions
and
17 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
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,204 @@ | ||
/* | ||
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 tagging_test | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/samber/lo" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
|
||
. "github.com/aws/karpenter-core/pkg/test/expectations" | ||
|
||
"github.com/aws/aws-sdk-go/aws" | ||
"github.com/aws/aws-sdk-go/service/ec2" | ||
. "knative.dev/pkg/logging/testing" | ||
|
||
coresettings "github.com/aws/karpenter-core/pkg/apis/settings" | ||
corev1beta1 "github.com/aws/karpenter-core/pkg/apis/v1beta1" | ||
coretest "github.com/aws/karpenter-core/pkg/test" | ||
"github.com/aws/karpenter/pkg/apis" | ||
"github.com/aws/karpenter/pkg/apis/settings" | ||
"github.com/aws/karpenter/pkg/apis/v1beta1" | ||
"github.com/aws/karpenter/pkg/controllers/nodeclaim/tagging" | ||
"github.com/aws/karpenter/pkg/fake" | ||
"github.com/aws/karpenter/pkg/providers/instance" | ||
"github.com/aws/karpenter/pkg/test" | ||
|
||
"github.com/aws/karpenter-core/pkg/operator/controller" | ||
"github.com/aws/karpenter-core/pkg/operator/injection" | ||
"github.com/aws/karpenter-core/pkg/operator/options" | ||
"github.com/aws/karpenter-core/pkg/operator/scheme" | ||
) | ||
|
||
var ctx context.Context | ||
var awsEnv *test.Environment | ||
var env *coretest.Environment | ||
var taggingController controller.Controller | ||
var opts options.Options | ||
|
||
func TestAPIs(t *testing.T) { | ||
ctx = TestContextWithLogger(t) | ||
RegisterFailHandler(Fail) | ||
RunSpecs(t, "Tagging") | ||
} | ||
|
||
var _ = BeforeSuite(func() { | ||
s := scheme.Scheme | ||
env = coretest.NewEnvironment(s, coretest.WithCRDs(apis.CRDs...)) | ||
ctx = coresettings.ToContext(ctx, coretest.Settings()) | ||
ctx = settings.ToContext(ctx, test.Settings()) | ||
awsEnv = test.NewEnvironment(ctx, env) | ||
taggingController = tagging.NewController(env.Client, awsEnv.InstanceProvider) | ||
}) | ||
var _ = AfterSuite(func() { | ||
Expect(env.Stop()).To(Succeed(), "Failed to stop environment") | ||
}) | ||
|
||
var _ = BeforeEach(func() { | ||
ctx = injection.WithOptions(ctx, opts) | ||
awsEnv.Reset() | ||
}) | ||
|
||
var _ = Describe("Tagging", func() { | ||
var ec2Instance *ec2.Instance | ||
|
||
BeforeEach(func() { | ||
ec2Instance = &ec2.Instance{ | ||
State: &ec2.InstanceState{ | ||
Name: aws.String(ec2.InstanceStateNameRunning), | ||
}, | ||
Tags: []*ec2.Tag{ | ||
{ | ||
Key: aws.String(fmt.Sprintf("kubernetes.io/cluster/%s", settings.FromContext(ctx).ClusterName)), | ||
Value: aws.String("owned"), | ||
}, | ||
{ | ||
Key: aws.String(corev1beta1.NodePoolLabelKey), | ||
Value: aws.String("default"), | ||
}, | ||
{ | ||
Key: aws.String(corev1beta1.ManagedByAnnotationKey), | ||
Value: aws.String(settings.FromContext(ctx).ClusterName), | ||
}, | ||
}, | ||
PrivateDnsName: aws.String(fake.PrivateDNSName()), | ||
Placement: &ec2.Placement{ | ||
AvailabilityZone: aws.String(fake.DefaultRegion), | ||
}, | ||
InstanceId: aws.String(fake.InstanceID()), | ||
InstanceType: aws.String("m5.large"), | ||
} | ||
|
||
awsEnv.EC2API.Instances.Store(*ec2Instance.InstanceId, ec2Instance) | ||
}) | ||
AfterEach(func() { | ||
ExpectCleanedUp(ctx, env.Client) | ||
}) | ||
|
||
It("shouldn't tag instances without a Node", func() { | ||
nodeClaim := coretest.NodeClaim(corev1beta1.NodeClaim{ | ||
Status: corev1beta1.NodeClaimStatus{ | ||
ProviderID: fake.ProviderID(*ec2Instance.InstanceId), | ||
}, | ||
}) | ||
|
||
ExpectApplied(ctx, env.Client, nodeClaim) | ||
ExpectReconcileSucceeded(ctx, taggingController, client.ObjectKeyFromObject(nodeClaim)) | ||
Expect(nodeClaim.Annotations).To(Not(HaveKey(v1beta1.AnnotationInstanceTagged))) | ||
Expect(lo.ContainsBy(ec2Instance.Tags, func(tag *ec2.Tag) bool { | ||
return *tag.Key == tagging.TagName | ||
})).To(BeFalse()) | ||
}) | ||
|
||
It("should't tag instance with malformed providerID", func() { | ||
nodeClaim := coretest.NodeClaim(corev1beta1.NodeClaim{ | ||
Status: corev1beta1.NodeClaimStatus{ | ||
ProviderID: "Bad providerID", | ||
NodeName: "default", | ||
}, | ||
}) | ||
|
||
ExpectApplied(ctx, env.Client, nodeClaim) | ||
ExpectReconcileSucceeded(ctx, taggingController, client.ObjectKeyFromObject(nodeClaim)) | ||
Expect(nodeClaim.Annotations).To(Not(HaveKey(v1beta1.AnnotationInstanceTagged))) | ||
Expect(lo.ContainsBy(ec2Instance.Tags, func(tag *ec2.Tag) bool { | ||
return *tag.Key == tagging.TagName | ||
})).To(BeFalse()) | ||
}) | ||
|
||
It("should tag instance with a Node", func() { | ||
nodeClaim := coretest.NodeClaim(corev1beta1.NodeClaim{ | ||
Status: corev1beta1.NodeClaimStatus{ | ||
ProviderID: fake.ProviderID(*ec2Instance.InstanceId), | ||
NodeName: "default", | ||
}, | ||
}) | ||
|
||
ExpectApplied(ctx, env.Client, nodeClaim) | ||
ExpectReconcileSucceeded(ctx, taggingController, client.ObjectKeyFromObject(nodeClaim)) | ||
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim) | ||
Expect(nodeClaim.Annotations).To(HaveKey(v1beta1.AnnotationInstanceTagged)) | ||
|
||
inst := instance.NewInstance(ec2Instance).Tags | ||
Expect(inst).To(HaveKeyWithValue(tagging.TagName, "default")) | ||
Expect(inst).To(HaveKeyWithValue(tagging.TagNodeClaim, nodeClaim.Name)) | ||
}) | ||
|
||
DescribeTable( | ||
"should tag taggable instances", | ||
func(customTags ...string) { | ||
nodeClaim := coretest.NodeClaim(corev1beta1.NodeClaim{ | ||
Status: corev1beta1.NodeClaimStatus{ | ||
ProviderID: fake.ProviderID(*ec2Instance.InstanceId), | ||
NodeName: "default", | ||
}, | ||
}) | ||
|
||
for _, tag := range customTags { | ||
ec2Instance.Tags = append(ec2Instance.Tags, &ec2.Tag{ | ||
Key: aws.String(tag), | ||
Value: aws.String("custom-tag"), | ||
}) | ||
} | ||
awsEnv.EC2API.Instances.Store(*ec2Instance.InstanceId, ec2Instance) | ||
|
||
ExpectApplied(ctx, env.Client, nodeClaim) | ||
ExpectReconcileSucceeded(ctx, taggingController, client.ObjectKeyFromObject(nodeClaim)) | ||
nodeClaim = ExpectExists(ctx, env.Client, nodeClaim) | ||
Expect(nodeClaim.Annotations).To(HaveKey(v1beta1.AnnotationInstanceTagged)) | ||
|
||
expectedTags := map[string]string{ | ||
tagging.TagName: nodeClaim.Status.NodeName, | ||
tagging.TagNodeClaim: nodeClaim.Name, | ||
} | ||
instanceTags := instance.NewInstance(ec2Instance).Tags | ||
for tag, value := range expectedTags { | ||
if lo.Contains(customTags, tag) { | ||
value = "custom-tag" | ||
} | ||
Expect(instanceTags).To(HaveKeyWithValue(tag, value)) | ||
} | ||
}, | ||
Entry("with only NodeClaim", tagging.TagName), | ||
Entry("with only Name", tagging.TagNodeClaim), | ||
Entry("with both Name and NodeClaim"), | ||
Entry("with nothing", tagging.TagName, tagging.TagNodeClaim), | ||
) | ||
}) |
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