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

@@ -22,6 +22,7 @@ import (
"github.com/heptio/velero/pkg/client"
"github.com/heptio/velero/pkg/cmd/cli/backup"
"github.com/heptio/velero/pkg/cmd/cli/backuplocation"
"github.com/heptio/velero/pkg/cmd/cli/plugin"
"github.com/heptio/velero/pkg/cmd/cli/restore"
"github.com/heptio/velero/pkg/cmd/cli/schedule"
"github.com/heptio/velero/pkg/cmd/cli/snapshotlocation"
@@ -49,12 +50,16 @@ func NewCommand(f client.Factory) *cobra.Command {
snapshotLocationCommand := snapshotlocation.NewGetCommand(f, "snapshot-locations")
snapshotLocationCommand.Aliases = []string{"snapshot-location"}
pluginCommand := plugin.NewGetCommand(f, "plugins")
pluginCommand.Aliases = []string{"plugin"}
c.AddCommand(
backupCommand,
scheduleCommand,
restoreCommand,
backupLocationCommand,
snapshotLocationCommand,
pluginCommand,
)
return c

69
pkg/cmd/cli/plugin/get.go Normal file
View File

@@ -0,0 +1,69 @@
/*
Copyright 2019 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 plugin
import (
"fmt"
"os"
"time"
"github.com/spf13/cobra"
"github.com/heptio/velero/pkg/client"
"github.com/heptio/velero/pkg/cmd"
"github.com/heptio/velero/pkg/cmd/cli/serverstatus"
"github.com/heptio/velero/pkg/cmd/util/output"
)
func NewGetCommand(f client.Factory, use string) *cobra.Command {
serverStatusGetter := &serverstatus.DefaultServerStatusGetter{
Timeout: 5 * time.Second,
}
c := &cobra.Command{
Use: use,
Short: "Get information for all plugins on the velero server",
Run: func(c *cobra.Command, args []string) {
err := output.ValidateFlags(c)
cmd.CheckError(err)
serverStatusGetter := &serverstatus.DefaultServerStatusGetter{
Namespace: f.Namespace(),
Timeout: 5 * time.Second,
}
client, err := f.Client()
cmd.CheckError(err)
veleroClient := client.VeleroV1()
serverStatus, err := serverStatusGetter.GetServerStatus(veleroClient)
if err != nil {
fmt.Fprintf(os.Stdout, "<error getting plugin information: %s>\n", err)
return
}
_, err = output.PrintWithFormat(c, serverStatus)
cmd.CheckError(err)
},
}
c.Flags().DurationVar(&serverStatusGetter.Timeout, "timeout", serverStatusGetter.Timeout, "maximum time to wait for plugin information to be reported")
output.BindFlagsSimple(c.Flags())
return c
}

View File

@@ -32,6 +32,7 @@ func NewCommand(f client.Factory) *cobra.Command {
c.AddCommand(
NewAddCommand(f),
NewRemoveCommand(f),
NewGetCommand(f, "get"),
)
return c

View File

@@ -0,0 +1,94 @@
/*
Copyright 2019 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 serverstatus
import (
"time"
"github.com/pkg/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/watch"
velerov1api "github.com/heptio/velero/pkg/apis/velero/v1"
velerov1client "github.com/heptio/velero/pkg/generated/clientset/versioned/typed/velero/v1"
"github.com/heptio/velero/pkg/serverstatusrequest"
)
type ServerStatusGetter interface {
GetServerStatus(client velerov1client.ServerStatusRequestsGetter) (*velerov1api.ServerStatusRequest, error)
}
type DefaultServerStatusGetter struct {
Namespace string
Timeout time.Duration
}
func (g *DefaultServerStatusGetter) GetServerStatus(client velerov1client.ServerStatusRequestsGetter) (*velerov1api.ServerStatusRequest, error) {
req := serverstatusrequest.NewBuilder().Namespace(g.Namespace).GenerateName("velero-cli-").ServerStatusRequest()
created, err := client.ServerStatusRequests(g.Namespace).Create(req)
if err != nil {
return nil, errors.WithStack(err)
}
defer client.ServerStatusRequests(g.Namespace).Delete(created.Name, nil)
listOptions := metav1.ListOptions{
// TODO: once the minimum supported Kubernetes version is v1.9.0, uncomment the following line.
// See http://issue.k8s.io/51046 for details.
//FieldSelector: "metadata.name=" + req.Name
ResourceVersion: created.ResourceVersion,
}
watcher, err := client.ServerStatusRequests(g.Namespace).Watch(listOptions)
if err != nil {
return nil, errors.WithStack(err)
}
defer watcher.Stop()
expired := time.NewTimer(g.Timeout)
defer expired.Stop()
Loop:
for {
select {
case <-expired.C:
return nil, errors.New("timed out waiting for server status request to be processed")
case e := <-watcher.ResultChan():
updated, ok := e.Object.(*velerov1api.ServerStatusRequest)
if !ok {
return nil, errors.Errorf("unexpected type %T", e.Object)
}
// TODO: once the minimum supported Kubernetes version is v1.9.0, remove the following check.
// See http://issue.k8s.io/51046 for details.
if updated.Name != created.Name {
continue
}
switch e.Type {
case watch.Deleted:
return nil, errors.New("server status request was unexpectedly deleted")
case watch.Modified:
if updated.Status.Phase == velerov1api.ServerStatusRequestPhaseProcessed {
req = updated
break Loop
}
}
}
}
return req, nil
}

View File

@@ -0,0 +1,81 @@
/*
Copyright 2017, 2019 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 version
import (
"fmt"
"io"
"os"
"time"
"github.com/spf13/cobra"
"github.com/heptio/velero/pkg/buildinfo"
"github.com/heptio/velero/pkg/client"
"github.com/heptio/velero/pkg/cmd"
"github.com/heptio/velero/pkg/cmd/cli/serverstatus"
velerov1client "github.com/heptio/velero/pkg/generated/clientset/versioned/typed/velero/v1"
)
func NewCommand(f client.Factory) *cobra.Command {
clientOnly := false
serverStatusGetter := &serverstatus.DefaultServerStatusGetter{
Namespace: f.Namespace(),
Timeout: 5 * time.Second,
}
c := &cobra.Command{
Use: "version",
Short: "Print the velero version and associated image",
Run: func(c *cobra.Command, args []string) {
var veleroClient velerov1client.ServerStatusRequestsGetter
if !clientOnly {
client, err := f.Client()
cmd.CheckError(err)
veleroClient = client.VeleroV1()
}
printVersion(os.Stdout, clientOnly, veleroClient, serverStatusGetter)
},
}
c.Flags().DurationVar(&serverStatusGetter.Timeout, "timeout", serverStatusGetter.Timeout, "maximum time to wait for server version to be reported")
c.Flags().BoolVar(&clientOnly, "client-only", clientOnly, "only get velero client version, not server version")
return c
}
func printVersion(w io.Writer, clientOnly bool, client velerov1client.ServerStatusRequestsGetter, serverStatusGetter serverstatus.ServerStatusGetter) {
fmt.Fprintln(w, "Client:")
fmt.Fprintf(w, "\tVersion: %s\n", buildinfo.Version)
fmt.Fprintf(w, "\tGit commit: %s\n", buildinfo.FormattedGitSHA())
if clientOnly {
return
}
serverStatus, err := serverStatusGetter.GetServerStatus(client)
if err != nil {
fmt.Fprintf(w, "<error getting server version: %s>\n", err)
return
}
fmt.Fprintln(w, "Server:")
fmt.Fprintf(w, "\tVersion: %s\n", serverStatus.Status.ServerVersion)
}

View File

@@ -0,0 +1,129 @@
/*
Copyright 2019 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 version
import (
"bytes"
"fmt"
"testing"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
velerov1 "github.com/heptio/velero/pkg/apis/velero/v1"
"github.com/heptio/velero/pkg/buildinfo"
"github.com/heptio/velero/pkg/generated/clientset/versioned/fake"
v1 "github.com/heptio/velero/pkg/generated/clientset/versioned/typed/velero/v1"
"github.com/heptio/velero/pkg/serverstatusrequest"
)
func TestPrintVersion(t *testing.T) {
// set up some non-empty buildinfo values, but put them back to their
// defaults at the end of the test
var (
origVersion = buildinfo.Version
origGitSHA = buildinfo.GitSHA
origGitTreeState = buildinfo.GitTreeState
)
defer func() {
buildinfo.Version = origVersion
buildinfo.GitSHA = origGitSHA
buildinfo.GitTreeState = origGitTreeState
}()
buildinfo.Version = "v1.0.0"
buildinfo.GitSHA = "somegitsha"
buildinfo.GitTreeState = "dirty"
clientVersion := fmt.Sprintf("Client:\n\tVersion: %s\n\tGit commit: %s\n", buildinfo.Version, buildinfo.FormattedGitSHA())
tests := []struct {
name string
clientOnly bool
serverStatusRequest *velerov1.ServerStatusRequest
getterError error
want string
}{
{
name: "client-only",
clientOnly: true,
want: clientVersion,
},
{
name: "server status getter error",
clientOnly: false,
serverStatusRequest: nil,
getterError: errors.New("an error"),
want: clientVersion + "<error getting server version: an error>\n",
},
{
name: "server status getter returns normally",
clientOnly: false,
serverStatusRequest: serverstatusrequest.NewBuilder().ServerVersion("v1.0.1").ServerStatusRequest(),
getterError: nil,
want: clientVersion + "Server:\n\tVersion: v1.0.1\n",
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
var (
serverStatusGetter = new(mockServerStatusGetter)
buf = new(bytes.Buffer)
client = fake.NewSimpleClientset()
)
defer serverStatusGetter.AssertExpectations(t)
// GetServerStatus should only be called when clientOnly = false
if !tc.clientOnly {
serverStatusGetter.On("GetServerStatus", client.VeleroV1()).Return(tc.serverStatusRequest, tc.getterError)
}
printVersion(buf, tc.clientOnly, client.VeleroV1(), serverStatusGetter)
assert.Equal(t, tc.want, buf.String())
})
}
}
// serverStatusGetter is an autogenerated mock type for the serverStatusGetter type
type mockServerStatusGetter struct {
mock.Mock
}
// GetServerStatus provides a mock function with given fields: client
func (_m *mockServerStatusGetter) GetServerStatus(client v1.ServerStatusRequestsGetter) (*velerov1.ServerStatusRequest, error) {
ret := _m.Called(client)
var r0 *velerov1.ServerStatusRequest
if rf, ok := ret.Get(0).(func(v1.ServerStatusRequestsGetter) *velerov1.ServerStatusRequest); ok {
r0 = rf(client)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*velerov1.ServerStatusRequest)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(v1.ServerStatusRequestsGetter) error); ok {
r1 = rf(client)
} else {
r1 = ret.Error(1)
}
return r0, r1
}