Skip to content

Commit

Permalink
Update uncached client + Add session list in trigger backup
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Ishtiaq Islam <[email protected]>
  • Loading branch information
ishtiaqhimel committed Dec 27, 2023
1 parent 5c83a8b commit be035ce
Show file tree
Hide file tree
Showing 35 changed files with 986 additions and 243 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ BIN_PLATFORMS := $(DOCKER_PLATFORMS) windows/amd64 darwin/amd64 darwin/arm64
OS := $(if $(GOOS),$(GOOS),$(shell go env GOOS))
ARCH := $(if $(GOARCH),$(GOARCH),$(shell go env GOARCH))

GO_VERSION ?= 1.20
GO_VERSION ?= 1.21
BUILD_IMAGE ?= ghcr.io/appscode/golang-dev:$(GO_VERSION)

RESTIC_VER := 0.16.0
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ require (
k8s.io/utils v0.0.0-20220823124924-e9cbc92d1a73
kmodules.xyz/client-go v0.25.43
kmodules.xyz/offshoot-api v0.25.5
kubestash.dev/apimachinery v0.2.0
kubestash.dev/apimachinery v0.2.1-0.20231213061518-17b1436fc227
sigs.k8s.io/controller-runtime v0.13.1
)

Expand Down
6 changes: 3 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -372,8 +372,8 @@ github.com/konsorten/go-windows-terminal-sequences v1.0.1/go.mod h1:T0+1ngSBFLxv
github.com/konsorten/go-windows-terminal-sequences v1.0.3/go.mod h1:T0+1ngSBFLxvqU3pZ+m/2kptfBszLMUkC4ZK/EgS/cQ=
github.com/kr/logfmt v0.0.0-20140226030751-b84e30acd515/go.mod h1:+0opPa2QZZtGFBFZlji/RkVcI2GknAs/DXo4wKdlNEc=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pretty v0.2.0 h1:s5hAObm+yFO5uHYt5dYjxi2rXrsnmRpJx4OYvIWUaQs=
github.com/kr/pretty v0.2.0/go.mod h1:ipq/a2n7PKx3OHsz4KJII5eveXtPO4qwEXGdVfWzfnI=
github.com/kr/pretty v0.2.1 h1:Fmg33tUaq4/8ym9TJN1x7sLJnHVwhP33CNkpYV/7rwI=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/pty v1.1.5/go.mod h1:9r2w37qlBe7rQ6e1fg1S/9xpWHSnaqNdHD3WcMdbPDA=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
Expand Down Expand Up @@ -1069,8 +1069,8 @@ kmodules.xyz/offshoot-api v0.25.5 h1:erUtTDj9iljikd9CvrCz0E32P5mgEqq1NYxy06lxrNo
kmodules.xyz/offshoot-api v0.25.5/go.mod h1:wotLtcXWHw6KrWX6Ry2EsHn2I2QTvyLX7gXAuwBjkFc=
kmodules.xyz/prober v0.25.0 h1:R5uRLHJEvEtEoogj+vaTAob0Btph6+PX5IlS6hPh8PA=
kmodules.xyz/prober v0.25.0/go.mod h1:z4RTnjaajNQa/vPltsiOnO3xI716I/ziD2ac2Exm+1M=
kubestash.dev/apimachinery v0.2.0 h1:xu6Um9Z0gFgMbKzUHIJAsvh+izJ6Nl4LJPyK4SUnntE=
kubestash.dev/apimachinery v0.2.0/go.mod h1:ouqYby7/IIHRJgadPc++tzh3vxtjIgk1pDzU0YRCCw4=
kubestash.dev/apimachinery v0.2.1-0.20231213061518-17b1436fc227 h1:zuX9E4EyCUTEDV3LouAuSBL55c5Q+l+xatx2enOZF54=
kubestash.dev/apimachinery v0.2.1-0.20231213061518-17b1436fc227/go.mod h1:mZlerbYdxPXYFJQfOrXFDHBPV8NzZkVF1qHUsP0/Q0s=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
10 changes: 3 additions & 7 deletions pkg/clone.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ limitations under the License.
package pkg

