Skip to content

Commit 18f17a4

Browse files
author
Michael Sauter
committed
Add flag to exclude kinds, names and labels
1 parent 017506b commit 18f17a4

File tree

7 files changed

+216
-49
lines changed

7 files changed

+216
-49
lines changed

cli/options.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ type GlobalOptions struct {
1717
File string
1818
Namespace string
1919
Selector string
20+
Exclude string
2021
TemplateDirs []string
2122
ParamDirs []string
2223
PublicKeyDir string
@@ -102,6 +103,9 @@ func (o *GlobalOptions) UpdateWithFile(fileFlags map[string]string) {
102103
if val, ok := fileFlags["selector"]; ok {
103104
o.Selector = val
104105
}
106+
if val, ok := fileFlags["exclude"]; ok {
107+
o.Exclude = val
108+
}
105109
if val, ok := fileFlags["template-dir"]; ok {
106110
o.TemplateDirs = strings.Split(val, ",")
107111
}
@@ -122,7 +126,7 @@ func (o *GlobalOptions) UpdateWithFile(fileFlags map[string]string) {
122126
}
123127
}
124128

125-
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) {
129+
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) {
126130
if verboseFlag {
127131
o.Verbose = true
128132
}
@@ -147,6 +151,10 @@ func (o *GlobalOptions) UpdateWithFlags(verboseFlag bool, debugFlag bool, nonInt
147151
o.Selector = selectorFlag
148152
}
149153

154+
if len(excludeFlag) > 0 {
155+
o.Exclude = excludeFlag
156+
}
157+
150158
if len(o.TemplateDirs) == 0 {
151159
o.TemplateDirs = templateDirFlag
152160
} else if len(templateDirFlag) > 1 || templateDirFlag[0] != "." {

commands/export.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99

1010
// Export prints an export of targeted resources to STDOUT.
1111
func Export(exportOptions *cli.ExportOptions) error {
12-
filter, err := openshift.NewResourceFilter(exportOptions.Resource, exportOptions.Selector)
12+
filter, err := openshift.NewResourceFilter(exportOptions.Resource, exportOptions.Selector, exportOptions.Exclude)
1313
if err != nil {
1414
return err
1515
}

commands/status.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,8 @@ func calculateChangeset(compareOptions *cli.CompareOptions) (bool, *openshift.Ch
4949
}
5050

5151
resource := compareOptions.Resource
52-
selectorFlag := compareOptions.Selector
5352

54-
filter, err := openshift.NewResourceFilter(resource, selectorFlag)
53+
filter, err := openshift.NewResourceFilter(resource, compareOptions.Selector, compareOptions.Exclude)
5554
if err != nil {
5655
return updateRequired, &openshift.Changeset{}, err
5756
}

main.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ var (
4545
"selector",
4646
"Selector (label query) to filter on",
4747
).Short('l').String()
48+
excludeFlag = app.Flag(
49+
"exclude",
50+
"Exclude kinds, names and labels (comma separated)",
51+
).Short('e').String()
4852
templateDirFlag = app.Flag(
4953
"template-dir",
5054
"Path to local templates",
@@ -224,6 +228,7 @@ func main() {
224228
*ocBinaryFlag,
225229
*namespaceFlag,
226230
*selectorFlag,
231+
*excludeFlag,
227232
*templateDirFlag,
228233
*paramDirFlag,
229234
*publicKeyDirFlag,

openshift/filter.go

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,68 +24,88 @@ var availableKinds = []string{
2424
}
2525

2626
type ResourceFilter struct {
27-
Kinds []string
28-
Name string
29-
Label string
27+
Kinds []string
28+
Name string
29+
Label string
30+
ExcludedKinds []string
31+
ExcludedNames []string
32+
ExcludedLabels []string
3033
}
3134

3235
// NewResourceFilter returns a filter based on kinds and flags.
3336
// kindArg might be blank, or a list of kinds (e.g. 'pvc,dc') or
3437
// a kind/name combination (e.g. 'dc/foo').
3538
// selectorFlag might be blank or a key and a label, e.g. 'name=foo'.
36-
func NewResourceFilter(kindArg string, selectorFlag string) (*ResourceFilter, error) {
39+
func NewResourceFilter(kindArg string, selectorFlag string, excludeFlag string) (*ResourceFilter, error) {
3740
filter := &ResourceFilter{
3841
Kinds: []string{},
3942
Name: "",
4043
Label: selectorFlag,
4144
}
4245

43-
if len(kindArg) == 0 {
44-
return filter, nil
45-
}
46+
if len(kindArg) > 0 {
47+
kindArg = strings.ToLower(kindArg)
48+
49+
if strings.Contains(kindArg, "/") {
50+
if strings.Contains(kindArg, ",") {
51+
return nil, errors.New(
52+
"You cannot target more than one resource name",
53+
)
54+
}
55+
nameParts := strings.Split(kindArg, "/")
56+
filter.Name = KindMapping[nameParts[0]] + "/" + nameParts[1]
57+
return filter, nil
58+
}
4659

47-
kindArg = strings.ToLower(kindArg)
60+
targetedKinds := make(map[string]bool)
61+
unknownKinds := []string{}
62+
kinds := strings.Split(kindArg, ",")
63+
for _, kind := range kinds {
64+
if _, ok := KindMapping[kind]; !ok {
65+
unknownKinds = append(unknownKinds, kind)
66+
} else {
67+
targetedKinds[KindMapping[kind]] = true
68+
}
69+
}
4870

49-
if strings.Contains(kindArg, "/") {
50-
if strings.Contains(kindArg, ",") {
51-
return nil, errors.New(
52-
"You cannot target more than one resource name",
71+
if len(unknownKinds) > 0 {
72+
return nil, fmt.Errorf(
73+
"Unknown resource kinds: %s",
74+
strings.Join(unknownKinds, ","),
5375
)
5476
}
55-
nameParts := strings.Split(kindArg, "/")
56-
filter.Name = KindMapping[nameParts[0]] + "/" + nameParts[1]
57-
return filter, nil
58-
}
5977

60-
targetedKinds := make(map[string]bool)
61-
unknownKinds := []string{}
62-
kinds := strings.Split(kindArg, ",")
63-
for _, kind := range kinds {
64-
if _, ok := KindMapping[kind]; !ok {
65-
unknownKinds = append(unknownKinds, kind)
66-
} else {
67-
targetedKinds[KindMapping[kind]] = true
78+
for kind := range targetedKinds {
79+
filter.Kinds = append(filter.Kinds, kind)
6880
}
69-
}
7081

71-
if len(unknownKinds) > 0 {
72-
return nil, fmt.Errorf(
73-
"Unknown resource kinds: %s",
74-
strings.Join(unknownKinds, ","),
75-
)
82+
sort.Strings(filter.Kinds)
7683
}
7784

78-
for kind := range targetedKinds {
79-
filter.Kinds = append(filter.Kinds, kind)
85+
if len(excludeFlag) > 0 {
86+
unknownKinds := []string{}
87+
excludes := strings.Split(excludeFlag, ",")
88+
for _, v := range excludes {
89+
if strings.Contains(v, "/") { // Name
90+
nameParts := strings.Split(v, "/")
91+
filter.ExcludedNames = append(filter.ExcludedNames, KindMapping[nameParts[0]]+"/"+nameParts[1])
92+
} else if strings.Contains(v, "=") { // Label
93+
filter.ExcludedLabels = append(filter.ExcludedLabels, v)
94+
} else { // Kind
95+
if _, ok := KindMapping[v]; !ok {
96+
unknownKinds = append(unknownKinds, v)
97+
} else {
98+
filter.ExcludedKinds = append(filter.ExcludedKinds, KindMapping[v])
99+
}
100+
}
101+
}
80102
}
81103

82-
sort.Strings(filter.Kinds)
83-
84104
return filter, nil
85105
}
86106

87107
func (f *ResourceFilter) String() string {
88-
return fmt.Sprintf("Kind: %s, Name: %s, Label: %s", f.Kinds, f.Name, f.Label)
108+
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)
89109
}
90110

91111
func (f *ResourceFilter) SatisfiedBy(item *ResourceItem) bool {
@@ -100,10 +120,27 @@ func (f *ResourceFilter) SatisfiedBy(item *ResourceItem) bool {
100120
if len(f.Label) > 0 {
101121
labels := strings.Split(f.Label, ",")
102122
for _, label := range labels {
103-
labelParts := strings.Split(label, "=")
104-
if _, ok := item.Labels[labelParts[0]]; !ok {
123+
if !item.HasLabel(label) {
105124
return false
106-
} else if item.Labels[labelParts[0]].(string) != labelParts[1] {
125+
}
126+
}
127+
}
128+
129+
if len(f.ExcludedNames) > 0 {
130+
if utils.Includes(f.ExcludedNames, item.FullName()) {
131+
return false
132+
}
133+
}
134+
135+
if len(f.ExcludedKinds) > 0 {
136+
if utils.Includes(f.ExcludedKinds, item.Kind) {
137+
return false
138+
}
139+
}
140+
141+
if len(f.ExcludedLabels) > 0 {
142+
for _, el := range f.ExcludedLabels {
143+
if item.HasLabel(el) {
107144
return false
108145
}
109146
}

0 commit comments

Comments
 (0)