Add cmd to list plugins (#1535)

* Add cmd to list plugins

Signed-off-by: Carlisia <carlisiac@vmware.com>
This commit is contained in:
KubeKween
2019-06-05 10:41:03 -07:00
committed by Nolan Brubaker
parent bc7ee686d7
commit 0a771e6a53
20 changed files with 403 additions and 143 deletions

View File

@@ -28,6 +28,7 @@ type Builder struct {
serverStatusRequest velerov1api.ServerStatusRequest
}
// NewBuilder returns a Builder for a ServerStatusRequest.
func NewBuilder() *Builder {
return &Builder{
serverStatusRequest: velerov1api.ServerStatusRequest{
@@ -39,7 +40,8 @@ func NewBuilder() *Builder {
}
}
func (b *Builder) Build() *velerov1api.ServerStatusRequest {
// ServerStatusRequest returns the built ServerStatusRequest API object.
func (b *Builder) ServerStatusRequest() *velerov1api.ServerStatusRequest {
return &b.serverStatusRequest
}
@@ -72,3 +74,8 @@ func (b *Builder) ServerVersion(version string) *Builder {
b.serverStatusRequest.Status.ServerVersion = version
return b
}
func (b *Builder) Plugins(plugins []velerov1api.PluginInfo) *Builder {
b.serverStatusRequest.Status.Plugins = plugins
return b
}

View File

@@ -29,13 +29,19 @@ import (
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
"github.com/heptio/velero/pkg/buildinfo"
velerov1client "github.com/heptio/velero/pkg/generated/clientset/versioned/typed/velero/v1"
"github.com/heptio/velero/pkg/plugin/framework"
)
const ttl = time.Minute
type PluginLister interface {
// List returns all PluginIdentifiers for kind.
List(kind framework.PluginKind) []framework.PluginIdentifier
}
// Process fills out new ServerStatusRequest objects and deletes processed ones
// that have expired.
func Process(req *velerov1api.ServerStatusRequest, client velerov1client.ServerStatusRequestsGetter, clock clock.Clock, log logrus.FieldLogger) error {
func Process(req *velerov1api.ServerStatusRequest, client velerov1client.ServerStatusRequestsGetter, pluginLister PluginLister, clock clock.Clock, log logrus.FieldLogger) error {
switch req.Status.Phase {
case "", velerov1api.ServerStatusRequestPhaseNew:
log.Info("Processing new ServerStatusRequest")
@@ -43,6 +49,7 @@ func Process(req *velerov1api.ServerStatusRequest, client velerov1client.ServerS
req.Status.ServerVersion = buildinfo.Version
req.Status.ProcessedTimestamp.Time = clock.Now()
req.Status.Phase = velerov1api.ServerStatusRequestPhaseProcessed
req.Status.Plugins = plugins(pluginLister)
}))
case velerov1api.ServerStatusRequestPhaseProcessed:
log.Debug("Checking whether ServerStatusRequest has expired")
@@ -88,3 +95,18 @@ func patch(client velerov1client.ServerStatusRequestsGetter, req *velerov1api.Se
return nil
}
func plugins(pluginLister PluginLister) []velerov1api.PluginInfo {
var plugins []velerov1api.PluginInfo
for _, v := range framework.AllPluginKinds() {
list := pluginLister.List(v)
for _, plugin := range list {
pluginInfo := velerov1api.PluginInfo{
Name: plugin.Name,
Kind: plugin.Kind.String(),
}
plugins = append(plugins, pluginInfo)
}
}
return plugins
}

View File

@@ -30,6 +30,7 @@ import (
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
"github.com/heptio/velero/pkg/buildinfo"
"github.com/heptio/velero/pkg/generated/clientset/versioned/fake"
"github.com/heptio/velero/pkg/plugin/framework"
)
func statusRequestBuilder() *Builder {
@@ -46,37 +47,82 @@ func TestProcess(t *testing.T) {
buildinfo.Version = "test-version-val"
tests := []struct {
name string
req *velerov1api.ServerStatusRequest
expected *velerov1api.ServerStatusRequest
expectedErrMsg string
name string
req *velerov1api.ServerStatusRequest
reqPluginLister *fakePluginLister
expected *velerov1api.ServerStatusRequest
expectedErrMsg string
}{
{
name: "server status request with empty phase gets processed",
req: statusRequestBuilder().Build(),
req: statusRequestBuilder().ServerStatusRequest(),
reqPluginLister: &fakePluginLister{
plugins: []framework.PluginIdentifier{
{
Name: "custom.io/myown",
Kind: "VolumeSnapshotter",
},
},
},
expected: statusRequestBuilder().
ServerVersion(buildinfo.Version).
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
ProcessedTimestamp(now).
Build(),
Plugins([]velerov1api.PluginInfo{
{
Name: "custom.io/myown",
Kind: "VolumeSnapshotter",
},
}).
ServerStatusRequest(),
},
{
name: "server status request with phase=New gets processed",
req: statusRequestBuilder().
Phase(velerov1api.ServerStatusRequestPhaseNew).
Build(),
ServerStatusRequest(),
reqPluginLister: &fakePluginLister{
plugins: []framework.PluginIdentifier{
{
Name: "velero.io/aws",
Kind: "ObjectStore",
},
{
Name: "custom.io/myown",
Kind: "VolumeSnapshotter",
},
},
},
expected: statusRequestBuilder().
ServerVersion(buildinfo.Version).
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
ProcessedTimestamp(now).
Build(),
Plugins([]velerov1api.PluginInfo{
{
Name: "velero.io/aws",
Kind: "ObjectStore",
},
{
Name: "custom.io/myown",
Kind: "VolumeSnapshotter",
},
}).
ServerStatusRequest(),
},
{
name: "server status request with phase=Processed gets deleted if expired",
req: statusRequestBuilder().
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
ProcessedTimestamp(now.Add(-61 * time.Second)).
Build(),
ServerStatusRequest(),
reqPluginLister: &fakePluginLister{
plugins: []framework.PluginIdentifier{
{
Name: "custom.io/myown",
Kind: "VolumeSnapshotter",
},
},
},
expected: nil,
},
{
@@ -84,20 +130,20 @@ func TestProcess(t *testing.T) {
req: statusRequestBuilder().
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
ProcessedTimestamp(now.Add(-59 * time.Second)).
Build(),
ServerStatusRequest(),
expected: statusRequestBuilder().
Phase(velerov1api.ServerStatusRequestPhaseProcessed).
ProcessedTimestamp(now.Add(-59 * time.Second)).
Build(),
ServerStatusRequest(),
},
{
name: "server status request with invalid phase returns an error",
req: statusRequestBuilder().
Phase(velerov1api.ServerStatusRequestPhase("an-invalid-phase")).
Build(),
ServerStatusRequest(),
expected: statusRequestBuilder().
Phase(velerov1api.ServerStatusRequestPhase("an-invalid-phase")).
Build(),
ServerStatusRequest(),
expectedErrMsg: "unexpected ServerStatusRequest phase \"an-invalid-phase\"",
},
}
@@ -106,7 +152,7 @@ func TestProcess(t *testing.T) {
t.Run(tc.name, func(t *testing.T) {
client := fake.NewSimpleClientset(tc.req)
err := Process(tc.req, client.VeleroV1(), clock.NewFakeClock(now), logrus.StandardLogger())
err := Process(tc.req, client.VeleroV1(), tc.reqPluginLister, clock.NewFakeClock(now), logrus.StandardLogger())
if tc.expectedErrMsg == "" {
assert.Nil(t, err)
} else {
@@ -124,3 +170,18 @@ func TestProcess(t *testing.T) {
})
}
}
type fakePluginLister struct {
plugins []framework.PluginIdentifier
}
func (l *fakePluginLister) List(kind framework.PluginKind) []framework.PluginIdentifier {
var plugins []framework.PluginIdentifier
for _, plugin := range l.plugins {
if plugin.Kind == kind {
plugins = append(plugins, plugin)
}
}
return plugins
}