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

chore: delete deploy when upgrade #440

Merged
merged 4 commits into from
Sep 9, 2024
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
2 changes: 1 addition & 1 deletion docs/user_docs/cli/kbcli_kubeblocks_install.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ kbcli kubeblocks install [flags]
--set-file stringArray Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
--set-json stringArray Set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)
--set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--timeout duration Time to wait for installing KubeBlocks, such as --timeout=10m (default 10m0s)
--timeout duration Time to wait for installing KubeBlocks, such as --timeout=10m (default 30m0s)
--tolerations strings Tolerations for Kubeblocks, such as '"dev=true:NoSchedule,large=true:NoSchedule"'
--topology-keys stringArray Topology keys for affinity
-f, --values strings Specify values in a YAML file or a URL (can specify multiple)
Expand Down
2 changes: 1 addition & 1 deletion docs/user_docs/cli/kbcli_kubeblocks_upgrade.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ kbcli kubeblocks upgrade [flags]
--set-file stringArray Set values from respective files specified via the command line (can specify multiple or separate values with commas: key1=path1,key2=path2)
--set-json stringArray Set JSON values on the command line (can specify multiple or separate values with commas: key1=jsonval1,key2=jsonval2)
--set-string stringArray Set STRING values on the command line (can specify multiple or separate values with commas: key1=val1,key2=val2)
--timeout duration Time to wait for upgrading KubeBlocks, such as --timeout=10m (default 10m0s)
--timeout duration Time to wait for upgrading KubeBlocks, such as --timeout=10m (default 30m0s)
-f, --values strings Specify values in a YAML file or a URL (can specify multiple)
--version string Set KubeBlocks version
--wait Wait for KubeBlocks to be ready. It will wait for a --timeout period (default true)
Expand Down
3 changes: 1 addition & 2 deletions pkg/cmd/kubeblocks/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ func (o *InstallOptions) compare(args []string, detail bool) error {
}
// update repo
if err := helm.AddRepo(&repo.Entry{Name: types.KubeBlocksRepoName, URL: util.GetHelmChartRepoURL()}); err != nil {

return fmt.Errorf(err.Error())
return err
}
// check version is available
if exists, err := versionExists(versionA); !exists {
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/kubeblocks/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ func newInstallCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra
cmd.Flags().StringVar(&o.Version, "version", version.DefaultKubeBlocksVersion, "KubeBlocks version")
cmd.Flags().BoolVar(&o.CreateNamespace, "create-namespace", false, "Create the namespace if not present")
cmd.Flags().BoolVar(&o.Check, "check", true, "Check kubernetes environment before installation")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 600*time.Second, "Time to wait for installing KubeBlocks, such as --timeout=10m")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 1800*time.Second, "Time to wait for installing KubeBlocks, such as --timeout=10m")
cmd.Flags().BoolVar(&o.Wait, "wait", true, "Wait for KubeBlocks to be ready, including all the auto installed add-ons. It will wait for a --timeout period")
cmd.Flags().BoolVar(&o.WaitAddons, "wait-addons", true, "Wait for auto installed add-ons. It will wait for a --timeout period")
cmd.Flags().BoolVar(&p.force, flagForce, p.force, "If present, just print fail item and continue with the following steps")
Expand Down
56 changes: 37 additions & 19 deletions pkg/cmd/kubeblocks/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"k8s.io/klog/v2"
cmdutil "k8s.io/kubectl/pkg/cmd/util"
"k8s.io/kubectl/pkg/util/templates"
"sigs.k8s.io/yaml"

