-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: added tests, move logic to separate package
- Loading branch information
Showing
9 changed files
with
377 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,7 +21,7 @@ fmt: | |
go fmt ./... | ||
|
||
test: | ||
go test ./... | ||
go test -v ./... | ||
|
||
vet: | ||
go vet ./... | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,132 @@ | ||
package collector | ||
|
||
import ( | ||
"context" | ||
|
||
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" | ||
) | ||
|
||
const ( | ||
FLUX_HELM_NAME_LABEL = "helm.toolkit.fluxcd.io/name" | ||
FLUX_HELM_NAMESPACE_LABEL = "helm.toolkit.fluxcd.io/namespace" | ||
FLUX_KUSTOMIZE_NAME_LABEL = "kustomize.toolkit.fluxcd.io/name" | ||
FLUX_KUSTOMIZE_NAMESPACE_LABEL = "kustomize.toolkit.fluxcd.io/namespace" | ||
) | ||
|
||
type FilterFunc func(res unstructured.Unstructured, logger logger) bool | ||
|
||
type Interface interface { | ||
Discover(ctx context.Context, list *unstructured.UnstructuredList, ch chan unstructured.Unstructured) error | ||
} | ||
|
||
type logger interface { | ||
Debugf(format string, a ...interface{}) | ||
} | ||
|
||
type discovery struct { | ||
filters []FilterFunc | ||
logger logger | ||
} | ||
|
||
func NewDiscovery(logger logger, filters ...FilterFunc) Interface { | ||
return &discovery{ | ||
logger: logger, | ||
filters: filters, | ||
} | ||
} | ||
|
||
func (d *discovery) Discover(ctx context.Context, list *unstructured.UnstructuredList, ch chan unstructured.Unstructured) error { | ||
RESOURCES: | ||
for _, res := range list.Items { | ||
d.logger.Debugf("validate resource %s %s %s", res.GetName(), res.GetNamespace(), res.GetAPIVersion()) | ||
|
||
for _, filter := range d.filters { | ||
if filter(res, d.logger) { | ||
continue RESOURCES | ||
} | ||
} | ||
|
||
ch <- res | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func IgnoreOwnedResource() FilterFunc { | ||
return func(res unstructured.Unstructured, logger logger) bool { | ||
if refs := res.GetOwnerReferences(); len(refs) > 0 { | ||
logger.Debugf("ignore resource owned by parent %s %s %s", res.GetName(), res.GetNamespace(), res.GetAPIVersion()) | ||
return true | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
func IgnoreServiceAccountSecret() FilterFunc { | ||
return func(res unstructured.Unstructured, logger logger) bool { | ||
if res.GetKind() == "Secret" && res.GetAPIVersion() == "v1" { | ||
if _, ok := res.GetAnnotations()["kubernetes.io/service-account.name"]; ok { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
func IgnoreHelmSecret() FilterFunc { | ||
return func(res unstructured.Unstructured, logger logger) bool { | ||
if res.GetKind() == "Secret" && res.GetAPIVersion() == "v1" { | ||
if v, ok := res.GetLabels()["owner"]; ok && v == "helm" { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
func IgnoreIfHelmReleaseFound(helmReleases []unstructured.Unstructured) FilterFunc { | ||
return func(res unstructured.Unstructured, logger logger) bool { | ||
labels := res.GetLabels() | ||
if helmName, ok := labels[FLUX_HELM_NAME_LABEL]; ok { | ||
if helmNamespace, ok := labels[FLUX_HELM_NAMESPACE_LABEL]; ok { | ||
if hasResource(helmReleases, helmName, helmNamespace) { | ||
return true | ||
} else { | ||
logger.Debugf("helmrelease [%s.%s] not found from resource %s %s %s\n", helmName, helmNamespace, res.GetName(), res.GetNamespace(), res.GetAPIVersion()) | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
func IgnoreIfKustomizationFound(kustomizations []unstructured.Unstructured) FilterFunc { | ||
return func(res unstructured.Unstructured, logger logger) bool { | ||
labels := res.GetLabels() | ||
if ksName, ok := labels[FLUX_KUSTOMIZE_NAME_LABEL]; ok { | ||
if ksNamespace, ok := labels[FLUX_KUSTOMIZE_NAMESPACE_LABEL]; ok { | ||
if hasResource(kustomizations, ksName, ksNamespace) { | ||
return true | ||
} else { | ||
logger.Debugf("kustomization [%s.%s] not found from resource %s %s %s\n", ksName, ksNamespace, res.GetName(), res.GetNamespace(), res.GetAPIVersion()) | ||
} | ||
} | ||
} | ||
|
||
return false | ||
} | ||
} | ||
|
||
func hasResource(pool []unstructured.Unstructured, name, namespace string) bool { | ||
for _, res := range pool { | ||
if res.GetName() == name && res.GetNamespace() == namespace { | ||
return true | ||
} | ||
} | ||
|
||
return false | ||
} |
Oops, something went wrong.