Skip to content

Commit

Permalink
upgrade clusterset to v1beta2 (stolostron#537)
Browse files Browse the repository at this point in the history
Signed-off-by: ldpliu <[email protected]>

Signed-off-by: ldpliu <[email protected]>
  • Loading branch information
ldpliu authored Nov 15, 2022
1 parent 6d0c91a commit 7c55e8f
Show file tree
Hide file tree
Showing 53 changed files with 637 additions and 275 deletions.
7 changes: 5 additions & 2 deletions cmd/controller/app/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ import (
clusterv1 "open-cluster-management.io/api/cluster/v1"
clusterv1alaph1 "open-cluster-management.io/api/cluster/v1alpha1"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"

ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/healthz"
)
Expand All @@ -66,6 +68,7 @@ func init() {
_ = actionv1beta1.AddToScheme(scheme)
_ = clusterv1alaph1.Install(scheme)
_ = clusterv1beta1.Install(scheme)
_ = clusterv1beta2.Install(scheme)
_ = v1alpha1.AddToScheme(scheme)
_ = routev1.Install(scheme)
}
Expand Down Expand Up @@ -154,13 +157,13 @@ func Run(o *options.ControllerRunOptions, ctx context.Context) error {
}

clusterSetAdminCache := cache.NewClusterSetCache(
clusterInformers.Cluster().V1beta1().ManagedClusterSets(),
clusterInformers.Cluster().V1beta2().ManagedClusterSets(),
kubeInfomers.Rbac().V1().ClusterRoles(),
kubeInfomers.Rbac().V1().ClusterRoleBindings(),
utils.GetAdminResourceFromClusterRole,
)
clusterSetViewCache := cache.NewClusterSetCache(
clusterInformers.Cluster().V1beta1().ManagedClusterSets(),
clusterInformers.Cluster().V1beta2().ManagedClusterSets(),
kubeInfomers.Rbac().V1().ClusterRoles(),
kubeInfomers.Rbac().V1().ClusterRoleBindings(),
utils.GetViewResourceFromClusterRole,
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ require (
k8s.io/kube-aggregator v0.24.1
k8s.io/kube-openapi v0.0.0-20220803162953-67bda5d908f1
open-cluster-management.io/addon-framework v0.5.0
open-cluster-management.io/api v0.9.0
open-cluster-management.io/api v0.9.1-0.20221107101616-fde10e6996f6
sigs.k8s.io/controller-runtime v0.12.1
sigs.k8s.io/yaml v1.3.0
)
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1011,8 +1011,8 @@ k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed h1:jAne/RjBTyawwAy0utX5eqigAwz/l
k8s.io/utils v0.0.0-20220728103510-ee6ede2d64ed/go.mod h1:jPW/WVKK9YHAvNhRxK0md/EJ228hCsBRufyofKtW8HA=
open-cluster-management.io/addon-framework v0.5.0 h1:JsjzbS7gRvTbjxJdYGLJzJUO99zLQuOkyXL1fppcd5s=
open-cluster-management.io/addon-framework v0.5.0/go.mod h1:Fymctw1tnmCTXnmAMgc0zdHetAw6UaiAsj1S6w5VW6s=
open-cluster-management.io/api v0.9.0 h1:JQidCTMY3RdS0m3UDP24j6ykNpxWJ20LEdNtbxJApxo=
open-cluster-management.io/api v0.9.0/go.mod h1:+OEARSAl2jIhuLItUcS30UgLA3khmA9ihygLVxzEn+U=
open-cluster-management.io/api v0.9.1-0.20221107101616-fde10e6996f6 h1:jXx0C2GKScyZSxyyvswxmgspQgx7SnBic80EM57EdOw=
open-cluster-management.io/api v0.9.1-0.20221107101616-fde10e6996f6/go.mod h1:9KkJPh/zpDevXj2P+zkvSVjC2pq2PQ1JDNLLEes8TEc=
rsc.io/binaryregexp v0.2.0/go.mod h1:qTv7/COck+e2FymRvadv62gMdZztPaShugOCi3I+8D8=
rsc.io/quote/v3 v3.1.0/go.mod h1:yEA65RcK8LyAZtP9Kv3t0HmxON59tX3rD+tICJqUlj0=
rsc.io/sampler v1.3.0/go.mod h1:T1hPZKmBbMNahiBKFy5HrXp6adAjACjK9JXDnKaTXpA=
Expand Down
2 changes: 1 addition & 1 deletion hack/update-apiserver-gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,6 @@ SC_PKG='github.com/stolostron/multicloud-operators-foundation'
"${BINDIR}"/openapi-gen "$@" \
--v 1 --logtostderr \
--go-header-file "${REPO_ROOT}"/hack/custom-boilerplate.go.txt \
--input-dirs "${SC_PKG}/pkg/proxyserver/apis/proxy/v1beta1,${SC_PKG}/pkg/proxyserver/apis/clusterview/v1,${SC_PKG}/pkg/proxyserver/apis/clusterview/v1alpha1,open-cluster-management.io/api/cluster/v1,open-cluster-management.io/api/cluster/v1beta1,k8s.io/apimachinery/pkg/api/resource,k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/apis/meta/v1" \
--input-dirs "${SC_PKG}/pkg/proxyserver/apis/proxy/v1beta1,${SC_PKG}/pkg/proxyserver/apis/clusterview/v1,${SC_PKG}/pkg/proxyserver/apis/clusterview/v1alpha1,open-cluster-management.io/api/cluster/v1,open-cluster-management.io/api/cluster/v1beta2,k8s.io/apimachinery/pkg/api/resource,k8s.io/apimachinery/pkg/runtime,k8s.io/apimachinery/pkg/apis/meta/v1" \
--output-package "${SC_PKG}/pkg/proxyserver/apis/openapi" \
--report-filename ".api_violation.report"
18 changes: 9 additions & 9 deletions pkg/cache/managedclusterset.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package cache
import (
"time"

clusterinformerv1beta1 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta1"
clusterv1beta1lister "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterinformerv1beta2 "open-cluster-management.io/api/client/cluster/informers/externalversions/cluster/v1beta2"
clusterv1beta2lister "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta2"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"

v1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -21,15 +21,15 @@ import (
// ClusterSetLister enforces ability to enumerate clusterSet based on role
type ClusterSetLister interface {
// List returns the list of ManagedClusterSet items that the user can access
List(user user.Info, selector labels.Selector) (*clusterv1beta1.ManagedClusterSetList, error)
List(user user.Info, selector labels.Selector) (*clusterv1beta2.ManagedClusterSetList, error)
}

type ClusterSetCache struct {
Cache *AuthCache
clusterSetLister clusterv1beta1lister.ManagedClusterSetLister
clusterSetLister clusterv1beta2lister.ManagedClusterSetLister
}

func NewClusterSetCache(clusterSetInformer clusterinformerv1beta1.ManagedClusterSetInformer,
func NewClusterSetCache(clusterSetInformer clusterinformerv1beta2.ManagedClusterSetInformer,
clusterRoleInformer rbacv1informers.ClusterRoleInformer,
clusterRolebindingInformer rbacv1informers.ClusterRoleBindingInformer,
getResourceNamesFromClusterRole func(*v1.ClusterRole, string, string) (sets.String, bool),
Expand Down Expand Up @@ -64,10 +64,10 @@ func (c *ClusterSetCache) Run(period time.Duration) {
go utilwait.Forever(func() { c.Cache.synchronize() }, period)
}

func (c *ClusterSetCache) List(userInfo user.Info, selector labels.Selector) (*clusterv1beta1.ManagedClusterSetList, error) {
func (c *ClusterSetCache) List(userInfo user.Info, selector labels.Selector) (*clusterv1beta2.ManagedClusterSetList, error) {
names := c.Cache.listNames(userInfo)

clusterSetList := &clusterv1beta1.ManagedClusterSetList{}
clusterSetList := &clusterv1beta2.ManagedClusterSetList{}
for key := range names {
clusterSet, err := c.clusterSetLister.Get(key)
if errors.IsNotFound(err) {
Expand Down Expand Up @@ -96,7 +96,7 @@ func (c *ClusterSetCache) Get(name string) (runtime.Object, error) {
func (c *ClusterSetCache) ConvertResource(name string) runtime.Object {
clusterSet, err := c.clusterSetLister.Get(name)
if err != nil {
clusterSet = &clusterv1beta1.ManagedClusterSet{ObjectMeta: metav1.ObjectMeta{Name: name}}
clusterSet = &clusterv1beta2.ManagedClusterSet{ObjectMeta: metav1.ObjectMeta{Name: name}}
}

return clusterSet
Expand Down
20 changes: 10 additions & 10 deletions pkg/cache/managedclusterset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@ import (
"k8s.io/client-go/kubernetes/fake"
clusterfake "open-cluster-management.io/api/client/cluster/clientset/versioned/fake"
clusterinformers "open-cluster-management.io/api/client/cluster/informers/externalversions"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
)

var (
managedClusterSetList = clusterv1beta1.ManagedClusterSetList{
Items: []clusterv1beta1.ManagedClusterSet{
managedClusterSetList = clusterv1beta2.ManagedClusterSetList{
Items: []clusterv1beta2.ManagedClusterSet{
{
ObjectMeta: metav1.ObjectMeta{Name: "clusterset1", ResourceVersion: "1"},
},
Expand Down Expand Up @@ -138,16 +138,16 @@ var (
}
)

func newManagedClusterSet(names ...string) []*clusterv1beta1.ManagedClusterSet {
ret := []*clusterv1beta1.ManagedClusterSet{}
func newManagedClusterSet(names ...string) []*clusterv1beta2.ManagedClusterSet {
ret := []*clusterv1beta2.ManagedClusterSet{}
for _, name := range names {
ret = append(ret, &clusterv1beta1.ManagedClusterSet{ObjectMeta: metav1.ObjectMeta{Name: name}})
ret = append(ret, &clusterv1beta2.ManagedClusterSet{ObjectMeta: metav1.ObjectMeta{Name: name}})
}

return ret
}

func validateClusterSetCacheListList(clusterSetList *clusterv1beta1.ManagedClusterSetList, expectedSet sets.String) bool {
func validateClusterSetCacheListList(clusterSetList *clusterv1beta2.ManagedClusterSetList, expectedSet sets.String) bool {
clusterSets := sets.String{}
for _, clusterSet := range clusterSetList.Items {
clusterSets.Insert(clusterSet.Name)
Expand All @@ -171,12 +171,12 @@ func fakeNewClusterSetCache(stopCh chan struct{}) *ClusterSetCache {
fakeClusterSetClient := clusterfake.NewSimpleClientset(&managedClusterSetList)
clusterInformers := clusterinformers.NewSharedInformerFactory(fakeClusterSetClient, 10*time.Minute)
for key := range managedClusterSetList.Items {
clusterInformers.Cluster().V1beta1().ManagedClusterSets().Informer().GetIndexer().Add(&managedClusterSetList.Items[key])
clusterInformers.Cluster().V1beta2().ManagedClusterSets().Informer().GetIndexer().Add(&managedClusterSetList.Items[key])
}
clusterInformers.Start(stopCh)

return NewClusterSetCache(
clusterInformers.Cluster().V1beta1().ManagedClusterSets(),
clusterInformers.Cluster().V1beta2().ManagedClusterSets(),
informers.Rbac().V1().ClusterRoles(),
informers.Rbac().V1().ClusterRoleBindings(),
utils.GetViewResourceFromClusterRole,
Expand Down Expand Up @@ -267,7 +267,7 @@ func TestClusterSetCacheListObj(t *testing.T) {
t.Run(test.name, func(t *testing.T) {
clusterSetList, err := clusterSetCache.ListObjects(test.user)
validateError(t, err, test.expectedErr)
assert.Equal(t, validateClusterSetCacheListList(clusterSetList.(*clusterv1beta1.ManagedClusterSetList), test.expectedClusterSets), true)
assert.Equal(t, validateClusterSetCacheListList(clusterSetList.(*clusterv1beta2.ManagedClusterSetList), test.expectedClusterSets), true)
})
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog"

clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"
"sigs.k8s.io/controller-runtime/pkg/controller"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
Expand Down Expand Up @@ -133,7 +133,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
klog.V(5).Infof("Clusterclaim's clusterdeployment: %+v", clusterDeployment)

var isModified = false
utils.SyncMapField(&isModified, &clusterclaim.Labels, clusterDeployment.Labels, clusterv1beta1.ClusterSetLabel)
utils.SyncMapField(&isModified, &clusterclaim.Labels, clusterDeployment.Labels, clusterv1beta2.ClusterSetLabel)

if isModified {
err = r.client.Update(ctx, clusterclaim, &client.UpdateOptions{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -78,7 +78,7 @@ func TestReconcile(t *testing.T) {
Name: "dep1",
Namespace: "dep1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
Spec: hivev1.ClusterDeploymentSpec{},
Expand All @@ -91,7 +91,7 @@ func TestReconcile(t *testing.T) {
},
},
expectedlabel: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
{
Expand All @@ -102,7 +102,7 @@ func TestReconcile(t *testing.T) {
Name: "clusterClaim1",
Namespace: "ns1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
Spec: hivev1.ClusterClaimSpec{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"

"github.com/stolostron/multicloud-operators-foundation/pkg/utils"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"

hivev1 "github.com/openshift/hive/apis/hive/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -191,7 +191,7 @@ func (r *Reconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Resu
}

var isModified = false
utils.SyncMapField(&isModified, &clusterdeployment.Labels, targetLabels, clusterv1beta1.ClusterSetLabel)
utils.SyncMapField(&isModified, &clusterdeployment.Labels, targetLabels, clusterv1beta2.ClusterSetLabel)

if isModified {
err = r.client.Update(ctx, clusterdeployment, &client.UpdateOptions{})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"os"
"testing"

clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
clusterv1beta2 "open-cluster-management.io/api/cluster/v1beta2"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
Expand Down Expand Up @@ -92,7 +92,7 @@ func TestReconcile(t *testing.T) {
Name: "pool1",
Namespace: "poolNs1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
Spec: hivev1.ClusterPoolSpec{},
Expand All @@ -105,7 +105,7 @@ func TestReconcile(t *testing.T) {
},
},
expectedlabel: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
{
Expand All @@ -116,7 +116,7 @@ func TestReconcile(t *testing.T) {
Name: "clusterdeployment1",
Namespace: "ns1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
Spec: hivev1.ClusterDeploymentSpec{
Expand Down Expand Up @@ -167,7 +167,7 @@ func TestReconcile(t *testing.T) {
Name: "pool1",
Namespace: "poolNs1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
Spec: hivev1.ClusterPoolSpec{},
Expand All @@ -178,7 +178,7 @@ func TestReconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "cd1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
},
Expand All @@ -190,7 +190,7 @@ func TestReconcile(t *testing.T) {
},
},
expectedlabel: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
{
Expand All @@ -210,7 +210,7 @@ func TestReconcile(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "cd1",
Labels: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
},
Expand All @@ -222,7 +222,7 @@ func TestReconcile(t *testing.T) {
},
},
expectedlabel: map[string]string{
clusterv1beta1.ClusterSetLabel: "clusterSet1",
clusterv1beta2.ClusterSetLabel: "clusterSet1",
},
},
}
Expand Down
Loading

0 comments on commit 7c55e8f

Please sign in to comment.