Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Define a role for unregister-member command and delete the referenced secret when unregistering the member #98

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pkg/cmd/adm/unregister_member.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
"github.com/kubesaw/ksctl/pkg/configuration"
clicontext "github.com/kubesaw/ksctl/pkg/context"
"github.com/kubesaw/ksctl/pkg/ioutils"
corev1 "k8s.io/api/core/v1"

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
Expand Down Expand Up @@ -53,6 +54,10 @@
if err := ctx.PrintObject(toolchainCluster, "Toolchain Member cluster"); err != nil {
return err
}
secret := &corev1.Secret{}
if err := hostClusterClient.Get(context.TODO(), types.NamespacedName{Namespace: toolchainCluster.Namespace, Name: toolchainCluster.Spec.SecretRef.Name}, secret); err != nil {
return err
}

Check warning on line 60 in pkg/cmd/adm/unregister_member.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/unregister_member.go#L59-L60

Added lines #L59 - L60 were not covered by tests
confirmation := ctx.AskForConfirmation(ioutils.WithDangerZoneMessagef("unregistering member cluster form host cluster. Make sure there is no users left in the member cluster before unregistering it.",
"Delete Member cluster stated above from the Host cluster?"))
if !confirmation {
Expand All @@ -62,6 +67,9 @@
if err := hostClusterClient.Delete(context.TODO(), toolchainCluster); err != nil {
return err
}
if err := hostClusterClient.Delete(context.TODO(), secret); err != nil {
return err
}

Check warning on line 72 in pkg/cmd/adm/unregister_member.go

View check run for this annotation

Codecov / codecov/patch

pkg/cmd/adm/unregister_member.go#L71-L72

Added lines #L71 - L72 were not covered by tests
ctx.Printlnf("\nThe deletion of the Toolchain member cluster from the Host cluster has been triggered")

return restart(ctx, "host", getConfigFlagsAndClient)
Expand Down
44 changes: 36 additions & 8 deletions pkg/cmd/adm/unregister_member_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,26 @@ import (
"fmt"
"testing"

"github.com/codeready-toolchain/toolchain-common/pkg/test"
clicontext "github.com/kubesaw/ksctl/pkg/context"
. "github.com/kubesaw/ksctl/pkg/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func TestUnregisterMemberWhenAnswerIsY(t *testing.T) {
// given
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"))
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"), ReferencedSecret("member-cool-server-creds"))
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "member-cool-server-creds",
Namespace: test.HostOperatorNs,
},
}

newClient, fakeClient := NewFakeClients(t, toolchainCluster)
newClient, fakeClient := NewFakeClients(t, toolchainCluster, secret)

SetFileConfig(t, Host(), Member())
term := NewFakeTerminalWithResponse("y")
Expand All @@ -37,9 +46,15 @@ func TestUnregisterMemberWhenAnswerIsY(t *testing.T) {

func TestUnregisterMemberWhenRestartError(t *testing.T) {
// given
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"))
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"), ReferencedSecret("member-cool-server-creds"))
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "member-cool-server-creds",
Namespace: test.HostOperatorNs,
},
}

newClient, _ := NewFakeClients(t, toolchainCluster)
newClient, _ := NewFakeClients(t, toolchainCluster, secret)

SetFileConfig(t, Host(), Member())
term := NewFakeTerminalWithResponse("y")
Expand All @@ -56,9 +71,15 @@ func TestUnregisterMemberWhenRestartError(t *testing.T) {

func TestUnregisterMemberCallsRestart(t *testing.T) {
// given
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"))
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"), ReferencedSecret("member-cool-server-creds"))
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "member-cool-server-creds",
Namespace: test.HostOperatorNs,
},
}

newClient, _ := NewFakeClients(t, toolchainCluster)
newClient, _ := NewFakeClients(t, toolchainCluster, secret)

SetFileConfig(t, Host(), Member())
term := NewFakeTerminalWithResponse("y")
Expand All @@ -77,8 +98,15 @@ func TestUnregisterMemberCallsRestart(t *testing.T) {

func TestUnregisterMemberWhenAnswerIsN(t *testing.T) {
// given
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"))
newClient, fakeClient := NewFakeClients(t, toolchainCluster)
toolchainCluster := NewToolchainCluster(ToolchainClusterName("member-cool-server.com"), ReferencedSecret("member-cool-server-creds"))
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "member-cool-server-creds",
Namespace: test.HostOperatorNs,
},
}

newClient, fakeClient := NewFakeClients(t, toolchainCluster, secret)
SetFileConfig(t, Host(), Member())
term := NewFakeTerminalWithResponse("n")
ctx := clicontext.NewCommandContext(term, newClient)
Expand Down
6 changes: 6 additions & 0 deletions pkg/test/toolchaincluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,9 @@ func ToolchainClusterName(name string) ToolchainClusterModifier {
toolchainCluster.Name = name
}
}

func ReferencedSecret(name string) ToolchainClusterModifier {
return func(toolchainCluster *toolchainv1alpha1.ToolchainCluster) {
toolchainCluster.Spec.SecretRef.Name = name
}
}
22 changes: 21 additions & 1 deletion resources/roles/host.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -262,4 +262,24 @@ objects:
- "get"
- "list"
- "patch"
- "update"
- kind: Role
apiVersion: rbac.authorization.k8s.io/v1
metadata:
name: unregister-member
labels:
provider: ksctl
rules:
- apiGroups:
- ""
resources:
- "secrets"
verbs:
- "get"
- "delete"
- apiGroups:
- toolchain.dev.openshift.com
resources:
- "toolchainclusters"
verbs:
- "get"
- "delete"
Loading