K8s/Unified: Consolidate generation logic in apistore client (#102260)

This commit is contained in:
Ryan McKinley
2025-03-21 10:45:25 +02:00
committed by GitHub
parent 996ff7d65e
commit 2e2b5942c8
9 changed files with 237 additions and 56 deletions
+2 -16
View File
@@ -3,8 +3,6 @@ package generic
import (
"context"
"github.com/grafana/grafana/pkg/apimachinery/utils"
apiequality "k8s.io/apimachinery/pkg/api/equality"
"k8s.io/apimachinery/pkg/api/meta"
"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
@@ -14,6 +12,8 @@ import (
"k8s.io/apiserver/pkg/storage"
"k8s.io/apiserver/pkg/storage/names"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
"github.com/grafana/grafana/pkg/apimachinery/utils"
)
type genericStrategy struct {
@@ -81,20 +81,6 @@ func (g *genericStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime
} else {
_ = newMeta.SetStatus(status)
}
spec, err := newMeta.GetSpec()
if err != nil {
return
}
oldSpec, err := oldMeta.GetSpec()
if err != nil {
return
}
if !apiequality.Semantic.DeepEqual(spec, oldSpec) {
newMeta.SetGeneration(oldMeta.GetGeneration() + 1)
}
}
func (g *genericStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
@@ -4,12 +4,13 @@ import (
"context"
"testing"
"github.com/grafana/grafana/pkg/apiserver/registry/generic"
"github.com/stretchr/testify/require"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apiserver/pkg/apis/example"
"github.com/grafana/grafana/pkg/apiserver/registry/generic"
)
func TestPrepareForUpdate(t *testing.T) {
@@ -69,37 +70,6 @@ func TestPrepareForUpdate(t *testing.T) {
},
},
},
{
name: "increment generation if spec changes",
newObj: &example.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
Generation: 1,
},
Spec: example.PodSpec{
NodeSelector: map[string]string{"foo": "baz"},
},
Status: example.PodStatus{
Phase: example.PodPhase("Running"),
},
},
oldObj: oldObj.DeepCopy(),
expectedGen: 2,
expectedObj: &example.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
Namespace: "default",
Generation: 2,
},
Spec: example.PodSpec{
NodeSelector: map[string]string{"foo": "baz"},
},
Status: example.PodStatus{
Phase: example.PodPhase("Running"),
},
},
},
}
for _, tc := range testCases {