Skip to content

Commit

Permalink
Migrate pkg/kanctl to errkit
Browse files Browse the repository at this point in the history
  • Loading branch information
e-sumin committed Oct 9, 2024
1 parent b11df8c commit 6bcb9ee
Show file tree
Hide file tree
Showing 9 changed files with 103 additions and 75 deletions.
72 changes: 36 additions & 36 deletions pkg/kanctl/actionset.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions pkg/kanctl/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
package kanctl

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

"github.com/kanisterio/kanister/pkg/blueprint"
"github.com/kanisterio/kanister/pkg/blueprint/validate"
)

func performBlueprintValidation(p *validateParams) error {
if p.filename == "" {
return errors.New("--name is not supported for blueprint resources, please specify blueprint manifest using -f.")
return errkit.New("--name is not supported for blueprint resources, please specify blueprint manifest using -f.")
}

// read blueprint from specified file
Expand Down
12 changes: 5 additions & 7 deletions pkg/kanctl/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,16 @@
package kanctl

import (
"fmt"

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

var errArgsLength = fmt.Errorf("Incorrect number of arguments")
var errArgsLength = errkit.NewSentinelErr("Incorrect number of arguments")

func newArgsLengthError(format string, args ...interface{}) error {
return errors.Wrapf(errArgsLength, format, args...)
return errkit.Wrap(errArgsLength, format, args...)
}

// IsArgsLengthError returns true iff the underlying cause was an errArgsLength.
// IsArgsLengthError returns true if the underlying cause was an errArgsLength.
func IsArgsLengthError(err error) bool {
return errors.Cause(err) == errArgsLength
return errkit.Is(err, errArgsLength)
}
28 changes: 28 additions & 0 deletions pkg/kanctl/error_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Copyright 2024 The Kanister Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package kanctl

import (
"gopkg.in/check.v1"
)

type KanctlErrTestSuite struct{}

var _ = check.Suite(&KanctlErrTestSuite{})

func (k *KanctlTestSuite) TestLengthError(c *check.C) {
err := newArgsLengthError("Some formatted error with %d arg", 10)
c.Assert(IsArgsLengthError(err), check.Equals, true)
}
8 changes: 4 additions & 4 deletions pkg/kanctl/kanctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ package kanctl
import (
"os"

"github.com/kanisterio/errkit"
osversioned "github.com/openshift/client-go/apps/clientset/versioned"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"k8s.io/client-go/kubernetes"

Expand Down Expand Up @@ -79,17 +79,17 @@ func initializeClients() (kubernetes.Interface, versioned.Interface, osversioned
}
cli, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "could not get the kubernetes client")
return nil, nil, nil, errkit.Wrap(err, "could not get the kubernetes client")
}

osCli, err := osversioned.NewForConfig(config)
if err != nil {
return nil, nil, nil, errors.Wrapf(err, "could not get openshift client")
return nil, nil, nil, errkit.Wrap(err, "could not get openshift client")
}

crCli, err := versioned.NewForConfig(config)
if err != nil {
return nil, nil, nil, errors.Wrap(err, "could not get the CRD client")
return nil, nil, nil, errkit.Wrap(err, "could not get the CRD client")
}
return cli, crCli, osCli, nil
}
16 changes: 8 additions & 8 deletions pkg/kanctl/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"os"
"reflect"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
"github.com/spf13/cobra"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
Expand Down Expand Up @@ -174,15 +174,15 @@ func createNewProfile(cmd *cobra.Command, args []string) error {
}
secret, err = createSecret(ctx, secret, cli)
if err != nil {
return errors.Wrap(err, "failed to create secret")
return errkit.Wrap(err, "failed to create secret")
}
err = validateProfile(ctx, profile, cli, skipValidation, true)
if err != nil {
fmt.Printf("validation failed, deleting secret '%s'\n", secret.GetName())
if rmErr := deleteSecret(ctx, secret, cli); rmErr != nil {
return errors.Wrap(rmErr, "failed to delete secret after validation failed")
return errkit.Wrap(rmErr, "failed to delete secret after validation failed")
}
return errors.Wrap(err, "profile validation failed")
return errkit.Wrap(err, "profile validation failed")
}
return createProfile(ctx, profile, crCli)
}
Expand Down Expand Up @@ -211,7 +211,7 @@ func getLocationParams(cmd *cobra.Command) (*locationParams, error) {
lType = crv1alpha1.LocationTypeAzure
profileName = "azure-profile-"
default:
return nil, errors.New("Profile type not supported: " + cmd.Name())
return nil, errkit.New("Profile type not supported: " + cmd.Name())
}
skipSSLVerify, _ := cmd.Flags().GetBool(skipSSLVerifyFlag)
return &locationParams{
Expand Down Expand Up @@ -368,7 +368,7 @@ func printSecret(secret *corev1.Secret) error {
}
secYAML, err := yaml.Marshal(secret)
if err != nil {
return errors.New("could not convert generated secret to YAML")
return errkit.New("could not convert generated secret to YAML")
}
fmt.Printf("%s", secYAML)
return nil
Expand All @@ -381,7 +381,7 @@ func printProfile(profile *crv1alpha1.Profile) error {
}
profYAML, err := yaml.Marshal(profile)
if err != nil {
return errors.New("could not convert generated profile to YAML")
return errkit.New("could not convert generated profile to YAML")
}
fmt.Printf("%s", profYAML)
return nil
Expand All @@ -399,7 +399,7 @@ func performProfileValidation(p *validateParams) error {
ctx := context.Background()
cli, crCli, _, err := initializeClients()
if err != nil {
return errors.Wrap(err, "could not initialize clients for validation")
return errkit.Wrap(err, "could not initialize clients for validation")
}
prof, err := getProfileFromCmd(ctx, crCli, p)
if err != nil {
Expand Down
24 changes: 12 additions & 12 deletions pkg/kanctl/repositoryserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (
"strings"
"time"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand Down Expand Up @@ -111,11 +111,11 @@ func createNewRepositoryServer(cmd *cobra.Command, args []string) error {
}
cli, err := kubernetes.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "could not get the kubernetes client")
return errkit.Wrap(err, "could not get the kubernetes client")
}
crCli, err := versioned.NewForConfig(config)
if err != nil {
return errors.Wrap(err, "could not get the CRD client")
return errkit.Wrap(err, "could not get the CRD client")
}

ctx := context.Background()
Expand All @@ -141,38 +141,38 @@ func generateRepositoryServerParams(cmd *cobra.Command) (*repositoryServerParams
// Fetch values of the flags
tlsSecret, _ := cmd.Flags().GetString(tlsSecretFlag)
if strings.Contains(tlsSecret, "/") {
return nil, errors.Errorf("Invalid secret name %s, it should not be of the form namespace/name )", tlsSecret)
return nil, errkit.New(fmt.Sprintf("Invalid secret name %s, it should not be of the form namespace/name )", tlsSecret))
}

repositoryServerUser, _ := cmd.Flags().GetString(repoServerUserFlag)

repositoryServerUserAccessSecret, _ := cmd.Flags().GetString(repoServerUserAccessSecretFlag)
if strings.Contains(repositoryServerUserAccessSecret, "/") {
return nil, errors.Errorf("Invalid secret name %s, it should not be of the form namespace/name )", repositoryServerUserAccessSecret)
return nil, errkit.New(fmt.Sprintf("Invalid secret name %s, it should not be of the form namespace/name )", repositoryServerUserAccessSecret))
}

repositoryServerAdminUserAccessSecret, _ := cmd.Flags().GetString(repoServerAdminUserAccessSecretFlag)
if strings.Contains(repositoryServerAdminUserAccessSecret, "/") {
return nil, errors.Errorf("Invalid secret name %s, it should not be of the form namespace/name )", repositoryServerAdminUserAccessSecret)
return nil, errkit.New(fmt.Sprintf("Invalid secret name %s, it should not be of the form namespace/name )", repositoryServerAdminUserAccessSecret))
}

repositoryUser, _ := cmd.Flags().GetString(kopiaRepoUserFlag)

repositoryPassword, _ := cmd.Flags().GetString(kopiaRepoPasswordSecretFlag)
if strings.Contains(repositoryPassword, "/") {
return nil, errors.Errorf("Invalid secret name %s, it should not be of the form namespace/name )", repositoryPassword)
return nil, errkit.New(fmt.Sprintf("Invalid secret name %s, it should not be of the form namespace/name )", repositoryPassword))
}

prefix, _ := cmd.Flags().GetString(prefixFlag)

location, _ := cmd.Flags().GetString(locationSecretFlag)
if strings.Contains(location, "/") {
return nil, errors.Errorf("Invalid secret name %s, it should not be of the form namespace/name )", location)
return nil, errkit.New(fmt.Sprintf("Invalid secret name %s, it should not be of the form namespace/name )", location))
}

locationCreds, _ := cmd.Flags().GetString(locationCredsSecretFlag)
if strings.Contains(locationCreds, "/") {
return nil, errors.Errorf("Invalid secret name %s, it should not be of the form namespace/name )", locationCreds)
return nil, errkit.New(fmt.Sprintf("Invalid secret name %s, it should not be of the form namespace/name )", locationCreds))
}

ns, err := resolveNamespace(cmd)
Expand Down Expand Up @@ -203,7 +203,7 @@ func validateSecretsAndConstructRepositoryServer(rsParams *repositoryServerParam
}
cli, err := kubernetes.NewForConfig(config)
if err != nil {
return nil, errors.Wrap(err, "could not get the kubernetes client")
return nil, errkit.Wrap(err, "could not get the kubernetes client")
}
tlsSecret, err := cli.CoreV1().Secrets(rsParams.namespace).Get(ctx, rsParams.tls, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -288,7 +288,7 @@ func waitForRepositoryServerReady(ctx context.Context, cli *kubernetes.Clientset
if pollErr != nil {
repositoryServer, err := crCli.CrV1alpha1().RepositoryServers(rs.GetNamespace()).Get(ctx, rs.GetName(), metav1.GetOptions{})
if err != nil {
return errors.Wrapf(err, "Error Getting repository server %s", repositoryServer.GetName())
return errkit.Wrap(err, fmt.Sprintf("Error Getting repository server %s", repositoryServer.GetName()))
}

opts := metav1.ListOptions{
Expand All @@ -299,7 +299,7 @@ func waitForRepositoryServerReady(ctx context.Context, cli *kubernetes.Clientset
return err
}

return errors.Wrapf(pollErr, "Repository Server is not ready.\nCurrent Status: %s\nReason: %s\n", repositoryServer.Status.Progress, events.Items[0].Message)
return errkit.Wrap(pollErr, fmt.Sprintf("Repository Server is not ready.\nCurrent Status: %s\nReason: %s\n", repositoryServer.Status.Progress, events.Items[0].Message))
}
return nil
}
6 changes: 3 additions & 3 deletions pkg/kanctl/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
"context"
"os"

"github.com/pkg/errors"
"github.com/kanisterio/errkit"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
k8sYAML "k8s.io/apimachinery/pkg/util/yaml"
Expand All @@ -34,7 +34,7 @@ func performRepoServerSecretsValidation(ctx context.Context, p *validateParams)

cli, err := kube.NewClient()
if err != nil {
return errors.Wrap(err, "could not get the kubernetes client")
return errkit.Wrap(err, "could not get the kubernetes client")
}

secret, err = getSecretFromCmd(ctx, cli, p)
Expand Down Expand Up @@ -68,7 +68,7 @@ func getSecretFromFile(ctx context.Context, filename string) (*corev1.Secret, er
secret := &corev1.Secret{}
err = d.Decode(secret)
if err != nil {
return nil, errors.Wrap(err, "failed to decode the secret passed")
return nil, errkit.Wrap(err, "failed to decode the secret passed")
}
return secret, nil
}
8 changes: 5 additions & 3 deletions pkg/kanctl/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
package kanctl

import (
"github.com/pkg/errors"
"fmt"

"github.com/kanisterio/errkit"
"github.com/spf13/cobra"

kanister "github.com/kanisterio/kanister/pkg"
Expand Down Expand Up @@ -67,7 +69,7 @@ func performValidation(cmd *cobra.Command, args []string) error {
case "repository-server-secrets":
return performRepoServerSecretsValidation(cmd.Context(), p)
default:
return errors.Errorf("resource %s is not supported for validate subcommand", p.resourceKind)
return errkit.New(fmt.Sprintf("resource %s is not supported for validate subcommand", p.resourceKind))
}
}

Expand All @@ -79,7 +81,7 @@ func extractValidateParams(cmd *cobra.Command, args []string) (*validateParams,
name, _ := cmd.Flags().GetString(nameFlag)
filename, _ := cmd.Flags().GetString(filenameFlag)
if name == "" && filename == "" {
return nil, errors.New("neither name nor filename specified")
return nil, errkit.New("neither name nor filename specified")
}
rns, _ := cmd.Flags().GetString(resourceNamespaceFlag)
schemaValidationOnly, _ := cmd.Flags().GetBool(schemaValidationOnlyFlag)
Expand Down

0 comments on commit 6bcb9ee

Please sign in to comment.