forked from vmware-tanzu/velero
-
Notifications
You must be signed in to change notification settings - Fork 0
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 #43 from vmware-tanzu/main
Fork Sync: Update from parent repository
- Loading branch information
Showing
6 changed files
with
286 additions
and
47 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 @@ | ||
Enhance the restore priorities list to support specifying the low prioritized resources that need to be restored in the last |
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,92 @@ | ||
/* | ||
Copyright 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 restore | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
) | ||
|
||
const ( | ||
prioritySeparator = "-" | ||
) | ||
|
||
// Priorities defines the desired order of resource operations: | ||
// Resources in the HighPriorities list will be handled first | ||
// Resources in the LowPriorities list will be handled last | ||
// Other resources will be handled alphabetically after the high prioritized resources and before the low prioritized resources | ||
type Priorities struct { | ||
HighPriorities []string | ||
LowPriorities []string | ||
} | ||
|
||
// String returns a string representation of Priority. | ||
func (p *Priorities) String() string { | ||
priorities := p.HighPriorities | ||
if len(p.LowPriorities) > 0 { | ||
priorities = append(priorities, prioritySeparator) | ||
priorities = append(priorities, p.LowPriorities...) | ||
} | ||
return strings.Join(priorities, ",") | ||
} | ||
|
||
// Set parses the provided string to the priority object | ||
func (p *Priorities) Set(s string) error { | ||
if len(s) == 0 { | ||
return nil | ||
} | ||
strs := strings.Split(s, ",") | ||
separatorIndex := -1 | ||
for i, str := range strs { | ||
if str == prioritySeparator { | ||
if separatorIndex > -1 { | ||
return fmt.Errorf("multiple priority separator %q found", prioritySeparator) | ||
} | ||
separatorIndex = i | ||
} | ||
} | ||
// has no separator | ||
if separatorIndex == -1 { | ||
p.HighPriorities = strs | ||
return nil | ||
} | ||
// start with separator | ||
if separatorIndex == 0 { | ||
// contain only separator | ||
if len(strs) == 1 { | ||
return nil | ||
} | ||
p.LowPriorities = strs[1:] | ||
return nil | ||
} | ||
// end with separator | ||
if separatorIndex == len(strs)-1 { | ||
p.HighPriorities = strs[:len(strs)-1] | ||
return nil | ||
} | ||
|
||
// separator in the middle | ||
p.HighPriorities = strs[:separatorIndex] | ||
p.LowPriorities = strs[separatorIndex+1:] | ||
|
||
return nil | ||
} | ||
|
||
// Type specifies the flag type | ||
func (p *Priorities) Type() string { | ||
return "stringArray" | ||
} |
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,110 @@ | ||
/* | ||
Copyright 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 restore | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestStringOfPriorities(t *testing.T) { | ||
priority := Priorities{ | ||
HighPriorities: []string{"high"}, | ||
} | ||
assert.Equal(t, "high", priority.String()) | ||
|
||
priority = Priorities{ | ||
HighPriorities: []string{"high"}, | ||
LowPriorities: []string{"low"}, | ||
} | ||
assert.Equal(t, "high,-,low", priority.String()) | ||
} | ||
|
||
func TestSetOfPriority(t *testing.T) { | ||
cases := []struct { | ||
name string | ||
input string | ||
priorities Priorities | ||
hasErr bool | ||
}{ | ||
{ | ||
name: "empty input", | ||
input: "", | ||
priorities: Priorities{}, | ||
hasErr: false, | ||
}, | ||
{ | ||
name: "only high priorities", | ||
input: "p0", | ||
priorities: Priorities{ | ||
HighPriorities: []string{"p0"}, | ||
}, | ||
hasErr: false, | ||
}, | ||
{ | ||
name: "only low priorities", | ||
input: "-,p9", | ||
priorities: Priorities{ | ||
LowPriorities: []string{"p9"}, | ||
}, | ||
hasErr: false, | ||
}, | ||
{ | ||
name: "only separator", | ||
input: "-", | ||
priorities: Priorities{}, | ||
hasErr: false, | ||
}, | ||
{ | ||
name: "multiple separators", | ||
input: "-,-", | ||
priorities: Priorities{}, | ||
hasErr: true, | ||
}, | ||
{ | ||
name: "contain both high and low priorities", | ||
input: "p0,p1,p2,-,p9", | ||
priorities: Priorities{ | ||
HighPriorities: []string{"p0", "p1", "p2"}, | ||
LowPriorities: []string{"p9"}, | ||
}, | ||
hasErr: false, | ||
}, | ||
{ | ||
name: "end with separator", | ||
input: "p0,-", | ||
priorities: Priorities{ | ||
HighPriorities: []string{"p0"}, | ||
}, | ||
hasErr: false, | ||
}, | ||
} | ||
for _, c := range cases { | ||
t.Run(c.name, func(t *testing.T) { | ||
p := Priorities{} | ||
err := p.Set(c.input) | ||
if c.hasErr { | ||
require.NotNil(t, err) | ||
} else { | ||
require.Nil(t, err) | ||
} | ||
assert.Equal(t, c.priorities, p) | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.
5ae3906
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
velero – ./
velero-kaovilai.vercel.app
velero-git-main-kaovilai.vercel.app
velero.vercel.app
velero.tig.pw