diff --git a/cmd/ilm-rule-add.go b/cmd/ilm-rule-add.go index 57ae31bc..6b5c37a1 100644 --- a/cmd/ilm-rule-add.go +++ b/cmd/ilm-rule-add.go @@ -49,13 +49,13 @@ DESCRIPTION: Add a lifecycle configuration rule. EXAMPLES: - 1. Add a lifecycle rule with a transition and a noncurrent version transition action for objects with prefix doc/ in mybucket. + 1. Add a lifecycle rule with a transition and a noncurrent version transition action for objects with prefix doc/ whose size is greater than 1MiB in mybucket. Tiers must exist in MinIO. Use existing tiers or add new tiers. {{.Prompt}} mc ilm tier add minio myminio MINIOTIER-1 --endpoint https://warm-minio-1.com \ --access-key ACCESSKEY --secret-key SECRETKEY --bucket bucket1 --prefix prefix1 - {{.Prompt}} {{.HelpName}} --prefix "doc/" --transition-days "90" --transition-tier "MINIOTIER-1" \ + {{.Prompt}} {{.HelpName}} --prefix "doc/" --size-gt 1MiB --transition-days "90" --transition-tier "MINIOTIER-1" \ --noncurrent-transition-days "45" --noncurrent-transition-tier "MINIOTIER-1" \ myminio/mybucket/ @@ -77,6 +77,14 @@ var ilmAddFlags = []cli.Flag{ Name: "tags", Usage: "key value pairs of the form '=&=&='", }, + cli.StringFlag{ + Name: "size-lt", + Usage: "objects with size less than this value will be selected for the lifecycle action", + }, + cli.StringFlag{ + Name: "size-gt", + Usage: "objects with size greater than this value will be selected for the lifecycle action", + }, cli.StringFlag{ Name: "expiry-date", Usage: "format 'YYYY-MM-DD' the date of expiration", diff --git a/cmd/ilm/options.go b/cmd/ilm/options.go index 6a0fdab8..ccd46f5e 100644 --- a/cmd/ilm/options.go +++ b/cmd/ilm/options.go @@ -20,9 +20,11 @@ package ilm import ( "errors" "fmt" + "math" "strconv" "strings" + "github.com/dustin/go-humanize" "github.com/minio/cli" "github.com/minio/mc/pkg/probe" "github.com/minio/minio-go/v7/pkg/lifecycle" @@ -61,13 +63,15 @@ type LifecycleOptions struct { Status *bool - Prefix *string - Tags *string - ExpiryDate *string - ExpiryDays *string - TransitionDate *string - TransitionDays *string - StorageClass *string + Prefix *string + Tags *string + ObjectSizeLessThan *int64 + ObjectSizeGreaterThan *int64 + ExpiryDate *string + ExpiryDays *string + TransitionDate *string + TransitionDays *string + StorageClass *string ExpiredObjectDeleteMarker *bool NoncurrentVersionExpirationDays *int @@ -77,13 +81,58 @@ type LifecycleOptions struct { NoncurrentVersionTransitionStorageClass *string } +// Filter returns lifecycle.Filter appropriate for opts +func (opts LifecycleOptions) Filter() lifecycle.Filter { + var f lifecycle.Filter + var tags []lifecycle.Tag + var predCount int + if opts.Tags != nil { + tags = extractILMTags(*opts.Tags) + predCount += len(tags) + } + var prefix string + if opts.Prefix != nil { + prefix = *opts.Prefix + predCount++ + } + + var szLt, szGt int64 + if opts.ObjectSizeLessThan != nil { + szLt = *opts.ObjectSizeLessThan + predCount++ + } + + if opts.ObjectSizeGreaterThan != nil { + szGt = *opts.ObjectSizeGreaterThan + predCount++ + } + + if predCount >= 2 { + f.And = lifecycle.And{ + Tags: tags, + Prefix: prefix, + ObjectSizeLessThan: szLt, + ObjectSizeGreaterThan: szGt, + } + } else { + // In a valid lifecycle rule filter at most one of the + // following will only be set. + f.Prefix = prefix + f.ObjectSizeGreaterThan = szGt + f.ObjectSizeLessThan = szLt + if len(tags) >= 1 { + f.Tag = tags[0] + } + } + + return f +} + // ToILMRule creates lifecycle.Configuration based on LifecycleOptions func (opts LifecycleOptions) ToILMRule() (lifecycle.Rule, *probe.Error) { var ( id, status string - filter lifecycle.Filter - nonCurrentVersionExpirationDays lifecycle.ExpirationDays newerNonCurrentExpirationVersions int nonCurrentVersionTransitionDays lifecycle.ExpirationDays @@ -110,23 +159,6 @@ func (opts LifecycleOptions) ToILMRule() (lifecycle.Rule, *probe.Error) { return lifecycle.Rule{}, err } - andVal := lifecycle.And{} - if opts.Tags != nil { - andVal.Tags = extractILMTags(*opts.Tags) - } - - if opts.Prefix != nil { - filter.Prefix = *opts.Prefix - } - - if len(andVal.Tags) > 0 { - filter.And = andVal - if opts.Prefix != nil { - filter.And.Prefix = *opts.Prefix - } - filter.Prefix = "" - } - if opts.NoncurrentVersionExpirationDays != nil { nonCurrentVersionExpirationDays = lifecycle.ExpirationDays(*opts.NoncurrentVersionExpirationDays) } @@ -145,7 +177,7 @@ func (opts LifecycleOptions) ToILMRule() (lifecycle.Rule, *probe.Error) { newRule := lifecycle.Rule{ ID: id, - RuleFilter: filter, + RuleFilter: opts.Filter(), Status: status, Expiration: expiry, Transition: transition, @@ -177,6 +209,10 @@ func intPtr(i int) *int { return &ptr } +func int64Ptr(i int64) *int64 { + return &i +} + func boolPtr(b bool) *bool { ptr := b return &ptr @@ -191,6 +227,8 @@ func GetLifecycleOptions(ctx *cli.Context) (LifecycleOptions, *probe.Error) { prefix *string tags *string + sizeLt *int64 + sizeGt *int64 expiryDate *string expiryDays *string transitionDate *string @@ -232,6 +270,24 @@ func GetLifecycleOptions(ctx *cli.Context) (LifecycleOptions, *probe.Error) { } } + if ctx.IsSet("size-lt") { + szStr := ctx.String("size-lt") + szLt, err := humanize.ParseBytes(szStr) + if err != nil || szLt > math.MaxInt64 { + return LifecycleOptions{}, probe.NewError(fmt.Errorf("size-lt value %s is invalid", szStr)) + } + + sizeLt = int64Ptr(int64(szLt)) + } + if ctx.IsSet("size-gt") { + szStr := ctx.String("size-gt") + szGt, err := humanize.ParseBytes(szStr) + if err != nil || szGt > math.MaxInt64 { + return LifecycleOptions{}, probe.NewError(fmt.Errorf("size-gt value %s is invalid", szStr)) + } + sizeGt = int64Ptr(int64(szGt)) + } + // For backward-compatibility if ctx.IsSet("storage-class") { tier = strPtr(strings.ToUpper(ctx.String("storage-class"))) @@ -318,6 +374,8 @@ func GetLifecycleOptions(ctx *cli.Context) (LifecycleOptions, *probe.Error) { Status: status, Prefix: prefix, Tags: tags, + ObjectSizeLessThan: sizeLt, + ObjectSizeGreaterThan: sizeGt, ExpiryDate: expiryDate, ExpiryDays: expiryDays, TransitionDate: transitionDate, diff --git a/cmd/ilm/options_test.go b/cmd/ilm/options_test.go new file mode 100644 index 00000000..f739669b --- /dev/null +++ b/cmd/ilm/options_test.go @@ -0,0 +1,155 @@ +// Copyright (c) 2015-2023 MinIO, Inc. +// +// This file is part of MinIO Object Storage stack +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU Affero General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Affero General Public License for more details. +// +// You should have received a copy of the GNU Affero General Public License +// along with this program. If not, see . + +package ilm + +import ( + "fmt" + "testing" + + "github.com/dustin/go-humanize" + "github.com/minio/minio-go/v7/pkg/lifecycle" +) + +func TestOptionFilter(t *testing.T) { + emptyFilter := lifecycle.Filter{} + emptyOpts := LifecycleOptions{} + + filterWithPrefix := lifecycle.Filter{ + Prefix: "doc/", + } + optsWithPrefix := LifecycleOptions{ + Prefix: strPtr("doc/"), + } + + filterWithTag := lifecycle.Filter{ + Tag: lifecycle.Tag{ + Key: "key1", + Value: "value1", + }, + } + optsWithTag := LifecycleOptions{ + Tags: strPtr("key1=value1"), + } + + filterWithSzLt := lifecycle.Filter{ + ObjectSizeLessThan: 100 * humanize.MiByte, + } + optsWithSzLt := LifecycleOptions{ + ObjectSizeLessThan: int64Ptr(100 * humanize.MiByte), + } + + filterWithSzGt := lifecycle.Filter{ + ObjectSizeGreaterThan: 1 * humanize.MiByte, + } + optsWithSzGt := LifecycleOptions{ + ObjectSizeGreaterThan: int64Ptr(1 * humanize.MiByte), + } + + filterWithAnd := lifecycle.Filter{ + And: lifecycle.And{ + Prefix: "doc/", + Tags: []lifecycle.Tag{ + { + Key: "key1", + Value: "value1", + }, + }, + ObjectSizeLessThan: 100 * humanize.MiByte, + ObjectSizeGreaterThan: 1 * humanize.MiByte, + }, + } + optsWithAnd := LifecycleOptions{ + Prefix: strPtr("doc/"), + Tags: strPtr("key1=value1"), + ObjectSizeLessThan: int64Ptr(100 * humanize.MiByte), + ObjectSizeGreaterThan: int64Ptr(1 * humanize.MiByte), + } + + tests := []struct { + opts LifecycleOptions + want lifecycle.Filter + }{ + { + opts: emptyOpts, + want: emptyFilter, + }, + { + opts: optsWithPrefix, + want: filterWithPrefix, + }, + { + opts: optsWithTag, + want: filterWithTag, + }, + { + opts: optsWithSzGt, + want: filterWithSzGt, + }, + { + opts: optsWithSzLt, + want: filterWithSzLt, + }, + { + opts: optsWithAnd, + want: filterWithAnd, + }, + } + + filterEq := func(a, b lifecycle.Filter) bool { + if a.ObjectSizeGreaterThan != b.ObjectSizeGreaterThan { + return false + } + if a.ObjectSizeLessThan != b.ObjectSizeLessThan { + return false + } + if a.Prefix != b.Prefix { + return false + } + if a.Tag != b.Tag { + return false + } + + if a.And.ObjectSizeGreaterThan != b.And.ObjectSizeGreaterThan { + return false + } + if a.And.ObjectSizeLessThan != b.And.ObjectSizeLessThan { + return false + } + if a.And.Prefix != b.And.Prefix { + return false + } + if len(a.And.Tags) != len(b.And.Tags) { + return false + } + for i := range a.And.Tags { + if a.And.Tags[i] != b.And.Tags[i] { + return false + } + } + + return true + } + + for i, test := range tests { + t.Run(fmt.Sprintf("Test %d", i+1), func(t *testing.T) { + if got := test.opts.Filter(); !filterEq(got, test.want) { + t.Fatalf("Expected %#v but got %#v", test.want, got) + } + }) + } +} diff --git a/go.mod b/go.mod index b4489f89..4bd14189 100644 --- a/go.mod +++ b/go.mod @@ -20,7 +20,7 @@ require ( github.com/minio/colorjson v1.0.6 github.com/minio/filepath v1.0.0 github.com/minio/md5-simd v1.1.2 // indirect - github.com/minio/minio-go/v7 v7.0.63 + github.com/minio/minio-go/v7 v7.0.65-0.20231122233251-1f7dd6b7e3e1 github.com/minio/selfupdate v0.6.0 github.com/minio/sha256-simd v1.0.1 github.com/mitchellh/go-homedir v1.1.0 diff --git a/go.sum b/go.sum index e1d8fd25..b2a37d95 100644 --- a/go.sum +++ b/go.sum @@ -146,6 +146,8 @@ github.com/minio/md5-simd v1.1.2 h1:Gdi1DZK69+ZVMoNHRXJyNcxrMA4dSxoYHZSQbirFg34= github.com/minio/md5-simd v1.1.2/go.mod h1:MzdKDxYpY2BT9XQFocsiZf/NKVtR7nkE4RoEpN+20RM= github.com/minio/minio-go/v7 v7.0.63 h1:GbZ2oCvaUdgT5640WJOpyDhhDxvknAJU2/T3yurwcbQ= github.com/minio/minio-go/v7 v7.0.63/go.mod h1:Q6X7Qjb7WMhvG65qKf4gUgA5XaiSox74kR1uAEjxRS4= +github.com/minio/minio-go/v7 v7.0.65-0.20231122233251-1f7dd6b7e3e1 h1:oGua0HckYk7JuIW/c1IuInHe9MCef1U6Q2Qf9ea/V5U= +github.com/minio/minio-go/v7 v7.0.65-0.20231122233251-1f7dd6b7e3e1/go.mod h1:R4WVUR6ZTedlCcGwZRauLMIKjgyaWxhs4Mqi/OMPmEc= github.com/minio/mux v1.9.0 h1:dWafQFyEfGhJvK6AwLOt83bIG5bxKxKJnKMCi0XAaoA= github.com/minio/mux v1.9.0/go.mod h1:1pAare17ZRL5GpmNL+9YmqHoWnLmMZF9C/ioUCfy0BQ= github.com/minio/pkg/v2 v2.0.2 h1:cytXmC21fBNS+0NVKEE5FuYmQfY+HFTqis6Kkj3U9ac=