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

Check if the target namespace is terminating and abort installation #1649

Merged
merged 1 commit into from
Aug 21, 2020
Merged
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
11 changes: 10 additions & 1 deletion pkg/kudoctl/kudoinit/prereq/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,18 @@ func (o KudoNamespace) String() string {
}

func (o KudoNamespace) PreInstallVerify(client *kube.Client, result *verifier.Result) error {
ns, err := client.KubeClient.CoreV1().Namespaces().Get(context.TODO(), o.opts.Namespace, metav1.GetOptions{})

// If either custom or default ns is terminating, we can't install
if err == nil {
if ns.Status.Phase == v1.NamespaceTerminating {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure but wdyt about putting this check in the health.go? E.g. a healthy namespace has the NamespaceActive phase?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Phase can currently only be Active or Terminating, so we don't need a health check for NS (at the moment)

result.AddErrors(fmt.Sprintf("Namespace %s is being terminated - Wait until it is fully gone and retry", o.opts.Namespace))
return nil
}
}

// We only manage kudo-system namespace. For others we expect they exist.
if !o.opts.IsDefaultNamespace() {
_, err := client.KubeClient.CoreV1().Namespaces().Get(context.TODO(), o.opts.Namespace, metav1.GetOptions{})
if err != nil {
if kerrors.IsNotFound(err) {
result.AddErrors(fmt.Sprintf("Namespace %s does not exist - KUDO expects that any namespace except the default %s is created beforehand", o.opts.Namespace, kudoinit.DefaultNamespace))
Expand Down
25 changes: 23 additions & 2 deletions pkg/kudoctl/kudoinit/prereq/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestPrereq_Fail_PreValidate_CustomNamespace(t *testing.T) {
func TestPrereq_Ok_PreValidate_CustomNamespace(t *testing.T) {
client := getFakeClient()

mockGetNamespace(client, "customNS")
mockGetNamespace(client, "customNS", false)

init := NewNamespaceInitializer(kudoinit.NewOptions("", "customNS", "", true, true))

Expand All @@ -38,13 +38,34 @@ func TestPrereq_Ok_PreValidate_CustomNamespace(t *testing.T) {
assert.EqualValues(t, verifier.NewResult(), result)
}

func mockGetNamespace(client *kube.Client, nsName string) {
func TestPrereq_Fail_DefaultNamespaceTerminating(t *testing.T) {
client := getFakeClient()

mockGetNamespace(client, "kudo-system", true)

init := NewNamespaceInitializer(kudoinit.NewOptions("", "", "", true, true))

result := verifier.NewResult()
_ = init.PreInstallVerify(client, &result)

expectedResult := verifier.NewResult()
expectedResult.AddErrors("Namespace kudo-system is being terminated - Wait until it is fully gone and retry")

assert.EqualValues(t, expectedResult, result)
}

func mockGetNamespace(client *kube.Client, nsName string, terminating bool) {
client.KubeClient.(*fake.Clientset).Fake.PrependReactor("get", "namespaces", func(action testing2.Action) (handled bool, ret runtime.Object, err error) {
ns := &core.Namespace{
ObjectMeta: metav1.ObjectMeta{
Name: nsName,
},
}
if terminating {
ns.Status = core.NamespaceStatus{
Phase: core.NamespaceTerminating,
}
}
return true, ns, nil
})
}