Skip to content

Commit

Permalink
Implement cluster backup interface
Browse files Browse the repository at this point in the history
Signed-off-by: Emruz Hossain <[email protected]>
  • Loading branch information
Emruz Hossain committed Mar 21, 2022
1 parent 18612c3 commit a0a61a7
Show file tree
Hide file tree
Showing 64 changed files with 1,278 additions and 430 deletions.
11 changes: 5 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,14 @@ require (
gomodules.xyz/flags v0.1.3
gomodules.xyz/go-sh v0.1.0
gomodules.xyz/logs v0.0.6
gomodules.xyz/x v0.0.10
k8s.io/api v0.21.1
gomodules.xyz/x v0.0.13
k8s.io/apimachinery v0.21.1
k8s.io/client-go v0.21.1
k8s.io/klog/v2 v2.9.0
kmodules.xyz/client-go v0.0.0-20220203031013-1de48437aaf3
kmodules.xyz/custom-resources v0.0.0-20220208103158-61b298634e43
kmodules.xyz/offshoot-api v0.0.0-20211207130839-cc7187e020cf
stash.appscode.dev/apimachinery v0.18.1-0.20220316053843-5bd9e9bde0fd
kmodules.xyz/client-go v0.0.0-20220308080632-2543b61b64fd
kmodules.xyz/custom-resources v0.0.0-20220317043135-5c76c52c975d
kmodules.xyz/offshoot-api v0.0.0-20220317044008-01567138fc2f
stash.appscode.dev/apimachinery v0.18.1-0.20220321055929-ee132c17707b
)

