mirror of
https://github.com/rancher/k3k.git
synced 2026-08-18 03:46:31 +00:00
Fix cluster delete for missing clusters (#1051)
* Fix cluster delete for missing clusters Signed-off-by: nightcityblade <nightcityblade@gmail.com> * Adjust missing cluster error message --------- Signed-off-by: nightcityblade <nightcityblade@gmail.com> Co-authored-by: nightcityblade <nightcityblade@gmail.com>
This commit is contained in:
co-authored by
nightcityblade
parent
3a89a4d53b
commit
d9219da469
@@ -3,6 +3,7 @@ package cmds
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
@@ -48,14 +49,22 @@ func delete(appCtx *AppContext) func(cmd *cobra.Command, args []string) error {
|
||||
|
||||
namespace := appCtx.Namespace(name)
|
||||
|
||||
logrus.Infof("Deleting '%s' cluster in namespace '%s'", name, namespace)
|
||||
|
||||
cluster := v1beta1.Cluster{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Namespace: namespace,
|
||||
},
|
||||
}
|
||||
if err := client.Get(ctx, ctrlclient.ObjectKeyFromObject(&cluster), &cluster); err != nil {
|
||||
if apierrors.IsNotFound(err) {
|
||||
return fmt.Errorf("cluster %q not found in namespace %q", name, namespace)
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
logrus.Infof("Deleting '%s' cluster in namespace '%s'", name, namespace)
|
||||
|
||||
// keep bootstrap secrets and tokens if --keep-data flag is passed
|
||||
if keepData {
|
||||
// skip removing tokenSecret
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
package cmds
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/spf13/cobra"
|
||||
"github.com/stretchr/testify/require"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client/fake"
|
||||
|
||||
"github.com/rancher/k3k/pkg/apis/k3k.io/v1beta1"
|
||||
)
|
||||
|
||||
func TestDeleteMissingCluster(t *testing.T) {
|
||||
scheme := runtime.NewScheme()
|
||||
require.NoError(t, v1beta1.AddToScheme(scheme))
|
||||
|
||||
appCtx := &AppContext{Client: fake.NewClientBuilder().WithScheme(scheme).Build()}
|
||||
err := delete(appCtx)(&cobra.Command{}, []string{"missing"})
|
||||
|
||||
require.EqualError(t, err, `cluster "missing" not found in namespace "k3k-missing"`)
|
||||
}
|
||||
Reference in New Issue
Block a user