Skip to content

Commit

Permalink
Update to k8s api version 0.21 (#95)
Browse files Browse the repository at this point in the history
Signed-off-by: Philip Wu <[email protected]>

Update unit tests

Signed-off-by: Philip Wu <[email protected]>

Update reference to latest placementrule

Signed-off-by: Philip Wu <[email protected]>

Fix lint errors

Signed-off-by: Philip Wu <[email protected]>
  • Loading branch information
philipwu08 authored Aug 16, 2021
1 parent 2bc1453 commit f9fe854
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 207 deletions.
2 changes: 1 addition & 1 deletion cmd/manager/exec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func RunManager() {
sig := signals.SetupSignalHandler()

klog.Info("Detecting ACM cluster API service...")
utils.DetectClusterRegistry(mgr.GetAPIReader(), sig)
utils.DetectClusterRegistry(sig, mgr.GetAPIReader())

klog.Info("Starting the Cmd.")

Expand Down
25 changes: 13 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ go 1.16
require (
github.com/cameront/go-jsonpatch v0.0.0-20180223123257-a8710867776e
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-openapi/spec v0.19.4
github.com/onsi/gomega v1.10.1
github.com/open-cluster-management/api v0.0.0-20201007180356-41d07eee4294
github.com/open-cluster-management/multicloud-operators-placementrule v1.2.2-2-20201130-98cfd
github.com/go-openapi/spec v0.19.5
github.com/onsi/gomega v1.13.0
github.com/open-cluster-management/api v0.0.0-20210513122330-d76f10481f05
github.com/open-cluster-management/multicloud-operators-placementrule v1.2.4-0-20210816-699e5
github.com/spf13/pflag v1.0.5
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
golang.org/x/net v0.0.0-20200707034311-ab3426394381
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
k8s.io/api v0.19.3
k8s.io/apiextensions-apiserver v0.19.3
k8s.io/apimachinery v0.19.3
k8s.io/client-go v0.19.3
golang.org/x/net v0.0.0-20210428140749-89ef3d95e781
k8s.io/api v0.21.3
k8s.io/apiextensions-apiserver v0.21.3
k8s.io/apimachinery v0.21.3
k8s.io/client-go v12.0.0+incompatible
k8s.io/klog v1.0.0
k8s.io/kube-openapi v0.0.0-20200805222855-6aeccd4b50c6
sigs.k8s.io/controller-runtime v0.6.3
k8s.io/kube-openapi v0.0.0-20210305001622-591a79e4bda7
sigs.k8s.io/controller-runtime v0.9.1
)

replace k8s.io/client-go => k8s.io/client-go v0.21.3
546 changes: 385 additions & 161 deletions go.sum

Large diffs are not rendered by default.

38 changes: 23 additions & 15 deletions pkg/controller/deployable/deployable_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ type placementruleMapper struct {
client.Client
}

func (mapper *placementruleMapper) Map(obj handler.MapObject) []reconcile.Request {
func (mapper *placementruleMapper) Map(obj client.Object) []reconcile.Request {
if klog.V(utils.QuiteLogLel) {
fnName := utils.GetFnName()
klog.Infof("Entering: %v()", fnName)

defer klog.Infof("Exiting: %v()", fnName)
}

cname := obj.Meta.GetName()
cname := obj.GetName()
klog.V(5).Info("In placement Mapper:", cname)

var requests []reconcile.Request
Expand All @@ -91,7 +91,7 @@ func (mapper *placementruleMapper) Map(obj handler.MapObject) []reconcile.Reques
}

for _, dpl := range dplList.Items {
if dpl.Spec.Placement == nil || dpl.Spec.Placement.PlacementRef == nil || dpl.Spec.Placement.PlacementRef.Name != obj.Meta.GetName() {
if dpl.Spec.Placement == nil || dpl.Spec.Placement.PlacementRef == nil || dpl.Spec.Placement.PlacementRef.Name != obj.GetName() {
continue
}

Expand All @@ -111,15 +111,15 @@ type clusterMapper struct {
client.Client
}

func (mapper *clusterMapper) Map(obj handler.MapObject) []reconcile.Request {
func (mapper *clusterMapper) Map(obj client.Object) []reconcile.Request {
if klog.V(utils.QuiteLogLel) {
fnName := utils.GetFnName()
klog.Infof("Entering: %v()", fnName)

defer klog.Infof("Exiting: %v()", fnName)
}

cname := obj.Meta.GetName()
cname := obj.GetName()
klog.V(5).Info("In cluster Mapper for ", cname)

var requests []reconcile.Request
Expand Down Expand Up @@ -174,15 +174,20 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}

// Watch for changes to primary resource Deployable
dMapper := &deployableMapper{mgr.GetClient()}
err = c.Watch(
&source.Kind{Type: &appv1alpha1.Deployable{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &deployableMapper{mgr.GetClient()}}, utils.DeployablePredicateFunc)
handler.EnqueueRequestsFromMapFunc(dMapper.Map),
utils.DeployablePredicateFunc)

if err != nil {
return err
}

// watch for placementrule changes
err = c.Watch(&source.Kind{Type: &placementv1alpha1.PlacementRule{}}, &handler.EnqueueRequestsFromMapFunc{ToRequests: &placementruleMapper{mgr.GetClient()}},
pMapper := &placementruleMapper{mgr.GetClient()}
err = c.Watch(&source.Kind{Type: &placementv1alpha1.PlacementRule{}},
handler.EnqueueRequestsFromMapFunc(pMapper.Map),
predicate.Funcs{
UpdateFunc: func(e event.UpdateEvent) bool {
newpr := e.ObjectNew.(*placementv1alpha1.PlacementRule)
Expand All @@ -191,17 +196,20 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return !reflect.DeepEqual(newpr.Status, oldpr.Status)
},
})

if err != nil {
return err
}

// watch for cluster change excluding heartbeat
if placementutils.IsReadyACMClusterRegistry(mgr.GetAPIReader()) {
cMapper := &clusterMapper{mgr.GetClient()}
err = c.Watch(
&source.Kind{Type: &spokeClusterV1.ManagedCluster{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &clusterMapper{mgr.GetClient()}},
handler.EnqueueRequestsFromMapFunc(cMapper.Map),
placementutils.ClusterPredicateFunc,
)

if err != nil {
return err
}
Expand All @@ -214,7 +222,7 @@ type deployableMapper struct {
client.Client
}

func (mapper *deployableMapper) Map(obj handler.MapObject) []reconcile.Request {
func (mapper *deployableMapper) Map(obj client.Object) []reconcile.Request {
if klog.V(utils.QuiteLogLel) {
fnName := utils.GetFnName()
klog.Infof("Entering: %v()", fnName)
Expand All @@ -229,15 +237,15 @@ func (mapper *deployableMapper) Map(obj handler.MapObject) []reconcile.Request {
requests = append(requests,
reconcile.Request{
NamespacedName: types.NamespacedName{
Name: obj.Meta.GetName(),
Namespace: obj.Meta.GetNamespace(),
Name: obj.GetName(),
Namespace: obj.GetNamespace(),
},
},
)

// list thing for rolling update check
dplList := &appv1alpha1.DeployableList{}
listopts := &client.ListOptions{Namespace: obj.Meta.GetNamespace()}
listopts := &client.ListOptions{Namespace: obj.GetNamespace()}
err := mapper.List(context.TODO(), dplList, listopts)

if err != nil {
Expand All @@ -251,7 +259,7 @@ func (mapper *deployableMapper) Map(obj handler.MapObject) []reconcile.Request {
continue
}

if annotations[appv1alpha1.AnnotationRollingUpdateTarget] != obj.Meta.GetName() {
if annotations[appv1alpha1.AnnotationRollingUpdateTarget] != obj.GetName() {
// rolling to annother one, skipping
continue
}
Expand All @@ -267,7 +275,7 @@ func (mapper *deployableMapper) Map(obj handler.MapObject) []reconcile.Request {
// end of rolling update check

// reconcile hosting one, if there is change in cluster, assuming no 2-hop hosting
hdplkey := utils.GetHostDeployableFromObject(obj.Meta)
hdplkey := utils.GetHostDeployableFromObject(obj)
if hdplkey != nil && hdplkey.Name != "" {
requests = append(requests, reconcile.Request{NamespacedName: *hdplkey})
}
Expand All @@ -293,7 +301,7 @@ type ReconcileDeployable struct {

// Reconcile reads that state of the cluster for a Deployable object and makes changes based on the state read
// and what is in the Deployable.Spec
func (r *ReconcileDeployable) Reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *ReconcileDeployable) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
// Fetch the Deployable instance
instance := &appv1alpha1.Deployable{}
err := r.Get(context.TODO(), request.NamespacedName, instance)
Expand Down
12 changes: 6 additions & 6 deletions pkg/controller/deployable/deployable_controller_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package deployable

import (
"context"
stdlog "log"
"os"
"path/filepath"
Expand Down Expand Up @@ -58,9 +59,9 @@ func TestMain(m *testing.M) {
// writes the request to requests after Reconcile is finished.
func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan reconcile.Request) {
requests := make(chan reconcile.Request)
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
requests <- req
result, err := inner.Reconcile(req)
result, err := inner.Reconcile(ctx, req)

return result, err
})
Expand All @@ -69,15 +70,14 @@ func SetupTestReconcile(inner reconcile.Reconciler) (reconcile.Reconciler, chan
}

// StartTestManager adds recFn
func StartTestManager(mgr manager.Manager, g *gomega.GomegaWithT) (chan struct{}, *sync.WaitGroup) {
stop := make(chan struct{})
func StartTestManager(ctx context.Context, mgr manager.Manager, g *gomega.GomegaWithT) *sync.WaitGroup {
wg := &sync.WaitGroup{}
wg.Add(1)

go func() {
defer wg.Done()
g.Expect(mgr.Start(stop)).NotTo(gomega.HaveOccurred())
mgr.Start(ctx)
}()

return stop, wg
return wg
}
25 changes: 15 additions & 10 deletions pkg/controller/deployable/deployable_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,11 @@ func TestReconcile(t *testing.T) {
recFn, requests := SetupTestReconcile(newReconciler(mgr))
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
mgrStopped := StartTestManager(ctx, mgr, g)

defer func() {
close(stopMgr)
cancel()
mgrStopped.Wait()
}()

Expand Down Expand Up @@ -167,10 +168,11 @@ func TestPropagate(t *testing.T) {
recFn, requests := SetupTestReconcile(newReconciler(mgr))
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
mgrStopped := StartTestManager(ctx, mgr, g)

defer func() {
close(stopMgr)
cancel()
mgrStopped.Wait()
}()

Expand Down Expand Up @@ -305,10 +307,11 @@ func TestOverride(t *testing.T) {
recFn, requests := SetupTestReconcile(newReconciler(mgr))
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
mgrStopped := StartTestManager(ctx, mgr, g)

defer func() {
close(stopMgr)
cancel()
mgrStopped.Wait()
}()

Expand Down Expand Up @@ -459,10 +462,11 @@ func TestRollingUpdateStatus(t *testing.T) {
recFn, requests := SetupTestReconcile(newReconciler(mgr))
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
mgrStopped := StartTestManager(ctx, mgr, g)

defer func() {
close(stopMgr)
cancel()
mgrStopped.Wait()
}()

Expand Down Expand Up @@ -813,10 +817,11 @@ func TestRollingUpdate(t *testing.T) {
recFn, requests = SetupTestReconcile(newReconciler(mgr))
g.Expect(add(mgr, recFn)).NotTo(gomega.HaveOccurred())

stopMgr, mgrStopped := StartTestManager(mgr, g)
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Minute)
mgrStopped := StartTestManager(ctx, mgr, g)

defer func() {
close(stopMgr)
cancel()
mgrStopped.Wait()
}()

Expand Down
2 changes: 0 additions & 2 deletions pkg/utils/deployable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,7 @@ func TestDeployablePredicate(t *testing.T) {

updateEvt := event.UpdateEvent{
ObjectOld: oldDeployable,
MetaOld: oldDeployable.GetObjectMeta(),
ObjectNew: newDeployable,
MetaNew: newDeployable.GetObjectMeta(),
}
ret := instance.Update(updateEvt)
g.Expect(ret).To(gomega.Equal(true))
Expand Down

0 comments on commit f9fe854

Please sign in to comment.