Skip to content

Commit

Permalink
Add PreInstall check if the target namespace is terminating and abort…
Browse files Browse the repository at this point in the history
… installation (#1649)

Signed-off-by: Andreas Neumann <aneumann@mesosphere.com>
  • Loading branch information
ANeumann82 committed Aug 21, 2020
1 parent 031e44f commit 88295ec
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
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 {
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
})
}

0 comments on commit 88295ec

Please sign in to comment.