grafana/pkg/apiserver/rest/dualwriter_mode1.go
Leonor Oliveira 9baf96dbd2
Storage: dualwriter update implementation (#85844)
* Add update methods for the dual writer

* improve errors

* [WIP] add tests for the update method

* Move example package to its own package so it can be used by the rest package. Finish tests

* Add codeowners

* Use Pod as a dummy resource

* :int

* Lint

* [REVIEW] rename var

* [REVIEW] don't rely on legacy storage at all in mode4

* Update pkg/apiserver/rest/dualwriter_mode2.go

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>

* [REVIEW] improve comment

* Update pkg/apiserver/rest/dualwriter_mode1.go

Co-authored-by: Dan Cech <dcech@grafana.com>

* [REVIEW] improve mode3

* Lint

* Move test files

* Update pkg/apiserver/rest/dualwriter_mode2.go

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>

* Lint

* Update pkg/apiserver/rest/dualwriter_mode4_test.go

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>

* Fix error

* Lint

* Update pkg/apiserver/rest/dualwriter_mode2.go

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>

* Don't set the flag to true as updatedObj creates an object in case it's not found

* Lint

* Lint

* Add tests on update

* Lint

---------

Co-authored-by: Arati R. <33031346+suntala@users.noreply.github.com>
Co-authored-by: Dan Cech <dcech@grafana.com>
2024-04-17 08:32:34 -06:00

77 lines
3.0 KiB
Go

package rest
import (
"context"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog/v2"
)
type DualWriterMode1 struct {
DualWriter
}
// NewDualWriterMode1 returns a new DualWriter in mode 1.
// Mode 1 represents writing to and reading from LegacyStorage.
func NewDualWriterMode1(legacy LegacyStorage, storage Storage) *DualWriterMode1 {
return &DualWriterMode1{*NewDualWriter(legacy, storage)}
}
// Create overrides the behavior of the generic DualWriter and writes only to LegacyStorage.
func (d *DualWriterMode1) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
legacy, ok := d.Legacy.(rest.Creater)
if !ok {
klog.FromContext(ctx).Error(errDualWriterCreaterMissing, "legacy storage rest.Creater is missing")
return nil, errDualWriterCreaterMissing
}
return legacy.Create(ctx, obj, createValidation, options)
}
// Get overrides the behavior of the generic DualWriter and reads only from LegacyStorage.
func (d *DualWriterMode1) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return d.Legacy.Get(ctx, name, options)
}
// List overrides the behavior of the generic DualWriter and reads only from LegacyStorage.
func (d *DualWriterMode1) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
legacy, ok := d.Legacy.(rest.Lister)
if !ok {
return nil, errDualWriterListerMissing
}
return legacy.List(ctx, options)
}
func (d *DualWriterMode1) Delete(ctx context.Context, name string, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
legacy, ok := d.Legacy.(rest.GracefulDeleter)
if !ok {
return nil, false, errDualWriterDeleterMissing
}
return legacy.Delete(ctx, name, deleteValidation, options)
}
// DeleteCollection overrides the behavior of the generic DualWriter and deletes only from LegacyStorage.
func (d *DualWriterMode1) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
legacy, ok := d.Legacy.(rest.CollectionDeleter)
if !ok {
return nil, errDualWriterCollectionDeleterMissing
}
return legacy.DeleteCollection(ctx, deleteValidation, options, listOptions)
}
func (d *DualWriterMode1) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
legacy, ok := d.Legacy.(rest.Updater)
if !ok {
klog.FromContext(ctx).Error(errDualWriterUpdaterMissing, "legacy storage rest.Updater is missing")
return nil, false, errDualWriterUpdaterMissing
}
return legacy.Update(ctx, name, objInfo, createValidation, updateValidation, forceAllowCreate, options)
}