Skip to content

Commit

Permalink
* [feat] imp status show
Browse files Browse the repository at this point in the history
  • Loading branch information
ysicing committed Sep 12, 2024
1 parent 206e0f0 commit 83945fd
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 36 deletions.
24 changes: 24 additions & 0 deletions internal/pkg/k8s/pod.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Copyright (c) 2021-2024 北京渠成软件有限公司(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 k8s

import (
"context"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

func (c *Client) CountPodNumByNamespace(ctx context.Context, ns string) (int, error) {
if len(ns) == 0 {
ns = metav1.NamespaceAll
}
podList, err := c.Clientset.CoreV1().Pods(ns).List(ctx, metav1.ListOptions{})
if err != nil {
return 0, err
}
return len(podList.Items), nil
}
20 changes: 19 additions & 1 deletion internal/pkg/status/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (

"github.com/cockroachdb/errors"
"github.com/ergoapi/util/file"
"github.com/ergoapi/util/version"

"github.com/easysoft/qcadmin/common"
"github.com/easysoft/qcadmin/internal/app/config"
Expand Down Expand Up @@ -211,7 +212,13 @@ func (k *K8sStatusCollector) serviceStatus(ctx context.Context, status *Status)
k.deploymentStatus(ctx, "kube-system", "coredns", "coredns", "k8s", status)
k.deploymentStatus(ctx, "kube-system", "metrics-server", "metrics-server", "k8s", status)
if k.cfg.Storage.Type == "local" {
k.deploymentStatus(ctx, "kube-system", "local-path-provisioner", "local-path-provisioner", "k8s", status)
name := "q-local-provisioner"
if len(k.cfg.Install.Version) == 0 || version.LTv2(k.cfg.Install.Version, common.Version) {
name = "local-path-provisioner"
}
k.deploymentStatus(ctx, "kube-system", name, "q-local", "k8s", status)
} else if k.cfg.Storage.Type == "nfs" {
k.deploymentStatus(ctx, "kube-system", "q-nfs-nfs-subdir-external-provisioner", "q-nfs", "k8s", status)
}
// 业务层
k.deploymentStatus(ctx, common.GetDefaultSystemNamespace(true), common.GetReleaseName(k.cfg.Quickon.DevOps), common.GetReleaseName(k.cfg.Quickon.DevOps), "", status)
Expand All @@ -224,6 +231,7 @@ func (k *K8sStatusCollector) serviceStatus(ctx context.Context, status *Status)
for _, p := range plugins {
k.platformPluginStatus(ctx, p, status)
}
k.platformInstallStatus(ctx, status)
return nil
}

Expand Down Expand Up @@ -333,3 +341,13 @@ func (k *K8sStatusCollector) ingressStatus(ctx context.Context, ns, name, aliasn
status.QStatus.PodState[aliasname] = stateCount
return false, nil
}

func (k *K8sStatusCollector) platformInstallStatus(ctx context.Context, status *Status) {
k.option.Log.Debug("check platform init status")
num, err := k.client.CountPodNumByNamespace(ctx, common.DefaultSystemNamespace)
if err != nil || num == 0 {
status.QStatus.Init = false
return
}
status.QStatus.Init = true
}
74 changes: 39 additions & 35 deletions internal/pkg/status/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type PodStateMap map[string]PodStateCount
type Status struct {
output string `json:"-" yaml:"-"`
KubeStatus KubeStatus `json:"k8s" yaml:"k8s"`
QStatus QStatus `json:"platform" yaml:"platform"`
QStatus QStatus `json:"platform,omitempty" yaml:"platform,omitempty"`
}

type KubeStatus struct {
Expand Down Expand Up @@ -60,6 +60,7 @@ type PodStateCount struct {
}

type QStatus struct {
Init bool `json:"init" yaml:"init"`
PodState PodStateMap `json:"service,omitempty" yaml:"service,omitempty"`
PluginState PodStateMap `json:"plugin,omitempty" yaml:"plugin,omitempty"`
}
Expand All @@ -73,6 +74,7 @@ func newStatus(output string) *Status {
PodState: PodStateMap{},
},
QStatus: QStatus{
Init: false,
PodState: PodStateMap{},
PluginState: PodStateMap{},
},
Expand Down Expand Up @@ -109,47 +111,49 @@ func (s *Status) Format() error {
}
}
fmt.Fprintf(w, "\n")
fmt.Fprintf(w, "Platform Status: \n")
if s.QStatus.PodState["platform"].Disabled {
fmt.Fprintf(w, " %s\t%s\n", "status", color.SBlue("disabled"))
w.Flush()
return output.EncodeText(os.Stdout, buf.Bytes())
}
cfg, _ := config.LoadConfig()
consoleURL := kutil.GetConsoleURL(cfg)
fmt.Fprintf(w, " namespace:\t%s\n", color.SBlue(common.GetDefaultSystemNamespace(true)))
if cfg.Quickon.DevOps {
fmt.Fprintf(w, " console: %s\n", color.SGreen(consoleURL))
} else {
fmt.Fprintf(w, " console: %s(%s/%s)\n", color.SGreen(consoleURL), color.SGreen(common.QuchengDefaultUser), color.SGreen(cfg.ConsolePassword))
}
ptOK := true
fmt.Fprintf(w, " component status: \n")
for name, state := range s.QStatus.PodState {
if state.Disabled {
fmt.Fprintf(w, " %s\t%s\n", name, color.SBlue("disabled"))
if s.QStatus.Init {
fmt.Fprintf(w, "Platform Status: \n")
if s.QStatus.PodState["platform"].Disabled {
fmt.Fprintf(w, " %s\t%s\n", "status", color.SBlue("disabled"))
w.Flush()
return output.EncodeText(os.Stdout, buf.Bytes())
}
cfg, _ := config.LoadConfig()
consoleURL := kutil.GetConsoleURL(cfg)
fmt.Fprintf(w, " namespace:\t%s\n", color.SBlue(common.GetDefaultSystemNamespace(true)))
if cfg.Quickon.DevOps {
fmt.Fprintf(w, " console: %s\n", color.SGreen(consoleURL))
} else {
if state.Available > 0 {
fmt.Fprintf(w, " %s\t%s\n", name, color.SGreen("ok"))
fmt.Fprintf(w, " console: %s(%s/%s)\n", color.SGreen(consoleURL), color.SGreen(common.QuchengDefaultUser), color.SGreen(cfg.ConsolePassword))
}
ptOK := true
fmt.Fprintf(w, " component status: \n")
for name, state := range s.QStatus.PodState {
if state.Disabled {
fmt.Fprintf(w, " %s\t%s\n", name, color.SBlue("disabled"))
} else {
fmt.Fprintf(w, " %s\t%s\n", name, color.SRed("warn"))
ptOK = false
if state.Available > 0 {
fmt.Fprintf(w, " %s\t%s\n", name, color.SGreen("ok"))
} else {
fmt.Fprintf(w, " %s\t%s\n", name, color.SRed("warn"))
ptOK = false
}
}
}
}
fmt.Fprintf(w, " plugin status: \n")
for name, state := range s.QStatus.PluginState {
if state.Disabled {
fmt.Fprintf(w, " %s\t%s\n", name, color.SBlue("disabled"))
fmt.Fprintf(w, " plugin status: \n")
for name, state := range s.QStatus.PluginState {
if state.Disabled {
fmt.Fprintf(w, " %s\t%s\n", name, color.SBlue("disabled"))
} else {
fmt.Fprintf(w, " %s\t%s\n", name, color.SGreen("enabled"))
}
}
if ptOK {
fmt.Fprintf(w, " %s\t%s\n", "status", color.SGreen("health"))
} else {
fmt.Fprintf(w, " %s\t%s\n", name, color.SGreen("enabled"))
fmt.Fprintf(w, " %s\t%s\n", "status", color.SRed("unhealth"))
}
}
if ptOK {
fmt.Fprintf(w, " %s\t%s\n", "status", color.SGreen("health"))
} else {
fmt.Fprintf(w, " %s\t%s\n", "status", color.SRed("unhealth"))
}
w.Flush()
return output.EncodeText(os.Stdout, buf.Bytes())
}
Expand Down

0 comments on commit 83945fd

Please sign in to comment.