Skip to content

Commit

Permalink
Allow non-glob and lower case targets (#306)
Browse files Browse the repository at this point in the history
* Allow non-glob and lower case targets

* Remove duplication, add needed Makefile change

* Fix tests, check for slashes
  • Loading branch information
malcolmholmes authored Jan 19, 2024
1 parent 44fbcb0 commit 3dba642
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.PHONY: lint test static install uninstall cross build-test-image run-test-image run-test-image-locally test-clean
.PHONY: dev lint test static install uninstall cross build-test-image run-test-image run-test-image-locally test-clean
VERSION := $(shell git describe --tags --dirty --always)
BIN_DIR := $(GOPATH)/bin
GOX := $(BIN_DIR)/gox
Expand Down
4 changes: 2 additions & 2 deletions pkg/grizzly/parsing.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ func ParseYAML(yamlFile string, opts Opts) (Resources, error) {
}
targets := currentContext.GetTargets(opts.Targets)
for _, parsedResource := range parsedResources {
if parsedResource.MatchesTarget(targets) {
if Registry.ResourceMatchesTarget(handler, parsedResource.UID(), targets) {
resources = append(resources, parsedResource)
}
}
Expand Down Expand Up @@ -249,7 +249,7 @@ func ParseJsonnet(jsonnetFile string, opts Opts) (Resources, error) {
return nil, err
}
for _, parsedResource := range parsedResources {
if parsedResource.MatchesTarget(targets) {
if Registry.ResourceMatchesTarget(handler, parsedResource.UID(), targets) {
resources = append(resources, parsedResource)
}
}
Expand Down
22 changes: 0 additions & 22 deletions pkg/grizzly/providers.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"encoding/json"
"fmt"

"github.com/gobwas/glob"
"github.com/grafana/tanka/pkg/kubernetes/manifest"
"gopkg.in/yaml.v3"
)
Expand Down Expand Up @@ -131,27 +130,6 @@ func (r *Resource) YAML() (string, error) {
return string(y), nil
}

// MatchesTarget identifies whether a resource is in a target list
func (r *Resource) MatchesTarget(targets []string) bool {
if len(targets) == 0 {
return true
}
UID := r.UID()
dotKey := r.Key()
slashKey := fmt.Sprintf("%s/%s", r.Kind(), UID)
for _, target := range targets {
g, err := glob.Compile(target)
if err != nil {
continue
}

if g.Match(slashKey) || g.Match(dotKey) {
return true
}
}
return false
}

// Resources represents a set of resources
type Resources []Resource

Expand Down
21 changes: 14 additions & 7 deletions pkg/grizzly/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ func (r *registry) HandlerMatchesTarget(handler Handler, targets []string) bool
if (strings.Contains(target, "/") && strings.Split(target, "/")[0] == key) ||
(strings.Contains(target, ".") && strings.Split(target, ".")[0] == key) {
return true
} else if strings.ToLower(target) == strings.ToLower(key) {
return true
}
}
return false
Expand All @@ -70,17 +72,22 @@ func (r *registry) ResourceMatchesTarget(handler Handler, UID string, targets []
if len(targets) == 0 {
return true
}
kind := handler.Kind()
// I mistakenly assumed 'dot' was a special character for globs, so opted for '/' as separator.
// This keeps back-compat
slashKey := fmt.Sprintf("%s/%s", handler.Kind(), UID)
dotKey := fmt.Sprintf("%s.%s", handler.Kind(), UID)
slashKey := fmt.Sprintf("%s/%s", kind, UID)
dotKey := fmt.Sprintf("%s.%s", kind, UID)
for _, target := range targets {
g, err := glob.Compile(target)
if err != nil {
continue
}
if strings.Contains(target, ".") || strings.Contains(target, "/") {
g, err := glob.Compile(target)
if err != nil {
continue
}

if g.Match(slashKey) || g.Match(dotKey) {
if g.Match(slashKey) || g.Match(dotKey) {
return true
}
} else if strings.ToLower(target) == strings.ToLower(kind) {
return true
}
}
Expand Down

0 comments on commit 3dba642

Please sign in to comment.