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,29 +1,21 @@
/*
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 pod
import (
"context"
"fmt"
"strings"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/informers"
"k8s.io/apimachinery/pkg/types"
"sigs.k8s.io/controller-runtime/pkg/client"
runtimeclient "sigs.k8s.io/controller-runtime/pkg/client"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query"
@@ -45,34 +37,35 @@ const (
)
type podsGetter struct {
informer informers.SharedInformerFactory
cache runtimeclient.Reader
}
func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
return &podsGetter{informer: sharedInformers}
func New(cache runtimeclient.Reader) v1alpha3.Interface {
return &podsGetter{cache: cache}
}
func (p *podsGetter) Get(namespace, name string) (runtime.Object, error) {
return p.informer.Core().V1().Pods().Lister().Pods(namespace).Get(name)
pod := &corev1.Pod{}
if err := p.cache.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: name}, pod); err != nil {
return nil, err
}
return p.setPodStatus(pod.DeepCopy()), nil
}
func (p *podsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
pods, err := p.informer.Core().V1().Pods().Lister().Pods(namespace).List(query.Selector())
if err != nil {
pods := &corev1.PodList{}
if err := p.cache.List(context.Background(), pods, client.InNamespace(namespace),
client.MatchingLabelsSelector{Selector: query.Selector()}); err != nil {
return nil, err
}
var result []runtime.Object
for _, pod := range pods {
result = append(result, pod)
for _, item := range pods.Items {
result = append(result, p.setPodStatus(item.DeepCopy()))
}
return v1alpha3.DefaultList(result, query, p.compare, p.filter), nil
}
func (p *podsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
leftPod, ok := left.(*corev1.Pod)
if !ok {
return false
@@ -100,8 +93,7 @@ func (p *podsGetter) filter(object runtime.Object, filter query.Filter) bool {
case fieldServiceName:
return p.podBelongToService(pod, string(filter.Value))
case fieldStatus:
_, statusType := p.getPodStatus(pod)
return statusType == string(filter.Value)
return p.getPodStatus(pod) == string(filter.Value)
case fieldPhase:
return string(pod.Status.Phase) == string(filter.Value)
case fieldPodIP:
@@ -131,8 +123,8 @@ func (p *podsGetter) podBindPVC(item *corev1.Pod, pvcName string) bool {
}
func (p *podsGetter) podBelongToService(item *corev1.Pod, serviceName string) bool {
service, err := p.informer.Core().V1().Services().Lister().Services(item.Namespace).Get(serviceName)
if err != nil {
service := &corev1.Service{}
if err := p.cache.Get(context.Background(), types.NamespacedName{Namespace: item.Namespace, Name: serviceName}, service); err != nil {
return false
}
selector := labels.Set(service.Spec.Selector).AsSelectorPreValidated()
@@ -142,13 +134,18 @@ func (p *podsGetter) podBelongToService(item *corev1.Pod, serviceName string) bo
return true
}
func (p *podsGetter) setPodStatus(pod *corev1.Pod) *corev1.Pod {
pod.Status.Phase = corev1.PodPhase(p.getPodStatus(pod))
return pod
}
// getPodStatus refer to `kubectl get po` result.
// https://github.com/kubernetes/kubernetes/blob/45279654db87f4908911569c07afc42804f0e246/pkg/printers/internalversion/printers.go#L820-920
// podStatusPhase = []string("Pending", "Running","Succeeded","Failed","Unknown")
// podStatusReasons = []string{"Evicted", "NodeAffinity", "NodeLost", "Shutdown", "UnexpectedAdmissionError"}
// containerWaitingReasons = []string{"ContainerCreating", "CrashLoopBackOff", "CreateContainerConfigError", "ErrImagePull", "ImagePullBackOff", "CreateContainerError", "InvalidImageName"}
// containerTerminatedReasons = []string{"OOMKilled", "Completed", "Error", "ContainerCannotRun", "DeadlineExceeded", "Evicted"}
func (p *podsGetter) getPodStatus(pod *corev1.Pod) (string, string) {
func (p *podsGetter) getPodStatus(pod *corev1.Pod) string {
reason := string(pod.Status.Phase)
if pod.Status.Reason != "" {
@@ -251,7 +248,7 @@ func (p *podsGetter) getPodStatus(pod *corev1.Pod) (string, string) {
statusType = statusTypeError
}
}
return reason, statusType
return statusType
}
func hasPodReadyCondition(conditions []corev1.PodCondition) bool {

View File

@@ -1,180 +0,0 @@
/*
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 pod
import (
"testing"
"github.com/google/go-cmp/cmp"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes/fake"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
)
func TestListPods(t *testing.T) {
tests := []struct {
description string
namespace string
query *query.Query
expected *api.ListResult
expectedErr error
}{
{
"test name filter",
"default",
&query.Query{
Pagination: &query.Pagination{
Limit: 10,
Offset: 0,
},
SortBy: query.FieldName,
Ascending: false,
Filters: map[query.Field]query.Value{query.FieldNamespace: query.Value("default")},
},
&api.ListResult{
Items: []interface{}{foo5, foo4, foo3, foo2, foo1},
TotalItems: len(pods),
},
nil,
},
{
"test pvcName filter",
"default",
&query.Query{
Pagination: &query.Pagination{
Limit: 10,
Offset: 0,
},
SortBy: query.FieldName,
Ascending: false,
Filters: map[query.Field]query.Value{
query.FieldNamespace: query.Value("default"),
fieldPVCName: query.Value(foo4.Spec.Volumes[0].PersistentVolumeClaim.ClaimName),
},
},
&api.ListResult{
Items: []interface{}{foo4},
TotalItems: 1,
},
nil,
},
{
"test phase filter",
"default",
&query.Query{
Pagination: &query.Pagination{
Limit: 10,
Offset: 0,
},
SortBy: query.FieldName,
Ascending: false,
Filters: map[query.Field]query.Value{
query.FieldNamespace: query.Value("default"),
fieldPhase: query.Value(corev1.PodRunning),
},
},
&api.ListResult{
Items: []interface{}{foo5},
TotalItems: 1,
},
nil,
},
}
getter := prepare()
for _, test := range tests {
got, err := getter.List(test.namespace, test.query)
if test.expectedErr != nil && err != test.expectedErr {
t.Errorf("expected error, got nothing")
} else if err != nil {
t.Fatal(err)
}
if diff := cmp.Diff(got, test.expected); diff != "" {
t.Errorf("%T differ (-got, +want): %s", test.expected, diff)
}
}
}
var (
foo1 = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo1",
Namespace: "default",
},
}
foo2 = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo2",
Namespace: "default",
},
}
foo3 = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo3",
Namespace: "default",
},
}
foo4 = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo4",
Namespace: "default",
},
Spec: corev1.PodSpec{
Volumes: []corev1.Volume{
{
Name: "data",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "pvc-1",
ReadOnly: false,
},
},
},
},
},
}
foo5 = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "foo5",
Namespace: "default",
},
Status: corev1.PodStatus{
Phase: corev1.PodRunning,
},
}
pods = []interface{}{foo1, foo2, foo3, foo4, foo5}
)
func prepare() v1alpha3.Interface {
client := fake.NewSimpleClientset()
informer := informers.NewSharedInformerFactory(client, 0)
for _, pod := range pods {
_ = informer.Core().V1().Pods().Informer().GetIndexer().Add(pod)
}
return New(informer)
}