Skip to content

Commit

Permalink
Add option to export only recently created resources (#436)
Browse files Browse the repository at this point in the history
* add option to export only recent resources
  • Loading branch information
bakito authored Aug 19, 2024
1 parent 487adac commit 52ec705
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 97 deletions.
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ MOCKGEN ?= $(LOCALBIN)/mockgen
SEMVER ?= $(LOCALBIN)/semver

## Tool Versions
GOLANGCI_LINT_VERSION ?= v1.59.1
GORELEASER_VERSION ?= v2.0.1
GOLANGCI_LINT_VERSION ?= v1.60.1
GORELEASER_VERSION ?= v2.2.0
MOCKGEN_VERSION ?= v0.4.0
SEMVER_VERSION ?= v1.1.3

Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ Flags:
--cluster string The name of the kubeconfig cluster to use
--config string config file
--context string The name of the kubeconfig context to use
--created-within duration The max allowed age duration for the resources
--disable-compression If true, opt-out of response compression for all requests to the server
-e, --exclude-kinds strings Do not export excluded kinds
-h, --help help for kubexporter
Expand Down
4 changes: 4 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ func readConfig(cmd *cobra.Command, configFlags *genericclioptions.ConfigFlags,
case "exclude-kinds":
sl, _ := cmd.Flags().GetStringSlice(f.Name)
config.Excluded.Kinds = sl
case "created-within":
cw, _ := cmd.Flags().GetDuration(f.Name)
config.CreatedWithin = cw
case "archive":
sl, _ := cmd.Flags().GetBool(f.Name)
config.Archive = sl
Expand Down Expand Up @@ -123,6 +126,7 @@ func init() {
rootCmd.Flags().BoolP("lists", "l", false, "If enabled, all resources are exported as lists instead of individual files")
rootCmd.Flags().StringSliceP("include-kinds", "i", []string{}, "Export only included kinds, if included kinds are defined, excluded will be ignored")
rootCmd.Flags().StringSliceP("exclude-kinds", "e", []string{}, "Do not export excluded kinds")
rootCmd.Flags().Duration("created-within", 0, "The max allowed age duration for the resources")

configFlags = genericclioptions.NewConfigFlags(true)
configFlags.AddFlags(rootCmd.Flags())
Expand Down
1 change: 1 addition & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace:
asLists: false
#queryPageSize: 1000
clearTarget: true
#createdWithin: 24h
worker: 5
encrypted:
aesKey: "12345678901234567890123456789012"
Expand Down
3 changes: 0 additions & 3 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ require (
github.com/chai2010/gettext-go v1.0.2 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/exponent-io/jsonpath v0.0.0-20210407135951-1de76d718b3f // indirect
github.com/fxamacker/cbor/v2 v2.7.0 // indirect
github.com/go-errors/errors v1.5.1 // indirect
Expand Down Expand Up @@ -74,9 +73,7 @@ require (
github.com/mxk/go-flowrate v0.0.0-20140419014527-cca7078d478f // indirect
github.com/peterbourgon/diskv v2.0.1+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/rogpeppe/go-internal v1.12.0 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/stretchr/objx v0.5.2 // indirect
Expand Down
71 changes: 2 additions & 69 deletions go.sum

Large diffs are not rendered by default.

6 changes: 4 additions & 2 deletions pkg/export/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,9 @@ func (e *exporter) writeIntro() {
if len(e.config.Encrypted.KindFields) > 0 {
e.l.Printf(" encrypted fields 🔒 %v\n", e.config.Encrypted.KindFields)
}

if e.config.CreatedWithin > 0 {
e.l.Printf(" created within %s ⏱️\n", e.config.CreatedWithin.String())
}
if e.config.AsLists {
e.l.Printf(" as lists 📦\n")
} else if e.config.QueryPageSize != 0 {
Expand Down Expand Up @@ -239,7 +241,7 @@ func (e *exporter) printSummary(resources []*types.GroupResource) {
if e.config.Worker > 1 {
total = "CUMULATED " + total
}
totalRow := []string{total, "", "", "", strconv.Itoa(inst), strconv.Itoa(totalInst), qd.Sub(start).String()}
totalRow := []string{total, "", "", "", strconv.Itoa(totalInst), strconv.Itoa(inst), qd.Sub(start).String()}
if withPages {
totalRow = append(totalRow, strconv.Itoa(pages))
}
Expand Down
47 changes: 26 additions & 21 deletions pkg/types/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"sort"
"strings"
"text/template"
"time"

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

// Config export config
type Config struct {
Excluded Excluded `json:"excluded" yaml:"excluded"`
Included Included `json:"included" yaml:"included"`
ConsiderOwnerReferences bool `json:"considerOwnerReferences" yaml:"considerOwnerReferences"`
Masked *Masked `json:"masked" yaml:"masked"`
Encrypted *Encrypted `json:"encrypted" yaml:"masked"`
SortSlices KindFields `json:"sortSlices" yaml:"sortSlices"`
FileNameTemplate string `json:"fileNameTemplate" yaml:"fileNameTemplate"`
ListFileNameTemplate string `json:"listFileNameTemplate" yaml:"listFileNameTemplate"`
AsLists bool `json:"asLists" yaml:"asLists"`
QueryPageSize int `json:"queryPageSize" yaml:"queryPageSize"`
Target string `json:"target" yaml:"target"`
ClearTarget bool `json:"clearTarget" yaml:"clearTarget"`
Summary bool `json:"summary" yaml:"summary"`
Progress Progress `json:"progress" yaml:"progress"`
Namespace string `json:"namespace" yaml:"namespace"`
Worker int `json:"worker" yaml:"worker"`
Archive bool `json:"archive" yaml:"archive"`
ArchiveRetentionDays int `json:"archiveRetentionDays" yaml:"archiveRetentionDays"`
ArchiveTarget string `json:"archiveTarget" yaml:"archiveTarget"`
Quiet bool `json:"quiet" yaml:"quiet"`
Verbose bool `json:"verbose" yaml:"verbose"`
Excluded Excluded `json:"excluded" yaml:"excluded"`
Included Included `json:"included" yaml:"included"`
CreatedWithin time.Duration `json:"createdWithin" yaml:"createdWithin"`
ConsiderOwnerReferences bool `json:"considerOwnerReferences" yaml:"considerOwnerReferences"`
Masked *Masked `json:"masked" yaml:"masked"`
Encrypted *Encrypted `json:"encrypted" yaml:"masked"`
SortSlices KindFields `json:"sortSlices" yaml:"sortSlices"`
FileNameTemplate string `json:"fileNameTemplate" yaml:"fileNameTemplate"`
ListFileNameTemplate string `json:"listFileNameTemplate" yaml:"listFileNameTemplate"`
AsLists bool `json:"asLists" yaml:"asLists"`
QueryPageSize int `json:"queryPageSize" yaml:"queryPageSize"`
Target string `json:"target" yaml:"target"`
ClearTarget bool `json:"clearTarget" yaml:"clearTarget"`
Summary bool `json:"summary" yaml:"summary"`
Progress Progress `json:"progress" yaml:"progress"`
Namespace string `json:"namespace" yaml:"namespace"`
Worker int `json:"worker" yaml:"worker"`
Archive bool `json:"archive" yaml:"archive"`
ArchiveRetentionDays int `json:"archiveRetentionDays" yaml:"archiveRetentionDays"`
ArchiveTarget string `json:"archiveTarget" yaml:"archiveTarget"`
Quiet bool `json:"quiet" yaml:"quiet"`
Verbose bool `json:"verbose" yaml:"verbose"`

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

0 comments on commit 52ec705

Please sign in to comment.