Skip to content

Commit 52ec705

Browse files
authored
Add option to export only recently created resources (#436)
* add option to export only recent resources
1 parent 487adac commit 52ec705

File tree

8 files changed

+40
-97
lines changed

8 files changed

+40
-97
lines changed

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ MOCKGEN ?= $(LOCALBIN)/mockgen
4646
SEMVER ?= $(LOCALBIN)/semver
4747

4848
## Tool Versions
49-
GOLANGCI_LINT_VERSION ?= v1.59.1
50-
GORELEASER_VERSION ?= v2.0.1
49+
GOLANGCI_LINT_VERSION ?= v1.60.1
50+
GORELEASER_VERSION ?= v2.2.0
5151
MOCKGEN_VERSION ?= v0.4.0
5252
SEMVER_VERSION ?= v1.1.3
5353

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Flags:
4141
--cluster string The name of the kubeconfig cluster to use
4242
--config string config file
4343
--context string The name of the kubeconfig context to use
44+
--created-within duration The max allowed age duration for the resources
4445
--disable-compression If true, opt-out of response compression for all requests to the server
4546
-e, --exclude-kinds strings Do not export excluded kinds
4647
-h, --help help for kubexporter

cmd/root.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ func readConfig(cmd *cobra.Command, configFlags *genericclioptions.ConfigFlags,
8383
case "exclude-kinds":
8484
sl, _ := cmd.Flags().GetStringSlice(f.Name)
8585
config.Excluded.Kinds = sl
86+
case "created-within":
87+
cw, _ := cmd.Flags().GetDuration(f.Name)
88+
config.CreatedWithin = cw
8689
case "archive":
8790
sl, _ := cmd.Flags().GetBool(f.Name)
8891
config.Archive = sl
@@ -123,6 +126,7 @@ func init() {
123126
rootCmd.Flags().BoolP("lists", "l", false, "If enabled, all resources are exported as lists instead of individual files")
124127
rootCmd.Flags().StringSliceP("include-kinds", "i", []string{}, "Export only included kinds, if included kinds are defined, excluded will be ignored")
125128
rootCmd.Flags().StringSliceP("exclude-kinds", "e", []string{}, "Do not export excluded kinds")
129+
rootCmd.Flags().Duration("created-within", 0, "The max allowed age duration for the resources")
126130

127131
configFlags = genericclioptions.NewConfigFlags(true)
128132
configFlags.AddFlags(rootCmd.Flags())

config.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ namespace:
77
asLists: false
88
#queryPageSize: 1000
99
clearTarget: true
10+
#createdWithin: 24h
1011
worker: 5
1112
encrypted:
1213
aesKey: "12345678901234567890123456789012"

go.mod

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ require (
3535
github.com/chai2010/gettext-go v1.0.2 // indirect
3636
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
3737
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
38-
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
3938
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
4039
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
4140
github.com/go-errors/errors v1.5.1 // indirect
@@ -74,9 +73,7 @@ require (
7473
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
7574
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
7675
github.com/pkg/errors v0.9.1 // indirect
77-
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
7876
github.com/rivo/uniseg v0.4.7 // indirect
79-
github.com/rogpeppe/go-internal v1.12.0 // indirect
8077
github.com/russross/blackfriday/v2 v2.1.0 // indirect
8178
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
8279
github.com/stretchr/objx v0.5.2 // indirect

go.sum

Lines changed: 2 additions & 69 deletions
Large diffs are not rendered by default.

pkg/export/export.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,9 @@ func (e *exporter) writeIntro() {
150150
if len(e.config.Encrypted.KindFields) > 0 {
151151
e.l.Printf(" encrypted fields 🔒 %v\n", e.config.Encrypted.KindFields)
152152
}
153-
153+
if e.config.CreatedWithin > 0 {
154+
e.l.Printf(" created within %s ⏱️\n", e.config.CreatedWithin.String())
155+
}
154156
if e.config.AsLists {
155157
e.l.Printf(" as lists 📦\n")
156158
} else if e.config.QueryPageSize != 0 {
@@ -239,7 +241,7 @@ func (e *exporter) printSummary(resources []*types.GroupResource) {
239241
if e.config.Worker > 1 {
240242
total = "CUMULATED " + total
241243
}
242-
totalRow := []string{total, "", "", "", strconv.Itoa(inst), strconv.Itoa(totalInst), qd.Sub(start).String()}
244+
totalRow := []string{total, "", "", "", strconv.Itoa(totalInst), strconv.Itoa(inst), qd.Sub(start).String()}
243245
if withPages {
244246
totalRow = append(totalRow, strconv.Itoa(pages))
245247
}

pkg/types/config.go

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010
"sort"
1111
"strings"
1212
"text/template"
13+
"time"
1314

1415
"github.com/Masterminds/sprig"
1516
"github.com/bakito/kubexporter/pkg/log"
@@ -97,27 +98,28 @@ func NewConfig(configFlags *genericclioptions.ConfigFlags, printFlags *genericcl
9798

9899
// Config export config
99100
type Config struct {
100-
Excluded Excluded `json:"excluded" yaml:"excluded"`
101-
Included Included `json:"included" yaml:"included"`
102-
ConsiderOwnerReferences bool `json:"considerOwnerReferences" yaml:"considerOwnerReferences"`
103-
Masked *Masked `json:"masked" yaml:"masked"`
104-
Encrypted *Encrypted `json:"encrypted" yaml:"masked"`
105-
SortSlices KindFields `json:"sortSlices" yaml:"sortSlices"`
106-
FileNameTemplate string `json:"fileNameTemplate" yaml:"fileNameTemplate"`
107-
ListFileNameTemplate string `json:"listFileNameTemplate" yaml:"listFileNameTemplate"`
108-
AsLists bool `json:"asLists" yaml:"asLists"`
109-
QueryPageSize int `json:"queryPageSize" yaml:"queryPageSize"`
110-
Target string `json:"target" yaml:"target"`
111-
ClearTarget bool `json:"clearTarget" yaml:"clearTarget"`
112-
Summary bool `json:"summary" yaml:"summary"`
113-
Progress Progress `json:"progress" yaml:"progress"`
114-
Namespace string `json:"namespace" yaml:"namespace"`
115-
Worker int `json:"worker" yaml:"worker"`
116-
Archive bool `json:"archive" yaml:"archive"`
117-
ArchiveRetentionDays int `json:"archiveRetentionDays" yaml:"archiveRetentionDays"`
118-
ArchiveTarget string `json:"archiveTarget" yaml:"archiveTarget"`
119-
Quiet bool `json:"quiet" yaml:"quiet"`
120-
Verbose bool `json:"verbose" yaml:"verbose"`
101+
Excluded Excluded `json:"excluded" yaml:"excluded"`
102+
Included Included `json:"included" yaml:"included"`
103+
CreatedWithin time.Duration `json:"createdWithin" yaml:"createdWithin"`
104+
ConsiderOwnerReferences bool `json:"considerOwnerReferences" yaml:"considerOwnerReferences"`
105+
Masked *Masked `json:"masked" yaml:"masked"`
106+
Encrypted *Encrypted `json:"encrypted" yaml:"masked"`
107+
SortSlices KindFields `json:"sortSlices" yaml:"sortSlices"`
108+
FileNameTemplate string `json:"fileNameTemplate" yaml:"fileNameTemplate"`
109+
ListFileNameTemplate string `json:"listFileNameTemplate" yaml:"listFileNameTemplate"`
110+
AsLists bool `json:"asLists" yaml:"asLists"`
111+
QueryPageSize int `json:"queryPageSize" yaml:"queryPageSize"`
112+
Target string `json:"target" yaml:"target"`
113+
ClearTarget bool `json:"clearTarget" yaml:"clearTarget"`
114+
Summary bool `json:"summary" yaml:"summary"`
115+
Progress Progress `json:"progress" yaml:"progress"`
116+
Namespace string `json:"namespace" yaml:"namespace"`
117+
Worker int `json:"worker" yaml:"worker"`
118+
Archive bool `json:"archive" yaml:"archive"`
119+
ArchiveRetentionDays int `json:"archiveRetentionDays" yaml:"archiveRetentionDays"`
120+
ArchiveTarget string `json:"archiveTarget" yaml:"archiveTarget"`
121+
Quiet bool `json:"quiet" yaml:"quiet"`
122+
Verbose bool `json:"verbose" yaml:"verbose"`
121123

122124
excludedSet set
123125
includedSet set
@@ -337,6 +339,9 @@ func (c *Config) IsInstanceExcluded(res *GroupResource, us unstructured.Unstruct
337339
if c.isExcludedByOwnerReference(us) {
338340
return true
339341
}
342+
if c.CreatedWithin > 0 && us.GetCreationTimestamp().Time.Before(time.Now().Add(-c.CreatedWithin)) {
343+
return true
344+
}
340345
if fvs, ok := c.Excluded.KindsByField[res.GroupKind()]; ok {
341346
for _, fv := range fvs {
342347
for _, v := range fv.Values {

0 commit comments

Comments
 (0)