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

Incrementing EndpointSlice generation when labels change #99750

Merged
merged 1 commit into from Mar 4, 2021
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/registry/discovery/endpointslice/strategy.go
Expand Up @@ -65,7 +65,7 @@ func (endpointSliceStrategy) PrepareForUpdate(ctx context.Context, obj, old runt
newEPS.ObjectMeta = v1.ObjectMeta{}
oldEPS.ObjectMeta = v1.ObjectMeta{}

if !apiequality.Semantic.DeepEqual(newEPS, oldEPS) {
if !apiequality.Semantic.DeepEqual(newEPS, oldEPS) || !apiequality.Semantic.DeepEqual(ogNewMeta.Labels, ogOldMeta.Labels) {
Copy link
Member

Choose a reason for hiding this comment

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

I was actually suggesting going even step further and adding:

else {
  ogNewMeta.Generation = og.OldMeta.Generation
}

But I would like to hear Jordan`s opinion (if that doesn't violate some conventions, backward compatibility, etc.)

Copy link
Member

Choose a reason for hiding this comment

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

rest.BeforeUpdate already resets generation to the current value before calling strategy.PrepareForUpdate:

objectMeta.SetGeneration(oldMeta.GetGeneration())
// Ensure managedFields state is removed unless ServerSideApply is enabled
if !utilfeature.DefaultFeatureGate.Enabled(features.ServerSideApply) {
oldMeta.SetManagedFields(nil)
objectMeta.SetManagedFields(nil)
}
strategy.PrepareForUpdate(ctx, obj, old)

Copy link
Member

Choose a reason for hiding this comment

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

Good point - I missed that thanks.

ogNewMeta.Generation = ogOldMeta.Generation + 1
}

Expand Down
93 changes: 93 additions & 0 deletions pkg/registry/discovery/endpointslice/strategy_test.go
Expand Up @@ -17,9 +17,11 @@ limitations under the License.
package endpointslice

import (
"context"
"testing"

apiequality "k8s.io/apimachinery/pkg/api/equality"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
utilfeature "k8s.io/apiserver/pkg/util/feature"
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/kubernetes/pkg/apis/discovery"
Expand Down Expand Up @@ -600,3 +602,94 @@ func Test_dropDisabledFieldsOnUpdate(t *testing.T) {
})
}
}

func TestPrepareForUpdate(t *testing.T) {
testCases := []struct {
name string
oldEPS *discovery.EndpointSlice
newEPS *discovery.EndpointSlice
expectedEPS *discovery.EndpointSlice
}{
{
name: "unchanged EPS should not increment generation",
oldEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Generation: 1},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
newEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Generation: 1},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
expectedEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Generation: 1},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
},
{
name: "changed endpoints should increment generation",
oldEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Generation: 1},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
newEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Generation: 1},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.5"},
}},
},
expectedEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{Generation: 2},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.5"},
}},
},
},
{
name: "changed labels should increment generation",
oldEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Generation: 1,
Labels: map[string]string{"example": "one"},
},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
newEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Generation: 1,
Labels: map[string]string{"example": "two"},
},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
expectedEPS: &discovery.EndpointSlice{
ObjectMeta: metav1.ObjectMeta{
Generation: 2,
Labels: map[string]string{"example": "two"},
},
Endpoints: []discovery.Endpoint{{
Addresses: []string{"1.2.3.4"},
}},
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
Strategy.PrepareForUpdate(context.TODO(), tc.newEPS, tc.oldEPS)
if !apiequality.Semantic.DeepEqual(tc.newEPS, tc.expectedEPS) {
t.Errorf("Expected %+v\nGot: %+v", tc.expectedEPS, tc.newEPS)
}
})
}
}