Skip to content

Commit

Permalink
Add backup and restore support of saved objects in spaces of kibana d…
Browse files Browse the repository at this point in the history
…ashboard

Signed-off-by: Md. Ishtiaq Islam <[email protected]>
  • Loading branch information
ishtiaqhimel committed Feb 8, 2024
1 parent c64c7f0 commit 1deeb13
Show file tree
Hide file tree
Showing 91 changed files with 13,003 additions and 485 deletions.
15 changes: 8 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module stash.appscode.dev/elasticsearch

go 1.21.5
go 1.21.6

require (
github.com/spf13/cobra v1.7.0
Expand All @@ -14,11 +14,11 @@ require (
k8s.io/client-go v0.29.0
k8s.io/klog/v2 v2.110.1
kmodules.xyz/client-go v0.29.6
kmodules.xyz/custom-resources v0.29.0
kmodules.xyz/custom-resources v0.29.1
kmodules.xyz/offshoot-api v0.29.0
kubedb.dev/apimachinery v0.41.0-beta.1.0.20240124061503-ce4799bb0e5c
kubedb.dev/db-client-go v0.0.9-0.20240126103627-22edae9f6b92
sigs.k8s.io/controller-runtime v0.16.3
kubedb.dev/apimachinery v0.41.0
kubedb.dev/db-client-go v0.0.11-0.20240208083800-50462091d436
sigs.k8s.io/controller-runtime v0.17.0
stash.appscode.dev/apimachinery v0.32.1-0.20240118085630-4c06ed8c04a7
)

Expand All @@ -33,14 +33,15 @@ require (
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/emicklei/go-restful/v3 v3.11.0 // indirect
github.com/evanphx/json-patch v5.7.0+incompatible // indirect
github.com/evanphx/json-patch/v5 v5.7.0 // indirect
github.com/evanphx/json-patch/v5 v5.8.0 // indirect
github.com/fatih/structs v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-openapi/jsonpointer v0.20.0 // indirect
github.com/go-openapi/jsonreference v0.20.2 // indirect
github.com/go-openapi/swag v0.22.4 // indirect
github.com/go-resty/resty/v2 v2.11.0 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand Down Expand Up @@ -74,7 +75,7 @@ require (
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.19.0 // indirect
golang.org/x/oauth2 v0.15.0 // indirect
golang.org/x/sys v0.15.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/term v0.15.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.5.0 // indirect
Expand Down
22 changes: 12 additions & 10 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxER
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/evanphx/json-patch v5.7.0+incompatible h1:vgGkfT/9f8zE6tvSCe74nfpAVDQ2tG6yudJd8LBksgI=
github.com/evanphx/json-patch v5.7.0+incompatible/go.mod h1:50XU6AFN0ol/bzJsmQLiYLvXMP4fmwYFNcr97nuDLSk=
github.com/evanphx/json-patch/v5 v5.7.0 h1:nJqP7uwL84RJInrohHfW0Fx3awjbm8qZeFv0nW9SYGc=
github.com/evanphx/json-patch/v5 v5.7.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/evanphx/json-patch/v5 v5.8.0 h1:lRj6N9Nci7MvzrXuX6HFzU8XjmhPiXPlsKEy1u0KQro=
github.com/evanphx/json-patch/v5 v5.8.0/go.mod h1:VNkHZ/282BpEyt/tObQO8s5CMPmYYq14uClGH4abBuQ=
github.com/fatih/color v1.7.0/go.mod h1:Zm6kSWBoL9eyXnKyktHP6abPY2pDugNf5KwzbycvMj4=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
Expand Down Expand Up @@ -87,6 +87,8 @@ github.com/go-openapi/swag v0.22.4 h1:QLMzNJnMGPRNDCbySlcj1x01tzU8/9LTTL9hZZZogB
github.com/go-openapi/swag v0.22.4/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14=
github.com/go-resty/resty/v2 v2.11.0 h1:i7jMfNOJYMp69lq7qozJP+bjgzfAzeOhuGlyDrqxT/8=
github.com/go-resty/resty/v2 v2.11.0/go.mod h1:iiP/OpA0CkcL3IGt1O0+/SIItFUbkkyw5BGXiVdTu+A=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI=
github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls=
Expand Down Expand Up @@ -417,8 +419,8 @@ golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.13.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc=
golang.org/x/sys v0.15.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/sys v0.16.0 h1:xWw16ngr6ZMtmxDyKyIgsE93KNKz5HKmMa3b8ALHidU=
golang.org/x/sys v0.16.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210927222741-03fcf44c2211/go.mod h1:jbD1KX2456YbFQfuXm/mYQcufACuNUgVhRMnK/tPxf8=
golang.org/x/term v0.5.0/go.mod h1:jMB1sMXY+tzblOD4FWmEbocvup2/aLOaQEp7JmGp78k=
Expand Down Expand Up @@ -569,8 +571,8 @@ kmodules.xyz/apiversion v0.2.0 h1:vAQYqZFm4xu4pbB1cAdHbFEPES6EQkcR4wc06xdTOWk=
kmodules.xyz/apiversion v0.2.0/go.mod h1:oPX8g8LvlPdPX3Yc5YvCzJHQnw3YF/X4/jdW0b1am80=
kmodules.xyz/client-go v0.29.6 h1:xTVq5LZvsPBUTLY7PORq7zveLOj/vpuTDvkpHWOk3RM=
kmodules.xyz/client-go v0.29.6/go.mod h1:pHuzpwzEcDUIGjVVvwz9N8lY+6A7HXwvs2d7NtK7Hho=
kmodules.xyz/custom-resources v0.29.0 h1:RaDM2+wSVXiwIvLqmkTVYpwoH83AC8wruXe2p2rOZNY=
kmodules.xyz/custom-resources v0.29.0/go.mod h1:MzZyXtxdg1PDxGk3RTTO1Xv3KiVqZnIonSwmxVbagOY=
kmodules.xyz/custom-resources v0.29.1 h1:xiNylhs3ILRbcUhxxy306AOy9GMA4Mq7xFIptZKgal4=
kmodules.xyz/custom-resources v0.29.1/go.mod h1:829zDY1EjaxPP52h1T73LZx/vgv8Pld9/uTT/ViZTc0=
kmodules.xyz/monitoring-agent-api v0.29.0 h1:gpFl6OZrlMLb/ySMHdREI9EwGtnJ91oZBn9H1UFRwB4=
kmodules.xyz/monitoring-agent-api v0.29.0/go.mod h1:iNbvaMTgVFOI5q2LJtGK91j4Dmjv4ZRiRdasGmWLKQI=
kmodules.xyz/objectstore-api v0.29.0 h1:dK53fQXdoboyW/EyBBAMjykT8u7jstKrM1DS4RJvhEU=
Expand All @@ -579,10 +581,10 @@ kmodules.xyz/offshoot-api v0.29.0 h1:GHLhxxT9jU1N8+FvOCCeJNyU5g0duYS46UGrs6AHNLY
kmodules.xyz/offshoot-api v0.29.0/go.mod h1:5NxhBblXoDHWStx9HCDJR2KFTwYjEZ7i1Id3jelIunw=
kmodules.xyz/prober v0.29.0 h1:Ex7m4F9rH7uWNNJlLgP63ROOM+nUATJkC2L5OQ7nwMg=
kmodules.xyz/prober v0.29.0/go.mod h1:UtK+HKyI1lFLEKX+HFLyOCVju6TO93zv3kwGpzqmKOo=
kubedb.dev/apimachinery v0.41.0-beta.1.0.20240124061503-ce4799bb0e5c h1:QdnEBPdmd/Z3JOFJKMQ3nCFiJYiWxsuIAqMLgXep880=
kubedb.dev/apimachinery v0.41.0-beta.1.0.20240124061503-ce4799bb0e5c/go.mod h1:IqIwU4I/UfmcMi4X7G01M7XAEFUPehtiFAKZNKwH8Ek=
kubedb.dev/db-client-go v0.0.9-0.20240126103627-22edae9f6b92 h1:EmIulT/VTbJSPfuW1TPuB/mfUac1rp93jW1vbJJCHx4=
kubedb.dev/db-client-go v0.0.9-0.20240126103627-22edae9f6b92/go.mod h1:tCtLrUfxNHHTGKN9znhJo7vVLX+SHAY0nhCTXIDvo4o=
kubedb.dev/apimachinery v0.41.0 h1:VbGQnH3YL7ICFvnCjAumlnL3HeZzg5F4F+flpjOSnG4=
kubedb.dev/apimachinery v0.41.0/go.mod h1:rNWsbBzdnZA8G2FE8igi+nsGnlWqYurC+i3RFFDwluc=
kubedb.dev/db-client-go v0.0.11-0.20240208083800-50462091d436 h1:JmSnrw+IdzbkgisjDQ5X0oo5jSts4OhBLHgEw6SL3qs=
kubedb.dev/db-client-go v0.0.11-0.20240208083800-50462091d436/go.mod h1:sadvZUXo7tWz/75gdW7wpqeR25bi9kI/h2djyYLjN0A=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
sigs.k8s.io/gateway-api v0.8.0 h1:isQQ3Jx2qFP7vaA3ls0846F0Amp9Eq14P08xbSwVbQg=
sigs.k8s.io/gateway-api v0.8.0/go.mod h1:okOnjPNBFbIS/Rw9kAhuIUaIkLhTKEu+ARIuXk2dgaM=
Expand Down
27 changes: 19 additions & 8 deletions pkg/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -307,19 +307,30 @@ func (opt *esOptions) dumpDashboardObjects(appBinding *appcatalog.AppBinding) er
return err
}

response, err := dashboardClient.ExportSavedObjects()
spaces, err := dashboardClient.ListSpaces()
if err != nil {
return err
}

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
for _, space := range spaces {
response, err := dashboardClient.ExportSavedObjects(space)
if err != nil {
return err
}

if response.Code != http.StatusOK {
return fmt.Errorf("failed to export dashboard saved objects %s", string(body))
body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

if response.Code != http.StatusOK {
return fmt.Errorf("failed to export dashboard saved objects %s", string(body))
}

if err = os.WriteFile(opt.getDashboardFilePath(space), body, os.ModePerm); err != nil {
return err
}
}

return os.WriteFile(filepath.Join(opt.interimDataDir, DashboardObjectsFile), body, os.ModePerm)
return nil
}
31 changes: 19 additions & 12 deletions pkg/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,11 +225,6 @@ func (opt *esOptions) restoreElasticsearch(targetRef api_v1beta1.TargetRef) (*re
return nil, fmt.Errorf("failed to restore dashboard objects %w", err)
}

// delete the metadata file as it is not required for restoring the dumps
if err := clearFile(filepath.Join(opt.interimDataDir, DashboardObjectsFile)); err != nil {
return nil, err
}

// run separate shell to restore indices
// klog.Infoln("Performing multielasticdump on", hostname)
session.sh.ShowCMD = false
Expand Down Expand Up @@ -261,18 +256,30 @@ func (opt *esOptions) restoreDashboardObjects(appBinding *appcatalog.AppBinding)
return err
}

response, err := dashboardClient.ImportSavedObjects(filepath.Join(opt.interimDataDir, DashboardObjectsFile))
spaces, err := dashboardClient.ListSpaces()
if err != nil {
return err
}

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}
for _, space := range spaces {
response, err := dashboardClient.ImportSavedObjects(space, opt.getDashboardFilePath(space))
if err != nil {
return err
}

body, err := io.ReadAll(response.Body)
if err != nil {
return err
}

if response.Code != http.StatusOK {
return fmt.Errorf("failed to import dashboard saved objects %s", string(body))
if response.Code != http.StatusOK {
return fmt.Errorf("failed to import dashboard saved objects %s", string(body))
}

// delete the dashboard file(s) as it is not required for restoring the dumps
if err = clearFile(opt.getDashboardFilePath(space)); err != nil {
return err
}
}

return nil
Expand Down
15 changes: 9 additions & 6 deletions pkg/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,11 @@ import (
)

const (
ESUser = "ADMIN_USERNAME"
ESPassword = "ADMIN_PASSWORD"
MultiElasticDumpCMD = "multielasticdump"
ESCACertFile = "root.pem"
ESAuthFile = "auth.txt"
DashboardObjectsFile = "dashboard.ndjson"
ESUser = "ADMIN_USERNAME"
ESPassword = "ADMIN_PASSWORD"
MultiElasticDumpCMD = "multielasticdump"
ESCACertFile = "root.pem"
ESAuthFile = "auth.txt"
)

type esOptions struct {
Expand Down Expand Up @@ -293,3 +292,7 @@ func (opt esOptions) getDashboardClient(appBinding *appcatalog.AppBinding) (*es_
WithDbVersionInfo(versionInfo).
GetElasticsearchDashboardClient()
}

func (opt esOptions) getDashboardFilePath(space string) string {
return filepath.Join(opt.interimDataDir, space+".ndjson")
}
Loading

0 comments on commit 1deeb13

Please sign in to comment.