diff --git a/Makefile b/Makefile index d62ac0bc..05a0b6b5 100644 --- a/Makefile +++ b/Makefile @@ -71,7 +71,7 @@ build: ## build binary generate: ## generate go generate ./... -dev: ## dev test +dev: generate ## dev test GO_ENABLED=1 gox -osarch="linux/amd64" \ -output="dist/{{.Dir}}_{{.OS}}_{{.Arch}}" \ -ldflags ${LDFLAGS} diff --git a/VERSION b/VERSION index 238d6e88..b0f3d96f 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -1.0.7 +1.0.8 diff --git a/cmd/upgrade.go b/cmd/upgrade.go index 915509e7..14e65c84 100644 --- a/cmd/upgrade.go +++ b/cmd/upgrade.go @@ -15,9 +15,10 @@ import ( func newCmdUpgrade(f factory.Factory) *cobra.Command { up := &cobra.Command{ Use: "upgrade", - Short: "Upgrades the Q CLI to the newest version", + Short: "Upgrades the Qucheng cli or plugin to the newest version", Aliases: []string{"ug", "ugc"}, } up.AddCommand(upgrade.NewUpgradeQ(f)) + up.AddCommand(upgrade.NewUpgradeOperator(f)) return up } diff --git a/cmd/upgrade/operator.go b/cmd/upgrade/operator.go new file mode 100644 index 00000000..5b8be57c --- /dev/null +++ b/cmd/upgrade/operator.go @@ -0,0 +1,38 @@ +// Copyright (c) 2021-2022 北京渠成软件有限公司(Beijing Qucheng Software Co., Ltd. www.qucheng.com) All rights reserved. +// Use of this source code is covered by the following dual licenses: +// (1) Z PUBLIC LICENSE 1.2 (ZPL 1.2) +// (2) Affero General Public License 3.0 (AGPL 3.0) +// license that can be found in the LICENSE file. + +package upgrade + +import ( + "os" + + qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec" + "github.com/easysoft/qcadmin/internal/pkg/util/factory" + "github.com/spf13/cobra" +) + +func NewUpgradeOperator(f factory.Factory) *cobra.Command { + up := option{ + log: f.GetLog(), + } + upq := &cobra.Command{ + Use: "operator", + Short: "upgrade operator to the newest version", + Args: cobra.NoArgs, + Run: func(cmd *cobra.Command, args []string) { + up.DoOperator() + }, + } + return upq +} + +func (up option) DoOperator() { + qcexec.CommandRun(os.Args[0], "manage", "plugins", "sync") + if err := qcexec.CommandRun(os.Args[0], "manage", "plugins", "enable", "cne-operator"); err != nil { + up.log.Errorf("upgrade plugin cne-operator err: %v", err) + return + } +} diff --git a/common/const.go b/common/const.go index 52058185..e5399ce5 100644 --- a/common/const.go +++ b/common/const.go @@ -56,31 +56,30 @@ const ( ) const ( - DefaultQuchengVersion = "stable-1.1.0" - K3sBinName = "k3s" - K3sBinPath = "/usr/local/bin/k3s" - HelmBinName = "helm" - HelmBinPath = "/usr/local/bin/helm" - K3sBinVersion = "v1.23.5+k3s1" - K3sBinURL = "https://github.com/k3s-io/k3s/releases/download" - K3sAgentEnv = "/etc/systemd/system/k3s-agent.service.env" - K3sKubeConfig = "/etc/rancher/k3s/k3s.yaml" - KubeQPS = 5.0 - KubeBurst = 10 - KubectlBinPath = "/usr/local/bin/kubectl" - QcAdminBinPath = "/usr/local/bin/qcadmin" - StatusWaitDuration = 5 * time.Minute - WaitRetryInterval = 5 * time.Second - DefaultHelmRepoName = "install" - DefaultSystem = "cne-system" - DefaultChartName = "qucheng" - DefaultAPIChartName = "cne-api" - DefaultQuchengName = "qucheng" - DefaultCneAPIName = "cne-api" - DefaultDBName = "qucheng-mysql" - InitFileName = ".initdone" - InitLockFileName = ".qlock" - InitModeCluster = ".incluster" + DefaultQuchengVersion = "stable-1.1.1" + K3sBinName = "k3s" + K3sBinPath = "/usr/local/bin/k3s" + HelmBinName = "helm" + HelmBinPath = "/usr/local/bin/helm" + K3sBinVersion = "v1.23.8+k3s1" + K3sBinURL = "https://github.com/k3s-io/k3s/releases/download" + K3sAgentEnv = "/etc/systemd/system/k3s-agent.service.env" + K3sKubeConfig = "/etc/rancher/k3s/k3s.yaml" + KubeQPS = 5.0 + KubeBurst = 10 + KubectlBinPath = "/usr/local/bin/kubectl" + QcAdminBinPath = "/usr/local/bin/qcadmin" + StatusWaitDuration = 5 * time.Minute + WaitRetryInterval = 5 * time.Second + DefaultHelmRepoName = "install" + DefaultSystem = "cne-system" + DefaultQuchengName = "qucheng" + DefaultCneOperatorName = "cne-operator" + DefaultIngressName = "nginx-ingress-controller" + DefaultDBName = "qucheng-mysql" + InitFileName = ".initdone" + InitLockFileName = ".qlock" + InitModeCluster = ".incluster" ) const ( diff --git a/hack/manifests/plugins/plugins.json b/hack/manifests/plugins/plugins.json index 686d45e6..192ec5e2 100644 --- a/hack/manifests/plugins/plugins.json +++ b/hack/manifests/plugins/plugins.json @@ -69,9 +69,9 @@ { "name": "cne-operator", "description": "渠成云原生应用交付平台控制器", - "version": "0.1.0", + "version": "1.1.1", "home": "https://www.qucheng.com", - "appversion": "0.1.0", + "appversion": "1.1.1", "path": "cne-operator", "tool": "helm", "builtin": true diff --git a/internal/pkg/cluster/qucheng.go b/internal/pkg/cluster/qucheng.go index 5e190e1c..814f4075 100644 --- a/internal/pkg/cluster/qucheng.go +++ b/internal/pkg/cluster/qucheng.go @@ -143,7 +143,7 @@ func (p *Cluster) InstallQuCheng() error { } helmchan := common.GetChannel(p.QuchengVersion) // helm upgrade -i nginx-ingress-controller bitnami/nginx-ingress-controller -n kube-system - helmargs := []string{"experimental", "helm", "upgrade", "--name", common.DefaultChartName, "--repo", common.DefaultHelmRepoName, "--chart", common.DefaultChartName, "--namespace", common.DefaultSystem, "--set", fmt.Sprintf("ingress.host=console.%s", p.Domain), "--set", "env.APP_DOMAIN=" + p.Domain, "--set", "env.CNE_API_TOKEN=" + token, "--set", "cloud.defaultChannel=" + helmchan} + helmargs := []string{"experimental", "helm", "upgrade", "--name", common.DefaultQuchengName, "--repo", common.DefaultHelmRepoName, "--chart", common.DefaultQuchengName, "--namespace", common.DefaultSystem, "--set", fmt.Sprintf("ingress.host=console.%s", p.Domain), "--set", "env.APP_DOMAIN=" + p.Domain, "--set", "env.CNE_API_TOKEN=" + token, "--set", "cloud.defaultChannel=" + helmchan} if helmchan != "stable" { helmargs = append(helmargs, "--set", "env.PHP_DEBUG=2") } @@ -156,12 +156,6 @@ func (p *Cluster) InstallQuCheng() error { p.Log.Errorf("upgrade install qucheng web failed: %s", string(output)) return err } - // Deprecated CNE_API_TOKEN - // output, err = qcexec.Command(os.Args[0], "experimental", "helm", "upgrade", "--name", common.DefaultCneAPIName, "--repo", common.DefaultHelmRepoName, "--chart", common.DefaultAPIChartName, "--namespace", common.DefaultSystem, "--set", "env.CNE_TOKEN="+token, "--set", "env.CNE_API_TOKEN="+token, "--set", "cloud.defaultChannel="+common.GetChannel(p.QuchengVersion)).CombinedOutput() - // if err != nil { - // p.Log.Errorf("upgrade install qucheng api failed: %s", string(output)) - // return err - // } p.Log.Done("install qucheng done") p.Ready() initfile := common.GetCustomConfig(common.InitFileName) diff --git a/internal/pkg/plugin/plugin.go b/internal/pkg/plugin/plugin.go index 6204fdbb..472d7bd2 100644 --- a/internal/pkg/plugin/plugin.go +++ b/internal/pkg/plugin/plugin.go @@ -14,6 +14,7 @@ import ( "os" "strings" + gv "github.com/Masterminds/semver/v3" "github.com/easysoft/qcadmin/common" qcexec "github.com/easysoft/qcadmin/internal/pkg/util/exec" "github.com/easysoft/qcadmin/internal/pkg/util/log" @@ -131,14 +132,23 @@ func (p *Item) UnInstall() error { func (p *Item) Install() error { pluginName := fmt.Sprintf("qc-plugin-%s", p.Type) - _, err := p.Client.GetSecret(context.TODO(), common.DefaultSystem, pluginName, metav1.GetOptions{}) + oldSecret, err := p.Client.GetSecret(context.TODO(), common.DefaultSystem, pluginName, metav1.GetOptions{}) + updatestatus := false if err == nil { - p.log.Warnf("plugin %s is already installed", p.Type) - return nil - } - if !errors.IsNotFound(err) { - p.log.Debugf("get plugin secret failed: %v", err) - return fmt.Errorf("plugin %s install failed", p.Name) + nowversion := gv.MustParse(strings.TrimPrefix(p.Version, "v")) + oldversion := string(oldSecret.Data["version"]) + p.log.Debugf("type: %s, old version: %s, now version: %s", p.Type, oldversion, nowversion) + needupgrade := nowversion.GreaterThan(gv.MustParse(oldversion)) + if !needupgrade { + p.log.Warnf("plugin %s is the latest version", p.Type) + return nil + } + updatestatus = true + } else { + if !errors.IsNotFound(err) { + p.log.Debugf("get plugin secret failed: %v", err) + return fmt.Errorf("plugin %s install failed", p.Name) + } } if p.Tool == "helm" { applycmd := qcexec.Command(os.Args[0], "experimental", "helm", "upgrade", "--name", p.Type, "--repo", common.DefaultHelmRepoName, "--chart", p.Path, "--namespace", common.DefaultSystem) @@ -162,11 +172,21 @@ func (p *Item) Install() error { "version": p.Version, "cliversion": common.Version, } - _, err = p.Client.CreateSecret(context.TODO(), common.DefaultSystem, &corev1.Secret{ - ObjectMeta: metav1.ObjectMeta{ - Name: pluginName, - }, - StringData: plugindata, - }, metav1.CreateOptions{}) + if updatestatus { + _, err = p.Client.UpdateSecret(context.TODO(), common.DefaultSystem, &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: pluginName, + }, + StringData: plugindata, + }, metav1.UpdateOptions{}) + } else { + _, err = p.Client.CreateSecret(context.TODO(), common.DefaultSystem, &corev1.Secret{ + ObjectMeta: metav1.ObjectMeta{ + Name: pluginName, + }, + StringData: plugindata, + }, metav1.CreateOptions{}) + } + return err } diff --git a/internal/pkg/status/k8s.go b/internal/pkg/status/k8s.go index c533c614..c2a50392 100644 --- a/internal/pkg/status/k8s.go +++ b/internal/pkg/status/k8s.go @@ -152,8 +152,6 @@ func (k *K8sStatusCollector) quchengStatus(ctx context.Context, status *Status) k.deploymentStatus(ctx, "kube-system", "local-path-provisioner", "k8s", status) // 业务层 k.deploymentStatus(ctx, common.DefaultSystem, common.DefaultQuchengName, "", status) - // 平台层 - // k.deploymentStatus(ctx, common.DefaultSystem, common.DefaultCneAPIName, "", status) // 数据库 k.deploymentStatus(ctx, common.DefaultSystem, common.DefaultDBName, "", status) @@ -173,6 +171,11 @@ func (k *K8sStatusCollector) quchengPluginStatus(ctx context.Context, p plugin.M stateCount.Disabled = true } else { stateCount.Disabled = false + if p.Type == "ingress" { + k.deploymentStatus(ctx, common.DefaultSystem, common.DefaultIngressName, "", status) + } else if p.Type == "cne-operator" { + k.deploymentStatus(ctx, common.DefaultSystem, common.DefaultCneOperatorName, "", status) + } } status.QStatus.PluginState[p.Type] = stateCount return nil