Skip to content

Commit

Permalink
Errkit migration 2 (pkg/kopia) (#2537)
Browse files Browse the repository at this point in the history
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
e-sumin and mergify[bot] committed Jun 25, 2024
1 parent 669f0b1 commit 766794e
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 154 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ func (s *RepoServerControllerSuite) waitForRepoServerInfoUpdateInCR(repoServerNa
}

func (s *RepoServerControllerSuite) waitOnRepositoryServerState(c *C, reposerverName string) (crv1alpha1.RepositoryServerProgress, error) {
ctxTimeout := 5 * time.Minute
ctxTimeout := 10 * time.Minute
ctx, cancel := context.WithTimeout(context.Background(), ctxTimeout)
defer cancel()
var repoServerState crv1alpha1.RepositoryServerProgress
Expand Down
1 change: 1 addition & 0 deletions pkg/kopia/cli/args/ephemeral_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"fmt"

"github.com/kanisterio/kanister/pkg/logsafe"

"github.com/kanisterio/safecli/command"
)

Expand Down
4 changes: 2 additions & 2 deletions pkg/kopia/cli/internal/args/args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
package args

import (
"github.com/kanisterio/safecli/command"

"github.com/kanisterio/kanister/pkg/kopia/cli"

"github.com/kanisterio/safecli/command"
)

// ID creates a new ID argument.
Expand Down
4 changes: 2 additions & 2 deletions pkg/kopia/cli/internal/opts/cache_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ package opts
import (
"strconv"

"github.com/kanisterio/safecli/command"

"github.com/kanisterio/kanister/pkg/kopia/cli/args"

"github.com/kanisterio/safecli/command"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion pkg/kopia/cli/repository/storage/gcs/gcs_opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@
package gcs

import (
"github.com/kanisterio/kanister/pkg/kopia/cli"
"github.com/kanisterio/safecli/command"

"github.com/kanisterio/kanister/pkg/kopia/cli"
)

var (
Expand Down
3 changes: 2 additions & 1 deletion pkg/kopia/cli/repository/storage/gcs/gcs_opts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ import (

"gopkg.in/check.v1"

"github.com/kanisterio/kanister/pkg/kopia/cli"
"github.com/kanisterio/safecli/command"
"github.com/kanisterio/safecli/test"

"github.com/kanisterio/kanister/pkg/kopia/cli"
)

func TestGCSOptions(t *testing.T) { check.TestingT(t) }
Expand Down
39 changes: 20 additions & 19 deletions pkg/kopia/command/parse_command_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ import (
"strings"

"github.com/dustin/go-humanize"

"github.com/kanisterio/errkit"
"github.com/kopia/kopia/repo/manifest"
"github.com/kopia/kopia/snapshot"
"github.com/kopia/kopia/snapshot/policy"
"github.com/pkg/errors"

"github.com/kanisterio/kanister/pkg/field"
"github.com/kanisterio/kanister/pkg/log"
Expand All @@ -48,7 +49,7 @@ const (
// SnapshotIDsFromSnapshot extracts root ID of a snapshot from the logs
func SnapshotIDsFromSnapshot(output string) (snapID, rootID string, err error) {
if output == "" {
return snapID, rootID, errors.New("Received empty output")
return snapID, rootID, errkit.New("Received empty output")
}

logs := regexp.MustCompile("[\r\n]").Split(output, -1)
Expand All @@ -62,7 +63,7 @@ func SnapshotIDsFromSnapshot(output string) (snapID, rootID string, err error) {
return snapID, rootID, nil
}
}
return snapID, rootID, errors.New("Failed to find Root ID from output")
return snapID, rootID, errkit.New("Failed to find Root ID from output")
}

// LatestSnapshotInfoFromManifestList returns snapshot ID and backup path of the latest snapshot from `manifests list` output
Expand All @@ -73,7 +74,7 @@ func LatestSnapshotInfoFromManifestList(output string) (string, string, error) {

err := json.Unmarshal([]byte(output), &manifestList)
if err != nil {
return snapID, backupPath, errors.Wrap(err, "Failed to unmarshal manifest list")
return snapID, backupPath, errkit.Wrap(err, "Failed to unmarshal manifest list")
}
for _, manifest := range manifestList {
for key, value := range manifest.Labels {
Expand All @@ -86,10 +87,10 @@ func LatestSnapshotInfoFromManifestList(output string) (string, string, error) {
}
}
if snapID == "" {
return "", "", errors.New("Failed to get latest snapshot ID from manifest list")
return "", "", errkit.New("Failed to get latest snapshot ID from manifest list")
}
if backupPath == "" {
return "", "", errors.New("Failed to get latest snapshot backup path from manifest list")
return "", "", errkit.New("Failed to get latest snapshot backup path from manifest list")
}
return snapID, backupPath, nil
}
Expand All @@ -109,15 +110,15 @@ func SnapshotInfoFromSnapshotCreateOutput(output string) (string, string, error)
if snapManifest.RootEntry != nil {
rootID = snapManifest.RootEntry.ObjectID.String()
if snapManifest.RootEntry.DirSummary != nil && snapManifest.RootEntry.DirSummary.FatalErrorCount > 0 {
return "", "", errors.New(fmt.Sprintf("Error occurred during snapshot creation. Output %s", output))
return "", "", errkit.New(fmt.Sprintf("Error occurred during snapshot creation. Output %s", output))
}
}
}
if snapID == "" {
return "", "", errors.New(fmt.Sprintf("Failed to get snapshot ID from create snapshot output %s", output))
return "", "", errkit.New(fmt.Sprintf("Failed to get snapshot ID from create snapshot output %s", output))
}
if rootID == "" {
return "", "", errors.New(fmt.Sprintf("Failed to get root ID from create snapshot output %s", output))
return "", "", errkit.New(fmt.Sprintf("Failed to get root ID from create snapshot output %s", output))
}
return snapID, rootID, nil
}
Expand All @@ -126,12 +127,12 @@ func SnapshotInfoFromSnapshotCreateOutput(output string) (string, string, error)
// is formatted as the output of a kopia snapshot list --all command.
func SnapSizeStatsFromSnapListAll(output string) (totalSizeB int64, numSnapshots int, err error) {
if output == "" {
return 0, 0, errors.New("Received empty output")
return 0, 0, errkit.New("Received empty output")
}

snapList, err := ParseSnapshotManifestList(output)
if err != nil {
return 0, 0, errors.Wrap(err, "Parsing snapshot list output as snapshot manifest list")
return 0, 0, errkit.Wrap(err, "Parsing snapshot list output as snapshot manifest list")
}

totalSizeB = sumSnapshotSizes(snapList)
Expand Down Expand Up @@ -163,7 +164,7 @@ func ParseSnapshotManifestList(output string) ([]*snapshot.Manifest, error) {
snapInfoList := []*snapshot.Manifest{}

if err := json.Unmarshal([]byte(output), &snapInfoList); err != nil {
return nil, errors.Wrap(err, "Failed to unmarshal snapshot manifest list")
return nil, errkit.Wrap(err, "Failed to unmarshal snapshot manifest list")
}

return snapInfoList, nil
Expand Down Expand Up @@ -435,7 +436,7 @@ func parseKopiaRestoreProgressLine(line string) (stats *RestoreStats) {
// size in bytes or an error if parsing is unsuccessful.
func RepoSizeStatsFromBlobStatsRaw(blobStats string) (phySizeTotal int64, blobCount int, err error) {
if blobStats == "" {
return phySizeTotal, blobCount, errors.New("received empty blob stats string")
return phySizeTotal, blobCount, errkit.New("received empty blob stats string")
}

sizePattern := regexp.MustCompile(repoTotalSizeFromBlobStatsRegEx)
Expand Down Expand Up @@ -465,21 +466,21 @@ func RepoSizeStatsFromBlobStatsRaw(blobStats string) (phySizeTotal int64, blobCo
}

if countStr == "" {
return phySizeTotal, blobCount, errors.New("could not find count field in the blob stats")
return phySizeTotal, blobCount, errkit.New("could not find count field in the blob stats")
}

if sizeStr == "" {
return phySizeTotal, blobCount, errors.New("could not find size field in the blob stats")
return phySizeTotal, blobCount, errkit.New("could not find size field in the blob stats")
}

countVal, err := strconv.Atoi(countStr)
if err != nil {
return phySizeTotal, blobCount, errors.Wrap(err, fmt.Sprintf("unable to convert parsed count value %s", countStr))
return phySizeTotal, blobCount, errkit.Wrap(err, fmt.Sprintf("unable to convert parsed count value %s", countStr))
}

sizeValBytes, err := strconv.Atoi(sizeStr)
if err != nil {
return phySizeTotal, blobCount, errors.Wrap(err, fmt.Sprintf("unable to convert parsed size value %s", countStr))
return phySizeTotal, blobCount, errkit.Wrap(err, fmt.Sprintf("unable to convert parsed size value %s", countStr))
}

return int64(sizeValBytes), countVal, nil
Expand Down Expand Up @@ -514,7 +515,7 @@ func ErrorsFromOutput(output string) []error {
clean := ANSIEscapeCode.ReplaceAllString(l, "") // Strip all ANSI escape codes from line
match := kopiaErrorPattern.FindAllStringSubmatch(clean, 1)
if len(match) > 0 {
err = append(err, errors.New(match[0][1]))
err = append(err, errkit.New(match[0][1]))
}
}

Expand All @@ -526,7 +527,7 @@ func ParsePolicyShow(output string) (policy.Policy, error) {
policy := policy.Policy{}

if err := json.Unmarshal([]byte(output), &policy); err != nil {
return policy, errors.Wrap(err, "Failed to unmarshal snapshot manifest list")
return policy, errkit.Wrap(err, "Failed to unmarshal snapshot manifest list")
}

return policy, nil
Expand Down
6 changes: 3 additions & 3 deletions pkg/kopia/command/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"time"

"github.com/go-openapi/strfmt"
"github.com/pkg/errors"
"github.com/kanisterio/errkit"

"github.com/kanisterio/kanister/pkg/kopia/cli/args"
"github.com/kanisterio/kanister/pkg/kopia/command/storage"
Expand Down Expand Up @@ -68,7 +68,7 @@ func RepositoryConnectCommand(cmdArgs RepositoryCommandArgs) ([]string, error) {
RepoPathPrefix: cmdArgs.RepoPathPrefix,
})
if err != nil {
return nil, errors.Wrap(err, "Failed to generate storage args")
return nil, errkit.Wrap(err, "Failed to generate storage args")
}

if !time.Time(cmdArgs.PITFlag).IsZero() {
Expand Down Expand Up @@ -106,7 +106,7 @@ func RepositoryCreateCommand(cmdArgs RepositoryCommandArgs) ([]string, error) {
RepoPathPrefix: cmdArgs.RepoPathPrefix,
})
if err != nil {
return nil, errors.Wrap(err, "Failed to generate storage args")
return nil, errkit.Wrap(err, "Failed to generate storage args")
}

return stringSliceCommand(command.Combine(bsArgs)), nil
Expand Down
7 changes: 4 additions & 3 deletions pkg/kopia/command/storage/secret_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ import (
"context"
"time"

"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"

"github.com/kanisterio/errkit"

crv1alpha1 "github.com/kanisterio/kanister/pkg/apis/cr/v1alpha1"
"github.com/kanisterio/kanister/pkg/aws"
"github.com/kanisterio/kanister/pkg/blockstorage/azure"
Expand Down Expand Up @@ -64,7 +65,7 @@ func locationType(m map[string][]byte) repositoryserver.LocType {
// list of EnvVar based on secret type
func GenerateEnvSpecFromCredentialSecret(s *corev1.Secret, assumeRoleDurationS3 time.Duration) ([]corev1.EnvVar, error) {
if s == nil {
return nil, errors.New("Secret cannot be nil")
return nil, errkit.New("Secret cannot be nil")
}
secType := string(s.Type)
switch secType {
Expand Down Expand Up @@ -111,7 +112,7 @@ func getEnvSpecForAzureCredentialSecret(s *corev1.Secret) ([]corev1.EnvVar, erro
if storageEnv != "" {
env, err := azure.EnvironmentFromName(storageEnv)
if err != nil {
return nil, errors.Wrapf(err, "Failed to get azure environment from name: %s", storageEnv)
return nil, errkit.Wrap(err, "Failed to get azure environment from name", "storageEnv", storageEnv)
}
blobDomain := "blob." + env.StorageEndpointSuffix
// TODO : Check how we can set this env to use value from secret
Expand Down
4 changes: 2 additions & 2 deletions pkg/kopia/command/storage/storage_args.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
package storage

import (
"fmt"
"github.com/kanisterio/errkit"

"github.com/kanisterio/kanister/pkg/logsafe"
"github.com/kanisterio/kanister/pkg/secrets/repositoryserver"
Expand Down Expand Up @@ -47,6 +47,6 @@ func KopiaStorageArgs(params *StorageCommandParams) (logsafe.Cmd, error) {
case repositoryserver.LocTypeAzure:
return azureArgs(params.Location, params.RepoPathPrefix), nil
default:
return nil, fmt.Errorf("unsupported type for the location: %s", LocType)
return nil, errkit.New("unsupported type for the location", "locationType", LocType)
}
}
50 changes: 2 additions & 48 deletions pkg/kopia/errors/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,62 +15,16 @@
package errors

import (
"bytes"
"regexp"
"strings"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
)

type errorList []error

var _ error = errorList{}

func (e errorList) String() string {
sep := ""
var buf bytes.Buffer
buf.WriteRune('[')
for _, err := range e {
buf.WriteString(sep)
sep = ","
buf.WriteRune('"')
buf.WriteString(err.Error())
buf.WriteRune('"')
}
buf.WriteRune(']')
return buf.String()
}

func (e errorList) Error() string {
return e.String()
}

// Append creates a new combined error from err1, err2. If either error is nil,
// then the other error is returned.
func Append(err1, err2 error) error {
if err1 == nil {
return err2
}
if err2 == nil {
return err1
}
el1, ok1 := err1.(errorList)
el2, ok2 := err2.(errorList)
switch {
case ok1 && ok2:
return append(el1, el2...)
case ok1:
return append(el1, err2)
case ok2:
return append(el2, err1)
}
return errorList{err1, err2}
}

// FirstMatching returns the first error that matches the predicate in a
// causal dependency err->Cause()->Cause() ....
func FirstMatching(err error, predicate func(error) bool) error {
for ; err != nil; err = errors.Unwrap(err) {
for ; err != nil; err = errkit.Unwrap(err) {
if predicate(err) {
return err
}
Expand Down
Loading

0 comments on commit 766794e

Please sign in to comment.