-
Notifications
You must be signed in to change notification settings - Fork 152
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Kanister function to backup data on all pods in parallel (#6096)
* WIP: func to backup data on all pods in parallel * Refactor and fix errors * Address review suggestions * avoid errors * Update blueprint after testing with k10 * nit:remove commented code * Add unit test * Add context cancellation * Revert context cancellation & remove podChan
- Loading branch information
1 parent
81b2730
commit 23d62f9
Showing
3 changed files
with
208 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package function | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/pkg/errors" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
kanister "github.com/kanisterio/kanister/pkg" | ||
"github.com/kanisterio/kanister/pkg/kube" | ||
"github.com/kanisterio/kanister/pkg/param" | ||
"github.com/kanisterio/kanister/pkg/restic" | ||
) | ||
|
||
const ( | ||
// BackupDataAllNamespaceArg provides the namespace | ||
BackupDataAllNamespaceArg = "namespace" | ||
// BackupDataAllPodsArg provides the pods connected to the data volumes | ||
BackupDataAllPodsArg = "pods" | ||
// BackupDataAllContainerArg provides the container on which the backup is taken | ||
BackupDataAllContainerArg = "container" | ||
// BackupDataAllIncludePathArg provides the path of the volume or sub-path for required backup | ||
BackupDataAllIncludePathArg = "includePath" | ||
// BackupDataAllBackupArtifactPrefixArg provides the path to store artifacts on the object store | ||
BackupDataAllBackupArtifactPrefixArg = "backupArtifactPrefix" | ||
// BackupDataAllEncryptionKeyArg provides the encryption key to be used for backups | ||
BackupDataAllEncryptionKeyArg = "encryptionKey" | ||
// BackupDataAllOutput is the key name of the output generated by BackupDataAll func | ||
BackupDataAllOutput = "BackupAllInfo" | ||
) | ||
|
||
type BackupInfo struct { | ||
PodName string | ||
BackupID string | ||
BackupTag string | ||
} | ||
|
||
func init() { | ||
kanister.Register(&backupDataAllFunc{}) | ||
} | ||
|
||
var _ kanister.Func = (*backupDataAllFunc)(nil) | ||
|
||
type backupDataAllFunc struct{} | ||
|
||
func (*backupDataAllFunc) Name() string { | ||
return "BackupDataAll" | ||
} | ||
|
||
func (*backupDataAllFunc) Exec(ctx context.Context, tp param.TemplateParams, args map[string]interface{}) (map[string]interface{}, error) { | ||
var namespace, pods, container, includePath, backupArtifactPrefix, encryptionKey string | ||
var err error | ||
if err = Arg(args, BackupDataAllNamespaceArg, &namespace); err != nil { | ||
return nil, err | ||
} | ||
if err = Arg(args, BackupDataAllContainerArg, &container); err != nil { | ||
return nil, err | ||
} | ||
if err = Arg(args, BackupDataAllIncludePathArg, &includePath); err != nil { | ||
return nil, err | ||
} | ||
if err = Arg(args, BackupDataAllBackupArtifactPrefixArg, &backupArtifactPrefix); err != nil { | ||
return nil, err | ||
} | ||
if err = OptArg(args, BackupDataAllPodsArg, &pods, ""); err != nil { | ||
return nil, err | ||
} | ||
if err = OptArg(args, BackupDataAllEncryptionKeyArg, &encryptionKey, restic.GeneratePassword()); err != nil { | ||
return nil, err | ||
} | ||
// Validate the Profile | ||
if err = validateProfile(tp.Profile); err != nil { | ||
return nil, errors.Wrapf(err, "Failed to validate Profile") | ||
} | ||
cli, err := kube.NewClient() | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to create Kubernetes client") | ||
} | ||
var ps []string | ||
if pods == "" { | ||
switch { | ||
case tp.Deployment != nil: | ||
ps = tp.Deployment.Pods | ||
case tp.StatefulSet != nil: | ||
ps = tp.StatefulSet.Pods | ||
default: | ||
return nil, errors.New("Failed to get pods") | ||
} | ||
} else { | ||
ps = strings.Fields(pods) | ||
} | ||
return backupDataAll(ctx, cli, namespace, ps, container, backupArtifactPrefix, includePath, encryptionKey, tp) | ||
} | ||
|
||
func (*backupDataAllFunc) RequiredArgs() []string { | ||
return []string{BackupDataAllNamespaceArg, BackupDataAllContainerArg, | ||
BackupDataAllIncludePathArg, BackupDataAllBackupArtifactPrefixArg} | ||
} | ||
|
||
func backupDataAll(ctx context.Context, cli kubernetes.Interface, namespace string, ps []string, container string, backupArtifactPrefix, includePath, encryptionKey string, tp param.TemplateParams) (map[string]interface{}, error) { | ||
errChan := make(chan error, len(ps)) | ||
outChan := make(chan BackupInfo, len(ps)) | ||
Output := make(map[string]BackupInfo) | ||
// Run the command | ||
for _, pod := range ps { | ||
go func(pod string, container string) { | ||
backupID, backupTag, err := backupData(ctx, cli, namespace, pod, container, fmt.Sprintf("%s/%s", backupArtifactPrefix, pod), includePath, encryptionKey, tp) | ||
errChan <- errors.Wrapf(err, "Failed to backup data for pod %s", pod) | ||
outChan <- BackupInfo{PodName: pod, BackupID: backupID, BackupTag: backupTag} | ||
}(pod, container) | ||
} | ||
errs := make([]string, 0, len(ps)) | ||
for i := 0; i < len(ps); i++ { | ||
err := <-errChan | ||
output := <-outChan | ||
if err != nil { | ||
errs = append(errs, err.Error()) | ||
} else { | ||
Output[output.PodName] = output | ||
} | ||
} | ||
if len(errs) != 0 { | ||
return nil, errors.New(strings.Join(errs, "\n")) | ||
} | ||
manifestData, err := json.Marshal(Output) | ||
if err != nil { | ||
return nil, errors.Wrapf(err, "Failed to encode JSON data") | ||
} | ||
return map[string]interface{}{BackupDataAllOutput: string(manifestData)}, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters