Skip to content

Commit

Permalink
chore: AssumeRoleDuration to time.Duration (aws#4433)
Browse files Browse the repository at this point in the history
  • Loading branch information
engedaam authored Aug 21, 2023
1 parent 7ed36d8 commit 99ea905
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
10 changes: 4 additions & 6 deletions pkg/apis/settings/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ import (
"github.com/go-playground/validator/v10"
"go.uber.org/multierr"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"knative.dev/pkg/apis"
"knative.dev/pkg/configmap"

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

Expand All @@ -38,7 +36,7 @@ var ContextKey = settingsKeyType{}

var defaultSettings = &Settings{
AssumeRoleARN: "",
AssumeRoleDuration: &metav1.Duration{Duration: time.Minute * 15},
AssumeRoleDuration: time.Minute * 15,
ClusterName: "",
ClusterEndpoint: "",
DefaultInstanceProfile: "",
Expand All @@ -54,8 +52,8 @@ var defaultSettings = &Settings{
// +k8s:deepcopy-gen=true
type Settings struct {
AssumeRoleARN string
AssumeRoleDuration *metav1.Duration `validate:"min=15m"`
ClusterName string `validate:"required"`
AssumeRoleDuration time.Duration `validate:"min=15m"`
ClusterName string `validate:"required"`
ClusterEndpoint string
DefaultInstanceProfile string
EnablePodENI bool
Expand All @@ -77,7 +75,7 @@ func (*Settings) Inject(ctx context.Context, cm *v1.ConfigMap) (context.Context,

if err := configmap.Parse(cm.Data,
configmap.AsString("aws.assumeRoleARN", &s.AssumeRoleARN),
coresettings.AsMetaDuration("aws.assumeRoleDuration", &s.AssumeRoleDuration),
configmap.AsDuration("aws.assumeRoleDuration", &s.AssumeRoleDuration),
configmap.AsString("aws.clusterName", &s.ClusterName),
configmap.AsString("aws.clusterEndpoint", &s.ClusterEndpoint),
configmap.AsString("aws.defaultInstanceProfile", &s.DefaultInstanceProfile),
Expand Down
15 changes: 13 additions & 2 deletions pkg/apis/settings/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ var _ = Describe("Validation", func() {
Expect(err).ToNot(HaveOccurred())
s := settings.FromContext(ctx)
Expect(s.AssumeRoleARN).To(Equal(""))
Expect(s.AssumeRoleDuration.Duration).To(Equal(time.Duration(15) * time.Minute))
Expect(s.AssumeRoleDuration).To(Equal(time.Duration(15) * time.Minute))
Expect(s.DefaultInstanceProfile).To(Equal(""))
Expect(s.EnablePodENI).To(BeFalse())
Expect(s.EnableENILimitedPodDensity).To(BeTrue())
Expand Down Expand Up @@ -76,7 +76,7 @@ var _ = Describe("Validation", func() {
Expect(err).ToNot(HaveOccurred())
s := settings.FromContext(ctx)
Expect(s.AssumeRoleARN).To(Equal("arn:aws:iam::111222333444:role/testrole"))
Expect(s.AssumeRoleDuration.Duration).To(Equal(time.Duration(27) * time.Minute))
Expect(s.AssumeRoleDuration).To(Equal(time.Duration(27) * time.Minute))
Expect(s.DefaultInstanceProfile).To(Equal("karpenter"))
Expect(s.EnablePodENI).To(BeTrue())
Expect(s.EnableENILimitedPodDensity).To(BeFalse())
Expand Down Expand Up @@ -198,6 +198,17 @@ var _ = Describe("Validation", func() {
cm := &v1.ConfigMap{
Data: map[string]string{
"aws.reservedENIs": "-1",
"aws.clusterName": "my-cluster",
},
}
_, err := (&settings.Settings{}).Inject(ctx, cm)
Expect(err).To(HaveOccurred())
})
It("should fail validation with assumeDurationRole is less then 15m", func() {
cm := &v1.ConfigMap{
Data: map[string]string{
"aws.assumeRoleDuration": "2m",
"aws.clusterName": "my-cluster",
},
}
_, err := (&settings.Settings{}).Inject(ctx, cm)
Expand Down
9 changes: 1 addition & 8 deletions pkg/apis/settings/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/operator/operator.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,6 @@ func kubeDNSIP(ctx context.Context, kubernetesInterface kubernetes.Interface) (n
}

func setDurationAndExpiry(ctx context.Context, provider *stscreds.AssumeRoleProvider) {
provider.Duration = settings.FromContext(ctx).AssumeRoleDuration.Duration
provider.Duration = settings.FromContext(ctx).AssumeRoleDuration
provider.ExpiryWindow = time.Duration(10) * time.Second
}

0 comments on commit 99ea905

Please sign in to comment.