Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion cli/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type GlobalOptions struct {
File string
Namespace string
Selector string
Exclude string
TemplateDirs []string
ParamDirs []string
PublicKeyDir string
Expand Down Expand Up @@ -102,6 +103,9 @@ func (o *GlobalOptions) UpdateWithFile(fileFlags map[string]string) {
if val, ok := fileFlags["selector"]; ok {
o.Selector = val
}
if val, ok := fileFlags["exclude"]; ok {
o.Exclude = val
}
if val, ok := fileFlags["template-dir"]; ok {
o.TemplateDirs = strings.Split(val, ",")
}
Expand All @@ -122,7 +126,7 @@ func (o *GlobalOptions) UpdateWithFile(fileFlags map[string]string) {
}
}

func (o *GlobalOptions) UpdateWithFlags(verboseFlag bool, debugFlag bool, nonInteractiveFlag bool, ocBinaryFlag string, namespaceFlag string, selectorFlag string, templateDirFlag []string, paramDirFlag []string, publicKeyDirFlag string, privateKeyFlag string, passphraseFlag string, forceFlag bool) {
func (o *GlobalOptions) UpdateWithFlags(verboseFlag bool, debugFlag bool, nonInteractiveFlag bool, ocBinaryFlag string, namespaceFlag string, selectorFlag string, excludeFlag string, templateDirFlag []string, paramDirFlag []string, publicKeyDirFlag string, privateKeyFlag string, passphraseFlag string, forceFlag bool) {
if verboseFlag {
o.Verbose = true
}
Expand All @@ -147,6 +151,10 @@ func (o *GlobalOptions) UpdateWithFlags(verboseFlag bool, debugFlag bool, nonInt
o.Selector = selectorFlag
}

if len(excludeFlag) > 0 {
o.Exclude = excludeFlag
}

if len(o.TemplateDirs) == 0 {
o.TemplateDirs = templateDirFlag
} else if len(templateDirFlag) > 1 || templateDirFlag[0] != "." {
Expand Down
2 changes: 1 addition & 1 deletion commands/export.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

// Export prints an export of targeted resources to STDOUT.
func Export(exportOptions *cli.ExportOptions) error {
filter, err := openshift.NewResourceFilter(exportOptions.Resource, exportOptions.Selector)
filter, err := openshift.NewResourceFilter(exportOptions.Resource, exportOptions.Selector, exportOptions.Exclude)
if err != nil {
return err
}
Expand Down
3 changes: 1 addition & 2 deletions commands/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,8 @@ func calculateChangeset(compareOptions *cli.CompareOptions) (bool, *openshift.Ch
}

resource := compareOptions.Resource
selectorFlag := compareOptions.Selector

filter, err := openshift.NewResourceFilter(resource, selectorFlag)
filter, err := openshift.NewResourceFilter(resource, compareOptions.Selector, compareOptions.Exclude)
if err != nil {
return updateRequired, &openshift.Changeset{}, err
}
Expand Down
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ var (
"selector",
"Selector (label query) to filter on",
).Short('l').String()
excludeFlag = app.Flag(
"exclude",
"Exclude kinds, names and labels (comma separated)",
).Short('e').String()
templateDirFlag = app.Flag(
"template-dir",
"Path to local templates",
Expand Down Expand Up @@ -224,6 +228,7 @@ func main() {
*ocBinaryFlag,
*namespaceFlag,
*selectorFlag,
*excludeFlag,
*templateDirFlag,
*paramDirFlag,
*publicKeyDirFlag,
Expand Down
2 changes: 1 addition & 1 deletion openshift/change_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func TestDiff(t *testing.T) {
actualPatches := change.Patches
if !reflect.DeepEqual(actualPatches, tt.expectedPatches) {
t.Errorf(
"Diff()\n===== expected =====\n%s\n===== actual =====\n%s",
"Diff()\n===== expected =====\n%v\n===== actual =====\n%v",
tt.expectedPatches,
actualPatches,
)
Expand Down
126 changes: 88 additions & 38 deletions openshift/filter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,68 +24,101 @@ var availableKinds = []string{
}

type ResourceFilter struct {
Kinds []string
Name string
Label string
Kinds []string
Name string
Label string
ExcludedKinds []string
ExcludedNames []string
ExcludedLabels []string
}

// NewResourceFilter returns a filter based on kinds and flags.
// kindArg might be blank, or a list of kinds (e.g. 'pvc,dc') or
// a kind/name combination (e.g. 'dc/foo').
// selectorFlag might be blank or a key and a label, e.g. 'name=foo'.
func NewResourceFilter(kindArg string, selectorFlag string) (*ResourceFilter, error) {
func NewResourceFilter(kindArg string, selectorFlag string, excludeFlag string) (*ResourceFilter, error) {
filter := &ResourceFilter{
Kinds: []string{},
Name: "",
Label: selectorFlag,
}

if len(kindArg) == 0 {
return filter, nil
}
if len(kindArg) > 0 {
kindArg = strings.ToLower(kindArg)

if strings.Contains(kindArg, "/") {
if strings.Contains(kindArg, ",") {
return nil, errors.New(
"You cannot target more than one resource name",
)
}
nameParts := strings.Split(kindArg, "/")
filter.Name = KindMapping[nameParts[0]] + "/" + nameParts[1]
return filter, nil
}

kindArg = strings.ToLower(kindArg)
targetedKinds := make(map[string]bool)
unknownKinds := []string{}
kinds := strings.Split(kindArg, ",")
for _, kind := range kinds {
if _, ok := KindMapping[kind]; !ok {
unknownKinds = append(unknownKinds, kind)
} else {
targetedKinds[KindMapping[kind]] = true
}
}

if strings.Contains(kindArg, "/") {
if strings.Contains(kindArg, ",") {
return nil, errors.New(
"You cannot target more than one resource name",
if len(unknownKinds) > 0 {
return nil, fmt.Errorf(
"Unknown resource kinds: %s",
strings.Join(unknownKinds, ","),
)
}
nameParts := strings.Split(kindArg, "/")
filter.Name = KindMapping[nameParts[0]] + "/" + nameParts[1]
return filter, nil
}

targetedKinds := make(map[string]bool)
unknownKinds := []string{}
kinds := strings.Split(kindArg, ",")
for _, kind := range kinds {
if _, ok := KindMapping[kind]; !ok {
unknownKinds = append(unknownKinds, kind)
} else {
targetedKinds[KindMapping[kind]] = true
for kind := range targetedKinds {
filter.Kinds = append(filter.Kinds, kind)
}
}

if len(unknownKinds) > 0 {
return nil, fmt.Errorf(
"Unknown resource kinds: %s",
strings.Join(unknownKinds, ","),
)
sort.Strings(filter.Kinds)
}

for kind := range targetedKinds {
filter.Kinds = append(filter.Kinds, kind)
}
if len(excludeFlag) > 0 {
unknownKinds := []string{}
excludes := strings.Split(excludeFlag, ",")
for _, v := range excludes {
v = strings.ToLower(v)
if strings.Contains(v, "/") { // Name
nameParts := strings.Split(v, "/")
k := nameParts[0]
if _, ok := KindMapping[k]; !ok {
unknownKinds = append(unknownKinds, k)
} else {
filter.ExcludedNames = append(filter.ExcludedNames, KindMapping[k]+"/"+nameParts[1])
}
} else if strings.Contains(v, "=") { // Label
filter.ExcludedLabels = append(filter.ExcludedLabels, v)
} else { // Kind
if _, ok := KindMapping[v]; !ok {
unknownKinds = append(unknownKinds, v)
} else {
filter.ExcludedKinds = append(filter.ExcludedKinds, KindMapping[v])
}
}
}

sort.Strings(filter.Kinds)
if len(unknownKinds) > 0 {
return nil, fmt.Errorf(
"Unknown excluded resource kinds: %s",
strings.Join(unknownKinds, ","),
)
}
}

return filter, nil
}

func (f *ResourceFilter) String() string {
return fmt.Sprintf("Kind: %s, Name: %s, Label: %s", f.Kinds, f.Name, f.Label)
return fmt.Sprintf("Kinds: %s, Name: %s, Label: %s, ExcludedKinds: %s, ExcludedNames: %s, ExcludedLabels: %s", f.Kinds, f.Name, f.Label, f.ExcludedKinds, f.ExcludedNames, f.ExcludedLabels)
}

func (f *ResourceFilter) SatisfiedBy(item *ResourceItem) bool {
Expand All @@ -100,10 +133,27 @@ func (f *ResourceFilter) SatisfiedBy(item *ResourceItem) bool {
if len(f.Label) > 0 {
labels := strings.Split(f.Label, ",")
for _, label := range labels {
labelParts := strings.Split(label, "=")
if _, ok := item.Labels[labelParts[0]]; !ok {
if !item.HasLabel(label) {
return false
} else if item.Labels[labelParts[0]].(string) != labelParts[1] {
}
}
}

if len(f.ExcludedNames) > 0 {
if utils.Includes(f.ExcludedNames, item.FullName()) {
return false
}
}

if len(f.ExcludedKinds) > 0 {
if utils.Includes(f.ExcludedKinds, item.Kind) {
return false
}
}

if len(f.ExcludedLabels) > 0 {
for _, el := range f.ExcludedLabels {
if item.HasLabel(el) {
return false
}
}
Expand Down
Loading