Skip to content

Commit

Permalink
Upgrade k8s api to v0.21 (#115)
Browse files Browse the repository at this point in the history
* Update to k8s api version 0.21

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

* Update unit tests

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

* Fix lint errors

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

* Fix lint errors

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

* Update unit tests

Signed-off-by: Philip Wu <[email protected]>
  • Loading branch information
philipwu08 authored Aug 16, 2021
1 parent 6abcfaf commit 699e546
Show file tree
Hide file tree
Showing 15 changed files with 429 additions and 196 deletions.
2 changes: 1 addition & 1 deletion cmd/gitopscluster/exec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,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
2 changes: 1 addition & 1 deletion cmd/manager/exec/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,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
24 changes: 10 additions & 14 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,21 @@ module github.com/open-cluster-management/multicloud-operators-placementrule
go 1.16

require (
github.com/IBM/controller-filtered-cache v0.2.2
github.com/IBM/controller-filtered-cache v0.3.2
github.com/ghodss/yaml v1.0.1-0.20190212211648-25d852aebe32
github.com/go-logr/logr v0.3.0 // indirect
github.com/go-openapi/spec v0.19.4
github.com/onsi/gomega v1.10.1
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/klusterlet-addon-controller v0.0.0-20210303215539-1d12cebe6f19
github.com/spf13/pflag v1.0.5
golang.org/x/net v0.0.0-20201110031124-69a78807bb2b
k8s.io/api v0.20.0
k8s.io/apiextensions-apiserver v0.19.3
k8s.io/apimachinery v0.20.0
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-20201113171705-d219536bb9fd
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/api => k8s.io/api v0.19.3
k8s.io/client-go => k8s.io/client-go v0.19.3
)
replace k8s.io/client-go => k8s.io/client-go v0.21.3
394 changes: 312 additions & 82 deletions go.sum

Large diffs are not rendered by default.

18 changes: 10 additions & 8 deletions pkg/controller/gitopscluster/gitopscluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,26 +86,26 @@ type placementDecisionMapper struct {
client.Client
}

func (mapper *placementDecisionMapper) Map(obj handler.MapObject) []reconcile.Request {
func (mapper *placementDecisionMapper) Map(obj client.Object) []reconcile.Request {
var requests []reconcile.Request

gitOpsClusterList := &gitopsclusterV1alpha1.GitOpsClusterList{}
listopts := &client.ListOptions{Namespace: obj.Meta.GetNamespace()}
listopts := &client.ListOptions{Namespace: obj.GetNamespace()}
err := mapper.List(context.TODO(), gitOpsClusterList, listopts)

if err != nil {
klog.Error("failed to list GitOpsClusters, error:", err)
}

labels := obj.Meta.GetLabels()
labels := obj.GetLabels()

// if placementDecision is created/updated/deleted, its relative GitOpsCluster should be reconciled.
for _, gitOpsCluster := range gitOpsClusterList.Items {
if strings.EqualFold(gitOpsCluster.Spec.PlacementRef.Name, labels["cluster.open-cluster-management.io/placement"]) &&
strings.EqualFold(gitOpsCluster.Namespace, obj.Meta.GetNamespace()) {
strings.EqualFold(gitOpsCluster.Namespace, obj.GetNamespace()) {
klog.Infof("Placement decision %s/%s affects GitOpsCluster %s/%s",
obj.Meta.GetNamespace(),
obj.Meta.GetName(),
obj.GetNamespace(),
obj.GetName(),
gitOpsCluster.Namespace,
gitOpsCluster.Name)

Expand Down Expand Up @@ -150,10 +150,12 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}

// Watch cluster list changes in placement decision
pdMapper := &placementDecisionMapper{mgr.GetClient()}
err = c.Watch(
&source.Kind{Type: &clusterv1alpha1.PlacementDecision{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &placementDecisionMapper{mgr.GetClient()}},
handler.EnqueueRequestsFromMapFunc(pdMapper.Map),
utils.PlacementDecisionPredicateFunc)

if err != nil {
return err
}
Expand All @@ -162,7 +164,7 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
return nil
}

func (r *ReconcileGitOpsCluster) Reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *ReconcileGitOpsCluster) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
klog.Info("Reconciling GitOpsClusters for watched resource change: ", request.NamespacedName)

// Get all existing GitOps managed cluster secrets, not the ones from the managed cluster namespaces
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package gitopscluster

import (
"context"
stdlog "log"
"os"
"path/filepath"
Expand Down Expand Up @@ -63,8 +64,8 @@ func TestMain(m *testing.M) {

// SetupTestReconcile returns a reconcile.Reconcile implementation that delegates to inner
func SetupTestReconcile(inner reconcile.Reconciler) reconcile.Reconciler {
fn := reconcile.Func(func(req reconcile.Request) (reconcile.Result, error) {
result, err := inner.Reconcile(req)
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
result, err := inner.Reconcile(ctx, req)

return result, err
})
Expand All @@ -73,15 +74,14 @@ func SetupTestReconcile(inner reconcile.Reconciler) reconcile.Reconciler {
}

// 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())
wg.Done()
mgr.Start(ctx)
}()

return stop, wg
return wg
}
24 changes: 14 additions & 10 deletions pkg/controller/gitopscluster/gitopscluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,10 +291,11 @@ func TestReconcileCreateSecretInArgo(t *testing.T) {
recFn := 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 All @@ -309,7 +310,7 @@ func TestReconcileCreateSecretInArgo(t *testing.T) {
g.Expect(c.Create(context.TODO(), test1PlDc.DeepCopy())).NotTo(gomega.HaveOccurred())
defer c.Delete(context.TODO(), test1PlDc)

time.Sleep(time.Second * 3)
time.Sleep(time.Second * 5)

// Update placement decision status
placementDecision1 := &clusterv1alpha1.PlacementDecision{}
Expand All @@ -322,7 +323,7 @@ func TestReconcileCreateSecretInArgo(t *testing.T) {

g.Expect(c.Status().Update(context.TODO(), newPlacementDecision1)).NotTo(gomega.HaveOccurred())

time.Sleep(time.Second * 3)
time.Sleep(time.Second * 5)

placementDecisionAfterupdate := &clusterv1alpha1.PlacementDecision{}
g.Expect(c.Get(context.TODO(),
Expand Down Expand Up @@ -375,10 +376,11 @@ func TestReconcileNoSecretInInvalidArgoNamespace(t *testing.T) {
recFn := 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 @@ -454,10 +456,11 @@ func TestReconcileCreateSecretInOpenshiftGitops(t *testing.T) {
recFn := 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 @@ -608,10 +611,11 @@ func TestReconcileDeleteOrphanSecret(t *testing.T) {
recFn := 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
14 changes: 8 additions & 6 deletions pkg/controller/placementrule/placementrule_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,13 @@ func add(mgr manager.Manager, r reconcile.Reconciler) error {
}

if utils.IsReadyACMClusterRegistry(mgr.GetAPIReader()) {
cpMapper := &ClusterPlacementRuleMapper{mgr.GetClient()}
err = c.Watch(
&source.Kind{Type: &spokeClusterV1.ManagedCluster{}},
&handler.EnqueueRequestsFromMapFunc{ToRequests: &ClusterPlacementRuleMapper{mgr.GetClient()}},
handler.EnqueueRequestsFromMapFunc(cpMapper.Map),
utils.ClusterPredicateFunc,
)

if err != nil {
return err
}
Expand All @@ -102,7 +104,7 @@ type ClusterPlacementRuleMapper struct {
}

// Map triggers all placements.
func (mapper *ClusterPlacementRuleMapper) Map(obj handler.MapObject) []reconcile.Request {
func (mapper *ClusterPlacementRuleMapper) Map(obj client.Object) []reconcile.Request {
plList := &appv1alpha1.PlacementRuleList{}

listopts := &client.ListOptions{}
Expand Down Expand Up @@ -134,8 +136,8 @@ type PolicyPlacementRuleMapper struct {
}

// Map triggers all placements
func (mapper *PolicyPlacementRuleMapper) Map(obj handler.MapObject) []reconcile.Request {
cname := obj.Meta.GetName()
func (mapper *PolicyPlacementRuleMapper) Map(obj client.Object) []reconcile.Request {
cname := obj.GetName()

klog.Info("In policy Mapper for ", cname)

Expand Down Expand Up @@ -168,10 +170,10 @@ func (mapper *PolicyPlacementRuleMapper) Map(obj handler.MapObject) []reconcile.
// Automatically generate RBAC rules to allow the Controller to read and write Deployments
// +kubebuilder:rbac:groups=multicloud-apps.io,resources=placementrules,verbs=get;list;watch;create;update;patch;delete
// +kubebuilder:rbac:groups=multicloud.io,resources=placementrules/status,verbs=get;update;patch
func (r *ReconcilePlacementRule) Reconcile(request reconcile.Request) (reconcile.Result, error) {
func (r *ReconcilePlacementRule) Reconcile(ctx context.Context, request reconcile.Request) (reconcile.Result, error) {
// Fetch the PlacementRule instance
instance := &appv1alpha1.PlacementRule{}
err := r.Get(context.TODO(), request.NamespacedName, instance)
err := r.Get(ctx, request.NamespacedName, instance)

klog.Info("Reconciling:", request.NamespacedName, " with Get err:", err)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package placementrule

import (
"context"
stdlog "log"
"os"
"path/filepath"
Expand Down Expand Up @@ -61,8 +62,8 @@ 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) {
result, err := inner.Reconcile(req)
fn := reconcile.Func(func(ctx context.Context, req reconcile.Request) (reconcile.Result, error) {
result, err := inner.Reconcile(ctx, req)
requests <- req
return result, err
})
Expand All @@ -71,15 +72,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())
wg.Done()
mgr.Start(ctx)
}()

return stop, wg
return wg
}
34 changes: 22 additions & 12 deletions pkg/controller/placementrule/placementrule_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,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 @@ -118,10 +119,12 @@ func TestClusterNames(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 @@ -176,10 +179,12 @@ func TestClusterLabels(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 @@ -241,10 +246,12 @@ func TestAllClusters(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 @@ -332,10 +339,12 @@ func TestClusterReplica(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 @@ -391,10 +400,11 @@ func TestClusterChange(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
Loading

0 comments on commit 699e546

Please sign in to comment.