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:
committed by
GitHub
parent
b5015ec7b9
commit
447a51f08b
@@ -1,344 +1,134 @@
|
||||
/*
|
||||
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 group
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"reflect"
|
||||
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
|
||||
corev1 "k8s.io/api/core/v1"
|
||||
"k8s.io/apimachinery/pkg/api/errors"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
rbacv1 "k8s.io/api/rbac/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
|
||||
"k8s.io/client-go/kubernetes"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
typedcorev1 "k8s.io/client-go/kubernetes/typed/core/v1"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/apimachinery/pkg/util/validation"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"k8s.io/client-go/util/workqueue"
|
||||
"k8s.io/klog/v2"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
iamv1beta1 "kubesphere.io/api/iam/v1beta1"
|
||||
ctrl "sigs.k8s.io/controller-runtime"
|
||||
"sigs.k8s.io/controller-runtime/pkg/builder"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/predicate"
|
||||
|
||||
iam1alpha2 "kubesphere.io/api/iam/v1alpha2"
|
||||
tenantv1alpha2 "kubesphere.io/api/tenant/v1alpha2"
|
||||
fedv1beta1types "kubesphere.io/api/types/v1beta1"
|
||||
|
||||
kubesphere "kubesphere.io/kubesphere/pkg/client/clientset/versioned"
|
||||
iamv1alpha2informers "kubesphere.io/kubesphere/pkg/client/informers/externalversions/iam/v1alpha2"
|
||||
fedv1beta1informers "kubesphere.io/kubesphere/pkg/client/informers/externalversions/types/v1beta1"
|
||||
iamv1alpha1listers "kubesphere.io/kubesphere/pkg/client/listers/iam/v1alpha2"
|
||||
fedv1beta1lister "kubesphere.io/kubesphere/pkg/client/listers/types/v1beta1"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
"kubesphere.io/kubesphere/pkg/controller/utils/controller"
|
||||
"kubesphere.io/kubesphere/pkg/utils/k8sutil"
|
||||
"kubesphere.io/kubesphere/pkg/controller"
|
||||
"kubesphere.io/kubesphere/pkg/utils/sliceutil"
|
||||
)
|
||||
|
||||
const (
|
||||
successSynced = "Synced"
|
||||
messageResourceSynced = "Group synced successfully"
|
||||
controllerName = "group-controller"
|
||||
finalizer = "finalizers.kubesphere.io/groups"
|
||||
controllerName = "group"
|
||||
finalizer = "finalizers.kubesphere.io/groups"
|
||||
)
|
||||
|
||||
type Controller struct {
|
||||
controller.BaseController
|
||||
k8sClient kubernetes.Interface
|
||||
ksClient kubesphere.Interface
|
||||
groupInformer iamv1alpha2informers.GroupInformer
|
||||
groupLister iamv1alpha1listers.GroupLister
|
||||
recorder record.EventRecorder
|
||||
federatedGroupLister fedv1beta1lister.FederatedGroupLister
|
||||
multiClusterEnabled bool
|
||||
type Reconciler struct {
|
||||
client.Client
|
||||
|
||||
recorder record.EventRecorder
|
||||
}
|
||||
|
||||
// NewController creates Group Controller instance
|
||||
func NewController(k8sClient kubernetes.Interface, ksClient kubesphere.Interface, groupInformer iamv1alpha2informers.GroupInformer,
|
||||
federatedGroupInformer fedv1beta1informers.FederatedGroupInformer,
|
||||
multiClusterEnabled bool) *Controller {
|
||||
|
||||
klog.V(4).Info("Creating event broadcaster")
|
||||
eventBroadcaster := record.NewBroadcaster()
|
||||
eventBroadcaster.StartLogging(klog.Infof)
|
||||
eventBroadcaster.StartRecordingToSink(&typedcorev1.EventSinkImpl{Interface: k8sClient.CoreV1().Events("")})
|
||||
ctl := &Controller{
|
||||
BaseController: controller.BaseController{
|
||||
Workqueue: workqueue.NewNamedRateLimitingQueue(workqueue.DefaultControllerRateLimiter(), "Group"),
|
||||
Synced: []cache.InformerSynced{groupInformer.Informer().HasSynced},
|
||||
Name: controllerName,
|
||||
},
|
||||
recorder: eventBroadcaster.NewRecorder(scheme.Scheme, corev1.EventSource{Component: controllerName}),
|
||||
k8sClient: k8sClient,
|
||||
ksClient: ksClient,
|
||||
groupInformer: groupInformer,
|
||||
groupLister: groupInformer.Lister(),
|
||||
multiClusterEnabled: multiClusterEnabled,
|
||||
}
|
||||
|
||||
if ctl.multiClusterEnabled {
|
||||
ctl.federatedGroupLister = federatedGroupInformer.Lister()
|
||||
ctl.Synced = append(ctl.Synced, federatedGroupInformer.Informer().HasSynced)
|
||||
}
|
||||
|
||||
ctl.Handler = ctl.reconcile
|
||||
|
||||
klog.Info("Setting up event handlers")
|
||||
groupInformer.Informer().AddEventHandler(cache.ResourceEventHandlerFuncs{
|
||||
AddFunc: ctl.Enqueue,
|
||||
UpdateFunc: func(old, new interface{}) {
|
||||
ctl.Enqueue(new)
|
||||
},
|
||||
DeleteFunc: ctl.Enqueue,
|
||||
})
|
||||
return ctl
|
||||
func (r *Reconciler) SetupWithManager(mgr ctrl.Manager) error {
|
||||
r.recorder = mgr.GetEventRecorderFor(controllerName)
|
||||
r.Client = mgr.GetClient()
|
||||
return builder.
|
||||
ControllerManagedBy(mgr).
|
||||
For(
|
||||
&iamv1beta1.Group{},
|
||||
builder.WithPredicates(
|
||||
predicate.ResourceVersionChangedPredicate{},
|
||||
),
|
||||
).
|
||||
Named(controllerName).
|
||||
Complete(r)
|
||||
}
|
||||
|
||||
func (c *Controller) Start(ctx context.Context) error {
|
||||
return c.Run(1, ctx.Done())
|
||||
}
|
||||
|
||||
// reconcile handles Group informer events, clear up related reource when group is being deleted.
|
||||
func (c *Controller) reconcile(key string) error {
|
||||
|
||||
group, err := c.groupLister.Get(key)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
utilruntime.HandleError(fmt.Errorf("group '%s' in work queue no longer exists", key))
|
||||
return nil
|
||||
}
|
||||
klog.Error(err)
|
||||
return err
|
||||
func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
|
||||
group := &iamv1beta1.Group{}
|
||||
if err := r.Get(ctx, req.NamespacedName, group); err != nil {
|
||||
return ctrl.Result{}, client.IgnoreNotFound(err)
|
||||
}
|
||||
|
||||
if group.ObjectMeta.DeletionTimestamp.IsZero() {
|
||||
var g *iam1alpha2.Group
|
||||
var g *iamv1beta1.Group
|
||||
if !sliceutil.HasString(group.Finalizers, finalizer) {
|
||||
g = group.DeepCopy()
|
||||
g.ObjectMeta.Finalizers = append(g.ObjectMeta.Finalizers, finalizer)
|
||||
}
|
||||
|
||||
if c.multiClusterEnabled {
|
||||
// Ensure not controlled by Kubefed
|
||||
if group.Labels == nil || group.Labels[constants.KubefedManagedLabel] != "false" {
|
||||
if g == nil {
|
||||
g = group.DeepCopy()
|
||||
}
|
||||
if g.Labels == nil {
|
||||
g.Labels = make(map[string]string, 0)
|
||||
}
|
||||
g.Labels[constants.KubefedManagedLabel] = "false"
|
||||
}
|
||||
}
|
||||
|
||||
// Set OwnerReferences when the group has a parent or Workspace. And it's not owned by kubefed
|
||||
if group.Labels != nil && group.Labels[constants.KubefedManagedLabel] != "true" {
|
||||
if parent, ok := group.Labels[iam1alpha2.GroupParent]; ok {
|
||||
// If the Group is owned by a Parent
|
||||
if !k8sutil.IsControlledBy(group.OwnerReferences, "Group", parent) {
|
||||
if g == nil {
|
||||
g = group.DeepCopy()
|
||||
}
|
||||
groupParent, err := c.groupLister.Get(parent)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
utilruntime.HandleError(fmt.Errorf("Parent group '%s' no longer exists", key))
|
||||
delete(g.Labels, iam1alpha2.GroupParent)
|
||||
} else {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if err := controllerutil.SetControllerReference(groupParent, g, scheme.Scheme); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if ws, ok := group.Labels[constants.WorkspaceLabelKey]; ok {
|
||||
// If the Group is owned by a Workspace
|
||||
if !k8sutil.IsControlledBy(group.OwnerReferences, tenantv1alpha2.ResourceKindWorkspaceTemplate, ws) {
|
||||
workspace, err := c.ksClient.TenantV1alpha2().WorkspaceTemplates().Get(context.Background(), ws, metav1.GetOptions{})
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
utilruntime.HandleError(fmt.Errorf("Workspace '%s' no longer exists", ws))
|
||||
} else {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
} else {
|
||||
if g == nil {
|
||||
g = group.DeepCopy()
|
||||
}
|
||||
g.OwnerReferences = k8sutil.RemoveWorkspaceOwnerReference(g.OwnerReferences)
|
||||
if err := controllerutil.SetControllerReference(workspace, g, scheme.Scheme); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if g != nil {
|
||||
if _, err = c.ksClient.IamV1alpha2().Groups().Update(context.Background(), g, metav1.UpdateOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
// Skip reconcile when group is updated.
|
||||
return nil
|
||||
return ctrl.Result{}, r.Update(ctx, g)
|
||||
}
|
||||
} else {
|
||||
// The object is being deleted
|
||||
if sliceutil.HasString(group.ObjectMeta.Finalizers, finalizer) {
|
||||
if err = c.deleteGroupBindings(group); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
if err := r.deleteGroupBindings(ctx, group); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
if err = c.deleteRoleBindings(group); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
if err := r.deleteRoleBindings(ctx, group); err != nil {
|
||||
return ctrl.Result{}, err
|
||||
}
|
||||
|
||||
group.Finalizers = sliceutil.RemoveString(group.ObjectMeta.Finalizers, func(item string) bool {
|
||||
return item == finalizer
|
||||
})
|
||||
|
||||
if _, err = c.ksClient.IamV1alpha2().Groups().Update(context.Background(), group, metav1.UpdateOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
return ctrl.Result{}, r.Update(ctx, group)
|
||||
}
|
||||
return nil
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
// synchronization through kubefed-controller when multi cluster is enabled
|
||||
if c.multiClusterEnabled {
|
||||
if err = c.multiClusterSync(group); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
// TODO: sync logic needs to be updated and no longer relies on KubeFed, it needs to be synchronized manually.
|
||||
|
||||
c.recorder.Event(group, corev1.EventTypeNormal, successSynced, messageResourceSynced)
|
||||
return nil
|
||||
r.recorder.Event(group, corev1.EventTypeNormal, controller.Synced, controller.MessageResourceSynced)
|
||||
return ctrl.Result{}, nil
|
||||
}
|
||||
|
||||
func (c *Controller) deleteGroupBindings(group *iam1alpha2.Group) error {
|
||||
func (r *Reconciler) deleteGroupBindings(ctx context.Context, group *iamv1beta1.Group) error {
|
||||
if len(group.Name) > validation.LabelValueMaxLength {
|
||||
// ignore invalid label value error
|
||||
return nil
|
||||
}
|
||||
// Groupbindings that created by kubesphere will be deleted directly.
|
||||
listOptions := metav1.ListOptions{
|
||||
LabelSelector: labels.SelectorFromValidatedSet(labels.Set{iam1alpha2.GroupReferenceLabel: group.Name}).String(),
|
||||
}
|
||||
if err := c.ksClient.IamV1alpha2().GroupBindings().
|
||||
DeleteCollection(context.Background(), *metav1.NewDeleteOptions(0), listOptions); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
|
||||
// Group bindings that created by kubesphere will be deleted directly.
|
||||
return r.DeleteAllOf(ctx, &iamv1beta1.GroupBinding{}, client.GracePeriodSeconds(0), client.MatchingLabelsSelector{
|
||||
Selector: labels.SelectorFromValidatedSet(labels.Set{iamv1beta1.GroupReferenceLabel: group.Name}),
|
||||
})
|
||||
}
|
||||
|
||||
// remove all RoleBindings.
|
||||
func (c *Controller) deleteRoleBindings(group *iam1alpha2.Group) error {
|
||||
func (r *Reconciler) deleteRoleBindings(ctx context.Context, group *iamv1beta1.Group) error {
|
||||
if len(group.Name) > validation.LabelValueMaxLength {
|
||||
// ignore invalid label value error
|
||||
return nil
|
||||
}
|
||||
listOptions := metav1.ListOptions{
|
||||
LabelSelector: labels.SelectorFromValidatedSet(labels.Set{iam1alpha2.GroupReferenceLabel: group.Name}).String(),
|
||||
}
|
||||
deleteOptions := *metav1.NewDeleteOptions(0)
|
||||
|
||||
if err := c.ksClient.IamV1alpha2().WorkspaceRoleBindings().
|
||||
DeleteCollection(context.Background(), deleteOptions, listOptions); err != nil {
|
||||
klog.Error(err)
|
||||
selector := labels.SelectorFromValidatedSet(labels.Set{iamv1beta1.GroupReferenceLabel: group.Name})
|
||||
deleteOption := client.GracePeriodSeconds(0)
|
||||
|
||||
if err := r.DeleteAllOf(ctx, &iamv1beta1.WorkspaceRoleBinding{}, deleteOption, client.MatchingLabelsSelector{Selector: selector}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if err := c.k8sClient.RbacV1().ClusterRoleBindings().
|
||||
DeleteCollection(context.Background(), deleteOptions, listOptions); err != nil {
|
||||
klog.Error(err)
|
||||
if err := r.DeleteAllOf(ctx, &rbacv1.ClusterRoleBinding{}, deleteOption, client.MatchingLabelsSelector{Selector: selector}); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if result, err := c.k8sClient.CoreV1().Namespaces().List(context.Background(), metav1.ListOptions{}); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
} else {
|
||||
for _, namespace := range result.Items {
|
||||
if err = c.k8sClient.RbacV1().RoleBindings(namespace.Name).DeleteCollection(context.Background(), deleteOptions, listOptions); err != nil {
|
||||
klog.Error(err)
|
||||
return err
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) multiClusterSync(group *iam1alpha2.Group) error {
|
||||
|
||||
obj, err := c.federatedGroupLister.Get(group.Name)
|
||||
if err != nil {
|
||||
if errors.IsNotFound(err) {
|
||||
return c.createFederatedGroup(group)
|
||||
}
|
||||
klog.Error(err)
|
||||
namespaces := &corev1.NamespaceList{}
|
||||
if err := r.List(ctx, namespaces); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !reflect.DeepEqual(obj.Spec.Template.Labels, group.Labels) {
|
||||
|
||||
obj.Spec.Template.Labels = group.Labels
|
||||
|
||||
if _, err = c.ksClient.TypesV1beta1().FederatedGroups().Update(context.Background(), obj, metav1.UpdateOptions{}); err != nil {
|
||||
for _, namespace := range namespaces.Items {
|
||||
if err := r.DeleteAllOf(ctx, &rbacv1.RoleBinding{}, deleteOption, client.MatchingLabelsSelector{Selector: selector}, client.InNamespace(namespace.Name)); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Controller) createFederatedGroup(group *iam1alpha2.Group) error {
|
||||
federatedGroup := &fedv1beta1types.FederatedGroup{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: group.Name,
|
||||
},
|
||||
Spec: fedv1beta1types.FederatedGroupSpec{
|
||||
Template: fedv1beta1types.GroupTemplate{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: group.Labels,
|
||||
},
|
||||
Spec: group.Spec,
|
||||
},
|
||||
Placement: fedv1beta1types.GenericPlacementFields{
|
||||
ClusterSelector: &metav1.LabelSelector{},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
// must bind group lifecycle
|
||||
err := controllerutil.SetControllerReference(group, federatedGroup, scheme.Scheme)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if _, err = c.ksClient.TypesV1beta1().FederatedGroups().Create(context.Background(), federatedGroup, metav1.CreateOptions{}); err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -1,454 +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 group
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/labels"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apimachinery/pkg/util/diff"
|
||||
kubeinformers "k8s.io/client-go/informers"
|
||||
k8sfake "k8s.io/client-go/kubernetes/fake"
|
||||
"k8s.io/client-go/kubernetes/scheme"
|
||||
core "k8s.io/client-go/testing"
|
||||
"k8s.io/client-go/tools/cache"
|
||||
"k8s.io/client-go/tools/record"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
|
||||
v1alpha2 "kubesphere.io/api/iam/v1alpha2"
|
||||
tenantv1alpha2 "kubesphere.io/api/tenant/v1alpha2"
|
||||
fedv1beta1types "kubesphere.io/api/types/v1beta1"
|
||||
|
||||
"kubesphere.io/kubesphere/pkg/client/clientset/versioned/fake"
|
||||
ksinformers "kubesphere.io/kubesphere/pkg/client/informers/externalversions"
|
||||
"kubesphere.io/kubesphere/pkg/constants"
|
||||
)
|
||||
|
||||
var (
|
||||
noResyncPeriodFunc = func() time.Duration { return 0 }
|
||||
)
|
||||
|
||||
func init() {
|
||||
v1alpha2.AddToScheme(scheme.Scheme)
|
||||
tenantv1alpha2.AddToScheme(scheme.Scheme)
|
||||
}
|
||||
|
||||
type fixture struct {
|
||||
t *testing.T
|
||||
|
||||
ksclient *fake.Clientset
|
||||
k8sclient *k8sfake.Clientset
|
||||
// Objects to put in the store.
|
||||
groupLister []*v1alpha2.Group
|
||||
fedgroupLister []*fedv1beta1types.FederatedGroup
|
||||
// Actions expected to happen on the client.
|
||||
kubeactions []core.Action
|
||||
actions []core.Action
|
||||
// Objects from here preloaded into NewSimpleFake.
|
||||
kubeobjects []runtime.Object
|
||||
objects []runtime.Object
|
||||
}
|
||||
|
||||
func newFixture(t *testing.T) *fixture {
|
||||
f := &fixture{}
|
||||
f.t = t
|
||||
f.objects = []runtime.Object{}
|
||||
f.kubeobjects = []runtime.Object{}
|
||||
return f
|
||||
}
|
||||
|
||||
func newGroup(name string) *v1alpha2.Group {
|
||||
return &v1alpha2.Group{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: v1alpha2.SchemeGroupVersion.String(), Kind: "Group"},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
},
|
||||
Spec: v1alpha2.GroupSpec{},
|
||||
}
|
||||
}
|
||||
|
||||
func newUnmanagedGroup(name string) *v1alpha2.Group {
|
||||
return &v1alpha2.Group{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: v1alpha2.SchemeGroupVersion.String(), Kind: "Group"},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: name,
|
||||
Labels: map[string]string{constants.KubefedManagedLabel: "false"},
|
||||
Finalizers: []string{"finalizers.kubesphere.io/groups"},
|
||||
},
|
||||
Spec: v1alpha2.GroupSpec{},
|
||||
}
|
||||
}
|
||||
|
||||
func newFederatedGroup(group *v1alpha2.Group) *fedv1beta1types.FederatedGroup {
|
||||
return &fedv1beta1types.FederatedGroup{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: group.Name,
|
||||
},
|
||||
Spec: fedv1beta1types.FederatedGroupSpec{
|
||||
Template: fedv1beta1types.GroupTemplate{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Labels: group.Labels,
|
||||
},
|
||||
Spec: group.Spec,
|
||||
},
|
||||
Placement: fedv1beta1types.GenericPlacementFields{
|
||||
ClusterSelector: &metav1.LabelSelector{},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
func (f *fixture) newController() (*Controller, ksinformers.SharedInformerFactory, kubeinformers.SharedInformerFactory) {
|
||||
f.ksclient = fake.NewSimpleClientset(f.objects...)
|
||||
f.k8sclient = k8sfake.NewSimpleClientset(f.kubeobjects...)
|
||||
|
||||
ksinformers := ksinformers.NewSharedInformerFactory(f.ksclient, noResyncPeriodFunc())
|
||||
k8sinformers := kubeinformers.NewSharedInformerFactory(f.k8sclient, noResyncPeriodFunc())
|
||||
|
||||
for _, group := range f.groupLister {
|
||||
err := ksinformers.Iam().V1alpha2().Groups().Informer().GetIndexer().Add(group)
|
||||
if err != nil {
|
||||
f.t.Errorf("add group:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
for _, group := range f.fedgroupLister {
|
||||
err := ksinformers.Types().V1beta1().FederatedGroups().Informer().GetIndexer().Add(group)
|
||||
if err != nil {
|
||||
f.t.Errorf("add federated group:%s", err)
|
||||
}
|
||||
}
|
||||
|
||||
c := NewController(f.k8sclient, f.ksclient,
|
||||
ksinformers.Iam().V1alpha2().Groups(),
|
||||
ksinformers.Types().V1beta1().FederatedGroups(), true)
|
||||
c.recorder = &record.FakeRecorder{}
|
||||
|
||||
return c, ksinformers, k8sinformers
|
||||
}
|
||||
|
||||
func (f *fixture) run(userName string) {
|
||||
f.runController(userName, true, false)
|
||||
}
|
||||
|
||||
//nolint:unused
|
||||
func (f *fixture) runExpectError(userName string) {
|
||||
f.runController(userName, true, true)
|
||||
}
|
||||
|
||||
func (f *fixture) runController(group string, startInformers bool, expectError bool) {
|
||||
c, i, k8sI := f.newController()
|
||||
if startInformers {
|
||||
stopCh := make(chan struct{})
|
||||
defer close(stopCh)
|
||||
i.Start(stopCh)
|
||||
k8sI.Start(stopCh)
|
||||
}
|
||||
|
||||
err := c.Handler(group)
|
||||
if !expectError && err != nil {
|
||||
f.t.Errorf("error syncing group: %v", err)
|
||||
} else if expectError && err == nil {
|
||||
f.t.Error("expected error syncing group, got nil")
|
||||
}
|
||||
|
||||
actions := filterInformerActions(f.ksclient.Actions())
|
||||
for i, action := range actions {
|
||||
if len(f.actions) < i+1 {
|
||||
f.t.Errorf("%d unexpected actions: %+v", len(actions)-len(f.actions), actions[i:])
|
||||
break
|
||||
}
|
||||
|
||||
expectedAction := f.actions[i]
|
||||
checkAction(expectedAction, action, f.t)
|
||||
}
|
||||
|
||||
if len(f.actions) > len(actions) {
|
||||
f.t.Errorf("%d additional expected actions:%+v", len(f.actions)-len(actions), f.actions[len(actions):])
|
||||
}
|
||||
|
||||
k8sActions := filterInformerActions(f.k8sclient.Actions())
|
||||
for i, action := range k8sActions {
|
||||
if len(f.kubeactions) < i+1 {
|
||||
f.t.Errorf("%d unexpected actions: %+v", len(k8sActions)-len(f.kubeactions), k8sActions[i:])
|
||||
break
|
||||
}
|
||||
|
||||
expectedAction := f.kubeactions[i]
|
||||
checkAction(expectedAction, action, f.t)
|
||||
}
|
||||
|
||||
if len(f.kubeactions) > len(k8sActions) {
|
||||
f.t.Errorf("%d additional expected actions:%+v", len(f.kubeactions)-len(k8sActions), f.kubeactions[len(k8sActions):])
|
||||
}
|
||||
}
|
||||
|
||||
// checkAction verifies that expected and actual actions are equal and both have
|
||||
// same attached resources
|
||||
func checkAction(expected, actual core.Action, t *testing.T) {
|
||||
if !(expected.Matches(actual.GetVerb(), actual.GetResource().Resource) && actual.GetSubresource() == expected.GetSubresource()) {
|
||||
t.Errorf("Expected\n\t%#v\ngot\n\t%#v", expected, actual)
|
||||
return
|
||||
}
|
||||
|
||||
if reflect.TypeOf(actual) != reflect.TypeOf(expected) {
|
||||
t.Errorf("Action has wrong type. Expected: %t. Got: %t", expected, actual)
|
||||
return
|
||||
}
|
||||
|
||||
switch a := actual.(type) {
|
||||
case core.CreateActionImpl:
|
||||
e, _ := expected.(core.CreateActionImpl)
|
||||
expObject := e.GetObject()
|
||||
object := a.GetObject()
|
||||
|
||||
if !reflect.DeepEqual(expObject, object) {
|
||||
t.Errorf("Action %s %s has wrong object\nDiff:\n %s",
|
||||
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(expObject, object))
|
||||
}
|
||||
case core.UpdateActionImpl:
|
||||
e, _ := expected.(core.UpdateActionImpl)
|
||||
expObject := e.GetObject()
|
||||
object := a.GetObject()
|
||||
|
||||
if !reflect.DeepEqual(expObject, object) {
|
||||
t.Errorf("Action %s %s has wrong object\nDiff:\n %s",
|
||||
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(expObject, object))
|
||||
}
|
||||
case core.PatchActionImpl:
|
||||
e, _ := expected.(core.PatchActionImpl)
|
||||
expPatch := e.GetPatch()
|
||||
patch := a.GetPatch()
|
||||
|
||||
if !reflect.DeepEqual(expPatch, patch) {
|
||||
t.Errorf("Action %s %s has wrong patch\nDiff:\n %s",
|
||||
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(expPatch, patch))
|
||||
}
|
||||
case core.DeleteCollectionActionImpl:
|
||||
e, _ := expected.(core.DeleteCollectionActionImpl)
|
||||
exp := e.GetListRestrictions()
|
||||
target := a.GetListRestrictions()
|
||||
if !reflect.DeepEqual(exp, target) {
|
||||
t.Errorf("Action %s %s has wrong Query\nDiff:\n %s",
|
||||
a.GetVerb(), a.GetResource().Resource, diff.ObjectGoPrintSideBySide(exp, target))
|
||||
}
|
||||
|
||||
default:
|
||||
t.Errorf("Uncaptured Action %s %s, you should explicitly add a case to capture it",
|
||||
actual.GetVerb(), actual.GetResource().Resource)
|
||||
}
|
||||
}
|
||||
|
||||
// filterInformerActions filters list and watch actions for testing resources.
|
||||
// Since list and watch don't change resource state we can filter it to lower
|
||||
// nose level in our tests.
|
||||
func filterInformerActions(actions []core.Action) []core.Action {
|
||||
var ret []core.Action
|
||||
for _, action := range actions {
|
||||
if len(action.GetNamespace()) == 0 &&
|
||||
(action.Matches("list", "groups") ||
|
||||
action.Matches("watch", "groups") ||
|
||||
action.Matches("list", "groups") ||
|
||||
action.Matches("list", "namespaces") ||
|
||||
action.Matches("get", "workspacetemplates") ||
|
||||
action.Matches("list", "federatedgroups") ||
|
||||
action.Matches("watch", "federatedgroups")) {
|
||||
continue
|
||||
}
|
||||
ret = append(ret, action)
|
||||
}
|
||||
|
||||
return ret
|
||||
}
|
||||
|
||||
func (f *fixture) expectUpdateGroupsFinalizerAction(group *v1alpha2.Group) {
|
||||
expect := group.DeepCopy()
|
||||
if expect.Labels == nil {
|
||||
expect.Labels = make(map[string]string, 0)
|
||||
}
|
||||
expect.Finalizers = []string{"finalizers.kubesphere.io/groups"}
|
||||
expect.Labels[constants.KubefedManagedLabel] = "false"
|
||||
action := core.NewUpdateAction(schema.GroupVersionResource{Resource: "groups"}, "", expect)
|
||||
f.actions = append(f.actions, action)
|
||||
}
|
||||
|
||||
func (f *fixture) expectUpdateWorkspaceRefAction(child *v1alpha2.Group, wsp *tenantv1alpha2.WorkspaceTemplate) {
|
||||
expect := child.DeepCopy()
|
||||
if expect.Labels == nil {
|
||||
expect.Labels = make(map[string]string, 0)
|
||||
}
|
||||
|
||||
controllerutil.SetControllerReference(wsp, expect, scheme.Scheme)
|
||||
|
||||
expect.Finalizers = []string{"finalizers.kubesphere.io/groups"}
|
||||
expect.Labels[constants.KubefedManagedLabel] = "false"
|
||||
updateAction := core.NewUpdateAction(schema.GroupVersionResource{Resource: "groups"}, "", expect)
|
||||
f.actions = append(f.actions, updateAction)
|
||||
}
|
||||
|
||||
func (f *fixture) expectUpdateParentsRefAction(parent, child *v1alpha2.Group) {
|
||||
expect := child.DeepCopy()
|
||||
if expect.Labels == nil {
|
||||
expect.Labels = make(map[string]string, 0)
|
||||
}
|
||||
|
||||
controllerutil.SetControllerReference(parent, expect, scheme.Scheme)
|
||||
|
||||
expect.Finalizers = []string{"finalizers.kubesphere.io/groups"}
|
||||
expect.Labels[constants.KubefedManagedLabel] = "false"
|
||||
action := core.NewUpdateAction(schema.GroupVersionResource{Resource: "groups"}, "", expect)
|
||||
f.actions = append(f.actions, action)
|
||||
}
|
||||
|
||||
func (f *fixture) expectCreateFederatedGroupsAction(group *v1alpha2.Group) {
|
||||
federatedGroup := newFederatedGroup(group)
|
||||
|
||||
controllerutil.SetControllerReference(group, federatedGroup, scheme.Scheme)
|
||||
|
||||
actionCreate := core.NewCreateAction(schema.GroupVersionResource{Resource: "federatedgroups"}, "", federatedGroup)
|
||||
f.actions = append(f.actions, actionCreate)
|
||||
}
|
||||
|
||||
func (f *fixture) expectUpdateFederatedGroupsAction(group *v1alpha2.Group) {
|
||||
g := newFederatedGroup(group)
|
||||
controllerutil.SetControllerReference(group, g, scheme.Scheme)
|
||||
actionCreate := core.NewUpdateAction(schema.GroupVersionResource{Group: "types.kubefed.io", Version: "v1beta1", Resource: "federatedgroups"}, "", g)
|
||||
f.actions = append(f.actions, actionCreate)
|
||||
}
|
||||
|
||||
func (f *fixture) expectUpdateGroupsDeleteAction(group *v1alpha2.Group) {
|
||||
expect := group.DeepCopy()
|
||||
expect.Finalizers = []string{}
|
||||
listOptions := metav1.ListOptions{
|
||||
LabelSelector: labels.SelectorFromSet(labels.Set{v1alpha2.GroupReferenceLabel: group.Name}).String(),
|
||||
}
|
||||
|
||||
actionDelete := core.NewDeleteCollectionAction(schema.GroupVersionResource{Resource: "groupbindings"}, "", listOptions)
|
||||
f.actions = append(f.actions, actionDelete)
|
||||
|
||||
actionDelete = core.NewDeleteCollectionAction(schema.GroupVersionResource{Group: "rbac.authorization.k8s.io", Version: "v1", Resource: "clusterrolebindings"}, "", listOptions)
|
||||
f.kubeactions = append(f.kubeactions, actionDelete)
|
||||
|
||||
actionDelete = core.NewDeleteCollectionAction(schema.GroupVersionResource{Resource: "workspacerolebindings"}, "", listOptions)
|
||||
f.actions = append(f.actions, actionDelete)
|
||||
|
||||
action := core.NewUpdateAction(schema.GroupVersionResource{Resource: "groups"}, "", expect)
|
||||
f.actions = append(f.actions, action)
|
||||
}
|
||||
|
||||
func getKey(group *v1alpha2.Group, t *testing.T) string {
|
||||
key, err := cache.DeletionHandlingMetaNamespaceKeyFunc(group)
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error getting key for group %v: %v", group.Name, err)
|
||||
return ""
|
||||
}
|
||||
return key
|
||||
}
|
||||
|
||||
func TestDeletesGroup(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
deletedGroup := newUnmanagedGroup("test")
|
||||
|
||||
now := metav1.Now()
|
||||
deletedGroup.ObjectMeta.DeletionTimestamp = &now
|
||||
|
||||
f.groupLister = append(f.groupLister, deletedGroup)
|
||||
f.objects = append(f.objects, deletedGroup)
|
||||
f.expectUpdateGroupsDeleteAction(deletedGroup)
|
||||
f.run(getKey(deletedGroup, t))
|
||||
}
|
||||
|
||||
func TestDoNothing(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
group := newGroup("test")
|
||||
|
||||
f.groupLister = append(f.groupLister, group)
|
||||
f.objects = append(f.objects, group)
|
||||
|
||||
f.expectUpdateGroupsFinalizerAction(group)
|
||||
f.run(getKey(group, t))
|
||||
}
|
||||
|
||||
func TestGroupCreateWithParent(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
parent := newGroup("parent")
|
||||
child := newGroup("child")
|
||||
child.Labels = map[string]string{v1alpha2.GroupParent: "parent"}
|
||||
|
||||
f.groupLister = append(f.groupLister, parent, child)
|
||||
f.objects = append(f.objects, parent, child)
|
||||
|
||||
f.expectUpdateParentsRefAction(parent, child)
|
||||
f.run(getKey(child, t))
|
||||
}
|
||||
|
||||
func TestGroupCreateWithWorkspace(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
child := newGroup("child")
|
||||
child.Labels = map[string]string{constants.WorkspaceLabelKey: "wsp"}
|
||||
|
||||
f.groupLister = append(f.groupLister, child)
|
||||
f.objects = append(f.objects, child)
|
||||
|
||||
wsp := tenantv1alpha2.WorkspaceTemplate{
|
||||
TypeMeta: metav1.TypeMeta{APIVersion: tenantv1alpha2.SchemeGroupVersion.String(), Kind: tenantv1alpha2.ResourceKindWorkspaceTemplate},
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "wsp",
|
||||
},
|
||||
}
|
||||
f.objects = append(f.objects, &wsp)
|
||||
|
||||
f.expectUpdateWorkspaceRefAction(child, &wsp)
|
||||
f.run(getKey(child, t))
|
||||
}
|
||||
|
||||
func TestFederetedGroupCreate(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
|
||||
group := newUnmanagedGroup("test")
|
||||
|
||||
f.groupLister = append(f.groupLister, group)
|
||||
f.objects = append(f.objects, group)
|
||||
|
||||
f.expectCreateFederatedGroupsAction(group)
|
||||
f.run(getKey(group, t))
|
||||
}
|
||||
|
||||
func TestFederetedGroupUpdate(t *testing.T) {
|
||||
f := newFixture(t)
|
||||
|
||||
group := newUnmanagedGroup("test")
|
||||
|
||||
federatedGroup := newFederatedGroup(group.DeepCopy())
|
||||
controllerutil.SetControllerReference(group, federatedGroup, scheme.Scheme)
|
||||
|
||||
f.fedgroupLister = append(f.fedgroupLister, federatedGroup)
|
||||
f.objects = append(f.objects, federatedGroup)
|
||||
|
||||
group.Labels["foo"] = "bar"
|
||||
f.groupLister = append(f.groupLister, group)
|
||||
f.objects = append(f.objects, group)
|
||||
|
||||
f.expectUpdateFederatedGroupsAction(group.DeepCopy())
|
||||
f.run(getKey(group, t))
|
||||
}
|
||||
Reference in New Issue
Block a user