@@ -24,68 +24,88 @@ var availableKinds = []string{
2424}
2525
2626type 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
87107func (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
91111func (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