Skip to content

Commit

Permalink
Remove generated client from pkg/cmd directory.
Browse files Browse the repository at this point in the history
Signed-off-by: Xun Jiang <jxun@vmware.com>
  • Loading branch information
Xun Jiang committed Nov 3, 2023
1 parent c326bd9 commit ad87449
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 33 deletions.
27 changes: 16 additions & 11 deletions pkg/cmd/cli/debug/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,12 @@ import (
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/vmware-tanzu/crash-diagnostics/exec"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
appsv1 "k8s.io/api/apps/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/tools/clientcmd"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/client"
"github.com/vmware-tanzu/velero/pkg/cmd"
)
Expand Down Expand Up @@ -110,30 +113,32 @@ func (o *option) complete(f client.Factory, fs *pflag.FlagSet) error {
}

func (o *option) validate(f client.Factory) error {
kubeClient, err := f.KubeClient()
crClient, err := f.KubebuilderClient()
if err != nil {
return err
}
l, err := kubeClient.AppsV1().Deployments(o.namespace).List(context.TODO(), metav1.ListOptions{
LabelSelector: "component=velero",
deploymentList := new(appsv1.DeploymentList)
selector, err := labels.Parse("component=velero")
cmd.CheckError(err)
err = crClient.List(context.TODO(), deploymentList, &ctrlclient.ListOptions{
Namespace: o.namespace,
LabelSelector: selector,
})
if err != nil {
return errors.Wrap(err, "failed to check velero deployment")
}
if len(l.Items) == 0 {
if len(deploymentList.Items) == 0 {
return fmt.Errorf("velero deployment does not exist in namespace: %s", o.namespace)
}
veleroClient, err := f.Client()
if err != nil {
return err
}
if len(o.backup) > 0 {
if _, err := veleroClient.VeleroV1().Backups(o.namespace).Get(context.TODO(), o.backup, metav1.GetOptions{}); err != nil {
backup := new(velerov1api.Backup)
if err := crClient.Get(context.TODO(), ctrlclient.ObjectKey{Namespace: o.namespace, Name: o.backup}, backup); err != nil {
return err
}
}
if len(o.restore) > 0 {
if _, err := veleroClient.VeleroV1().Restores(o.namespace).Get(context.TODO(), o.restore, metav1.GetOptions{}); err != nil {
restore := new(velerov1api.Restore)
if err := crClient.Get(context.TODO(), ctrlclient.ObjectKey{Namespace: o.namespace, Name: o.restore}, restore); err != nil {
return err
}
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/cmd/cli/repo/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/client"
Expand All @@ -38,19 +40,25 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
err := output.ValidateFlags(c)
cmd.CheckError(err)

veleroClient, err := f.Client()
crClient, err := f.KubebuilderClient()
cmd.CheckError(err)

var repos *api.BackupRepositoryList
if len(args) > 0 {
repos = new(api.BackupRepositoryList)
for _, name := range args {
repo, err := veleroClient.VeleroV1().BackupRepositories(f.Namespace()).Get(context.TODO(), name, metav1.GetOptions{})
repo := new(api.BackupRepository)
err := crClient.Get(context.TODO(), ctrlclient.ObjectKey{Namespace: f.Namespace(), Name: name}, repo)
cmd.CheckError(err)
repos.Items = append(repos.Items, *repo)
}
} else {
repos, err = veleroClient.VeleroV1().BackupRepositories(f.Namespace()).List(context.TODO(), listOptions)
selector := labels.NewSelector()
if listOptions.LabelSelector != "" {
selector, err = labels.Parse(listOptions.LabelSelector)
cmd.CheckError(err)
}
err = crClient.List(context.TODO(), repos, &ctrlclient.ListOptions{LabelSelector: selector})
cmd.CheckError(err)
}

Expand Down
8 changes: 3 additions & 5 deletions pkg/cmd/cli/restore/logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import (

"github.com/spf13/cobra"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/client"
Expand All @@ -49,13 +49,11 @@ func NewLogsCommand(f client.Factory) *cobra.Command {
Run: func(c *cobra.Command, args []string) {
restoreName := args[0]

veleroClient, err := f.Client()
cmd.CheckError(err)

kbClient, err := f.KubebuilderClient()
cmd.CheckError(err)

restore, err := veleroClient.VeleroV1().Restores(f.Namespace()).Get(context.TODO(), restoreName, metav1.GetOptions{})
restore := new(velerov1api.Restore)
err = kbClient.Get(context.TODO(), ctrlclient.ObjectKey{Namespace: f.Namespace(), Name: restoreName}, restore)
if apierrors.IsNotFound(err) {
cmd.Exit("Restore %q does not exist.", restoreName)
} else if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions pkg/cmd/cli/schedule/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (o *CreateOptions) Complete(args []string, f client.Factory) error {
func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
var orders map[string]string

veleroClient, err := f.Client()
crClient, err := f.KubebuilderClient()
if err != nil {
return err
}
Expand Down Expand Up @@ -171,7 +171,7 @@ func (o *CreateOptions) Run(c *cobra.Command, f client.Factory) error {
return err
}

_, err = veleroClient.VeleroV1().Schedules(schedule.Namespace).Create(context.TODO(), schedule, metav1.CreateOptions{})
err = crClient.Create(context.TODO(), schedule)
if err != nil {
return err
}
Expand Down
14 changes: 11 additions & 3 deletions pkg/cmd/cli/schedule/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import (

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

v1 "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/client"
Expand All @@ -36,19 +38,25 @@ func NewDescribeCommand(f client.Factory, use string) *cobra.Command {
Use: use + " [NAME1] [NAME2] [NAME...]",
Short: "Describe schedules",
Run: func(c *cobra.Command, args []string) {
veleroClient, err := f.Client()
crClient, err := f.KubebuilderClient()
cmd.CheckError(err)

var schedules *v1.ScheduleList
if len(args) > 0 {
schedules = new(v1.ScheduleList)
for _, name := range args {
schedule, err := veleroClient.VeleroV1().Schedules(f.Namespace()).Get(context.TODO(), name, metav1.GetOptions{})
schedule := new(v1.Schedule)
err := crClient.Get(context.TODO(), ctrlclient.ObjectKey{Namespace: f.Namespace(), Name: name}, schedule)
cmd.CheckError(err)
schedules.Items = append(schedules.Items, *schedule)
}
} else {
schedules, err = veleroClient.VeleroV1().Schedules(f.Namespace()).List(context.TODO(), listOptions)
selector := labels.NewSelector()
if listOptions.LabelSelector != "" {
selector, err = labels.Parse(listOptions.LabelSelector)
cmd.CheckError(err)
}
err = crClient.List(context.TODO(), schedules, &ctrlclient.ListOptions{LabelSelector: selector})
cmd.CheckError(err)
}

Expand Down
14 changes: 11 additions & 3 deletions pkg/cmd/cli/schedule/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import (

"github.com/spf13/cobra"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/client"
Expand All @@ -38,19 +40,25 @@ func NewGetCommand(f client.Factory, use string) *cobra.Command {
err := output.ValidateFlags(c)
cmd.CheckError(err)

veleroClient, err := f.Client()
crClient, err := f.KubebuilderClient()
cmd.CheckError(err)

var schedules *api.ScheduleList
if len(args) > 0 {
schedules = new(api.ScheduleList)
for _, name := range args {
schedule, err := veleroClient.VeleroV1().Schedules(f.Namespace()).Get(context.TODO(), name, metav1.GetOptions{})
schedule := new(api.Schedule)
err := crClient.Get(context.TODO(), ctrlclient.ObjectKey{Name: name, Namespace: f.Namespace()}, schedule)
cmd.CheckError(err)
schedules.Items = append(schedules.Items, *schedule)
}
} else {
schedules, err = veleroClient.VeleroV1().Schedules(f.Namespace()).List(context.TODO(), listOptions)
selector := labels.NewSelector()
if listOptions.LabelSelector != "" {
selector, err = labels.Parse(listOptions.LabelSelector)
cmd.CheckError(err)
}
err := crClient.List(context.TODO(), schedules, &ctrlclient.ListOptions{LabelSelector: selector})
cmd.CheckError(err)
}

Expand Down
19 changes: 13 additions & 6 deletions pkg/cmd/cli/schedule/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
kubeerrs "k8s.io/apimachinery/pkg/util/errors"
ctrlclient "sigs.k8s.io/controller-runtime/pkg/client"

velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/client"
Expand Down Expand Up @@ -63,7 +64,7 @@ func NewPauseCommand(f client.Factory, use string) *cobra.Command {
}

func runPause(f client.Factory, o *cli.SelectOptions, paused bool) error {
client, err := f.Client()
crClient, err := f.KubebuilderClient()
if err != nil {
return err
}
Expand All @@ -75,19 +76,25 @@ func runPause(f client.Factory, o *cli.SelectOptions, paused bool) error {
switch {
case len(o.Names) > 0:
for _, name := range o.Names {
schedule, err := client.VeleroV1().Schedules(f.Namespace()).Get(context.TODO(), name, metav1.GetOptions{})
schedule := new(velerov1api.Schedule)
err := crClient.Get(context.TODO(), ctrlclient.ObjectKey{Name: name, Namespace: f.Namespace()}, schedule)
if err != nil {
errs = append(errs, errors.WithStack(err))
continue
}
schedules = append(schedules, schedule)
}
default:
selector := labels.Everything().String()
selector := labels.Everything()
if o.Selector.LabelSelector != nil {
selector = o.Selector.String()
convertedSelector, err := metav1.LabelSelectorAsSelector(o.Selector.LabelSelector)
if err != nil {
return errors.WithStack(err)
}
selector = convertedSelector
}
res, err := client.VeleroV1().Schedules(f.Namespace()).List(context.TODO(), metav1.ListOptions{
res := new(velerov1api.ScheduleList)
err := crClient.List(context.TODO(), res, &ctrlclient.ListOptions{
LabelSelector: selector,
})
if err != nil {
Expand All @@ -113,7 +120,7 @@ func runPause(f client.Factory, o *cli.SelectOptions, paused bool) error {
continue
}
schedule.Spec.Paused = paused
if _, err := client.VeleroV1().Schedules(schedule.Namespace).Update(context.TODO(), schedule, metav1.UpdateOptions{}); err != nil {
if err := crClient.Update(context.TODO(), schedule); err != nil {
return errors.Wrapf(err, "failed to update schedule %s", schedule.Name)
}
fmt.Printf("Schedule %s %s successfully\n", schedule.Name, msg)
Expand Down

0 comments on commit ad87449

Please sign in to comment.