"github.com/apecloud/kbcli/pkg/printer"
"github.com/apecloud/kbcli/pkg/spinner"
Expand All @@ -59,7 +60,7 @@ var (
kbcli kubeblocks upgrade --set replicaCount=3`)
)

type getDeploymentFunc func(client kubernetes.Interface) (*appsv1.Deployment, error)
type deploymentGetter func(client kubernetes.Interface) (*appsv1.Deployment, error)

func newUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra.Command {
o := &InstallOptions{
Expand All @@ -81,7 +82,7 @@ func newUpgradeCmd(f cmdutil.Factory, streams genericiooptions.IOStreams) *cobra

cmd.Flags().StringVar(&o.Version, "version", "", "Set KubeBlocks version")
cmd.Flags().BoolVar(&o.Check, "check", true, "Check kubernetes environment before upgrade")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 600*time.Second, "Time to wait for upgrading KubeBlocks, such as --timeout=10m")
cmd.Flags().DurationVar(&o.Timeout, "timeout", 1800*time.Second, "Time to wait for upgrading KubeBlocks, such as --timeout=10m")
cmd.Flags().BoolVar(&o.Wait, "wait", true, "Wait for KubeBlocks to be ready. It will wait for a --timeout period")
cmd.Flags().BoolVar(&o.autoApprove, "auto-approve", false, "Skip interactive approval before upgrading KubeBlocks")
helm.AddValueOptionsFlags(cmd.Flags(), &o.ValueOpts)
Expand Down Expand Up @@ -181,15 +182,15 @@ func (o *InstallOptions) Upgrade() error {
// KubeBlocks after upgrade.
s = spinner.New(o.Out, spinnerMsg("Stop KubeBlocks "+kbVersion))
defer s.Fail()
if err = o.stopDeployment(util.GetKubeBlocksDeploy); err != nil {
if err = o.deleteDeployment(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.stopDeployment(util.GetDataProtectionDeploy); err != nil {
if err = o.deleteDeployment(util.GetDataProtectionDeploy); err != nil {
return err
}
s.Success()
Expand Down Expand Up @@ -258,9 +259,9 @@ func (o *InstallOptions) upgradeChart() error {
return o.buildChart().Upgrade(o.HelmCfg)
}

// stopDeployment stops the deployment by setting the replicas to 0
func (o *InstallOptions) stopDeployment(getDeployFn getDeploymentFunc) error {
deploy, err := getDeployFn(o.Client)
// deleteDeployment deletes deployment.
func (o *InstallOptions) deleteDeployment(getter deploymentGetter) error {
deploy, err := getter(o.Client)
if err != nil {
if apierrors.IsNotFound(err) {
return nil
Expand All @@ -273,26 +274,43 @@ func (o *InstallOptions) stopDeployment(getDeployFn getDeploymentFunc) error {
return nil
}

if _, err = o.Client.AppsV1().Deployments(deploy.Namespace).Patch(
context.TODO(), deploy.Name, apitypes.JSONPatchType,
[]byte(`[{"op": "replace", "path": "/spec/replicas", "value": 0}]`),
metav1.PatchOptions{}); err != nil {
// before delete deployment, output the deployment yaml, if deployment was deleted
// by mistake, we can recover it by apply the yaml.
deploy.ManagedFields = nil
bytes, err := yaml.Marshal(deploy)
if err != nil {
return err
}
klog.Infof(`
------------------- Deployment %s -------------------
%s
------------------ 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 stopped
return wait.PollUntilContextTimeout(context.Background(), 5*time.Second, o.Timeout, true,
// 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 = util.GetKubeBlocksDeploy(o.Client)
deploy, err = getter(o.Client)
if err != nil {
if apierrors.IsNotFound(err) {
return true, nil
}
return false, err
}
if *deploy.Spec.Replicas == 0 &&
deploy.Status.Replicas == 0 &&
deploy.Status.AvailableReplicas == 0 {
} else if deploy == nil {
return true, nil
}
return false, nil
return false, err
})
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/report/zipwritter.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func (w *reportZipWritter) WriteLogs(folderName string, ctx context.Context, cli
}

for _, pod := range pods.Items {
if pod.Spec.Containers == nil || len(pod.Spec.Containers) == 0 {
if len(pod.Spec.Containers) == 0 {
continue
}
// write pod log to file
Expand Down
4 changes: 2 additions & 2 deletions pkg/preflight/analyzer/kb_taint.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ func (a *AnalyzeTaintClassByKb) analyzeTaint(getFile GetCollectedFileContents, f
func (a *AnalyzeTaintClassByKb) doAnalyzeTaint(nodes v1.NodeList) (*analyze.AnalyzeResult, error) {
taintFailResult := []string{}
for _, node := range nodes.Items {
if node.Spec.Taints == nil || len(node.Spec.Taints) == 0 {
if len(node.Spec.Taints) == 0 {
return newAnalyzeResult(a.Title(), PassType, a.analyzer.Outcomes), nil
}
}

if a.analyzer.TolerationsMap == nil || len(a.analyzer.TolerationsMap) == 0 {
if len(a.analyzer.TolerationsMap) == 0 {
return newAnalyzeResult(a.Title(), FailType, a.analyzer.Outcomes), nil
}

Expand Down
Loading