-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6881 from ywk253100/230927_cherry_pick
Add 'orLabelSelector' for backup, restore command
- Loading branch information
Showing
13 changed files
with
242 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Add `orLabelSelectors` for backup, restore commands |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright 2017 the Velero contributors. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package flag | ||
|
||
import ( | ||
"strings" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
// OrLabelSelector is a Cobra-compatible wrapper for defining | ||
// a Kubernetes or-label-selector flag. | ||
type OrLabelSelector struct { | ||
OrLabelSelectors []*metav1.LabelSelector | ||
} | ||
|
||
// String returns a string representation of the or-label | ||
// selector flag. | ||
func (ls *OrLabelSelector) String() string { | ||
orLabels := []string{} | ||
for _, v := range ls.OrLabelSelectors { | ||
orLabels = append(orLabels, metav1.FormatLabelSelector(v)) | ||
} | ||
return strings.Join(orLabels, " or ") | ||
} | ||
|
||
// Set parses the provided string and assigns the result | ||
// to the or-label-selector receiver. It returns an error if | ||
// the string is not parseable. | ||
func (ls *OrLabelSelector) Set(s string) error { | ||
orItems := strings.Split(s, " or ") | ||
ls.OrLabelSelectors = make([]*metav1.LabelSelector, 0) | ||
for _, orItem := range orItems { | ||
parsed, err := metav1.ParseToLabelSelector(orItem) | ||
if err != nil { | ||
return err | ||
} | ||
ls.OrLabelSelectors = append(ls.OrLabelSelectors, parsed) | ||
} | ||
return nil | ||
} | ||
|
||
// Type returns a string representation of the | ||
// OrLabelSelector type. | ||
func (ls *OrLabelSelector) Type() string { | ||
return "orLabelSelector" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package flag | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
func TestStringOfOrLabelSelector(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
orLabelSelector *OrLabelSelector | ||
expectedStr string | ||
}{ | ||
{ | ||
name: "or between two labels", | ||
orLabelSelector: &OrLabelSelector{ | ||
OrLabelSelectors: []*metav1.LabelSelector{ | ||
{ | ||
MatchLabels: map[string]string{"k1": "v1"}, | ||
}, | ||
{ | ||
MatchLabels: map[string]string{"k2": "v2"}, | ||
}, | ||
}, | ||
}, | ||
expectedStr: "k1=v1 or k2=v2", | ||
}, | ||
{ | ||
name: "or between two label groups", | ||
orLabelSelector: &OrLabelSelector{ | ||
OrLabelSelectors: []*metav1.LabelSelector{ | ||
{ | ||
MatchLabels: map[string]string{"k1": "v1", "k2": "v2"}, | ||
}, | ||
{ | ||
MatchLabels: map[string]string{"a1": "b1", "a2": "b2"}, | ||
}, | ||
}, | ||
}, | ||
expectedStr: "k1=v1,k2=v2 or a1=b1,a2=b2", | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
assert.Equal(t, test.expectedStr, test.orLabelSelector.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestSetOfOrLabelSelector(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
inputStr string | ||
expectedSelector *OrLabelSelector | ||
}{ | ||
{ | ||
name: "or between two labels", | ||
inputStr: "k1=v1 or k2=v2", | ||
expectedSelector: &OrLabelSelector{ | ||
OrLabelSelectors: []*metav1.LabelSelector{ | ||
{ | ||
MatchLabels: map[string]string{"k1": "v1"}, | ||
}, | ||
{ | ||
MatchLabels: map[string]string{"k2": "v2"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
{ | ||
name: "or between two label groups", | ||
inputStr: "k1=v1,k2=v2 or a1=b1,a2=b2", | ||
expectedSelector: &OrLabelSelector{ | ||
OrLabelSelectors: []*metav1.LabelSelector{ | ||
{ | ||
MatchLabels: map[string]string{"k1": "v1", "k2": "v2"}, | ||
}, | ||
{ | ||
MatchLabels: map[string]string{"a1": "b1", "a2": "b2"}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
selector := &OrLabelSelector{} | ||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
require.Nil(t, selector.Set(test.inputStr)) | ||
assert.Equal(t, len(test.expectedSelector.OrLabelSelectors), len(selector.OrLabelSelectors)) | ||
assert.Equal(t, test.expectedSelector.String(), selector.String()) | ||
}) | ||
} | ||
} | ||
|
||
func TestTypeOfOrLabelSelector(t *testing.T) { | ||
selector := &OrLabelSelector{} | ||
assert.Equal(t, "orLabelSelector", selector.Type()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters