Pass Velero server command args to the plugins

Pass Velero server command args to the plugins

Fixes #7806

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
This commit is contained in:
Wenkai Yin(尹文开)
2024-07-23 14:54:41 +08:00
parent 981f30cb25
commit dc6eeafe98
22 changed files with 500 additions and 483 deletions

92
pkg/types/priority.go Normal file
View File

@@ -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 types
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"
}

110
pkg/types/priority_test.go Normal file
View File

@@ -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 types
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.Error(t, err)
} else {
require.NoError(t, err)
}
assert.Equal(t, c.priorities, p)
})
}
}