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,63 +1,60 @@
/*
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 workspacerole
import (
"context"
"encoding/json"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
iamv1alpha2 "kubesphere.io/api/iam/v1alpha2"
tenantv1alpha1 "kubesphere.io/api/tenant/v1alpha1"
iamv1beta1 "kubesphere.io/api/iam/v1beta1"
tenantv1beta1 "kubesphere.io/api/tenant/v1beta1"
"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"
informers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
)
type workspacerolesGetter struct {
sharedInformers informers.SharedInformerFactory
type workspaceRolesGetter struct {
cache runtimeclient.Reader
}
func New(sharedInformers informers.SharedInformerFactory) v1alpha3.Interface {
return &workspacerolesGetter{sharedInformers: sharedInformers}
func New(cache runtimeclient.Reader) v1alpha3.Interface {
return &workspaceRolesGetter{cache: cache}
}
func (d *workspacerolesGetter) Get(_, name string) (runtime.Object, error) {
return d.sharedInformers.Iam().V1alpha2().WorkspaceRoles().Lister().Get(name)
func (d *workspaceRolesGetter) Get(_, name string) (runtime.Object, error) {
workspaceRole := &iamv1beta1.WorkspaceRole{}
return workspaceRole, d.cache.Get(context.Background(), types.NamespacedName{Name: name}, workspaceRole)
}
func (d *workspacerolesGetter) List(_ string, queryParam *query.Query) (*api.ListResult, error) {
var roles []*iamv1alpha2.WorkspaceRole
func (d *workspaceRolesGetter) List(_ string, query *query.Query) (*api.ListResult, error) {
var roles []*iamv1beta1.WorkspaceRole
var err error
if aggregateTo := queryParam.Filters[iamv1alpha2.AggregateTo]; aggregateTo != "" {
if aggregateTo := query.Filters[iamv1beta1.AggregateTo]; aggregateTo != "" {
roles, err = d.fetchAggregationRoles(string(aggregateTo))
delete(queryParam.Filters, iamv1alpha2.AggregateTo)
if err != nil {
return nil, err
}
delete(query.Filters, iamv1beta1.AggregateTo)
} else {
roles, err = d.sharedInformers.Iam().V1alpha2().WorkspaceRoles().Lister().List(queryParam.Selector())
}
if err != nil {
return nil, err
workspaceRoleList := &iamv1beta1.WorkspaceRoleList{}
if err = d.cache.List(context.Background(), workspaceRoleList,
client.MatchingLabelsSelector{Selector: query.Selector()}); err != nil {
return nil, err
}
roles = make([]*iamv1beta1.WorkspaceRole, 0)
for _, item := range workspaceRoleList.Items {
roles = append(roles, item.DeepCopy())
}
}
var result []runtime.Object
@@ -65,17 +62,17 @@ func (d *workspacerolesGetter) List(_ string, queryParam *query.Query) (*api.Lis
result = append(result, role)
}
return v1alpha3.DefaultList(result, queryParam, d.compare, d.filter), nil
return v1alpha3.DefaultList(result, query, d.compare, d.filter), nil
}
func (d *workspacerolesGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
func (d *workspaceRolesGetter) compare(left runtime.Object, right runtime.Object, field query.Field) bool {
leftRole, ok := left.(*iamv1alpha2.WorkspaceRole)
leftRole, ok := left.(*iamv1beta1.WorkspaceRole)
if !ok {
return false
}
rightRole, ok := right.(*iamv1alpha2.WorkspaceRole)
rightRole, ok := right.(*iamv1beta1.WorkspaceRole)
if !ok {
return false
}
@@ -83,24 +80,23 @@ func (d *workspacerolesGetter) compare(left runtime.Object, right runtime.Object
return v1alpha3.DefaultObjectMetaCompare(leftRole.ObjectMeta, rightRole.ObjectMeta, field)
}
func (d *workspacerolesGetter) filter(object runtime.Object, filter query.Filter) bool {
role, ok := object.(*iamv1alpha2.WorkspaceRole)
func (d *workspaceRolesGetter) filter(object runtime.Object, filter query.Filter) bool {
role, ok := object.(*iamv1beta1.WorkspaceRole)
if !ok {
return false
}
switch filter.Field {
case iamv1alpha2.ScopeWorkspace:
return role.Labels[tenantv1alpha1.WorkspaceLabel] == string(filter.Value)
case iamv1beta1.ScopeWorkspace:
return role.Labels[tenantv1beta1.WorkspaceLabel] == string(filter.Value)
default:
return v1alpha3.DefaultObjectMetaFilter(role.ObjectMeta, filter)
}
}
func (d *workspacerolesGetter) fetchAggregationRoles(name string) ([]*iamv1alpha2.WorkspaceRole, error) {
roles := make([]*iamv1alpha2.WorkspaceRole, 0)
func (d *workspaceRolesGetter) fetchAggregationRoles(name string) ([]*iamv1beta1.WorkspaceRole, error) {
roles := make([]*iamv1beta1.WorkspaceRole, 0)
obj, err := d.Get("", name)
@@ -111,7 +107,7 @@ func (d *workspacerolesGetter) fetchAggregationRoles(name string) ([]*iamv1alpha
return nil, err
}
if annotation := obj.(*iamv1alpha2.WorkspaceRole).Annotations[iamv1alpha2.AggregationRolesAnnotation]; annotation != "" {
if annotation := obj.(*iamv1beta1.WorkspaceRole).Annotations[iamv1beta1.AggregationRolesAnnotation]; annotation != "" {
var roleNames []string
if err = json.Unmarshal([]byte(annotation), &roleNames); err == nil {
@@ -126,7 +122,7 @@ func (d *workspacerolesGetter) fetchAggregationRoles(name string) ([]*iamv1alpha
return nil, err
}
roles = append(roles, role.(*iamv1alpha2.WorkspaceRole))
roles = append(roles, role.(*iamv1beta1.WorkspaceRole))
}
}
}

View File

@@ -1,113 +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 workspacerole
import (
"testing"
"github.com/google/go-cmp/cmp"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
iamv1alpha2 "kubesphere.io/api/iam/v1alpha2"
"kubesphere.io/kubesphere/pkg/api"
"kubesphere.io/kubesphere/pkg/apiserver/query"
"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
informers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
"kubesphere.io/kubesphere/pkg/models/resources/v1alpha3"
)
func TestListWorkspaceRoles(t *testing.T) {
tests := []struct {
description string
namespace string
query *query.Query
expected *api.ListResult
expectedErr error
}{
{
"test name filter",
"bar",
&query.Query{
Pagination: &query.Pagination{
Limit: 1,
Offset: 0,
},
SortBy: query.FieldName,
Ascending: false,
Filters: map[query.Field]query.Value{query.FieldName: query.Value("foo2")},
},
&api.ListResult{
Items: []interface{}{
foo2,
},
TotalItems: 1,
},
nil,
},
}
getter := prepare()
for _, test := range tests {
t.Run(test.description, func(t *testing.T) {
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 = &iamv1alpha2.WorkspaceRole{
ObjectMeta: metav1.ObjectMeta{
Name: "foo1",
},
}
foo2 = &iamv1alpha2.WorkspaceRole{
ObjectMeta: metav1.ObjectMeta{
Name: "foo2",
},
}
bar1 = &iamv1alpha2.WorkspaceRole{
ObjectMeta: metav1.ObjectMeta{
Name: "bar1",
},
}
roles = []interface{}{foo1, foo2, bar1}
)
func prepare() v1alpha3.Interface {
client := fake.NewSimpleClientset()
informer := informers.NewSharedInformerFactory(client, 0)
for _, role := range roles {
informer.Iam().V1alpha2().WorkspaceRoles().Informer().GetIndexer().Add(role)
}
return New(informer)
}