Skip to content
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

Add LogRetentionInDays update to update-cluster-logging command #5404

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/ctl/create/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ func doCreateCluster(cmd *cmdutils.Cmd, ngFilter *filter.NodeGroupFilter, params
logger.Info("if you encounter any issues, check CloudFormation console or try 'eksctl utils describe-stacks --region=%s --cluster=%s'", meta.Region, meta.Name)

eks.LogEnabledFeatures(cfg)
postClusterCreationTasks := ctl.CreateExtraClusterConfigTasks(ctx, cfg) // THIS IS WHERE WE TIME OUT
postClusterCreationTasks := ctl.CreateExtraClusterConfigTasks(ctx, cfg)

var preNodegroupAddons, postNodegroupAddons *tasks.TaskTree
if len(cfg.Addons) > 0 {
Expand Down
5 changes: 5 additions & 0 deletions pkg/ctl/utils/update_cluster_logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,11 @@ func doEnableLogging(cmd *cmdutils.Cmd, logTypesToEnable []string, logTypesToDis
cmdutils.LogIntendedAction(cmd.Plan, "update CloudWatch logging for cluster %q in %q (%s & %s)",
meta.Name, meta.Region, describeTypesToEnable, describeTypesToDisable,
)
if period := cfg.CloudWatch.ClusterLogging.LogRetentionInDays; period > 0 {
cmdutils.LogIntendedAction(cmd.Plan, "update CloudWatch logging for log retention period set to %d",
period,
)
}
if !cmd.Plan {
if err := ctl.UpdateClusterConfigForLogging(ctx, cfg); err != nil {
return err
Expand Down
15 changes: 13 additions & 2 deletions pkg/eks/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import (
"strings"
"time"

ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
"github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"
"github.com/kris-nova/logger"
"github.com/pkg/errors"
"k8s.io/apimachinery/pkg/util/sets"
Expand Down Expand Up @@ -105,6 +105,17 @@ func (c *ClusterProvider) UpdateClusterConfigForLogging(ctx context.Context, cfg
logger.Success("configured CloudWatch logging for cluster %q in %q (%s & %s)",
cfg.Metadata.Name, cfg.Metadata.Region, describeEnabledTypes, describeDisabledTypes,
)

if logRetentionInDays := cfg.CloudWatch.ClusterLogging.LogRetentionInDays; logRetentionInDays > 0 {
if _, err := c.AWSProvider.CloudWatchLogs().PutRetentionPolicy(ctx, &cloudwatchlogs.PutRetentionPolicyInput{
// The format for log group name is documented here: https://docs.aws.amazon.com/eks/latest/userguide/control-plane-logs.html
LogGroupName: aws.String(fmt.Sprintf("/aws/eks/%s/cluster", cfg.Metadata.Name)),
RetentionInDays: aws.Int32(int32(logRetentionInDays)),
}); err != nil {
return fmt.Errorf("error updating log retention settings: %w", err)
}
logger.Success("configured CloudWatch log retention to %d days for CloudWatch logging", logRetentionInDays)
}
return nil
}

Expand Down
20 changes: 17 additions & 3 deletions pkg/eks/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ package eks_test

import (
"context"
"fmt"

"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatchlogs"
awseks "github.com/aws/aws-sdk-go-v2/service/eks"
ekstypes "github.com/aws/aws-sdk-go-v2/service/eks/types"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"github.com/stretchr/testify/mock"

api "github.com/weaveworks/eksctl/pkg/apis/eksctl.io/v1alpha5"
Expand All @@ -34,10 +34,12 @@ var _ = Describe("EKS API wrapper", func() {
err error

sentClusterLogging []ekstypes.LogSetup

p *mockprovider.MockProvider
)

BeforeEach(func() {
p := mockprovider.NewMockProvider()
p = mockprovider.NewMockProvider()
ctl = &ClusterProvider{
AWSProvider: p,
Status: &ProviderStatus{},
Expand Down Expand Up @@ -165,5 +167,17 @@ var _ = Describe("EKS API wrapper", func() {
Expect(sentClusterLogging[1].Types).NotTo(BeEmpty())
Expect(sentClusterLogging[1].Types).To(Equal([]ekstypes.LogType{"api", "audit", "scheduler"}))
})
It("should update logRetentionInDays in case if it's greater than 0", func() {
p.MockCloudWatchLogs().On("PutRetentionPolicy", mock.Anything, &cloudwatchlogs.PutRetentionPolicyInput{
LogGroupName: aws.String(fmt.Sprintf("/aws/eks/%s/cluster", cfg.Metadata.Name)),
RetentionInDays: aws.Int32(int32(30)),
}).Return(&cloudwatchlogs.PutRetentionPolicyOutput{}, nil)
cfg.CloudWatch.ClusterLogging.EnableTypes = []string{"authenticator"}
cfg.CloudWatch.ClusterLogging.LogRetentionInDays = 30

api.SetClusterConfigDefaults(cfg)
Expect(api.ValidateClusterConfig(cfg)).To(Succeed())
Expect(ctl.UpdateClusterConfigForLogging(context.Background(), cfg)).To(Succeed())
})
})
})