Skip to content

Commit

Permalink
chore: add more accurate error log when the resource cannot be found
Browse files Browse the repository at this point in the history
  • Loading branch information
lizzy-0323 committed Dec 17, 2024
1 parent fa09536 commit 0a298c6
Show file tree
Hide file tree
Showing 17 changed files with 95 additions and 19 deletions.
4 changes: 2 additions & 2 deletions internal/cli/backup/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ package backup

import (
"context"
"errors"
"fmt"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
Expand All @@ -37,7 +37,7 @@ func DeleteTenantBackupPolicy(ctx context.Context, o *DeleteOptions) error {
nn := types.NamespacedName{Name: o.Name, Namespace: o.Namespace}
policy, err := clients.GetTenantBackupPolicy(ctx, nn)
if policy == nil {
return errors.New("Backup policy not found for the tenant")
return fmt.Errorf("Backup policy for %s not found", o.Name)
}
if err != nil {
return err
Expand Down
4 changes: 4 additions & 0 deletions internal/cli/backup/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"github.com/spf13/cobra"
"github.com/spf13/pflag"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/generic"
Expand Down Expand Up @@ -48,6 +49,9 @@ func UpdateTenantBackupPolicy(ctx context.Context, o *UpdateOptions) error {
}
obtenant, err := clients.GetOBTenant(ctx, nn)
if err != nil {
if kubeerrors.IsNotFound(err) {
return errors.New("tenant not found")
}
return err
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/cluster/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cluster

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"

cluster "github.com/oceanbase/ob-operator/internal/cli/cluster"
"github.com/oceanbase/ob-operator/internal/cli/utils"
Expand All @@ -33,7 +34,11 @@ func NewDeleteCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
err := clients.DeleteOBCluster(cmd.Context(), o.Namespace, o.Name)
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBCluster %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
logger.Printf("Delete OBCluster %s successfully", o.Name)
},
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/cluster/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cluster

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"

cluster "github.com/oceanbase/ob-operator/internal/cli/cluster"
"github.com/oceanbase/ob-operator/internal/cli/utils"
Expand All @@ -34,7 +35,11 @@ func NewScaleCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
obcluster, err := clients.GetOBCluster(cmd.Context(), o.Namespace, o.Name)
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBCluster %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckClusterStatus(obcluster); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/cluster/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sort"

"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"

"github.com/oceanbase/ob-operator/internal/cli/cluster"
"github.com/oceanbase/ob-operator/internal/cli/utils"
Expand All @@ -36,7 +37,11 @@ func NewShowCmd() *cobra.Command {
Run: func(cmd *cobra.Command, args []string) {
obcluster, err := clients.GetOBCluster(cmd.Context(), o.Namespace, o.Name)
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBCluster %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
obclusterOperationList, err := clients.GetOBClusterOperations(cmd.Context(), obcluster)
if err != nil {
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/cluster/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cluster

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"

cluster "github.com/oceanbase/ob-operator/internal/cli/cluster"
"github.com/oceanbase/ob-operator/internal/cli/utils"
Expand All @@ -40,7 +41,11 @@ func NewUpdateCmd() *cobra.Command {
}
obcluster, err := clients.GetOBCluster(cmd.Context(), o.Namespace, o.Name)
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBCluster %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckClusterStatus(obcluster); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/cluster/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package cluster

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"

cluster "github.com/oceanbase/ob-operator/internal/cli/cluster"
"github.com/oceanbase/ob-operator/internal/cli/utils"
Expand All @@ -37,7 +38,11 @@ func NewUpgradeCmd() *cobra.Command {
}
obcluster, err := clients.GetOBCluster(cmd.Context(), o.Namespace, o.Name)
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBCluster %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckClusterStatus(obcluster); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/activate.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

apiconst "github.com/oceanbase/ob-operator/api/constants"
Expand All @@ -41,7 +42,11 @@ func NewActivateCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/changepwd.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/tenant"
Expand All @@ -40,7 +41,11 @@ func NewChangePwdCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/delete.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/tenant"
Expand All @@ -37,7 +38,11 @@ func NewDeleteCmd() *cobra.Command {
Name: o.Name,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
logger.Printf("Delete OBTenant %s successfully", o.Name)
},
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/replaylog.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

apiconst "github.com/oceanbase/ob-operator/api/constants"
Expand All @@ -38,7 +39,11 @@ func NewReplayLogCmd() *cobra.Command {
Name: o.Name,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/scale.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/tenant"
Expand All @@ -38,7 +39,11 @@ func NewScaleCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"sort"

"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/tenant"
Expand All @@ -40,7 +41,11 @@ func NewShowCmd() *cobra.Command {
Name: o.Name,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
obtenantOperationList, err := clients.GetOBTenantOperations(cmd.Context(), obtenant)
if err != nil {
Expand Down
13 changes: 11 additions & 2 deletions internal/cli/cmd/tenant/switchover.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

apiconst "github.com/oceanbase/ob-operator/api/constants"
Expand All @@ -41,7 +42,11 @@ func NewSwitchOverCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.StandbyTenant)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(standbyTenant); err != nil {
logger.Fatalln(err)
Expand All @@ -54,7 +59,11 @@ func NewSwitchOverCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.PrimaryTenant)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(primaryTenant); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/tenant"
Expand All @@ -38,7 +39,11 @@ func NewUpdateCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
logger.Fatalln(err)
Expand Down
7 changes: 6 additions & 1 deletion internal/cli/cmd/tenant/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package tenant

import (
"github.com/spf13/cobra"
kubeerrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

"github.com/oceanbase/ob-operator/internal/cli/tenant"
Expand All @@ -41,7 +42,11 @@ func NewUpgradeCmd() *cobra.Command {
Namespace: o.Namespace,
})
if err != nil {
logger.Fatalln(err)
if kubeerrors.IsNotFound(err) {
logger.Fatalf("OBTenant %s not found", o.Name)
} else {
logger.Fatalln(err)
}
}
if err := utils.CheckTenantStatus(obtenant); err != nil {
logger.Fatalln(err)
Expand Down
2 changes: 0 additions & 2 deletions internal/cli/update/update.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,6 @@ func (o *UpdateOptions) Parse(_ *cobra.Command, args []string) error {
components := config.GetAllComponents()
// check if the component is supported
defaultVersion, exist := components[name]

// check if the component is supported
if !exist {
return fmt.Errorf("component %s is not supported", name)
}
Expand Down

0 comments on commit 0a298c6

Please sign in to comment.