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:
nightcityblade
2026-07-22 20:58:52 +02:00
committed by GitHub
co-authored by nightcityblade
parent 3a89a4d53b
commit d9219da469
2 changed files with 33 additions and 2 deletions
+11 -2
View File
@@ -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
+22
View File
@@ -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"`)
}