|
| 1 | +package cluster_config |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + "os" |
| 8 | + "reflect" |
| 9 | + "runtime" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/samber/lo" |
| 13 | + "github.com/samber/lo/parallel" |
| 14 | + "github.com/spf13/cobra" |
| 15 | + corev1 "k8s.io/api/core/v1" |
| 16 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 17 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 18 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 19 | + "k8s.io/client-go/dynamic" |
| 20 | + "k8s.io/client-go/kubernetes" |
| 21 | + "k8s.io/kubectl/pkg/util/templates" |
| 22 | + |
| 23 | + "github.com/deckhouse/deckhouse-cli/internal/backup/k8s" |
| 24 | + "github.com/deckhouse/deckhouse-cli/internal/backup/tarball" |
| 25 | +) |
| 26 | + |
| 27 | +// TODO texts |
| 28 | +var clusterConfigLong = templates.LongDesc(` |
| 29 | +Take a snapshot of cluster configuration. |
| 30 | + |
| 31 | +This command creates a snapshot various resources . |
| 32 | +
|
| 33 | +© Flant JSC 2024`) |
| 34 | + |
| 35 | +func NewCommand() *cobra.Command { |
| 36 | + etcdCmd := &cobra.Command{ |
| 37 | + Use: "cluster-config <backup-tarball-path>", |
| 38 | + Short: "Take a snapshot of cluster configuration", |
| 39 | + Long: clusterConfigLong, |
| 40 | + ValidArgs: []string{"backup-tarball-path"}, |
| 41 | + SilenceErrors: true, |
| 42 | + SilenceUsage: true, |
| 43 | + // PreRunE: func(cmd *cobra.Command, args []string) error { |
| 44 | + // return validateFlags() |
| 45 | + // }, |
| 46 | + RunE: backupConfig, |
| 47 | + } |
| 48 | + |
| 49 | + addFlags(etcdCmd.Flags()) |
| 50 | + return etcdCmd |
| 51 | +} |
| 52 | + |
| 53 | +type BackupFunc func( |
| 54 | + kubeCl kubernetes.Interface, |
| 55 | + dynamicCl dynamic.Interface, |
| 56 | + namespaces []string, |
| 57 | +) ([]unstructured.Unstructured, error) |
| 58 | + |
| 59 | +func backupConfig(cmd *cobra.Command, args []string) error { |
| 60 | + if len(args) != 1 { |
| 61 | + return fmt.Errorf("This command requires exactly 1 argument") |
| 62 | + } |
| 63 | + |
| 64 | + // TODO move this to real file when done |
| 65 | + tarFile, err := os.CreateTemp(".", ".*.d8bkp") |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("failed to create temp file: %v", err) |
| 68 | + } |
| 69 | + defer func(fileName string) { |
| 70 | + _ = os.Remove(fileName) |
| 71 | + }(tarFile.Name()) |
| 72 | + |
| 73 | + backup := tarball.NewBackup(tarFile) |
| 74 | + kubeCl, dynamicCl, err := setupK8sClients(cmd) |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("setup k8s clients: %w", err) |
| 77 | + } |
| 78 | + |
| 79 | + namespaceList, err := kubeCl.CoreV1().Namespaces().List(context.TODO(), metav1.ListOptions{}) |
| 80 | + if err != nil { |
| 81 | + return fmt.Errorf("Failed to list namespaces: %w", err) |
| 82 | + } |
| 83 | + namespaces := lo.Map(namespaceList.Items, func(ns corev1.Namespace, _ int) string { |
| 84 | + return ns.Name |
| 85 | + }) |
| 86 | + |
| 87 | + // TODO move this to separate packages |
| 88 | + backups := []BackupFunc{ |
| 89 | + backupSecrets, |
| 90 | + backupConfigMaps, |
| 91 | + } |
| 92 | + |
| 93 | + parallel.ForEach(backups, func(bf BackupFunc, _ int) { |
| 94 | + thisFuncName := runtime.FuncForPC(reflect.ValueOf(bf).Pointer()).Name() |
| 95 | + resources, err := bf(kubeCl, dynamicCl, namespaces) |
| 96 | + if err != nil { |
| 97 | + log.Fatalf("%s failed: %v", thisFuncName, err) |
| 98 | + } |
| 99 | + |
| 100 | + if err = backup.PutResources(resources); err != nil { |
| 101 | + log.Fatalf("%s failed: %v", thisFuncName, err) |
| 102 | + } |
| 103 | + }) |
| 104 | + |
| 105 | + if err = backup.Close(); err != nil { |
| 106 | + return fmt.Errorf("close tarball failed: %w", err) |
| 107 | + } |
| 108 | + if err = tarFile.Sync(); err != nil { |
| 109 | + return fmt.Errorf("tarball flush failed: %w", err) |
| 110 | + } |
| 111 | + if err = tarFile.Close(); err != nil { |
| 112 | + return fmt.Errorf("tarball close failed: %w", err) |
| 113 | + } |
| 114 | + |
| 115 | + if err = os.Rename(tarFile.Name(), args[0]); err != nil { |
| 116 | + return fmt.Errorf("write tarball failed: %w", err) |
| 117 | + } |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func setupK8sClients(cmd *cobra.Command) (*kubernetes.Clientset, *dynamic.DynamicClient, error) { |
| 123 | + kubeconfigPath, err := cmd.Flags().GetString("kubeconfig") |
| 124 | + if err != nil { |
| 125 | + return nil, nil, fmt.Errorf("Failed to setup Kubernetes client: %w", err) |
| 126 | + } |
| 127 | + |
| 128 | + _, kubeCl, err := k8s.SetupK8sClientSet(kubeconfigPath) |
| 129 | + if err != nil { |
| 130 | + return nil, nil, fmt.Errorf("Failed to setup Kubernetes client: %w", err) |
| 131 | + } |
| 132 | + |
| 133 | + dynamicCl := k8s.SetupDynamicClientFromK8sClientset(kubeCl.RESTClient()) |
| 134 | + return kubeCl, dynamicCl, nil |
| 135 | +} |
| 136 | + |
| 137 | +func backupSecrets( |
| 138 | + _ kubernetes.Interface, |
| 139 | + dynamicCl dynamic.Interface, |
| 140 | + namespaces []string, |
| 141 | +) ([]unstructured.Unstructured, error) { |
| 142 | + namespaces = lo.Filter(namespaces, func(item string, _ int) bool { |
| 143 | + return strings.HasPrefix(item, "d8-") || strings.HasPrefix(item, "kube-") |
| 144 | + }) |
| 145 | + |
| 146 | + secrets := parallel.Map(namespaces, func(namespace string, index int) []unstructured.Unstructured { |
| 147 | + gvr := schema.GroupVersionResource{ |
| 148 | + Group: corev1.SchemeGroupVersion.Group, |
| 149 | + Version: corev1.SchemeGroupVersion.Version, |
| 150 | + Resource: "secrets", |
| 151 | + } |
| 152 | + |
| 153 | + list, err := dynamicCl.Resource(gvr).Namespace(namespace).List(context.TODO(), metav1.ListOptions{}) |
| 154 | + if err != nil { |
| 155 | + log.Fatalf("Failed to list secrets from : %v", err) |
| 156 | + } |
| 157 | + |
| 158 | + return list.Items |
| 159 | + }) |
| 160 | + |
| 161 | + return lo.Flatten(secrets), nil |
| 162 | +} |
| 163 | + |
| 164 | +func backupConfigMaps( |
| 165 | + _ kubernetes.Interface, |
| 166 | + dynamicCl dynamic.Interface, |
| 167 | + namespaces []string, |
| 168 | +) ([]unstructured.Unstructured, error) { |
| 169 | + namespaces = lo.Filter(namespaces, func(item string, _ int) bool { |
| 170 | + return strings.HasPrefix(item, "d8-") || strings.HasPrefix(item, "kube-") |
| 171 | + }) |
| 172 | + |
| 173 | + configmaps := parallel.Map(namespaces, func(namespace string, _ int) []unstructured.Unstructured { |
| 174 | + gvr := schema.GroupVersionResource{ |
| 175 | + Group: corev1.SchemeGroupVersion.Group, |
| 176 | + Version: corev1.SchemeGroupVersion.Version, |
| 177 | + Resource: "configmaps", |
| 178 | + } |
| 179 | + |
| 180 | + list, err := dynamicCl.Resource(gvr).Namespace(namespace).List(context.TODO(), metav1.ListOptions{}) |
| 181 | + if err != nil { |
| 182 | + log.Fatalf("Failed to list configmaps from : %v", err) |
| 183 | + } |
| 184 | + |
| 185 | + return list.Items |
| 186 | + }) |
| 187 | + |
| 188 | + return lo.Flatten(configmaps), nil |
| 189 | +} |
0 commit comments