Skip to content

Commit

Permalink
Update uncached client + Add session list in trigger backup (#10)
Browse files Browse the repository at this point in the history
Signed-off-by: Md. Ishtiaq Islam <[email protected]>
  • Loading branch information
ishtiaqhimel authored Jan 1, 2024
1 parent 72b688f commit c1b2cec
Show file tree
Hide file tree
Showing 35 changed files with 2,354 additions and 118 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ require (
golang.org/x/term v0.15.0 // indirect
golang.org/x/time v0.3.0 // indirect
gomodules.xyz/clock v0.0.0-20200817085942-06523dba733f // indirect
gomodules.xyz/envsubst v0.2.0 // indirect
gomodules.xyz/jsonpatch/v2 v2.4.0 // indirect
gomodules.xyz/mergo v0.3.13 // indirect
gomodules.xyz/pointer v0.1.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -601,6 +601,8 @@ golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8T
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gomodules.xyz/clock v0.0.0-20200817085942-06523dba733f h1:hTyhR4r+tj1Uq7/PpFxLTzbeA0LhMVp7bEYfhkzFjdY=
gomodules.xyz/clock v0.0.0-20200817085942-06523dba733f/go.mod h1:K3m7N+nBOlf91/tpv8REUGwsAgaKFwElQCuiLhm12AQ=
gomodules.xyz/envsubst v0.2.0 h1:piG4OcpUa/Mu7LxSo+2Ye8JH7pXXWM2XuCD8Ic7Hdwc=
gomodules.xyz/envsubst v0.2.0/go.mod h1:eB1KRKtXx6RSpl+WYxE8gFE7DhNJCoorHzSij7X3AQo=
gomodules.xyz/flags v0.1.3 h1:jQ06+EfmoMv5NvjXvJon03dOhLU+FF0TQMWN7I6qpzs=
gomodules.xyz/flags v0.1.3/go.mod h1:e+kvBLnqdEWGG670SKOYag1CXStM2Slrxq01OIK3tFs=
gomodules.xyz/go-sh v0.1.0 h1:1BJAuGREh2RhePt7HRrpmjnkbgfpXlCzc42SiyZ5dkc=
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: %v", 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
6 changes: 4 additions & 2 deletions pkg/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ const (
ResticRegistry = "restic"
ResticImage = "restic"
ResticTag = "0.16.0"
EnvHttpProxy = "HTTP_PROXY"
EnvHttpsProxy = "HTTPS_PROXY"
)

// Constants for debugging
Expand All @@ -54,8 +56,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: %v", 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: %v", 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
24 changes: 7 additions & 17 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 @@ -292,8 +295,8 @@ func (opt *downloadOptions) runRestoreViaDocker(destination string, args []strin
"-u", currentUser.Uid,
"-v", ScratchDir + ":" + ScratchDir,
"-v", opt.destinationDir + ":" + DestinationDir,
"--env", "HTTP_PROXY=" + os.Getenv("HTTP_PROXY"),
"--env", "HTTPS_PROXY=" + os.Getenv("HTTPS_PROXY"),
"--env", fmt.Sprintf("%s=", EnvHttpProxy) + os.Getenv(EnvHttpProxy),
"--env", fmt.Sprintf("%s=", EnvHttpsProxy) + os.Getenv(EnvHttpsProxy),
"--env-file", filepath.Join(ConfigDir, ResticEnvs),
imgRestic.ToContainerImage(),
}
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
}
7 changes: 4 additions & 3 deletions 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 Expand Up @@ -138,8 +139,8 @@ func (opt *keyOptions) runCmdViaDocker(args []string) error {
"--rm",
"-u", currentUser.Uid,
"-v", ScratchDir + ":" + ScratchDir,
"--env", "HTTP_PROXY=" + os.Getenv("HTTP_PROXY"),
"--env", "HTTPS_PROXY=" + os.Getenv("HTTPS_PROXY"),
"--env", fmt.Sprintf("%s=", EnvHttpProxy) + os.Getenv(EnvHttpProxy),
"--env", fmt.Sprintf("%s=", EnvHttpsProxy) + os.Getenv(EnvHttpsProxy),
"--env-file", filepath.Join(ConfigDir, ResticEnvs),
}

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: %v", 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: %v", 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 c1b2cec

Please sign in to comment.