Skip to content

Commit

Permalink
Add better logging to the dual writer (#85594)
Browse files Browse the repository at this point in the history
* Make Legacy a public field

* Remove duplicated Create method

* Add logger to dualwriter

* Use klog

* Add comment about selecting the dual writer

* Update pkg/apiserver/rest/dualwriter_mode1.go

Co-authored-by: Arati R. <[email protected]>

* Update pkg/apiserver/rest/dualwriter_mode2.go

Co-authored-by: Arati R. <[email protected]>

* Update pkg/apiserver/rest/dualwriter_mode3.go

Co-authored-by: Arati R. <[email protected]>

* Update pkg/apiserver/rest/dualwriter_mode3.go

Co-authored-by: Arati R. <[email protected]>

* Update pkg/apiserver/rest/dualwriter_mode2.go

Co-authored-by: Arati R. <[email protected]>

* Create error var

* Lint

---------

Co-authored-by: Arati R. <[email protected]>
  • Loading branch information
leonorfmartins and suntala authored Apr 9, 2024
1 parent 4c12d77 commit 8d75dce
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 15 deletions.
14 changes: 8 additions & 6 deletions pkg/apiserver/rest/dualwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ type LegacyStorage interface {
// - rest.CollectionDeleter
type DualWriter struct {
Storage
legacy LegacyStorage
Legacy LegacyStorage
}

type DualWriterMode int
Expand All @@ -80,15 +80,17 @@ var CurrentMode = Mode2

// NewDualWriter returns a new DualWriter.
func NewDualWriter(legacy LegacyStorage, storage Storage) *DualWriter {
//TODO: replace this with
// SelectDualWriter(CurrentMode, legacy, storage)
return &DualWriter{
Storage: storage,
legacy: legacy,
Legacy: legacy,
}
}

// Create overrides the default behavior of the Storage and writes to both the LegacyStorage and Storage.
func (d *DualWriter) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
if legacy, ok := d.legacy.(rest.Creater); ok {
if legacy, ok := d.Legacy.(rest.Creater); ok {
created, err := legacy.Create(ctx, obj, createValidation, options)
if err != nil {
return nil, err
Expand All @@ -113,7 +115,7 @@ func (d *DualWriter) Create(ctx context.Context, obj runtime.Object, createValid

// Update overrides the default behavior of the Storage and writes to both the LegacyStorage and Storage.
func (d *DualWriter) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
if legacy, ok := d.legacy.(rest.Updater); ok {
if legacy, ok := d.Legacy.(rest.Updater); ok {
// Get the previous version from k8s storage (the one)
old, err := d.Get(ctx, name, &metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -168,7 +170,7 @@ func (d *DualWriter) Delete(ctx context.Context, name string, deleteValidation r
// Delete from storage *first* so the item is still exists if a failure happens
obj, async, err := d.Storage.Delete(ctx, name, deleteValidation, options)
if err == nil {
if legacy, ok := d.legacy.(rest.GracefulDeleter); ok {
if legacy, ok := d.Legacy.(rest.GracefulDeleter); ok {
obj, async, err = legacy.Delete(ctx, name, deleteValidation, options)
}
}
Expand All @@ -179,7 +181,7 @@ func (d *DualWriter) Delete(ctx context.Context, name string, deleteValidation r
func (d *DualWriter) DeleteCollection(ctx context.Context, deleteValidation rest.ValidateObjectFunc, options *metav1.DeleteOptions, listOptions *metainternalversion.ListOptions) (runtime.Object, error) {
out, err := d.Storage.DeleteCollection(ctx, deleteValidation, options, listOptions)
if err == nil {
if legacy, ok := d.legacy.(rest.CollectionDeleter); ok {
if legacy, ok := d.Legacy.(rest.CollectionDeleter); ok {
out, err = legacy.DeleteCollection(ctx, deleteValidation, options, listOptions)
}
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/apiserver/rest/dualwriter_mode1.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,20 @@ package rest

import (
"context"
"fmt"
"errors"

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
}

var errNoCreaterMethod = errors.New("legacy storage rest.Creater is missing")

// NewDualWriterMode1 returns a new DualWriter in mode 1.
// Mode 1 represents writing to and reading from LegacyStorage.
func NewDualWriterMode1(legacy LegacyStorage, storage Storage) *DualWriterMode1 {
Expand All @@ -21,9 +24,10 @@ func NewDualWriterMode1(legacy LegacyStorage, storage Storage) *DualWriterMode1

// Create overrides the default behavior of the 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)
legacy, ok := d.Legacy.(rest.Creater)
if !ok {
return nil, fmt.Errorf("legacy storage rest.Creater is missing")
klog.FromContext(ctx).Error(errNoCreaterMethod, "legacy storage rest.Creater is missing")
return nil, errNoCreaterMethod
}

return legacy.Create(ctx, obj, createValidation, options)
Expand Down
9 changes: 6 additions & 3 deletions pkg/apiserver/rest/dualwriter_mode2.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog"
"k8s.io/klog/v2"
)

type DualWriterMode2 struct {
Expand All @@ -23,13 +23,14 @@ func NewDualWriterMode2(legacy LegacyStorage, storage Storage) *DualWriterMode2

// Create overrides the default behavior of the DualWriter and writes to LegacyStorage and Storage.
func (d *DualWriterMode2) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
legacy, ok := d.legacy.(rest.Creater)
legacy, ok := d.Legacy.(rest.Creater)
if !ok {
return nil, fmt.Errorf("legacy storage rest.Creater is missing")
}

created, err := legacy.Create(ctx, obj, createValidation, options)
if err != nil {
klog.FromContext(ctx).Error(err, "unable to create object in legacy storage", "mode", 2)
return created, err
}

Expand All @@ -42,12 +43,14 @@ func (d *DualWriterMode2) Create(ctx context.Context, obj runtime.Object, create
if err != nil {
return created, err
}

// create method expects an empty resource version
accessor.SetResourceVersion("")
accessor.SetUID("")

rsp, err := d.Storage.Create(ctx, c, createValidation, options)
if err != nil {
klog.Error("unable to create object in duplicate storage", "error", err, "mode", Mode2)
klog.FromContext(ctx).Error(err, "unable to create object in Storage", "mode", 2)
}
return rsp, err
}
Expand Down
7 changes: 4 additions & 3 deletions pkg/apiserver/rest/dualwriter_mode3.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/rest"
"k8s.io/klog"
"k8s.io/klog/v2"
)

type DualWriterMode3 struct {
Expand All @@ -22,18 +22,19 @@ func NewDualWriterMode3(legacy LegacyStorage, storage Storage) *DualWriterMode3

// Create overrides the default behavior of the DualWriter and writes to LegacyStorage and Storage.
func (d *DualWriterMode3) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
legacy, ok := d.legacy.(rest.Creater)
legacy, ok := d.Legacy.(rest.Creater)
if !ok {
return nil, fmt.Errorf("legacy storage rest.Creater is missing")
}

created, err := d.Storage.Create(ctx, obj, createValidation, options)
if err != nil {
klog.FromContext(ctx).Error(err, "unable to create object in Storage", "mode", 3)
return created, err
}

if _, err := legacy.Create(ctx, obj, createValidation, options); err != nil {
klog.Error("unable to create object in legacy storage", "error", err)
klog.FromContext(ctx).Error(err, "unable to create object in legacy storage", "mode", 3)
}
return created, nil
}

0 comments on commit 8d75dce

Please sign in to comment.