Skip to content

Commit

Permalink
fix: upgrade kb failed because no KB deployment was found. (#498)
Browse files Browse the repository at this point in the history
(cherry picked from commit 8413ce3)
  • Loading branch information
wangyelei committed Nov 25, 2024
1 parent b0f3f46 commit 8f1efde
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 37 deletions.
6 changes: 3 additions & 3 deletions pkg/cmd/kubeblocks/install_1.0.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func (o *InstallOptions) preInstallWhenUpgradeFrom09() error {
kbVersion := deploy.Labels[constant.AppVersionLabelKey]
o.kb09Namespace = deploy.Namespace
s := spinner.New(o.Out, spinnerMsg(fmt.Sprintf("Stop %s %s", msgKey, kbVersion)))
if err = o.stopDeployment(s, deploy); err != nil {
if err = o.stopDeploymentObject(s, deploy); err != nil {
return err
}
}
Expand Down Expand Up @@ -128,10 +128,10 @@ func (o *InstallOptions) configKB09() error {
return configKBRelease(configOpt)
}

func (o *InstallOptions) stopDeployment(s spinner.Interface, deploy *appsv1.Deployment) error {
func (o *InstallOptions) stopDeploymentObject(s spinner.Interface, deploy *appsv1.Deployment) error {
defer s.Fail()
ctx := context.TODO()
if *deploy.Spec.Replicas != 0 {
if deploy.Spec.Replicas != nil && *deploy.Spec.Replicas != 0 {
patch := []byte(fmt.Sprintf(`{"spec":{"replicas":%d}}`, 0))
_, err := o.Client.AppsV1().Deployments(deploy.Namespace).Patch(
ctx,
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/kubeblocks/install_1.0_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func TestStopDeployment(t *testing.T) {
}

s := spinner.New(o.Out, spinnerMsg("Stop KubeBlocks Deployment"))
err := o.stopDeployment(s, deploy)
err := o.stopDeploymentObject(s, deploy)
assert.NoError(t, err)

updatedDeploy, err := client.AppsV1().Deployments(namespace).Get(context.TODO(), "test-deploy", metav1.GetOptions{})
Expand Down
37 changes: 4 additions & 33 deletions pkg/cmd/kubeblocks/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
apitypes "k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"k8s.io/cli-runtime/pkg/genericiooptions"
"k8s.io/client-go/kubernetes"
"k8s.io/klog/v2"
Expand Down Expand Up @@ -202,19 +201,15 @@ func (o *InstallOptions) Upgrade() error {
// new version resources, which may be not compatible. helm will start the new version
// KubeBlocks after upgrade.
s = spinner.New(o.Out, spinnerMsg("Stop KubeBlocks "+kbVersion))
defer s.Fail()
if err = o.deleteDeployment(util.GetKubeBlocksDeploy); err != nil {
if err = o.stopDeployment(s, util.GetKubeBlocksDeploy); err != nil {
return err
}
s.Success()

// stop the data protection deployment
s = spinner.New(o.Out, spinnerMsg("Stop DataProtection"))
defer s.Fail()
if err = o.deleteDeployment(util.GetDataProtectionDeploy); err != nil {
if err = o.stopDeployment(s, util.GetDataProtectionDeploy); err != nil {
return err
}
s.Success()

msg = "to " + o.Version
}
Expand Down Expand Up @@ -296,7 +291,7 @@ func (o *InstallOptions) upgradeChart() error {
}

// deleteDeployment deletes deployment.
func (o *InstallOptions) deleteDeployment(getter deploymentGetter) error {
func (o *InstallOptions) stopDeployment(s spinner.Interface, getter deploymentGetter) error {
deploy, err := getter(o.Client)
if err != nil {
if apierrors.IsNotFound(err) {
Expand All @@ -323,31 +318,7 @@ func (o *InstallOptions) deleteDeployment(getter deploymentGetter) error {
------------------ Deployment %s end ----------------`,
deploy.Name, string(bytes), deploy.Name)

if err = o.Client.AppsV1().Deployments(deploy.Namespace).Delete(context.TODO(),
deploy.Name,
metav1.DeleteOptions{
GracePeriodSeconds: func() *int64 {
seconds := int64(0)
return &seconds
}(),
}); err != nil {
return err
}

// wait for deployment to be deleted
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, 1*time.Minute, true,
func(_ context.Context) (bool, error) {
deploy, err = getter(o.Client)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
} else if deploy == nil {
return true, nil
}
return false, err
})
return o.stopDeploymentObject(s, deploy)
}

// keepAddons set the addons to keep when upgrade KubeBlocks avoid the addons been deleted
Expand Down

0 comments on commit 8f1efde

Please sign in to comment.