Skip to content

Commit

Permalink
appliedmanifestwork eviction
Browse files Browse the repository at this point in the history
Signed-off-by: Wei Liu <liuweixa@redhat.com>
  • Loading branch information
skeeey committed Mar 13, 2023
1 parent 21fb8d7 commit 21f6c03
Show file tree
Hide file tree
Showing 17 changed files with 528 additions and 260 deletions.
4 changes: 4 additions & 0 deletions deploy/spoke/appliedmanifestworks.crd.yaml
Expand Up @@ -72,6 +72,10 @@ spec:
version:
description: Version is the version of the Kubernetes resource.
type: string
evictionStartTime:
description: 'EvictionStartTime represents the current appliedmanifestwork will be evicted after a grace period. An appliedmanifestwork will be evicted from the managed cluster in the following two scenarios: - the manifestwork of the current appliedmanifestwork is missing on the hub, or - the appliedmanifestwork hub hash does not match the current hub hash of the work agent.'
type: string
format: date-time
served: true
storage: true
subresources:
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Expand Up @@ -22,7 +22,7 @@ require (
k8s.io/component-base v0.26.1
k8s.io/klog/v2 v2.80.1
k8s.io/utils v0.0.0-20221128185143-99ec85e7a448
open-cluster-management.io/api v0.10.1-0.20230227155011-2c63272fa800
open-cluster-management.io/api v0.10.1-0.20230228092946-080d7b65f4f3
sigs.k8s.io/controller-runtime v0.14.4
)

Expand Down
4 changes: 2 additions & 2 deletions go.sum
Expand Up @@ -1024,8 +1024,8 @@ modernc.org/golex v1.0.0/go.mod h1:b/QX9oBD/LhixY6NDh+IdGv17hgB+51fET1i2kPSmvk=
modernc.org/mathutil v1.0.0/go.mod h1:wU0vUrJsVWBZ4P6e7xtFJEhFSNsfRLJ8H458uRjg03k=
modernc.org/strutil v1.0.0/go.mod h1:lstksw84oURvj9y3tn8lGvRxyRC1S2+g5uuIzNfIOBs=
modernc.org/xc v1.0.0/go.mod h1:mRNCo0bvLjGhHO9WsyuKVU4q0ceiDDDoEeWDJHrNx8I=
open-cluster-management.io/api v0.10.1-0.20230227155011-2c63272fa800 h1:/f9jJbabrWG/PCS42Vcxh/Gl0qxNCaGsZ59fQB1jLhc=
open-cluster-management.io/api v0.10.1-0.20230227155011-2c63272fa800/go.mod h1:TjWobG3dTZJf/Ye04358/F/381RjE/+HXVDGMnZBpjc=
open-cluster-management.io/api v0.10.1-0.20230228092946-080d7b65f4f3 h1:AJaXjQIIUYCbv10bRnurz4d2UZtN1PfpbWD80KtiZGU=
open-cluster-management.io/api v0.10.1-0.20230228092946-080d7b65f4f3/go.mod h1:TjWobG3dTZJf/Ye04358/F/381RjE/+HXVDGMnZBpjc=
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
87 changes: 86 additions & 1 deletion pkg/helper/helper_test.go
Expand Up @@ -4,11 +4,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-cmp/cmp"
"reflect"
"testing"
"time"

"github.com/google/go-cmp/cmp"

"github.com/openshift/library-go/pkg/operator/events/eventstesting"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/equality"
Expand Down Expand Up @@ -810,3 +811,87 @@ func TestBuildResourceMeta(t *testing.T) {
})
}
}

func TestAppliedManifestworkEvictionFilter(t *testing.T) {
cases := []struct {
name string
appliedManifestWork runtime.Object
hubHash string
agentID string
expected bool
}{
{
name: "nil appliedmanifestwork",
expected: false,
},
{
name: "hub and agent are unchanged",
appliedManifestWork: &workapiv1.AppliedManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: workapiv1.AppliedManifestWorkSpec{
HubHash: "h1234",
AgentID: "a1234",
},
},
hubHash: "h1234",
agentID: "a1234",
expected: true,
},
{
name: "hub is unchanged, agent is changed",
appliedManifestWork: &workapiv1.AppliedManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: workapiv1.AppliedManifestWorkSpec{
HubHash: "h1234",
AgentID: "a1234",
},
},
hubHash: "h1234",
agentID: "a5678",
expected: true,
},
{
name: "hub is changed, agent is unchanged",
appliedManifestWork: &workapiv1.AppliedManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: workapiv1.AppliedManifestWorkSpec{
HubHash: "h1234",
AgentID: "a1234",
},
},
hubHash: "h5678",
agentID: "a1234",
expected: true,
},
{
name: "hub is changed, agent is changed",
appliedManifestWork: &workapiv1.AppliedManifestWork{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Spec: workapiv1.AppliedManifestWorkSpec{
HubHash: "h1234",
AgentID: "a1234",
},
},
hubHash: "h5678",
agentID: "a5678",
expected: false,
},
}

for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
if AppliedManifestWorkEvictionFilter(c.hubHash, c.agentID)(c.appliedManifestWork) != c.expected {
t.Errorf("Expected %v, but opposite", c.expected)
}

})
}
}
16 changes: 16 additions & 0 deletions pkg/helper/helpers.go
Expand Up @@ -378,6 +378,22 @@ func AppliedManifestworkHubHashFilter(hubHash string) factory.EventFilterFunc {
}
}

// AppliedManifestWorkEvictionFilter filter the appliedmanifestwork belonging to this hub or this work agent
func AppliedManifestWorkEvictionFilter(hubHash, agentID string) factory.EventFilterFunc {
return func(obj interface{}) bool {
appliedWork, ok := obj.(*workapiv1.AppliedManifestWork)
if !ok {
return false
}

if appliedWork.Spec.HubHash == hubHash {
return true
}

return appliedWork.Spec.AgentID == agentID
}
}

// HubHash returns a hash of hubserver
// NOTE: the length of hash string is 64, meaning the length of manifestwork name should be less than 189
func HubHash(hubServer string) string {
Expand Down
Expand Up @@ -68,13 +68,11 @@ func (m *ManifestWorkFinalizeController) sync(ctx context.Context, controllerCon

manifestWork, err := m.manifestWorkLister.Get(manifestWorkName)

// Delete appliedmanifestwork if relating manfiestwork is not found or being deleted
// Delete appliedmanifestwork if relating manfiestwork is being deleted
switch {
case errors.IsNotFound(err):
err := m.deleteAppliedManifestWork(ctx, appliedManifestWorkName)
if err != nil {
return err
}
// the appliedmanifestwork will be evicted if the relating manfiestwork is not found
return nil
case err != nil:
return err
case !manifestWork.DeletionTimestamp.IsZero():
Expand Down

0 comments on commit 21f6c03

Please sign in to comment.