feat: kubesphere 4.0 (#6115)

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

* feat: kubesphere 4.0

Signed-off-by: ci-bot <ci-bot@kubesphere.io>

---------

Signed-off-by: ci-bot <ci-bot@kubesphere.io>
Co-authored-by: ks-ci-bot <ks-ci-bot@example.com>
Co-authored-by: joyceliu <joyceliu@yunify.com>
This commit is contained in:
KubeSphere CI Bot
2024-09-06 11:05:52 +08:00
committed by GitHub
parent b5015ec7b9
commit 447a51f08b
8557 changed files with 546695 additions and 1146174 deletions

View File

@@ -1,59 +1,50 @@
/*
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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package components
import (
"context"
"fmt"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/client-go/informers"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"kubesphere.io/kubesphere/pkg/api/resource/v1alpha2"
"kubesphere.io/kubesphere/pkg/constants"
)
type ComponentsGetter interface {
type Getter interface {
GetComponentStatus(name string) (v1alpha2.ComponentStatus, error)
GetSystemHealthStatus() (v1alpha2.HealthStatus, error)
GetAllComponentsStatus() ([]v1alpha2.ComponentStatus, error)
}
type componentsGetter struct {
informers informers.SharedInformerFactory
cache runtimeclient.Reader
}
func NewComponentsGetter(informers informers.SharedInformerFactory) ComponentsGetter {
return &componentsGetter{informers: informers}
func NewComponentsGetter(cache runtimeclient.Reader) Getter {
return &componentsGetter{cache: cache}
}
func (c *componentsGetter) GetComponentStatus(name string) (v1alpha2.ComponentStatus, error) {
var service *corev1.Service
service := &corev1.Service{}
var err error
for _, ns := range constants.SystemNamespaces {
service, err = c.informers.Core().V1().Services().Lister().Services(ns).Get(name)
if err == nil {
if err := c.cache.Get(context.Background(), types.NamespacedName{
Namespace: ns,
Name: name,
}, service); err == nil {
break
}
}
if err != nil {
return v1alpha2.ComponentStatus{}, err
}
@@ -62,9 +53,11 @@ func (c *componentsGetter) GetComponentStatus(name string) (v1alpha2.ComponentSt
return v1alpha2.ComponentStatus{}, fmt.Errorf("component %s has no selector", name)
}
pods, err := c.informers.Core().V1().Pods().Lister().Pods(service.Namespace).List(labels.SelectorFromValidatedSet(service.Spec.Selector))
if err != nil {
pods := &corev1.PodList{}
if err := c.cache.List(context.Background(), pods, &runtimeclient.ListOptions{
LabelSelector: labels.SelectorFromValidatedSet(service.Spec.Selector),
Namespace: service.Namespace,
}); err != nil {
return v1alpha2.ComponentStatus{}, err
}
@@ -76,9 +69,9 @@ func (c *componentsGetter) GetComponentStatus(name string) (v1alpha2.ComponentSt
HealthyBackends: 0,
TotalBackends: 0,
}
for _, pod := range pods {
for _, pod := range pods.Items {
component.TotalBackends++
if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(&pod) {
component.HealthyBackends++
}
}
@@ -106,16 +99,16 @@ func (c *componentsGetter) GetSystemHealthStatus() (v1alpha2.HealthStatus, error
status.KubeSphereComponents = components
nodes := &corev1.NodeList{}
// get node status
nodes, err := c.informers.Core().V1().Nodes().Lister().List(labels.Everything())
if err != nil {
if err := c.cache.List(context.Background(), nodes); err != nil {
klog.Errorln(err)
return status, nil
}
totalNodes := 0
healthyNodes := 0
for _, nodes := range nodes {
for _, nodes := range nodes.Items {
totalNodes++
for _, condition := range nodes.Status.Conditions {
if condition.Type == corev1.NodeReady && condition.Status == corev1.ConditionTrue {
@@ -138,15 +131,15 @@ func (c *componentsGetter) GetAllComponentsStatus() ([]v1alpha2.ComponentStatus,
var err error
for _, ns := range constants.SystemNamespaces {
services, err := c.informers.Core().V1().Services().Lister().Services(ns).List(labels.Everything())
if err != nil {
services := &corev1.ServiceList{}
if err := c.cache.List(context.Background(), services, &runtimeclient.ListOptions{
Namespace: ns,
}); err != nil {
klog.Error(err)
continue
}
for _, service := range services {
for _, service := range services.Items {
// skip services without a selector
if len(service.Spec.Selector) == 0 {
continue
@@ -161,16 +154,23 @@ func (c *componentsGetter) GetAllComponentsStatus() ([]v1alpha2.ComponentStatus,
TotalBackends: 0,
}
pods, err := c.informers.Core().V1().Pods().Lister().Pods(ns).List(labels.SelectorFromValidatedSet(service.Spec.Selector))
pods := &corev1.PodList{}
if err := c.cache.List(context.Background(), pods, &runtimeclient.ListOptions{
LabelSelector: labels.SelectorFromValidatedSet(service.Spec.Selector),
Namespace: ns,
}); err != nil {
klog.Error(err)
continue
}
if err != nil {
klog.Errorln(err)
continue
}
for _, pod := range pods {
for _, pod := range pods.Items {
component.TotalBackends++
if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(pod) {
if pod.Status.Phase == corev1.PodRunning && isAllContainersReady(&pod) {
component.HealthyBackends++
}
}

View File

@@ -1,18 +1,7 @@
/*
Copyright 2020 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.
*/
* Please refer to the LICENSE file in the root directory of the project.
* https://github.com/kubesphere/kubesphere/blob/master/LICENSE
*/
package components
@@ -21,12 +10,15 @@ import (
"testing"
"time"
"k8s.io/apimachinery/pkg/runtime"
runtimefakeclient "sigs.k8s.io/controller-runtime/pkg/client/fake"
"kubesphere.io/kubesphere/pkg/scheme"
"github.com/google/go-cmp/cmp"
. "github.com/onsi/ginkgo/v2"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"kubesphere.io/kubesphere/pkg/api/resource/v1alpha2"
)
@@ -109,6 +101,10 @@ func nodes(name string, healthNodes, totalNodes int) []runtime.Object {
return ns
}
var _ = Describe("Components", func() {
})
func TestGetSystemHealthStatus(t *testing.T) {
var tests = []struct {
description string
@@ -221,30 +217,17 @@ func TestGetSystemHealthStatus(t *testing.T) {
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
ps := pods(test.name, test.namespace, test.labels, test.healthPods, test.totalPods)
pods := pods(test.name, test.namespace, test.labels, test.healthPods, test.totalPods)
svc := service(test.name, test.namespace, test.labels)
ns := nodes(test.name, test.healthNodes, test.totalNodes)
nodes := nodes(test.name, test.healthNodes, test.totalNodes)
var objs []runtime.Object
objs = append(objs, ps...)
objs = append(objs, svc)
objs = append(objs, ns...)
client := runtimefakeclient.NewClientBuilder().
WithScheme(scheme.Scheme).
WithRuntimeObjects(pods...).
WithRuntimeObjects(svc).
WithRuntimeObjects(nodes...).Build()
client := fake.NewSimpleClientset(objs...)
informer := informers.NewSharedInformerFactory(client, time.Minute*10)
informer.Core().V1().Services().Informer().GetIndexer().Add(svc)
for _, obj := range ps {
informer.Core().V1().Pods().Informer().GetIndexer().Add(obj)
}
for _, obj := range ns {
informer.Core().V1().Nodes().Informer().GetIndexer().Add(obj)
}
c := NewComponentsGetter(informer)
c := NewComponentsGetter(client)
healthStatus, err := c.GetSystemHealthStatus()
if err != nil {
t.Fatal(err)
@@ -340,24 +323,15 @@ func TestGetComponentStatus(t *testing.T) {
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
ps := pods(test.name, test.namespace, test.labels, test.healthPods, test.totalPods)
pods := pods(test.name, test.namespace, test.labels, test.healthPods, test.totalPods)
svc := service(test.name, test.namespace, test.labels)
var objs []runtime.Object
objs = append(objs, ps...)
objs = append(objs, svc)
client := runtimefakeclient.NewClientBuilder().
WithScheme(scheme.Scheme).
WithRuntimeObjects(pods...).
WithRuntimeObjects(svc).Build()
client := fake.NewSimpleClientset(objs...)
informer := informers.NewSharedInformerFactory(client, time.Minute*10)
informer.Core().V1().Services().Informer().GetIndexer().Add(svc)
for _, obj := range ps {
informer.Core().V1().Pods().Informer().GetIndexer().Add(obj)
}
c := NewComponentsGetter(informer)
c := NewComponentsGetter(client)
healthStatus, err := c.GetComponentStatus(test.name)
if err == nil && test.expectedError {
t.Fatalf("expected error while got nothing")