From cacbc7ddef822c132b7bd0ec01a98b5a3f117842 Mon Sep 17 00:00:00 2001 From: "Md. Emruz Hossain" Date: Thu, 21 Mar 2019 17:21:05 +0600 Subject: [PATCH] Update workload controller for new design (#675) **Requirements:** - [x] Merge https://github.com/appscode/stash/pull/647 to master - [x] Merge https://github.com/appscode/stash/pull/673 to master - [x] Merge https://github.com/appscode/stash/pull/674 to master - [x] Merge https://github.com/appscode/stash/pull/671 to master - [x] Rebase against master **Tasks:** - [x] Update Restore Logic - [x] Update Backup Logic - [x] Update Workload Controllers - [x] Deployment - [x] DaemonSet - [x] ReplicationControler - [x] ReplicaSet - [x] StatefulSet - [x] Fix ConfigMap lock deletion and RBAC stuff deletion logic - [x] Ensure sidecar/init-container according to new design - [x] Ensure Restore init-container injection - [x] Ensure Restore init-container deletion - [x] Ensure Backup sidecar injection - [x] Ensure Backup sidecar deletion - [x] Add new backup command (sagor is working) - [x] Add new restore command - [x] Add controller (possibly in different PR) - [x] PersistentVolumeClaim - [x] AppBinding --- backup.go | 4 +-- backup_pvc.go | 2 +- restore.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++ root.go | 1 + server/options.go | 4 +++ server/start.go | 7 +++++ 6 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 restore.go diff --git a/backup.go b/backup.go index 81dc059d1..8eeaac4df 100644 --- a/backup.go +++ b/backup.go @@ -7,7 +7,7 @@ import ( "github.com/appscode/go/log" v "github.com/appscode/go/version" - api "github.com/appscode/stash/apis/stash/v1alpha1" + "github.com/appscode/stash/apis" cs "github.com/appscode/stash/client/clientset/versioned" "github.com/appscode/stash/pkg/backup" "github.com/appscode/stash/pkg/docker" @@ -80,7 +80,7 @@ func NewCmdBackup() *cobra.Command { log.Fatal(err) } } else { // for offline backup - if opt.Workload.Kind == api.KindDaemonSet || opt.Workload.Kind == api.KindStatefulSet { + if opt.Workload.Kind == apis.KindDaemonSet || opt.Workload.Kind == apis.KindStatefulSet { log.Infoln("Running backup once") if err = ctrl.Backup(); err != nil { log.Fatal(err) diff --git a/backup_pvc.go b/backup_pvc.go index 522a67f3d..857b3f419 100644 --- a/backup_pvc.go +++ b/backup_pvc.go @@ -40,7 +40,7 @@ func NewCmdBackupPVC() *cobra.Command { return handleResticError(outputDir, restic.DefaultOutputFileName, err) } // Run backup - backupOutput, backupErr := resticWrapper.RunBackup(&backupOpt) + backupOutput, backupErr := resticWrapper.RunBackup(backupOpt) // If metrics are enabled then generate metrics if metrics.Enabled { err := backupOutput.HandleMetrics(&metrics, backupErr) diff --git a/restore.go b/restore.go new file mode 100644 index 000000000..53b6bd4e9 --- /dev/null +++ b/restore.go @@ -0,0 +1,65 @@ +package cmds + +import ( + "github.com/appscode/go/log" + cs "github.com/appscode/stash/client/clientset/versioned" + "github.com/appscode/stash/pkg/restic" + "github.com/appscode/stash/pkg/restore" + "github.com/spf13/cobra" + "k8s.io/apimachinery/pkg/util/errors" + "k8s.io/client-go/kubernetes" + "k8s.io/client-go/tools/clientcmd" + "kmodules.xyz/client-go/meta" +) + +func NewCmdRestore() *cobra.Command { + opt := &restore.Options{ + MasterURL: "", + KubeconfigPath: "", + Namespace: meta.Namespace(), + SetupOpt: restic.SetupOptions{ + ScratchDir: "/tmp", + EnableCache: false, + }, + } + + cmd := &cobra.Command{ + Use: "restore", + Short: "Restore from backup", + DisableAutoGenTag: true, + RunE: func(cmd *cobra.Command, args []string) error { + // create client + config, err := clientcmd.BuildConfigFromFlags(opt.MasterURL, opt.KubeconfigPath) + if err != nil { + log.Fatal(err) + return err + } + opt.KubeClient = kubernetes.NewForConfigOrDie(config) + opt.StashClient = cs.NewForConfigOrDie(config) + + opt.Metrics.JobName = opt.RestoreSessionName + // run restore + err = restore.Restore(opt) + if err != nil { + // set RestoreSession status "Failed", write event and send prometheus metrics + e2 := restore.HandleRestoreFailure(opt, err) + if e2 != nil { + err = errors.NewAggregate([]error{err, e2}) + } + log.Fatalln(err) + } + return nil + }, + } + cmd.Flags().StringVar(&opt.MasterURL, "master", opt.MasterURL, "The address of the Kubernetes API server (overrides any value in kubeconfig)") + cmd.Flags().StringVar(&opt.KubeconfigPath, "kubeconfig", opt.KubeconfigPath, "Path to kubeconfig file with authorization information (the master location is set by the master flag).") + cmd.Flags().StringVar(&opt.RestoreSessionName, "restore-session", opt.RestoreSessionName, "Name of the RestoreSession CRD.") + cmd.Flags().DurationVar(&opt.BackoffMaxWait, "backoff-max-wait", 0, "Maximum wait for initial response from kube apiserver; 0 disables the timeout") + cmd.Flags().BoolVar(&opt.SetupOpt.EnableCache, "enable-cache", opt.SetupOpt.EnableCache, "Specify weather to enable caching for restic") + cmd.Flags().StringVar(&opt.SetupOpt.SecretDir, "secret-dir", opt.SetupOpt.SecretDir, "Directory where storage secret has been mounted") + + cmd.Flags().BoolVar(&opt.Metrics.Enabled, "metrics-enabled", opt.Metrics.Enabled, "Specify weather to export Prometheus metrics") + cmd.Flags().StringVar(&opt.Metrics.PushgatewayURL, "pushgateway-url", opt.Metrics.PushgatewayURL, "Pushgateway URL where the metrics will be pushed") + + return cmd +} diff --git a/root.go b/root.go index 4bc771d41..27204e177 100644 --- a/root.go +++ b/root.go @@ -52,6 +52,7 @@ func NewRootCmd() *cobra.Command { rootCmd.AddCommand(NewCmdSnapshots()) rootCmd.AddCommand(NewCmdForget()) rootCmd.AddCommand(NewBackupSession()) + rootCmd.AddCommand(NewCmdRestore()) return rootCmd } diff --git a/server/options.go b/server/options.go index b45fcf9e8..747a60e65 100644 --- a/server/options.go +++ b/server/options.go @@ -13,6 +13,7 @@ import ( "github.com/spf13/pflag" crd_cs "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset/typed/apiextensions/v1beta1" "k8s.io/client-go/kubernetes" + appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned" ) type ExtraOptions struct { @@ -87,5 +88,8 @@ func (s *ExtraOptions) ApplyTo(cfg *controller.Config) error { if cfg.CRDClient, err = crd_cs.NewForConfig(cfg.ClientConfig); err != nil { return err } + if cfg.AppCatalogClient, err = appcatalog_cs.NewForConfig(cfg.ClientConfig); err != nil { + return err + } return nil } diff --git a/server/start.go b/server/start.go index 1b5896847..30ddac3f5 100644 --- a/server/start.go +++ b/server/start.go @@ -85,6 +85,13 @@ func (o StashOptions) Config() (*server.StashConfig, error) { "/apis/admission.stash.appscode.com/v1alpha1/statefulsets", "/apis/admission.stash.appscode.com/v1alpha1/replicationcontrollers", "/apis/admission.stash.appscode.com/v1alpha1/replicasets", + // v1beta1 apis + "/apis/admission.stash.appscode.com/v1beta1/backupconfigurationtemplates", + "/apis/admission.stash.appscode.com/v1beta1/backupconfigurations", + "/apis/admission.stash.appscode.com/v1beta1/backupsessions", + "/apis/admission.stash.appscode.com/v1beta1/functions", + "/apis/admission.stash.appscode.com/v1beta1/restoresessions", + "/apis/admission.stash.appscode.com/v1beta1/tasks", } extraConfig := controller.NewConfig(serverConfig.ClientConfig)