Skip to content

Commit

Permalink
Implement rollout strategy
Browse files Browse the repository at this point in the history
Signed-off-by: melserngawy <melserng@redhat.com>
  • Loading branch information
serngawy committed Sep 28, 2023
1 parent 5405099 commit bf8bf67
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 6 deletions.
29 changes: 29 additions & 0 deletions pkg/work/helper/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,17 @@ import (
"k8s.io/apimachinery/pkg/api/meta"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/client-go/dynamic"
"k8s.io/klog/v2"

clusterlister "open-cluster-management.io/api/client/cluster/listers/cluster/v1beta1"
clusterv1alpha1 "open-cluster-management.io/api/cluster/v1alpha1"
clusterv1beta1 "open-cluster-management.io/api/cluster/v1beta1"
workapiv1 "open-cluster-management.io/api/work/v1"
)

Expand Down Expand Up @@ -467,3 +472,27 @@ func BuildResourceMeta(
resourceMeta.Resource = mapping.Resource.Resource
return resourceMeta, mapping.Resource, err
}

type PlacementDecisionGetter struct {
Client clusterlister.PlacementDecisionLister
}

func (pdl PlacementDecisionGetter) List(selector labels.Selector, namespace string) ([]*clusterv1beta1.PlacementDecision, error) {
return pdl.Client.PlacementDecisions(namespace).List(selector)
}

// Get added and deleted clusters names
func GetClusters(client clusterlister.PlacementDecisionLister, placement *clusterv1beta1.Placement,
existingClusters sets.Set[string]) (sets.Set[string], sets.Set[string], error) {
existingClusterGroups := make(map[clusterv1beta1.GroupKey]sets.Set[string])
pdtracker := clusterv1beta1.NewPlacementDecisionClustersTracker(placement, PlacementDecisionGetter{Client: client}, existingClusters, existingClusterGroups)

Check failure on line 488 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e-singleton

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 488 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / build

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 488 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 488 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 488 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e-hosted

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 488 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / verify

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

return pdtracker.Get()

Check failure on line 490 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e-singleton

pdtracker.Get undefined (type *"open-cluster-management.io/api/cluster/v1beta1".PlacementDecisionClustersTracker has no field or method Get)

Check failure on line 490 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / build

pdtracker.Get undefined (type *"open-cluster-management.io/api/cluster/v1beta1".PlacementDecisionClustersTracker has no field or method Get)

Check failure on line 490 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e

pdtracker.Get undefined (type *"open-cluster-management.io/api/cluster/v1beta1".PlacementDecisionClustersTracker has no field or method Get)

Check failure on line 490 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / unit

pdtracker.Get undefined (type *"open-cluster-management.io/api/cluster/v1beta1".PlacementDecisionClustersTracker has no field or method Get)

Check failure on line 490 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e-hosted

pdtracker.Get undefined (type *"open-cluster-management.io/api/cluster/v1beta1".PlacementDecisionClustersTracker has no field or method Get)

Check failure on line 490 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / verify

pdtracker.Get undefined (type *"open-cluster-management.io/api/cluster/v1beta1".PlacementDecisionClustersTracker has no field or method Get)
}

