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,73 +1,65 @@
/*
Copyright 2020 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 serviceaccount
import (
"context"
corev1 "k8s.io/api/core/v1"
"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"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
)
type serviceaccountsGetter struct {
informer informers.SharedInformerFactory
type serviceAccountsGetter struct {
cache runtimeclient.Reader
}
func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
return &serviceaccountsGetter{informer: sharedInformers}
func New(cache runtimeclient.Reader) v1alpha3.Interface {
return &serviceAccountsGetter{cache: cache}
}
func (d *serviceaccountsGetter) Get(namespace, name string) (runtime.Object, error) {
return d.informer.Core().V1().ServiceAccounts().Lister().ServiceAccounts(namespace).Get(name)
func (d *serviceAccountsGetter) Get(namespace, name string) (runtime.Object, error) {
serviceAccount := &corev1.ServiceAccount{}
return serviceAccount, d.cache.Get(context.Background(), types.NamespacedName{Namespace: namespace, Name: name}, serviceAccount)
}
func (d *serviceaccountsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
serviceaccounts, err := d.informer.Core().V1().ServiceAccounts().Lister().ServiceAccounts(namespace).List(query.Selector())
if err != nil {
func (d *serviceAccountsGetter) List(namespace string, query *query.Query) (*api.ListResult, error) {
serviceAccounts := &corev1.ServiceAccountList{}
if err := d.cache.List(context.Background(), serviceAccounts, client.InNamespace(namespace),
client.MatchingLabelsSelector{Selector: query.Selector()}); err != nil {
return nil, err
}
var result []runtime.Object
for _, serviceaccount := range serviceaccounts {
result = append(result, serviceaccount)
for _, item := range serviceAccounts.Items {
result = append(result, item.DeepCopy())
}
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
}
func (d *serviceaccountsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
leftCM, ok := left.(*corev1.ServiceAccount)
func (d *serviceAccountsGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
leftSA, ok := left.(*corev1.ServiceAccount)
if !ok {
return false
}
rightCM, ok := right.(*corev1.ServiceAccount)
rightSA, ok := right.(*corev1.ServiceAccount)
if !ok {
return false
}
return v1alpha3.DefaultObjectMetaCompare(leftCM.ObjectMeta, rightCM.ObjectMeta, field)
return v1alpha3.DefaultObjectMetaCompare(leftSA.ObjectMeta, rightSA.ObjectMeta, field)
}
func (d *serviceaccountsGetter) filter(object runtime.Object, filter query.Filter) bool {
func (d *serviceAccountsGetter) filter(object runtime.Object, filter query.Filter) bool {
serviceAccount, ok := object.(*corev1.ServiceAccount)
if !ok {
return false

View File

@@ -1,109 +0,0 @@
/*
Copyright 2020 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 serviceaccount
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 TestListServiceAccounts(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{}{foo3, foo2, foo1},
TotalItems: len(serviceAccounts),
},
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.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "foo1",
Namespace: "default",
},
}
foo2 = &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "foo2",
Namespace: "default",
},
}
foo3 = &corev1.ServiceAccount{
ObjectMeta: metav1.ObjectMeta{
Name: "foo3",
Namespace: "default",
},
}
serviceAccounts = []interface{}{foo1, foo2, foo3}
)
func prepare() v1alpha3.Interface {
client := fake.NewSimpleClientset()
informer := informers.NewSharedInformerFactory(client, 0)
for _, serviceAccount := range serviceAccounts {
informer.Core().V1().ServiceAccounts().Informer().GetIndexer().Add(serviceAccount)
}
return New(informer)
}