replace bitbucket.org/ww/goautoneg => gomodules.xyz/goautoneg v0.0.0-20120707110453-a547fc61f48d
Expand Down
42 changes: 21 additions & 21 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -361,8 +361,9 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
Expand Down Expand Up @@ -456,8 +457,9 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
Expand Down Expand Up @@ -662,7 +664,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
go.bytebuilders.dev/license-verifier v0.9.3/go.mod h1:GpIW0o8O0wpiBVt7IIz4z7bcPuG8nza8/bCDkaupDn8=
go.bytebuilders.dev/license-verifier v0.9.7 h1:m7Z3cuVJYUUctCfTK6qT0IKtPVZurxq1b1bKzWjqwsE=
go.bytebuilders.dev/license-verifier v0.9.7/go.mod h1:GpIW0o8O0wpiBVt7IIz4z7bcPuG8nza8/bCDkaupDn8=
go.bytebuilders.dev/license-verifier/kubernetes v0.9.7 h1:TN9N9FmmxrPTQoAPJsQnHRgY00liNZgiTUyZ2QNtqyg=
Expand Down Expand Up @@ -945,18 +946,17 @@ gomodules.xyz/pointer v0.1.0 h1:sG2UKrYVSo6E3r4itAjXfPfe4fuXMi0KdyTHpR3vGCg=
gomodules.xyz/pointer v0.1.0/go.mod h1:sPLsC0+yLTRecUiC5yVlyvXhZ6LAGojNCRWNNqoplvo=
gomodules.xyz/runtime v0.2.0/go.mod h1:/R/Dw906YIUHKJ5cfKUaJgJrojFjpRU+n0s3Wjn1tqs=
gomodules.xyz/sets v0.0.0-20200930152147-4f4543544799/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
gomodules.xyz/sets v0.1.0/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
gomodules.xyz/sets v0.2.0/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
gomodules.xyz/sets v0.2.1 h1:vK3oUWoGVrZKLDKO/bzEo/ucHFdCE7+DxWPeWxK72KQ=
gomodules.xyz/sets v0.2.1/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
gomodules.xyz/stow v0.2.4/go.mod h1:aAgUEHyZ+4nKznyVupMMkP0JX9MXW1ZRrbRGaclp3E0=
gomodules.xyz/wait v0.2.0 h1:HnRIh+cvIrrKIFaXoYznCVVirv2/2xu3KzjSzsQmYAY=
gomodules.xyz/wait v0.2.0/go.mod h1:g/epKzZQuCqgvhzhaoG4cSBNGHqnOrhFR4Q7szDJ1JM=
gomodules.xyz/x v0.0.5/go.mod h1:EGy7cB7ifSuOcbNFc0U1zUPTf5yY8RtXVJlAlhGS9OE=
gomodules.xyz/x v0.0.7/go.mod h1:CMXe28rpApV30pPw9cxdyEmvoC+aa5LiAqzks9dlxag=
gomodules.xyz/x v0.0.8/go.mod h1:YnL5CAnLrEtAV/NQGn5sKJe6u08/vDlRyJddXIuD9mc=
gomodules.xyz/x v0.0.10 h1:b78HYvlXlqOVb8OZEy2uZeN/bp78nH42qIFnLQVR6O0=
gomodules.xyz/x v0.0.10/go.mod h1:AgHPywNbRKtogAdk08vCqsfwjtXtXX1/FUPyG7OWdOs=
gomodules.xyz/x v0.0.13 h1:k4ABA8dVGUTxqiNYOHPAs5sZbmgLxYcL9WuBT/kDOiM=
gomodules.xyz/x v0.0.13/go.mod h1:AgHPywNbRKtogAdk08vCqsfwjtXtXX1/FUPyG7OWdOs=
gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
gonum.org/v1/gonum v0.0.0-20190331200053-3d26580ed485/go.mod h1:2ltnJ7xHfj0zHS40VVPYEAAMTa3ZGguvHGBSJeRWqE0=
gonum.org/v1/gonum v0.6.2/go.mod h1:9mxDZsDKxgMAuccQkewq682L+0eCu4dCN2yonUJTCLU=
Expand Down Expand Up @@ -1067,23 +1067,23 @@ k8s.io/system-validators v1.4.0/go.mod h1:bPldcLgkIUK22ALflnsXk8pvkTEndYdNuaHH6g
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 h1:CbnUZsM497iRC5QMVkHwyl8s2tB3g7yaSHkYPkpgelw=
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
kmodules.xyz/client-go v0.0.0-20210617233340-13d22e91512b/go.mod h1:A6GAK6xP5zBuWK6A/vUkkjKzcuywkms7fIxRf5wblO4=
kmodules.xyz/client-go v0.0.0-20211013093146-1fbfd52e78c9/go.mod h1:0gkPeALtYjB27OHt4rd6+ZmMgoVTHVLtEJQeU23/gtA=
kmodules.xyz/client-go v0.0.0-20211107190155-5bb4090d2728/go.mod h1:ENUu8pPK19xzBkVpAJHoGCI2QRvb1SqffWRt0K2sV5I=
kmodules.xyz/client-go v0.0.0-20220104114408-2a3a05dbe89f/go.mod h1:xxl1ve1Obe4xaW+XjXsNHyLTni4QPIvHn9TfnYEoQRo=
kmodules.xyz/client-go v0.0.0-20220203031013-1de48437aaf3 h1:CWux6RrrTkplf9F0ChJwkEEQuOorbc5rzmqC7uJUUZU=
kmodules.xyz/client-go v0.0.0-20220203031013-1de48437aaf3/go.mod h1:aOwnhdxO0uh54ds1wQYRlKVtYlzLyakaesmMQeupVek=
kmodules.xyz/client-go v0.0.0-20220215012708-9963581d69a7/go.mod h1:sOq5P3AkZdv6D/skvUPwEG15NDYy5olwBllH/JXfhGI=
kmodules.xyz/client-go v0.0.0-20220308080632-2543b61b64fd h1:9sOSO9moUpqn2CKeXh+GYP8ol1nU5IsLHJvZtNIzKz0=
kmodules.xyz/client-go v0.0.0-20220308080632-2543b61b64fd/go.mod h1:hu7eGess/SEbiss671Y062QBc8O45Qh0IGv19/0kPXM=
kmodules.xyz/constants v0.0.0-20210218100002-2c304bfda278/go.mod h1:DbiFk1bJ1KEO94t1SlAn7tzc+Zz95rSXgyUKa2nzPmY=
kmodules.xyz/crd-schema-fuzz v0.0.0-20210618002152-fae23aef5fb4/go.mod h1:IIkUctlfoptoci0BOrsUf8ya+MOG5uaeh1PE4uzaIbA=
kmodules.xyz/custom-resources v0.0.0-20220208103158-61b298634e43 h1:mwW2DgP7sAMambZe7Met/e9nrBYnzYgM/lupbm7jgGM=
kmodules.xyz/custom-resources v0.0.0-20220208103158-61b298634e43/go.mod h1:/XjDeILFV2wBota5kHo21DMvOt08nSAk1vm6buCuwt4=
kmodules.xyz/objectstore-api v0.0.0-20211207131029-3271069de43e h1:hbnb7Zy6pe0IwWWdIVbgfzBLM3kmppUMDpf7Sxy11d8=
kmodules.xyz/objectstore-api v0.0.0-20211207131029-3271069de43e/go.mod h1:IICnDdPFOEeGXdaPVHOGYfdwD1cyh/p1I/TWMkyNTIE=
kmodules.xyz/crd-schema-fuzz v0.0.0-20211025154117-6edb24ef11bc/go.mod h1:yLOBJKasPhnCodKSZGFZ6OGFFrp0tq3ALS9rDnYFjkg=
kmodules.xyz/custom-resources v0.0.0-20220317043135-5c76c52c975d h1:YMi4geEI+RROtXKjLp6f05G7KM0ancZSy69vZwe5fZ8=
kmodules.xyz/custom-resources v0.0.0-20220317043135-5c76c52c975d/go.mod h1:cDD2gsimPPj+Uu/6pzzw/wXjxLZyA0B/eM+UA3wWE9A=
kmodules.xyz/objectstore-api v0.0.0-20220317043839-8f754cdffcf0 h1:SznGzgK5Xu34cpdr24iJaW4AzZHZeMJLHV41AkfDmmA=
kmodules.xyz/objectstore-api v0.0.0-20220317043839-8f754cdffcf0/go.mod h1:xbaGWFbesdDp4I/k/w9+Xv8c7aiCzKlgVjmemxxXOgw=
kmodules.xyz/offshoot-api v0.0.0-20210829122105-6f4d481b0c61/go.mod h1:3LECbAL3FgbyK80NP3V3Pmiuo/a3hFWg/PR6SPFhTns=
kmodules.xyz/offshoot-api v0.0.0-20211207130839-cc7187e020cf h1:cVaxFPsZyaymGsx4XoTii1MEvtwGxmLg75qwMxNHB4w=
kmodules.xyz/offshoot-api v0.0.0-20211207130839-cc7187e020cf/go.mod h1:sJYyxf84ZvbVz4SivxMgSelGRYn19wOLUtObiEncCxk=
kmodules.xyz/prober v0.0.0-20210618020259-5836fb959027 h1:rtSPsruEkoBOLcbJkxMQjnPafpv8/vJnfWlTIj9yRBg=
kmodules.xyz/prober v0.0.0-20210618020259-5836fb959027/go.mod h1:H4NcvS1RQxeXtQONALbU6r9OT3Xa7cMhfamX/k9t9wo=
kmodules.xyz/resource-metrics v0.0.6/go.mod h1:M7rWuo2qh3BpHhogiEVPnvGY9Xx4Pfygqn1Rex8YbgM=
kmodules.xyz/offshoot-api v0.0.0-20220317044008-01567138fc2f h1:AD6ptk7qRZ8N6DY4Y2lCnqvfu3X/20YdQArTxN5g3sA=
kmodules.xyz/offshoot-api v0.0.0-20220317044008-01567138fc2f/go.mod h1:MhgruKArKzAxhZsBc8ZToiD/vzdtWYrnTu5hSkkYGfk=
kmodules.xyz/prober v0.0.0-20220317043828-5ae0114adcad h1:bMfimFOEgmaKCN/gnSxLIRvQbWg4dqBgcWrMnHWQgyc=
kmodules.xyz/prober v0.0.0-20220317043828-5ae0114adcad/go.mod h1:QC3O1p+sU/8NnpLKI0VCAO3IQwPlwuimf9l6+Qp3Vt8=
kmodules.xyz/resource-metrics v0.0.8/go.mod h1:M7rWuo2qh3BpHhogiEVPnvGY9Xx4Pfygqn1Rex8YbgM=
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
Expand All @@ -1108,5 +1108,5 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
stash.appscode.dev/apimachinery v0.18.1-0.20220316053843-5bd9e9bde0fd h1:pdQh2qRksCdHQ+RsaGmwYkkbcxkltHJuNMUkrP7HHnI=
stash.appscode.dev/apimachinery v0.18.1-0.20220316053843-5bd9e9bde0fd/go.mod h1:R+AE9wyi0sPZ0t3HWwZq+FiA7ewoGbqsdW6atczGf94=
stash.appscode.dev/apimachinery v0.18.1-0.20220321055929-ee132c17707b h1:NzNCh1LNlEAK1gdCNcRUY17s6f0oMfuFJ0JNP0SbLE4=
stash.appscode.dev/apimachinery v0.18.1-0.20220321055929-ee132c17707b/go.mod h1:f2VcuZ6mMvRaSVLavWxi9HDp3LNusFKmmaAqfY7H7d8=
22 changes: 11 additions & 11 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,15 @@ package pkg

