Skip to content

Commit

Permalink
Remove dependency of the legacy client code from pkg/cmd directory.
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <[email protected]>
  • Loading branch information
Xun Jiang committed Jul 14, 2023
1 parent e54a8af commit 89d3ad4
Show file tree
Hide file tree
Showing 22 changed files with 332 additions and 257 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/6469-blackpiglet
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Remove dependency of the legacy client code from pkg/cmd directory
37 changes: 37 additions & 0 deletions pkg/client/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ type Factory interface {
// types to its scheme. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
KubebuilderClient() (kbclient.Client, error)
// KubebuilderWatchClient returns a client with watcher for the controller runtime framework.
// It adds Kubernetes and Velero types to its scheme. It uses the following priority to specify the cluster
// configuration: --kubeconfig flag, KUBECONFIG environment variable, in-cluster configuration.
KubebuilderWatchClient() (kbclient.WithWatch, error)
// SetBasename changes the basename for an already-constructed client.
// This is useful for generating clients that require a different user-agent string below the root `velero`
// command, such as the server subcommand.
Expand Down Expand Up @@ -182,6 +186,39 @@ func (f *factory) KubebuilderClient() (kbclient.Client, error) {
return kubebuilderClient, nil
}

func (f *factory) KubebuilderWatchClient() (kbclient.WithWatch, error) {
clientConfig, err := f.ClientConfig()
if err != nil {
return nil, err
}

scheme := runtime.NewScheme()
if err := velerov1api.AddToScheme(scheme); err != nil {
return nil, err
}
if err := velerov2alpha1api.AddToScheme(scheme); err != nil {
return nil, err
}
if err := k8scheme.AddToScheme(scheme); err != nil {
return nil, err
}
if err := apiextv1beta1.AddToScheme(scheme); err != nil {
return nil, err
}
if err := apiextv1.AddToScheme(scheme); err != nil {
return nil, err
}
kubebuilderWatchClient, err := kbclient.NewWithWatch(clientConfig, kbclient.Options{
Scheme: scheme,
})

if err != nil {
return nil, err
}

return kubebuilderWatchClient, nil
}

func (f *factory) SetBasename(name string) {
f.baseName = name
}
Expand Down
26 changes: 26 additions & 0 deletions pkg/client/mocks/Factory.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 18 additions & 14 deletions pkg/cmd/cli/backup/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,20 @@ import (
"strings"
"time"

kbclient "sigs.k8s.io/controller-runtime/pkg/client"

"github.com/spf13/cobra"
"github.com/spf13/pflag"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
kubeerrs "k8s.io/apimachinery/pkg/util/errors"
"k8s.io/client-go/tools/cache"
kbclient "sigs.k8s.io/controller-runtime/pkg/client"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/builder"
"github.com/vmware-tanzu/velero/pkg/client"
"github.com/vmware-tanzu/velero/pkg/cmd"
"github.com/vmware-tanzu/velero/pkg/cmd/util/flag"
"github.com/vmware-tanzu/velero/pkg/cmd/util/output"
veleroclient "github.com/vmware-tanzu/velero/pkg/generated/clientset/versioned"
v1 "github.com/vmware-tanzu/velero/pkg/generated/informers/externalversions/velero/v1"
"github.com/vmware-tanzu/velero/pkg/util/collections"
"github.com/vmware-tanzu/velero/pkg/util/kube"
)

func NewCreateCommand(f client.Factory, use string) *cobra.Command {
Expand Down Expand Up @@ -107,7 +104,7 @@ type CreateOptions struct {
CSISnapshotTimeout time.Duration
ItemOperationTimeout time.Duration
ResPoliciesConfigmap string
client veleroclient.Interface
client kbclient.WithWatch
}

func NewCreateOptions() *CreateOptions {
Expand Down Expand Up @@ -171,7 +168,7 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
return err
}

client, err := f.KubebuilderClient()
client, err := f.KubebuilderWatchClient()
if err != nil {
return err
}
Expand Down Expand Up @@ -203,7 +200,8 @@ func (o *CreateOptions) Validate(c *cobra.Command, args []string, f client.Facto
}

for _, loc := range o.SnapshotLocations {
if _, err := o.client.VeleroV1().VolumeSnapshotLocations(f.Namespace()).Get(context.TODO(), loc, metav1.GetOptions{}); err != nil {
snapshotLocation := new(velerov1api.VolumeSnapshotLocation)
if err := o.client.Get(context.TODO(), kbclient.ObjectKey{Namespace: f.Namespace(), Name: loc}, snapshotLocation); err != nil {
return err
}
}
Expand All @@ -216,7 +214,7 @@ func (o *CreateOptions) Complete(args []string, f client.Factory) error {
if len(args) > 0 {
o.Name = args[0]
}
client, err := f.Client()
client, err := f.KubebuilderWatchClient()
if err != nil {
return err
}
Expand All @@ -238,20 +236,24 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
fmt.Println("Creating backup from schedule, all other filters are ignored.")
}

var backupInformer cache.SharedIndexInformer
var updates chan *velerov1api.Backup
if o.Wait {
stop := make(chan struct{})
defer close(stop)

updates = make(chan *velerov1api.Backup)

backupInformer = v1.NewBackupInformer(o.client, f.Namespace(), 0, nil)

lw := kube.InternalLW{
Client: o.client,
Namespace: f.Namespace(),
ObjectList: new(velerov1api.BackupList),
}
backupInformer := cache.NewSharedInformer(&lw, &velerov1api.Backup{}, time.Second)
backupInformer.AddEventHandler(
cache.FilteringResourceEventHandler{
FilterFunc: func(obj interface{}) bool {
backup, ok := obj.(*velerov1api.Backup)

if !ok {
return false
}
Expand All @@ -275,10 +277,11 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
},
},
)

go backupInformer.Run(stop)
}

_, err = o.client.VeleroV1().Backups(backup.Namespace).Create(context.TODO(), backup, metav1.CreateOptions{})
err = o.client.Create(context.TODO(), backup, &kbclient.CreateOptions{})
if err != nil {
return err
}
Expand Down Expand Up @@ -341,7 +344,8 @@ func (o *CreateOptions) BuildBackup(namespace string) (*velerov1api.Backup, erro
var backupBuilder *builder.BackupBuilder

if o.FromSchedule != "" {
schedule, err := o.client.VeleroV1().Schedules(namespace).Get(context.TODO(), o.FromSchedule, metav1.GetOptions{})
schedule := new(velerov1api.Schedule)
err := o.client.Get(context.TODO(), kbclient.ObjectKey{Namespace: namespace, Name: o.FromSchedule}, schedule)
if err != nil {
return nil, err
}
Expand Down
Loading

0 comments on commit 89d3ad4

Please sign in to comment.