import (
"fmt"

"github.com/spf13/cobra"
"gomodules.xyz/flags"
"k8s.io/cli-runtime/pkg/genericclioptions"
"kubestash.dev/apimachinery/pkg"
)

func NewCmdClone(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
Expand All @@ -32,17 +31,14 @@ func NewCmdClone(clientGetter genericclioptions.RESTClientGetter) *cobra.Command
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "to-namespace")

cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to read kubeconfig. Reason: %w", err)
}
var err error

srcNamespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

klient, err = newRuntimeClient(cfg)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down
13 changes: 4 additions & 9 deletions pkg/clone_pvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,10 @@ func NewCmdClonePVC() *cobra.Command {
Args: cobra.ExactArgs(1),
DisableAutoGenTag: true,
RunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "provider", "bucket", "encrypt-secret", "encrypt-secret-namespace")
flags.EnsureRequiredFlags(cmd, "encrypt-secret", "encrypt-secret-namespace")
if storageName == "" {
flags.EnsureRequiredFlags(cmd, "provider", "bucket")
}
if storageOpt.provider == string(storageapi.ProviderS3) {
flags.EnsureRequiredFlags(cmd, "endpoint")
}
Expand Down Expand Up @@ -213,14 +216,6 @@ func (opt *storageOption) getBackendInfo() storageapi.Backend {
Secret: opt.storageSecret,
},
}
case string(storageapi.ProviderRest):
backend = storageapi.Backend{
Provider: storageapi.ProviderRest,
Rest: &storageapi.RestServerSpec{
URL: opt.endpoint,
Secret: opt.storageSecret,
},
}
}

return backend
Expand Down
4 changes: 2 additions & 2 deletions pkg/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const (
// Constants for PVC cloning
const (
PVCAddon = "pvc-addon"
PVCBackupTask = "pvc-backup"
PVCRestoreTask = "pvc-restore"
PVCBackupTask = "LogicalBackup"
PVCRestoreTask = "LogicalBackupRestore"
PVCSchedule = "*/59 * * * *"
LatestSnapshot = "latest"
)
10 changes: 3 additions & 7 deletions pkg/copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ limitations under the License.
package pkg

import (
"fmt"

"github.com/spf13/cobra"
"gomodules.xyz/flags"
"k8s.io/cli-runtime/pkg/genericclioptions"
"kubestash.dev/apimachinery/pkg"
)

func NewCmdCopy(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
Expand All @@ -33,17 +32,14 @@ func NewCmdCopy(clientGetter genericclioptions.RESTClientGetter) *cobra.Command
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
flags.EnsureRequiredFlags(cmd, "to-namespace")

cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to read kubeconfig. Reason: %w", err)
}
var err error

srcNamespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

klient, err = newRuntimeClient(cfg)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/debug.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ limitations under the License.
package pkg

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"kubestash.dev/apimachinery/pkg"
)

