yonahd / kor

A Golang Tool to discover unused Kubernetes Resources
MIT License
1.04k stars 96 forks source link

Refactor some codes, add filter objects, and add support for lable filtering... #194

Closed Forget-C closed 9 months ago

Forget-C commented 9 months ago

refactor&feat&fix&style:

  1. Unify parameter types and divide them into two categories: normal parameters kor.Opts and filter parameters filters.Options.
  2. includeExcludeLists no longer participates in cmd args binding, and is taken over by filters.Options.
  3. The initialization operations of includeNamespace and excludeNamespace are taken over by filters.Options and will only be executed once.
  4. Add filters.Framework object to uniformly process filtering logic.
  5. All currently supported resources are supported by the kor/used=true tag.
  6. Fix the problem that filterOptions.Validate does not take effect
  7. Support includeable parameters

The unit tests have passed, but due to the large scope of the changes, more tests are needed.

Forget-C commented 9 months ago

The original purpose was to abstract the filter framework to facilitate the addition of filter parameters. Other changes are necessary to achieve this goal. #187 now, If we want to add a filter, we only need the following steps:

create filter func

// FilterFunc is a filter that is a function
// If the resource is legal, return true
// example:
// deployment.Spec.Replicas > 0; return true
// meta.GetLabels()["kor/used"] == "true"; return true
type FilterFunc func(object runtime.Object, opts *Options) bool
func TestFilter(object runtime.Object, opts *Options) bool {
    if meta, ok := object.(metav1.Object); ok {
        if meta.GetLabels()["kor/used"] == "true" {
            return true
        }
    }
    return false
}

Set a name

const  TestFilterName = "test"

Add to registry

func NewDefaultRegistry() Registry {
    return Registry{
        LabelFilterName:    LabelFilter,
        AgeFilterName:      AgeFilter,
        KorLabelFilterName: KorLabelFilter,
                TestFilterName: TestFilter,
    }
}
codecov-commenter commented 9 months ago

Codecov Report

Attention: 220 lines in your changes are missing coverage. Please review.

Comparison is base (1e391dd) 45.87% compared to head (69bc5c5) 44.43%. Report is 1 commits behind head on main.

Files Patch % Lines
pkg/filters/options.go 0.00% 77 Missing :warning:
pkg/filters/frameworks.go 0.00% 46 Missing :warning:
pkg/filters/registry.go 0.00% 24 Missing :warning:
pkg/kor/all.go 0.00% 20 Missing :warning:
pkg/filters/filters.go 64.58% 12 Missing and 5 partials :warning:
pkg/kor/crds.go 0.00% 6 Missing :warning:
pkg/kor/multi.go 0.00% 5 Missing :warning:
pkg/kor/exporter.go 0.00% 4 Missing :warning:
pkg/kor/finalizers.go 33.33% 3 Missing and 1 partial :warning:
pkg/kor/ingresses.go 71.42% 0 Missing and 2 partials :warning:
... and 13 more

:exclamation: Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #194 +/- ## ========================================== - Coverage 45.87% 44.43% -1.45% ========================================== Files 25 29 +4 Lines 2413 2444 +31 ========================================== - Hits 1107 1086 -21 - Misses 1122 1205 +83 + Partials 184 153 -31 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.