|
| 1 | +package kube_janitor |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/goccy/go-yaml" |
| 11 | + "github.com/jmespath-community/go-jmespath" |
| 12 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + JmesPath struct { |
| 17 | + Path string |
| 18 | + compiledPath jmespath.JMESPath |
| 19 | + } |
| 20 | +) |
| 21 | + |
| 22 | +func (path *JmesPath) IsEmpty() bool { |
| 23 | + return path == nil || path.compiledPath == nil |
| 24 | +} |
| 25 | + |
| 26 | +func UmarshallJmesPath(ctx context.Context, path *JmesPath, data []byte) error { |
| 27 | + var valString string |
| 28 | + |
| 29 | + err := yaml.UnmarshalContext(ctx, data, &valString, yaml.Strict()) |
| 30 | + if err != nil { |
| 31 | + return fmt.Errorf(`failed to parse jmespath as string: %w`, err) |
| 32 | + } |
| 33 | + |
| 34 | + valString = strings.TrimSpace(valString) |
| 35 | + |
| 36 | + if valString != "" { |
| 37 | + compiledPath, err := jmespath.Compile(valString) |
| 38 | + if err != nil { |
| 39 | + return fmt.Errorf(`failed to compile jmespath "%s": %w`, valString, err) |
| 40 | + } |
| 41 | + |
| 42 | + path.Path = valString |
| 43 | + path.compiledPath = compiledPath |
| 44 | + } |
| 45 | + |
| 46 | + return nil |
| 47 | +} |
| 48 | + |
| 49 | +// |
| 50 | +// func (path *JmesPath) UnmarshalJSON(data []byte) error { |
| 51 | +// var valString string |
| 52 | +// |
| 53 | +// err := json.Unmarshal(data, &valString) |
| 54 | +// if err != nil { |
| 55 | +// return fmt.Errorf(`failed to parse jmespath as string: %w`, err) |
| 56 | +// } |
| 57 | +// |
| 58 | +// valString = strings.TrimSpace(valString) |
| 59 | +// |
| 60 | +// if valString != "" { |
| 61 | +// compiledPath, err := jmespath.Compile(valString) |
| 62 | +// if err != nil { |
| 63 | +// return fmt.Errorf(`failed to compile jmespath "%s": %w`, valString, err) |
| 64 | +// } |
| 65 | +// |
| 66 | +// path.Path = valString |
| 67 | +// path.compiledPath = compiledPath |
| 68 | +// } |
| 69 | +// |
| 70 | +// return nil |
| 71 | +// } |
| 72 | + |
| 73 | +func (j *Janitor) fetchResourceValueByFromJmesPath(resource unstructured.Unstructured, jmesPath *JmesPath) (interface{}, error) { |
| 74 | + resourceRaw, err := resource.MarshalJSON() |
| 75 | + if err != nil { |
| 76 | + return true, err |
| 77 | + } |
| 78 | + |
| 79 | + var data any |
| 80 | + err = json.Unmarshal(resourceRaw, &data) |
| 81 | + if err != nil { |
| 82 | + return true, err |
| 83 | + } |
| 84 | + |
| 85 | + // check if resource is valid by JMES path |
| 86 | + result, err := jmesPath.compiledPath.Search(data) |
| 87 | + if err != nil { |
| 88 | + return true, err |
| 89 | + } |
| 90 | + |
| 91 | + return result, nil |
| 92 | +} |
| 93 | + |
| 94 | +func (j *Janitor) checkResourceIsSkippedFromJmesPath(resource unstructured.Unstructured, jmesPath *JmesPath) (bool, error) { |
| 95 | + result, err := j.fetchResourceValueByFromJmesPath(resource, jmesPath) |
| 96 | + if err != nil { |
| 97 | + return true, err |
| 98 | + } |
| 99 | + |
| 100 | + switch v := result.(type) { |
| 101 | + case string: |
| 102 | + // skip if string is empty |
| 103 | + if len(v) == 0 { |
| 104 | + return true, nil |
| 105 | + } |
| 106 | + case bool: |
| 107 | + // skip if false (not selected) |
| 108 | + return !v, nil |
| 109 | + case nil: |
| 110 | + // nil? jmes path didn't find anything? better skip the resource |
| 111 | + return true, nil |
| 112 | + } |
| 113 | + |
| 114 | + return false, nil |
| 115 | +} |
| 116 | + |
| 117 | +func (j *Janitor) parseResourceTimestampFromJmesPath(resource unstructured.Unstructured, jmesPath *JmesPath) (*time.Time, error) { |
| 118 | + result, err := j.fetchResourceValueByFromJmesPath(resource, jmesPath) |
| 119 | + fmt.Println(jmesPath.Path, result, err) |
| 120 | + if err != nil { |
| 121 | + return nil, err |
| 122 | + } |
| 123 | + |
| 124 | + switch v := result.(type) { |
| 125 | + case string: |
| 126 | + // skip if string is empty |
| 127 | + if timestamp := j.parseTimestamp(v); timestamp != nil { |
| 128 | + return timestamp, nil |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + return nil, nil |
| 133 | +} |
0 commit comments