Skip to content

Commit a0a61a7

Browse files
author
Emruz Hossain
committed
Implement cluster backup interface
Signed-off-by: Emruz Hossain <emruz@appscode.com>
1 parent 18612c3 commit a0a61a7

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1278
-430
lines changed

go.mod

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ require (
88
gomodules.xyz/flags v0.1.3
99
gomodules.xyz/go-sh v0.1.0
1010
gomodules.xyz/logs v0.0.6
11-
gomodules.xyz/x v0.0.10
12-
k8s.io/api v0.21.1
11+
gomodules.xyz/x v0.0.13
1312
k8s.io/apimachinery v0.21.1
1413
k8s.io/client-go v0.21.1
1514
k8s.io/klog/v2 v2.9.0
16-
kmodules.xyz/client-go v0.0.0-20220203031013-1de48437aaf3
17-
kmodules.xyz/custom-resources v0.0.0-20220208103158-61b298634e43
18-
kmodules.xyz/offshoot-api v0.0.0-20211207130839-cc7187e020cf
19-
stash.appscode.dev/apimachinery v0.18.1-0.20220316053843-5bd9e9bde0fd
15+
kmodules.xyz/client-go v0.0.0-20220308080632-2543b61b64fd
16+
kmodules.xyz/custom-resources v0.0.0-20220317043135-5c76c52c975d
17+
kmodules.xyz/offshoot-api v0.0.0-20220317044008-01567138fc2f
18+
stash.appscode.dev/apimachinery v0.18.1-0.20220321055929-ee132c17707b
2019
)
2120

2221
replace bitbucket.org/ww/goautoneg => gomodules.xyz/goautoneg v0.0.0-20120707110453-a547fc61f48d