func NewCmdDebug(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
Expand All @@ -29,17 +28,14 @@ func NewCmdDebug(clientGetter genericclioptions.RESTClientGetter) *cobra.Command
Short: `Debug common KubeStash issues`,
DisableAutoGenTag: true,
PersistentPreRunE: func(cmd *cobra.Command, args []string) error {
cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to read kubeconfig. Reason: %w", err)
}
var err error

srcNamespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

klient, err = newRuntimeClient(cfg)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down
20 changes: 4 additions & 16 deletions pkg/debug_backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"os"
"slices"

"github.com/spf13/cobra"
core "k8s.io/api/core/v1"
Expand Down Expand Up @@ -72,18 +73,18 @@ func NewCmdDebugBackup() *cobra.Command {
}

if latest {
if err := debugOpt.debugLatestBackupSessions(backupSessions); err != nil {
if err = debugOpt.debugLatestBackupSessions(backupSessions); err != nil {
return err
}
return nil
}

for _, bs := range backupSessions {
if !debugOpt.shouldDebugSession() {
if !slices.Contains(debugOpt.sessions, bs.Spec.Session) {
continue
}
debugOpt.backupSession = bs
if err := debugOpt.showTableForFailedBackupSession(); err != nil {
if err = debugOpt.showTableForFailedBackupSession(); err != nil {
return err
}
}
Expand Down Expand Up @@ -210,19 +211,6 @@ func (opt *backupDebugOptions) getLatestBackupSession(session string, backupSess
return bs
}

func (opt *backupDebugOptions) shouldDebugSession() bool {
if len(opt.sessions) == 0 {
return true
}

for _, session := range opt.sessions {
if opt.backupSession.Spec.Session == session {
return true
}
}
return false
}

func (opt *backupDebugOptions) showTableForFailedBackupSession() error {
var data [][]string

Expand Down
20 changes: 5 additions & 15 deletions pkg/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"os/exec"
"os/user"
"path/filepath"
"slices"
"strings"

"github.com/spf13/cobra"
Expand All @@ -32,6 +33,7 @@ import (
kmapi "kmodules.xyz/client-go/api/v1"
v1 "kmodules.xyz/offshoot-api/api/v1"
storageapi "kubestash.dev/apimachinery/apis/storage/v1alpha1"
"kubestash.dev/apimachinery/pkg"
"kubestash.dev/apimachinery/pkg/restic"
)

Expand Down Expand Up @@ -67,7 +69,7 @@ func NewCmdDownload(clientGetter genericclioptions.RESTClientGetter) *cobra.Comm
return err
}

klient, err = newRuntimeClient(downloadOpt.restConfig)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down Expand Up @@ -156,7 +158,8 @@ func NewCmdDownload(clientGetter genericclioptions.RESTClientGetter) *cobra.Comm
}

for compName, comp := range snapshot.Status.Components {
if !downloadOpt.shouldRestoreComponent(compName) {
if len(downloadOpt.components) != 0 &&
!slices.Contains(downloadOpt.components, compName) {
continue
}

Expand Down Expand Up @@ -321,16 +324,3 @@ func (opt *downloadOptions) runRestoreViaDocker(destination string, args []strin
}
return nil
}

func (opt *downloadOptions) shouldRestoreComponent(componentName string) bool {
if opt.components == nil {
return true
}

for _, comp := range opt.components {
if comp == componentName {
return true
}
}
return false
}
3 changes: 2 additions & 1 deletion pkg/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
storageapi "kubestash.dev/apimachinery/apis/storage/v1alpha1"
"kubestash.dev/apimachinery/pkg"
"kubestash.dev/apimachinery/pkg/restic"
)

Expand Down Expand Up @@ -59,7 +60,7 @@ func NewCmdKey(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
return err
}

klient, err = newRuntimeClient(opt.config)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/pause.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ limitations under the License.
package pkg

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"
"kubestash.dev/apimachinery/pkg"
)

func NewCmdPause(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
Expand All @@ -34,17 +33,14 @@ func NewCmdPause(clientGetter genericclioptions.RESTClientGetter) *cobra.Command
RunE: func(cmd *cobra.Command, args []string) error {
backupConfigName := args[0]

cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to read kubeconfig. Reason: %w", err)
}
var err error

srcNamespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

klient, err = newRuntimeClient(cfg)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down
10 changes: 3 additions & 7 deletions pkg/resume.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,10 @@ limitations under the License.
package pkg

import (
"fmt"

"github.com/spf13/cobra"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/klog/v2"
"kubestash.dev/apimachinery/pkg"
)

func NewCmdResume(clientGetter genericclioptions.RESTClientGetter) *cobra.Command {
Expand All @@ -34,17 +33,14 @@ func NewCmdResume(clientGetter genericclioptions.RESTClientGetter) *cobra.Comman
RunE: func(cmd *cobra.Command, args []string) error {
backupConfigName := args[0]

cfg, err := clientGetter.ToRESTConfig()
if err != nil {
return fmt.Errorf("failed to read kubeconfig. Reason: %w", err)
}
var err error

srcNamespace, _, err = clientGetter.ToRawKubeConfigLoader().Namespace()
if err != nil {
return err
}

klient, err = newRuntimeClient(cfg)
klient, err = pkg.NewUncachedClient()
if err != nil {
return err
}
Expand Down
Loading

0 comments on commit be035ce

Please sign in to comment.