import (
"context"
v1 "kmodules.xyz/offshoot-api/api/v1"
"path/filepath"
"stash.appscode.dev/apimachinery/pkg/invoker"
"stash.appscode.dev/elasticsearch/pkg/manifests"
"time"

api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
"stash.appscode.dev/apimachinery/pkg/invoker"
"stash.appscode.dev/apimachinery/pkg/restic"
api_util "stash.appscode.dev/apimachinery/pkg/util"
"stash.appscode.dev/elasticsearch/pkg/manager"

"github.com/spf13/cobra"
license "go.bytebuilders.dev/license-verifier/kubernetes"
Expand All @@ -37,7 +36,7 @@ import (
"k8s.io/client-go/tools/clientcmd"
"k8s.io/klog/v2"
appcatalog_cs "kmodules.xyz/custom-resources/client/clientset/versioned"
"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
v1 "kmodules.xyz/offshoot-api/api/v1"
)

func NewCmdBackup() *cobra.Command {
Expand Down Expand Up @@ -99,12 +98,12 @@ func NewCmdBackup() *cobra.Command {
backupOutput, err = opt.backupManifests(ti.Target.Ref)
if err != nil {
backupOutput = &restic.BackupOutput{
BackupTargetStatus: api_v1beta1.BackupTargetStatus{
BackupTargetStatus: v1beta1.BackupTargetStatus{
Ref: ti.Target.Ref,
Stats: []api_v1beta1.HostBackupStats{
Stats: []v1beta1.HostBackupStats{
{
Hostname: opt.backupOptions.Host,
Phase: api_v1beta1.HostBackupFailed,
Phase: v1beta1.HostBackupFailed,
Error: err.Error(),
},
},
Expand Down Expand Up @@ -161,7 +160,7 @@ func NewCmdBackup() *cobra.Command {
return cmd
}

func (opt *options) backupManifests(targetRef api_v1beta1.TargetRef) (*restic.BackupOutput, error) {
func (opt *options) backupManifests(targetRef v1beta1.TargetRef) (*restic.BackupOutput, error) {
var err error
opt.setupOptions.StorageSecret, err = opt.kubeClient.CoreV1().Secrets(opt.storageSecret.Namespace).Get(context.TODO(), opt.storageSecret.Name, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -200,9 +199,10 @@ func (opt *options) backupManifests(targetRef api_v1beta1.TargetRef) (*restic.Ba
return nil, err
}

dumper := manifests.NewDumper()
mgOpts := manager.BackupOptions{}
mgr := manager.NewBackupManager(mgOpts)

err = dumper.Dump()
err = mgr.Dump()
if err != nil {
return nil, err
}
Expand Down
15 changes: 15 additions & 0 deletions pkg/manager/application.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package manager

type applicationBackupManager struct {
BackupOptions
}

func newApplicationBackupManager(opt BackupOptions) BackupManager {
return applicationBackupManager{
BackupOptions: opt,
}
}

func (opt applicationBackupManager) Dump() error {
return nil
}
154 changes: 154 additions & 0 deletions pkg/manager/cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
package manager

import (
"context"
"path/filepath"
"strings"

"stash.appscode.dev/elasticsearch/pkg/manager/sanitizers"

core "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/runtime/serializer"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/discovery"
"k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/klog/v2"
"kmodules.xyz/client-go/tools/backup"
"sigs.k8s.io/yaml"
)

type clusterBackupManager struct {
BackupOptions
}

func newClusterBackupManager(opt BackupOptions) BackupManager {
return clusterBackupManager{
BackupOptions: opt,
}
}

func (opt clusterBackupManager) Dump() error {
// backup cluster resources yaml into opt.backupDir
mgr := backup.NewBackupManager(opt.Ctx, opt.Config, opt.Sanitize)

_, err := mgr.BackupToDir(opt.DataDir)
if err != nil {
return err
}
return nil
}

type ItemList struct {
Items []map[string]interface{} `json:"items,omitempty"`
}

type processorFunc func(relPath string, data []byte) error

func (opt clusterBackupManager) Backup(process processorFunc) error {
// ref: https://github.com/kubernetes/ingress-nginx/blob/0dab51d9eb1e5a9ba3661f351114825ac8bfc1af/pkg/ingress/controller/launch.go#L252
opt.Config.QPS = 1e6
opt.Config.Burst = 1e6
if err := rest.SetKubernetesDefaults(opt.Config); err != nil {
return err
}
opt.Config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
if opt.Config.UserAgent == "" {
opt.Config.UserAgent = rest.DefaultKubernetesUserAgent()
}

disClient, err := discovery.NewDiscoveryClientForConfig(opt.Config)
if err != nil {
return err
}
resourceLists, err := disClient.ServerPreferredResources()
if err != nil {
return err
}
resourceListBytes, err := yaml.Marshal(resourceLists)
if err != nil {
return err
}
err = process("resource_lists.yaml", resourceListBytes)
if err != nil {
return err
}

for _, list := range resourceLists {
gv, err := schema.ParseGroupVersion(list.GroupVersion)
if err != nil {
return err
}
for _, r := range list.APIResources {
if strings.ContainsRune(r.Name, '/') {
continue // skip subresource
}
if !sets.NewString(r.Verbs...).HasAll("list", "get") {
continue
}

klog.V(3).Infof("Taking backup of %s apiVersion:%s kind:%s", list.GroupVersion, r.Name, r.Kind)
opt.Config.GroupVersion = &gv
opt.Config.APIPath = "/apis"
if gv.Group == core.GroupName {
opt.Config.APIPath = "/api"
}
client, err := rest.RESTClientFor(opt.Config)
if err != nil {
return err
}
request := client.Get().Resource(r.Name).Param("pretty", "true")
resp, err := request.DoRaw(context.TODO())
if err != nil {
return err
}
items := &ItemList{}
err = yaml.Unmarshal(resp, &items)
if err != nil {
return err
}
for _, item := range items.Items {
var path string
item["apiVersion"] = list.GroupVersion
item["kind"] = r.Kind

md, ok := item["metadata"]
if ok {
path = getPathFromSelfLink(md)
}

if opt.Sanitize {
s := sanitizers.NewSanitizer(r)
item, err := s.Sanitize(item)
if err != nil {
return err
}
delete(item, "status")
}
data, err := yaml.Marshal(item)
if err != nil {
return err
}
absPath := filepath.Join(opt.DataDir, path)
err = opt.Storage.Write(absPath, data)
if err != nil {
return err
}
err = process(path, data)
if err != nil {
return err
}
}
}
}
return nil
}

func getPathFromSelfLink(md interface{}) string {
meta, ok := md.(map[string]interface{})
if ok {
return meta["selfLink"].(string) + ".yaml"
}
return ""
}
15 changes: 15 additions & 0 deletions pkg/manager/namespace.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package manager

type namespaceBackupManager struct {
BackupOptions
}

func newNamespaceBackupManager(opt BackupOptions) BackupManager {
return namespaceBackupManager{
BackupOptions: opt,
}
}

func (opt namespaceBackupManager) Dump() error {
return nil
}
Loading

0 comments on commit a0a61a7

Please sign in to comment.