go.sum

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -361,8 +361,9 @@ github.com/json-iterator/go v1.1.6/go.mod h1:+SdeFBvtyEkXs7REEP0seUULqWtbJapLOCV
361361
github.com/json-iterator/go v1.1.7/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
362362
github.com/json-iterator/go v1.1.8/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
363363
github.com/json-iterator/go v1.1.10/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
364-
github.com/json-iterator/go v1.1.11 h1:uVUAXhF2To8cbw/3xN3pxj6kk7TYKs98NIrTqPlMWAQ=
365364
github.com/json-iterator/go v1.1.11/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
365+
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
366+
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
366367
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
367368
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
368369
github.com/julienschmidt/httprouter v1.2.0/go.mod h1:SYymIcj16QtmaHHD7aYtjjsJG7VTCxuUUipMqKk8s4w=
@@ -456,8 +457,9 @@ github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJ
456457
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd h1:TRLaZ9cD/w8PVh93nsPXa1VrQ6jlwL5oN8l14QlcNfg=
457458
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
458459
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
459-
github.com/modern-go/reflect2 v1.0.1 h1:9f412s+6RmYXLWZSEzVVgPGK7C2PphHj5RJrvfx9AWI=
460460
github.com/modern-go/reflect2 v1.0.1/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
461+
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
462+
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
461463
github.com/mohae/deepcopy v0.0.0-20170603005431-491d3605edfb/go.mod h1:TaXosZuwdSHYgviHp1DAtfrULt5eUgsSMsZf+YrPgl8=
462464
github.com/monochromegane/go-gitignore v0.0.0-20200626010858-205db1a8cc00/go.mod h1:Pm3mSP3c5uWn86xMLZ5Sa7JB9GsEZySvHYXCTK4E9q4=
463465
github.com/morikuni/aec v1.0.0/go.mod h1:BbKIizmSmc5MMPqRYbxO4ZU0S0+P200+tUnFx7PXmsc=
@@ -662,7 +664,6 @@ github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9dec
662664
github.com/yvasiyarov/go-metrics v0.0.0-20140926110328-57bccd1ccd43/go.mod h1:aX5oPXxHm3bOH+xeAttToC8pqch2ScQN/JoXYupl6xs=
663665
github.com/yvasiyarov/gorelic v0.0.0-20141212073537-a9bba5b9ab50/go.mod h1:NUSPSUX/bi6SeDMUh6brw0nXpxHnc96TguQh0+r/ssA=
664666
github.com/yvasiyarov/newrelic_platform_go v0.0.0-20140908184405-b21fdbd4370f/go.mod h1:GlGEuHIJweS1mbCqG+7vt2nvWLzLLnRHbXz5JKd/Qbg=
665-
go.bytebuilders.dev/license-verifier v0.9.3/go.mod h1:GpIW0o8O0wpiBVt7IIz4z7bcPuG8nza8/bCDkaupDn8=
666667
go.bytebuilders.dev/license-verifier v0.9.7 h1:m7Z3cuVJYUUctCfTK6qT0IKtPVZurxq1b1bKzWjqwsE=
667668
go.bytebuilders.dev/license-verifier v0.9.7/go.mod h1:GpIW0o8O0wpiBVt7IIz4z7bcPuG8nza8/bCDkaupDn8=
668669
go.bytebuilders.dev/license-verifier/kubernetes v0.9.7 h1:TN9N9FmmxrPTQoAPJsQnHRgY00liNZgiTUyZ2QNtqyg=
@@ -945,18 +946,17 @@ gomodules.xyz/pointer v0.1.0 h1:sG2UKrYVSo6E3r4itAjXfPfe4fuXMi0KdyTHpR3vGCg=
945946
gomodules.xyz/pointer v0.1.0/go.mod h1:sPLsC0+yLTRecUiC5yVlyvXhZ6LAGojNCRWNNqoplvo=
946947
gomodules.xyz/runtime v0.2.0/go.mod h1:/R/Dw906YIUHKJ5cfKUaJgJrojFjpRU+n0s3Wjn1tqs=
947948
gomodules.xyz/sets v0.0.0-20200930152147-4f4543544799/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
948-
gomodules.xyz/sets v0.1.0/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
949949
gomodules.xyz/sets v0.2.0/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
950950
gomodules.xyz/sets v0.2.1 h1:vK3oUWoGVrZKLDKO/bzEo/ucHFdCE7+DxWPeWxK72KQ=
951951
gomodules.xyz/sets v0.2.1/go.mod h1:jKgNp01/iDs+svOWXaPk5cKP3VXy0mWUoTF/ore+aMc=
952952
gomodules.xyz/stow v0.2.4/go.mod h1:aAgUEHyZ+4nKznyVupMMkP0JX9MXW1ZRrbRGaclp3E0=
953953
gomodules.xyz/wait v0.2.0 h1:HnRIh+cvIrrKIFaXoYznCVVirv2/2xu3KzjSzsQmYAY=
954954
gomodules.xyz/wait v0.2.0/go.mod h1:g/epKzZQuCqgvhzhaoG4cSBNGHqnOrhFR4Q7szDJ1JM=
955955
gomodules.xyz/x v0.0.5/go.mod h1:EGy7cB7ifSuOcbNFc0U1zUPTf5yY8RtXVJlAlhGS9OE=
956-
gomodules.xyz/x v0.0.7/go.mod h1:CMXe28rpApV30pPw9cxdyEmvoC+aa5LiAqzks9dlxag=
957956
gomodules.xyz/x v0.0.8/go.mod h1:YnL5CAnLrEtAV/NQGn5sKJe6u08/vDlRyJddXIuD9mc=
958-
gomodules.xyz/x v0.0.10 h1:b78HYvlXlqOVb8OZEy2uZeN/bp78nH42qIFnLQVR6O0=
959957
gomodules.xyz/x v0.0.10/go.mod h1:AgHPywNbRKtogAdk08vCqsfwjtXtXX1/FUPyG7OWdOs=
958+
gomodules.xyz/x v0.0.13 h1:k4ABA8dVGUTxqiNYOHPAs5sZbmgLxYcL9WuBT/kDOiM=
959+
gomodules.xyz/x v0.0.13/go.mod h1:AgHPywNbRKtogAdk08vCqsfwjtXtXX1/FUPyG7OWdOs=
960960
gonum.org/v1/gonum v0.0.0-20180816165407-929014505bf4/go.mod h1:Y+Yx5eoAFn32cQvJDxZx5Dpnq+c3wtXuadVZAcxbbBo=
961961
gonum.org/v1/gonum v0.0.0-20190331200053-3d26580ed485/go.mod h1:2ltnJ7xHfj0zHS40VVPYEAAMTa3ZGguvHGBSJeRWqE0=
962962
gonum.org/v1/gonum v0.6.2/go.mod h1:9mxDZsDKxgMAuccQkewq682L+0eCu4dCN2yonUJTCLU=
@@ -1067,23 +1067,23 @@ k8s.io/system-validators v1.4.0/go.mod h1:bPldcLgkIUK22ALflnsXk8pvkTEndYdNuaHH6g
10671067
k8s.io/utils v0.0.0-20201110183641-67b214c5f920 h1:CbnUZsM497iRC5QMVkHwyl8s2tB3g7yaSHkYPkpgelw=
10681068
k8s.io/utils v0.0.0-20201110183641-67b214c5f920/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
10691069
kmodules.xyz/client-go v0.0.0-20210617233340-13d22e91512b/go.mod h1:A6GAK6xP5zBuWK6A/vUkkjKzcuywkms7fIxRf5wblO4=
1070-
kmodules.xyz/client-go v0.0.0-20211013093146-1fbfd52e78c9/go.mod h1:0gkPeALtYjB27OHt4rd6+ZmMgoVTHVLtEJQeU23/gtA=
10711070
kmodules.xyz/client-go v0.0.0-20211107190155-5bb4090d2728/go.mod h1:ENUu8pPK19xzBkVpAJHoGCI2QRvb1SqffWRt0K2sV5I=
1072-
kmodules.xyz/client-go v0.0.0-20220104114408-2a3a05dbe89f/go.mod h1:xxl1ve1Obe4xaW+XjXsNHyLTni4QPIvHn9TfnYEoQRo=
1073-
kmodules.xyz/client-go v0.0.0-20220203031013-1de48437aaf3 h1:CWux6RrrTkplf9F0ChJwkEEQuOorbc5rzmqC7uJUUZU=
1074-
kmodules.xyz/client-go v0.0.0-20220203031013-1de48437aaf3/go.mod h1:aOwnhdxO0uh54ds1wQYRlKVtYlzLyakaesmMQeupVek=
1071+
kmodules.xyz/client-go v0.0.0-20220215012708-9963581d69a7/go.mod h1:sOq5P3AkZdv6D/skvUPwEG15NDYy5olwBllH/JXfhGI=
1072+
kmodules.xyz/client-go v0.0.0-20220308080632-2543b61b64fd h1:9sOSO9moUpqn2CKeXh+GYP8ol1nU5IsLHJvZtNIzKz0=
1073+
kmodules.xyz/client-go v0.0.0-20220308080632-2543b61b64fd/go.mod h1:hu7eGess/SEbiss671Y062QBc8O45Qh0IGv19/0kPXM=
10751074
kmodules.xyz/constants v0.0.0-20210218100002-2c304bfda278/go.mod h1:DbiFk1bJ1KEO94t1SlAn7tzc+Zz95rSXgyUKa2nzPmY=
10761075
kmodules.xyz/crd-schema-fuzz v0.0.0-20210618002152-fae23aef5fb4/go.mod h1:IIkUctlfoptoci0BOrsUf8ya+MOG5uaeh1PE4uzaIbA=
1077-
kmodules.xyz/custom-resources v0.0.0-20220208103158-61b298634e43 h1:mwW2DgP7sAMambZe7Met/e9nrBYnzYgM/lupbm7jgGM=
1078-
kmodules.xyz/custom-resources v0.0.0-20220208103158-61b298634e43/go.mod h1:/XjDeILFV2wBota5kHo21DMvOt08nSAk1vm6buCuwt4=
1079-
kmodules.xyz/objectstore-api v0.0.0-20211207131029-3271069de43e h1:hbnb7Zy6pe0IwWWdIVbgfzBLM3kmppUMDpf7Sxy11d8=
1080-
kmodules.xyz/objectstore-api v0.0.0-20211207131029-3271069de43e/go.mod h1:IICnDdPFOEeGXdaPVHOGYfdwD1cyh/p1I/TWMkyNTIE=
1076+
kmodules.xyz/crd-schema-fuzz v0.0.0-20211025154117-6edb24ef11bc/go.mod h1:yLOBJKasPhnCodKSZGFZ6OGFFrp0tq3ALS9rDnYFjkg=
1077+
kmodules.xyz/custom-resources v0.0.0-20220317043135-5c76c52c975d h1:YMi4geEI+RROtXKjLp6f05G7KM0ancZSy69vZwe5fZ8=
1078+
kmodules.xyz/custom-resources v0.0.0-20220317043135-5c76c52c975d/go.mod h1:cDD2gsimPPj+Uu/6pzzw/wXjxLZyA0B/eM+UA3wWE9A=
1079+
kmodules.xyz/objectstore-api v0.0.0-20220317043839-8f754cdffcf0 h1:SznGzgK5Xu34cpdr24iJaW4AzZHZeMJLHV41AkfDmmA=
1080+
kmodules.xyz/objectstore-api v0.0.0-20220317043839-8f754cdffcf0/go.mod h1:xbaGWFbesdDp4I/k/w9+Xv8c7aiCzKlgVjmemxxXOgw=
10811081
kmodules.xyz/offshoot-api v0.0.0-20210829122105-6f4d481b0c61/go.mod h1:3LECbAL3FgbyK80NP3V3Pmiuo/a3hFWg/PR6SPFhTns=
1082-
kmodules.xyz/offshoot-api v0.0.0-20211207130839-cc7187e020cf h1:cVaxFPsZyaymGsx4XoTii1MEvtwGxmLg75qwMxNHB4w=
1083-
kmodules.xyz/offshoot-api v0.0.0-20211207130839-cc7187e020cf/go.mod h1:sJYyxf84ZvbVz4SivxMgSelGRYn19wOLUtObiEncCxk=
1084-
kmodules.xyz/prober v0.0.0-20210618020259-5836fb959027 h1:rtSPsruEkoBOLcbJkxMQjnPafpv8/vJnfWlTIj9yRBg=
1085-
kmodules.xyz/prober v0.0.0-20210618020259-5836fb959027/go.mod h1:H4NcvS1RQxeXtQONALbU6r9OT3Xa7cMhfamX/k9t9wo=
1086-
kmodules.xyz/resource-metrics v0.0.6/go.mod h1:M7rWuo2qh3BpHhogiEVPnvGY9Xx4Pfygqn1Rex8YbgM=
1082+
kmodules.xyz/offshoot-api v0.0.0-20220317044008-01567138fc2f h1:AD6ptk7qRZ8N6DY4Y2lCnqvfu3X/20YdQArTxN5g3sA=
1083+
kmodules.xyz/offshoot-api v0.0.0-20220317044008-01567138fc2f/go.mod h1:MhgruKArKzAxhZsBc8ZToiD/vzdtWYrnTu5hSkkYGfk=
1084+
kmodules.xyz/prober v0.0.0-20220317043828-5ae0114adcad h1:bMfimFOEgmaKCN/gnSxLIRvQbWg4dqBgcWrMnHWQgyc=
1085+
kmodules.xyz/prober v0.0.0-20220317043828-5ae0114adcad/go.mod h1:QC3O1p+sU/8NnpLKI0VCAO3IQwPlwuimf9l6+Qp3Vt8=
1086+
kmodules.xyz/resource-metrics v0.0.8/go.mod h1:M7rWuo2qh3BpHhogiEVPnvGY9Xx4Pfygqn1Rex8YbgM=
10871087
modernc.org/cc v1.0.0/go.mod h1:1Sk4//wdnYJiUIxnW8ddKpaOJCF37yAdqYnkxUpaYxw=
10881088
modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
10891089
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
@@ -1108,5 +1108,5 @@ sigs.k8s.io/yaml v1.1.0/go.mod h1:UJmg0vDUVViEyp3mgSv9WPwZCDxu4rQW1olrI1uml+o=
11081108
sigs.k8s.io/yaml v1.2.0 h1:kr/MCeFWJWTwyaHoR9c8EjH9OumOmoF9YGiZd7lFm/Q=
11091109
sigs.k8s.io/yaml v1.2.0/go.mod h1:yfXDCHCao9+ENCvLSE62v9VSji2MKu5jeNfTrofGhJc=
11101110
sourcegraph.com/sourcegraph/appdash v0.0.0-20190731080439-ebfcffb1b5c0/go.mod h1:hI742Nqp5OhwiqlzhgfbWU4mW4yO10fP+LoT9WOswdU=
1111-
stash.appscode.dev/apimachinery v0.18.1-0.20220316053843-5bd9e9bde0fd h1:pdQh2qRksCdHQ+RsaGmwYkkbcxkltHJuNMUkrP7HHnI=
1112-
stash.appscode.dev/apimachinery v0.18.1-0.20220316053843-5bd9e9bde0fd/go.mod h1:R+AE9wyi0sPZ0t3HWwZq+FiA7ewoGbqsdW6atczGf94=
1111+
stash.appscode.dev/apimachinery v0.18.1-0.20220321055929-ee132c17707b h1:NzNCh1LNlEAK1gdCNcRUY17s6f0oMfuFJ0JNP0SbLE4=
1112+
stash.appscode.dev/apimachinery v0.18.1-0.20220321055929-ee132c17707b/go.mod h1:f2VcuZ6mMvRaSVLavWxi9HDp3LNusFKmmaAqfY7H7d8=

