Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: use CRP defaulter to account for nil CRP policy in eviction controller #987

Merged
merged 5 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
bindingutils "go.goms.io/fleet/pkg/utils/binding"
"go.goms.io/fleet/pkg/utils/condition"
"go.goms.io/fleet/pkg/utils/controller"
"go.goms.io/fleet/pkg/utils/defaulter"
)

const (
Expand Down Expand Up @@ -102,6 +103,10 @@ func (r *Reconciler) validateEviction(ctx context.Context, eviction *placementv1
}
return nil, controller.NewAPIServerError(true, err)
}

// set default values for CRP.
defaulter.SetDefaultsClusterResourcePlacement(&crp)

if crp.DeletionTimestamp != nil {
klog.V(2).InfoS(evictionInvalidDeletingCRPMessage, "clusterResourcePlacementEviction", eviction.Name, "clusterResourcePlacement", eviction.Spec.PlacementName)
markEvictionInvalid(eviction, evictionInvalidDeletingCRPMessage)
Expand Down Expand Up @@ -277,7 +282,7 @@ func isEvictionAllowed(bindings []placementv1beta1.ClusterResourceBinding, crp p
desiredBindings = int(*crp.Spec.Policy.NumberOfClusters)
case placementv1beta1.PickFixedPlacementType:
ryanzhang-oss marked this conversation as resolved.
Show resolved Hide resolved
desiredBindings = len(crp.Spec.Policy.ClusterNames)
case placementv1beta1.PickAllPlacementType:
default:
// we don't know the desired bindings for PickAll.
}

Expand Down
54 changes: 31 additions & 23 deletions pkg/controllers/clusterresourceplacementeviction/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,13 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/utils/ptr"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/client/fake"

placementv1alpha1 "go.goms.io/fleet/apis/placement/v1alpha1"
placementv1beta1 "go.goms.io/fleet/apis/placement/v1beta1"
"go.goms.io/fleet/pkg/utils/defaulter"
)

const (
Expand All @@ -31,6 +33,13 @@ const (
testEvictionName = "test-eviction"
)

var (
validationResultCmpOptions = []cmp.Option{
cmp.AllowUnexported(evictionValidationResult{}),
cmpopts.IgnoreFields(metav1.ObjectMeta{}, "ResourceVersion"),
}
)

func TestValidateEviction(t *testing.T) {
testCRP := &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -40,6 +49,18 @@ func TestValidateEviction(t *testing.T) {
Policy: &placementv1beta1.PlacementPolicy{
PlacementType: placementv1beta1.PickAllPlacementType,
},
Strategy: placementv1beta1.RolloutStrategy{
Type: placementv1beta1.RollingUpdateRolloutStrategyType,
RollingUpdate: &placementv1beta1.RollingUpdateConfig{
MaxUnavailable: ptr.To(intstr.FromString(defaulter.DefaultMaxUnavailableValue)),
MaxSurge: ptr.To(intstr.FromString(defaulter.DefaultMaxSurgeValue)),
UnavailablePeriodSeconds: ptr.To(defaulter.DefaultUnavailablePeriodSeconds),
},
ApplyStrategy: &placementv1beta1.ApplyStrategy{
Type: placementv1beta1.ApplyStrategyTypeClientSideApply,
},
},
RevisionHistoryLimit: ptr.To(int32(defaulter.DefaultRevisionHistoryLimitValue)),
},
}
testBinding1 := placementv1beta1.ClusterResourceBinding{
Expand Down Expand Up @@ -153,9 +174,14 @@ func TestValidateEviction(t *testing.T) {
wantErr: nil,
},
{
name: "valid eviction",
name: "CRP with empty spec - valid eviction",
eviction: buildTestEviction(testEvictionName, testCRPName, testClusterName),
crp: testCRP,
crp: &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: "test-crp",
},
Spec: placementv1beta1.ClusterResourcePlacementSpec{},
},
bindings: []placementv1beta1.ClusterResourceBinding{testBinding2},
wantValidationResult: &evictionValidationResult{
isValid: true,
Expand Down Expand Up @@ -185,7 +211,7 @@ func TestValidateEviction(t *testing.T) {
Client: fakeClient,
}
gotValidationResult, gotErr := r.validateEviction(ctx, tc.eviction)
if diff := cmp.Diff(tc.wantValidationResult, gotValidationResult, cmp.AllowUnexported(evictionValidationResult{}), cmpopts.IgnoreFields(placementv1beta1.ClusterResourceBinding{}, "ResourceVersion")); diff != "" {
if diff := cmp.Diff(tc.wantValidationResult, gotValidationResult, validationResultCmpOptions...); diff != "" {
t.Errorf("validateEviction() validation result mismatch (-want, +got):\n%s", diff)
}
gotInvalidCondition := tc.eviction.GetCondition(string(placementv1alpha1.PlacementEvictionConditionTypeValid))
Expand Down Expand Up @@ -503,16 +529,7 @@ func TestExecuteEviction(t *testing.T) {
name: "PickAll CRP, Misconfigured PDB MaxUnavailable specified - eviction not executed",
validationResult: &evictionValidationResult{
crb: availableBinding,
crp: &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: testCRPName,
},
Spec: placementv1beta1.ClusterResourcePlacementSpec{
Policy: &placementv1beta1.PlacementPolicy{
PlacementType: placementv1beta1.PickAllPlacementType,
},
},
},
crp: ptr.To(buildTestPickAllCRP(testCRPName)),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder why sometimes we use this while sometimes we just use testCRP

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

testCRP is only used for validateEviction test where we use defaulter to set default values on the CRP. For all the other tests we can build CRP which doesn't need to have all default values

},
eviction: buildTestEviction(testEvictionName, testCRPName, testClusterName),
pdb: &placementv1alpha1.ClusterResourcePlacementDisruptionBudget{
Expand All @@ -539,16 +556,7 @@ func TestExecuteEviction(t *testing.T) {
name: "PickAll CRP, Misconfigured PDB MinAvailable specified as percentage - eviction not executed",
validationResult: &evictionValidationResult{
crb: availableBinding,
crp: &placementv1beta1.ClusterResourcePlacement{
ObjectMeta: metav1.ObjectMeta{
Name: testCRPName,
},
Spec: placementv1beta1.ClusterResourcePlacementSpec{
Policy: &placementv1beta1.PlacementPolicy{
PlacementType: placementv1beta1.PickAllPlacementType,
},
},
},
crp: ptr.To(buildTestPickAllCRP(testCRPName)),
},
eviction: buildTestEviction(testEvictionName, testCRPName, testClusterName),
pdb: &placementv1alpha1.ClusterResourcePlacementDisruptionBudget{
Expand Down
Loading