Add filtering by label to Toolbox container

Relates-To: #517

Change-Id: If2104fe87bf084d3d6e4fe602b0501dc3452349e
This commit is contained in:
Kostiantyn Kalynovskyi 2021-05-04 17:56:18 +00:00
parent 0caba8690d
commit b843a92a7b
2 changed files with 31 additions and 4 deletions

View File

@ -41,6 +41,9 @@ const (
ResourceVersionFilter = "RESOURCE_VERSION_FILTER"
// ResourceKindFilter used for filtering input bundle by document kind
ResourceKindFilter = "RESOURCE_KIND_FILTER"
// ResourceLabelFilter used for filtering input bundle by label selector
// example airshipit.org/ephemeral=true
ResourceLabelFilter = "RESOURCE_LABEL_FILTER"
scriptPath = "script.sh"
scriptKey = "script"
bundleFile = "bundle.yaml"
@ -168,9 +171,11 @@ func (c *ScriptRunner) FilterBundle(items []*kyaml.RNode) ([]*kyaml.RNode, error
group := os.Getenv(ResourceGroupFilter)
version := os.Getenv(ResourceVersionFilter)
kind := os.Getenv(ResourceKindFilter)
labelSelector := os.Getenv(ResourceLabelFilter)
log.Printf("Filtering input bundle by Group: %s, Version: %s, Kind: %s",
group, version, kind)
return kyamlutils.DocumentSelector{}.
ByGVK(group, version, kind).
ByLabel(labelSelector).
Filter(items)
}

View File

@ -174,6 +174,8 @@ func TestFilterBundle(t *testing.T) {
apiVersion: test/v1
kind: Pod
metadata:
labels:
app: nginx
name: t1
---
apiVersion: test/v1
@ -195,6 +197,7 @@ metadata:
groupFilter string
versionFilter string
kindFilter string
labelFilter string
errContains string
expectedDocs string
}{
@ -208,6 +211,8 @@ metadata:
expectedDocs: `apiVersion: test/v1
kind: Pod
metadata:
labels:
app: nginx
name: t1
`,
},
@ -234,6 +239,8 @@ metadata:
expectedDocs: `apiVersion: test/v1
kind: Pod
metadata:
labels:
app: nginx
name: t1
---
apiVersion: test/v1
@ -245,6 +252,19 @@ apiVersion: test/v1beta1
kind: Deployment
metadata:
name: t3
`,
},
{
name: "Filter by label only documents",
inputDocs: docs,
labelFilter: "app=nginx",
errContains: "",
expectedDocs: `apiVersion: test/v1
kind: Pod
metadata:
labels:
app: nginx
name: t1
`,
},
}
@ -270,6 +290,8 @@ metadata:
defer os.Unsetenv(ResourceVersionFilter)
os.Setenv(ResourceKindFilter, tc.kindFilter)
defer os.Unsetenv(ResourceKindFilter)
os.Setenv(ResourceLabelFilter, tc.labelFilter)
defer os.Unsetenv(ResourceLabelFilter)
cmd := &ScriptRunner{
ScriptFile: targetFile,