pkg/backup.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,15 @@ package pkg
1818

1919
import (
2020
"context"
21-
v1 "kmodules.xyz/offshoot-api/api/v1"
2221
"path/filepath"
23-
"stash.appscode.dev/apimachinery/pkg/invoker"
24-
"stash.appscode.dev/elasticsearch/pkg/manifests"
2522
"time"
2623

27-
api_v1beta1 "stash.appscode.dev/apimachinery/apis/stash/v1beta1"
24+
"stash.appscode.dev/apimachinery/apis/stash/v1beta1"
2825
stash "stash.appscode.dev/apimachinery/client/clientset/versioned"
26+
"stash.appscode.dev/apimachinery/pkg/invoker"
2927
"stash.appscode.dev/apimachinery/pkg/restic"
3028
api_util "stash.appscode.dev/apimachinery/pkg/util"
29+
"stash.appscode.dev/elasticsearch/pkg/manager"
3130

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

4342
func NewCmdBackup() *cobra.Command {
@@ -99,12 +98,12 @@ func NewCmdBackup() *cobra.Command {
9998
backupOutput, err = opt.backupManifests(ti.Target.Ref)
10099
if err != nil {
101100
backupOutput = &restic.BackupOutput{
102-
BackupTargetStatus: api_v1beta1.BackupTargetStatus{
101+
BackupTargetStatus: v1beta1.BackupTargetStatus{
103102
Ref: ti.Target.Ref,
104-
Stats: []api_v1beta1.HostBackupStats{
103+
Stats: []v1beta1.HostBackupStats{
105104
{
106105
Hostname: opt.backupOptions.Host,
107-
Phase: api_v1beta1.HostBackupFailed,
106+
Phase: v1beta1.HostBackupFailed,
108107
Error: err.Error(),
109108
},
110109
},
@@ -161,7 +160,7 @@ func NewCmdBackup() *cobra.Command {
161160
return cmd
162161
}
163162

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

203-
dumper := manifests.NewDumper()
202+
mgOpts := manager.BackupOptions{}
203+
mgr := manager.NewBackupManager(mgOpts)
204204

205-
err = dumper.Dump()
205+
err = mgr.Dump()
206206
if err != nil {
207207
return nil, err
208208
}

pkg/manager/application.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package manager
2+
3+
type applicationBackupManager struct {
4+
BackupOptions
5+
}
6+
7+
func newApplicationBackupManager(opt BackupOptions) BackupManager {
8+
return applicationBackupManager{
9+
BackupOptions: opt,
10+
}
11+
}
12+
13+
func (opt applicationBackupManager) Dump() error {
14+
return nil
15+
}

pkg/manager/cluster.go

Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
1+
package manager
2+
3+
import (
4+
"context"
5+
"path/filepath"
6+
"strings"
7+
8+
"stash.appscode.dev/elasticsearch/pkg/manager/sanitizers"
9+
10+
core "k8s.io/api/core/v1"
11+
"k8s.io/apimachinery/pkg/runtime/schema"
12+
"k8s.io/apimachinery/pkg/runtime/serializer"
13+
"k8s.io/apimachinery/pkg/util/sets"
14+
"k8s.io/client-go/discovery"
15+
"k8s.io/client-go/kubernetes/scheme"
16+
"k8s.io/client-go/rest"
17+
"k8s.io/klog/v2"
18+
"kmodules.xyz/client-go/tools/backup"
19+
"sigs.k8s.io/yaml"
20+
)
21+
22+
type clusterBackupManager struct {
23+
BackupOptions
24+
}
25+
26+
func newClusterBackupManager(opt BackupOptions) BackupManager {
27+
return clusterBackupManager{
28+
BackupOptions: opt,
29+
}
30+
}
31+
32+
func (opt clusterBackupManager) Dump() error {
33+
// backup cluster resources yaml into opt.backupDir
34+
mgr := backup.NewBackupManager(opt.Ctx, opt.Config, opt.Sanitize)
35+
36+
_, err := mgr.BackupToDir(opt.DataDir)
37+
if err != nil {
38+
return err
39+
}
40+
return nil
41+
}
42+
43+
type ItemList struct {
44+
Items []map[string]interface{} `json:"items,omitempty"`
45+
}
46+
47+
type processorFunc func(relPath string, data []byte) error
48+
49+
func (opt clusterBackupManager) Backup(process processorFunc) error {
50+
// ref: https://github.com/kubernetes/ingress-nginx/blob/0dab51d9eb1e5a9ba3661f351114825ac8bfc1af/pkg/ingress/controller/launch.go#L252
51+
opt.Config.QPS = 1e6
52+
opt.Config.Burst = 1e6
53+
if err := rest.SetKubernetesDefaults(opt.Config); err != nil {
54+
return err
55+
}
56+
opt.Config.NegotiatedSerializer = serializer.WithoutConversionCodecFactory{CodecFactory: scheme.Codecs}
57+
if opt.Config.UserAgent == "" {
58+
opt.Config.UserAgent = rest.DefaultKubernetesUserAgent()
59+
}
60+
61+
disClient, err := discovery.NewDiscoveryClientForConfig(opt.Config)
62+
if err != nil {
63+
return err
64+
}
65+
resourceLists, err := disClient.ServerPreferredResources()
66+
if err != nil {
67+
return err
68+
}
69+
resourceListBytes, err := yaml.Marshal(resourceLists)
70+
if err != nil {
71+
return err
72+
}
73+
err = process("resource_lists.yaml", resourceListBytes)
74+
if err != nil {
75+
return err
76+
}
77+
78+
for _, list := range resourceLists {
79+
gv, err := schema.ParseGroupVersion(list.GroupVersion)
80+
if err != nil {
81+
return err
82+
}
83+
for _, r := range list.APIResources {
84+
if strings.ContainsRune(r.Name, '/') {
85+
continue // skip subresource
86+
}
87+
if !sets.NewString(r.Verbs...).HasAll("list", "get") {
88+
continue
89+
}
90+
91+
klog.V(3).Infof("Taking backup of %s apiVersion:%s kind:%s", list.GroupVersion, r.Name, r.Kind)
92+
opt.Config.GroupVersion = &gv
93+
opt.Config.APIPath = "/apis"
94+
if gv.Group == core.GroupName {
95+
opt.Config.APIPath = "/api"
96+
}
97+
client, err := rest.RESTClientFor(opt.Config)
98+
if err != nil {
99+
return err
100+
}
101+
request := client.Get().Resource(r.Name).Param("pretty", "true")
102+
resp, err := request.DoRaw(context.TODO())
103+
if err != nil {
104+
return err
105+
}
106+
items := &ItemList{}
107+
err = yaml.Unmarshal(resp, &items)
108+
if err != nil {
109+
return err
110+
}
111+
for _, item := range items.Items {
112+
var path string
113+
item["apiVersion"] = list.GroupVersion
114+
item["kind"] = r.Kind
115+
116+
md, ok := item["metadata"]
117+
if ok {
118+
path = getPathFromSelfLink(md)
119+
}
120+
121+
if opt.Sanitize {
122+
s := sanitizers.NewSanitizer(r)
123+
item, err := s.Sanitize(item)
124+
if err != nil {
125+
return err
126+
}
127+
delete(item, "status")
128+
}
129+
data, err := yaml.Marshal(item)
130+
if err != nil {
131+
return err
132+
}
133+
absPath := filepath.Join(opt.DataDir, path)
134+
err = opt.Storage.Write(absPath, data)
135+
if err != nil {
136+
return err
137+
}
138+
err = process(path, data)
139+
if err != nil {
140+
return err
141+
}
142+
}
143+
}
144+
}
145+
return nil
146+
}
147+
148+
func getPathFromSelfLink(md interface{}) string {
149+
meta, ok := md.(map[string]interface{})
150+
if ok {
151+
return meta["selfLink"].(string) + ".yaml"
152+
}
153+
return ""
154+
}

pkg/manager/namespace.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package manager
2+
3+
type namespaceBackupManager struct {
4+
BackupOptions
5+
}
6+
7+
func newNamespaceBackupManager(opt BackupOptions) BackupManager {
8+
return namespaceBackupManager{
9+
BackupOptions: opt,
10+
}
11+
}
12+
13+
func (opt namespaceBackupManager) Dump() error {
14+
return nil
15+
}

0 commit comments

Comments
 (0)