components.go 4.6 KB
Newer Older
J
jeff 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*

 Copyright 2019 The KubeSphere Authors.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.

*/
package components

import (
J
Jeff 已提交
21
	"fmt"
H
hongming 已提交
22 23
	meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"kubesphere.io/kubesphere/pkg/models"
H
hongming 已提交
24
	"kubesphere.io/kubesphere/pkg/simple/client/k8s"
J
jeff 已提交
25 26 27 28

	"kubesphere.io/kubesphere/pkg/informers"

	"github.com/golang/glog"
H
hongming 已提交
29
	corev1 "k8s.io/api/core/v1"
J
jeff 已提交
30 31 32 33 34 35 36
	"k8s.io/apimachinery/pkg/labels"

	"kubesphere.io/kubesphere/pkg/constants"
)

func GetComponentStatus(name string) (interface{}, error) {

H
hongming 已提交
37
	var service *corev1.Service
J
jeff 已提交
38
	var err error
H
hongming 已提交
39 40 41

	serviceLister := informers.SharedInformerFactory().Core().V1().Services().Lister()

J
jeff 已提交
42 43 44 45 46 47 48 49 50 51 52
	for _, ns := range constants.SystemNamespaces {
		service, err = serviceLister.Services(ns).Get(name)
		if err == nil {
			break
		}
	}

	if err != nil {
		return nil, err
	}

J
Jeff 已提交
53 54 55 56
	if len(service.Spec.Selector) == 0 {
		return nil, fmt.Errorf("component %s has no selector", name)
	}

H
hongming 已提交
57 58
	podLister := informers.SharedInformerFactory().Core().V1().Pods().Lister()

J
jeff 已提交
59 60 61 62 63 64
	pods, err := podLister.Pods(service.Namespace).List(labels.SelectorFromValidatedSet(service.Spec.Selector))

	if err != nil {
		return nil, err
	}

H
hongming 已提交
65
	component := models.ComponentStatus{
J
jeff 已提交
66 67 68 69 70 71 72 73
		Name:            service.Name,
		Namespace:       service.Namespace,
		SelfLink:        service.SelfLink,
		Label:           service.Spec.Selector,
		StartedAt:       service.CreationTimestamp.Time,
		HealthyBackends: 0,
		TotalBackends:   0,
	}
H
hongming 已提交
74
	for _, pod := range pods {
J
jeff 已提交
75
		component.TotalBackends++
H
hongming 已提交
76 77
		if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
			component.HealthyBackends++
J
jeff 已提交
78 79 80 81 82
		}
	}
	return component, nil
}

H
hongming 已提交
83 84 85 86 87 88 89 90 91 92
func isAllContainersReady(pod *corev1.Pod) bool {
	for _, c := range pod.Status.ContainerStatuses {
		if !c.Ready {
			return false
		}
	}
	return true
}

func GetSystemHealthStatus() (*models.HealthStatus, error) {
J
jeff 已提交
93

H
hongming 已提交
94
	status := &models.HealthStatus{}
J
jeff 已提交
95

H
hongming 已提交
96
	componentStatuses, err := k8s.Client().CoreV1().ComponentStatuses().List(meta_v1.ListOptions{})
J
jeff 已提交
97 98 99
	if err != nil {
		return nil, err
	}
H
hongming 已提交
100
	status.KubernetesComponents = append(status.KubernetesComponents, componentStatuses.Items...)
J
jeff 已提交
101 102

	// get kubesphere-system components
H
hongming 已提交
103
	components, err := GetAllComponentsStatus()
J
jeff 已提交
104 105 106 107
	if err != nil {
		glog.Errorln(err)
	}

H
hongming 已提交
108
	status.KubeSphereComponents = components
H
hongming 已提交
109 110

	nodeLister := informers.SharedInformerFactory().Core().V1().Nodes().Lister()
J
jeff 已提交
111 112 113 114 115 116 117 118 119 120 121 122
	// get node status
	nodes, err := nodeLister.List(labels.Everything())
	if err != nil {
		glog.Errorln(err)
		return status, nil
	}

	totalNodes := 0
	healthyNodes := 0
	for _, nodes := range nodes {
		totalNodes++
		for _, condition := range nodes.Status.Conditions {
H
hongming 已提交
123
			if condition.Type == corev1.NodeReady && condition.Status == corev1.ConditionTrue {
J
jeff 已提交
124 125 126 127
				healthyNodes++
			}
		}
	}
H
hongming 已提交
128
	nodeStatus := models.NodeStatus{TotalNodes: totalNodes, HealthyNodes: healthyNodes}
J
jeff 已提交
129

H
hongming 已提交
130
	status.NodeStatus = nodeStatus
J
jeff 已提交
131 132 133 134 135

	return status, nil

}

H
hongming 已提交
136
func GetAllComponentsStatus() ([]models.ComponentStatus, error) {
H
hongming 已提交
137 138
	serviceLister := informers.SharedInformerFactory().Core().V1().Services().Lister()
	podLister := informers.SharedInformerFactory().Core().V1().Pods().Lister()
J
jeff 已提交
139

H
hongming 已提交
140
	components := make([]models.ComponentStatus, 0)
J
jeff 已提交
141 142 143 144 145 146 147 148 149 150 151 152

	var err error
	for _, ns := range constants.SystemNamespaces {

		services, err := serviceLister.Services(ns).List(labels.Everything())

		if err != nil {
			glog.Error(err)
			continue
		}

		for _, service := range services {
J
Jeff 已提交
153 154 155 156 157 158

			// skip services without a selector
			if len(service.Spec.Selector) == 0 {
				continue
			}

H
hongming 已提交
159
			component := models.ComponentStatus{
J
jeff 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
				Name:            service.Name,
				Namespace:       service.Namespace,
				SelfLink:        service.SelfLink,
				Label:           service.Spec.Selector,
				StartedAt:       service.CreationTimestamp.Time,
				HealthyBackends: 0,
				TotalBackends:   0,
			}

			pods, err := podLister.Pods(ns).List(labels.SelectorFromValidatedSet(service.Spec.Selector))

			if err != nil {
				glog.Errorln(err)
				continue
			}

			for _, pod := range pods {
				component.TotalBackends++
H
hongming 已提交
178 179
				if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
					component.HealthyBackends++
J
jeff 已提交
180 181 182
				}
			}

H
hongming 已提交
183
			components = append(components, component)
J
jeff 已提交
184 185 186
		}
	}

H
hongming 已提交
187
	return components, err
J
jeff 已提交
188
}