func GetRollOutHandler(client clusterlister.PlacementDecisionLister, placement *clusterv1beta1.Placement, existingClusters sets.Set[string]) (*clusterv1alpha1.RolloutHandler, error) {
existingClusterGroups := make(map[clusterv1beta1.GroupKey]sets.Set[string])
pdtracker := clusterv1beta1.NewPlacementDecisionClustersTracker(placement, PlacementDecisionGetter{Client: client}, existingClusters, existingClusterGroups)

Check failure on line 495 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e-singleton

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 495 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / build

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 495 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 495 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 495 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / e2e-hosted

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

Check failure on line 495 in pkg/work/helper/helpers.go

View workflow job for this annotation

GitHub Actions / verify

too many arguments in call to clusterv1beta1.NewPlacementDecisionClustersTracker

return clusterv1alpha1.NewRolloutHandler(pdtracker)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const (
// TODO move this to the api repo
ManifestWorkReplicaSetControllerNameLabelKey = "work.open-cluster-management.io/manifestworkreplicaset"

// ManifestWorkReplicaSetPlacementNameLabelKey is the label key on manifestwork to ref to the Placement that select
// the managedCluster on the manifestWorkReplicaSet's PlacementRef.
ManifestWorkReplicaSetPlacementNameLabelKey = "work.open-cluster-management.io/PlacementName"

// ManifestWorkReplicaSetFinalizer is the name of the finalizer added to ManifestWorkReplicaSet. It is used to ensure
// related manifestworks is deleted
ManifestWorkReplicaSetFinalizer = "work.open-cluster-management.io/manifest-work-cleanup"
Expand Down Expand Up @@ -180,3 +184,19 @@ func listManifestWorksByManifestWorkReplicaSet(mwrs *workapiv1alpha1.ManifestWor
selector := labels.NewSelector().Add(*req)
return manifestWorkLister.List(selector)
}

func listManifestWorksByMWRSetPlacementRef(mwrs *workapiv1alpha1.ManifestWorkReplicaSet, placementName string,
manifestWorkLister worklisterv1.ManifestWorkLister) ([]*workapiv1.ManifestWork, error) {
reqMWRSet, err := labels.NewRequirement(ManifestWorkReplicaSetControllerNameLabelKey, selection.Equals, []string{manifestWorkReplicaSetKey(mwrs)})
if err != nil {
return nil, err
}

reqPlacementRef, err := labels.NewRequirement(ManifestWorkReplicaSetPlacementNameLabelKey, selection.Equals, []string{placementName})
if err != nil {
return nil, err
}

selector := labels.NewSelector().Add(*reqMWRSet, *reqPlacementRef)
return manifestWorkLister.List(selector)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import (

func TestDeployReconcileAsExpected(t *testing.T) {
mwrSet := helpertest.CreateTestManifestWorkReplicaSet("mwrSet-test", "default", "place-test")
mw, _ := CreateManifestWork(mwrSet, "cls1")
mw, _ := CreateManifestWork(mwrSet, "cls1", "plc1")

Check failure on line 23 in pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_deploy_test.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to CreateManifestWork
fWorkClient := fakeworkclient.NewSimpleClientset(mwrSet, mw)
workInformerFactory := workinformers.NewSharedInformerFactoryWithOptions(fWorkClient, 1*time.Second)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
// Test finalize reconcile
func TestFinalizeReconcile(t *testing.T) {
mwrSetTest := helpertest.CreateTestManifestWorkReplicaSet("mwrSet-test", "default", "place-test")
mw, _ := CreateManifestWork(mwrSetTest, "cluster1")
mw, _ := CreateManifestWork(mwrSetTest, "cluster1", "plc1")

Check failure on line 21 in pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_finalizer_test.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to CreateManifestWork
fakeClient := fakeclient.NewSimpleClientset(mwrSetTest, mw)
manifestWorkInformerFactory := workinformers.NewSharedInformerFactoryWithOptions(fakeClient, 1*time.Second)
mwLister := manifestWorkInformerFactory.Work().V1().ManifestWorks().Lister()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func TestPlaceMWControllerIndex(t *testing.T) {
mwrSetTest := helpertest.CreateTestManifestWorkReplicaSet("mwrSet-test", "default", "place-test")
mwrSetTest.Status.Summary.Total = 1
mw, _ := CreateManifestWork(mwrSetTest, "cls1")
mw, _ := CreateManifestWork(mwrSetTest, "cls1", "plc1")

Check failure on line 21 in pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_index_test.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to CreateManifestWork
fWorkClient := fakeworkclient.NewSimpleClientset(mwrSetTest, mw)
workInformerFactory := workinformers.NewSharedInformerFactoryWithOptions(fWorkClient, 1*time.Second)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func TestStatusReconcileAsExpected(t *testing.T) {
}

for _, cls := range clusters {
mw, _ := CreateManifestWork(mwrSetTest, cls)
mw, _ := CreateManifestWork(mwrSetTest, cls, "plc1")

Check failure on line 32 in pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_status_test.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to CreateManifestWork
cond := getCondition(workv1.WorkApplied, "", "", metav1.ConditionTrue)
apimeta.SetStatusCondition(&mw.Status.Conditions, cond)

Expand Down Expand Up @@ -94,7 +94,7 @@ func TestStatusReconcileAsProcessing(t *testing.T) {
}

for id, cls := range clusters {
mw, _ := CreateManifestWork(mwrSetTest, cls)
mw, _ := CreateManifestWork(mwrSetTest, cls, "plc1")

Check failure on line 97 in pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_status_test.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to CreateManifestWork
cond := getCondition(workv1.WorkApplied, "", "", metav1.ConditionTrue)
apimeta.SetStatusCondition(&mw.Status.Conditions, cond)

Expand Down Expand Up @@ -166,7 +166,7 @@ func TestStatusReconcileNotAsExpected(t *testing.T) {

avaCount, processingCount, degradCount := 0, 0, 0
for id, cls := range clusters {
mw, _ := CreateManifestWork(mwrSetTest, cls)
mw, _ := CreateManifestWork(mwrSetTest, cls, "plc1")

Check failure on line 169 in pkg/work/hub/controllers/manifestworkreplicasetcontroller/manifestworkreplicaset_status_test.go

View workflow job for this annotation

GitHub Actions / unit

too many arguments in call to CreateManifestWork
cond := getCondition(workv1.WorkApplied, "", "", metav1.ConditionTrue)
apimeta.SetStatusCondition(&mw.Status.Conditions, cond)

Expand Down

0 comments on commit bf8bf67

Please sign in to comment.