2024-04-04 20:02:51 +08:00
|
|
|
package rest
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
|
|
|
|
"k8s.io/apimachinery/pkg/runtime"
|
|
|
|
|
"k8s.io/apiserver/pkg/registry/rest"
|
2024-04-09 16:54:09 +08:00
|
|
|
"k8s.io/klog/v2"
|
2024-04-04 20:02:51 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type DualWriterMode3 struct {
|
|
|
|
|
DualWriter
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// NewDualWriterMode3 returns a new DualWriter in mode 3.
|
|
|
|
|
// Mode 3 represents writing to LegacyStorage and Storage and reading from Storage.
|
|
|
|
|
func NewDualWriterMode3(legacy LegacyStorage, storage Storage) *DualWriterMode3 {
|
|
|
|
|
return &DualWriterMode3{*NewDualWriter(legacy, storage)}
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-11 19:41:05 +08:00
|
|
|
// Create overrides the behavior of the generic DualWriter and writes to LegacyStorage and Storage.
|
2024-04-04 20:02:51 +08:00
|
|
|
func (d *DualWriterMode3) Create(ctx context.Context, obj runtime.Object, createValidation rest.ValidateObjectFunc, options *metav1.CreateOptions) (runtime.Object, error) {
|
2024-04-09 16:54:09 +08:00
|
|
|
legacy, ok := d.Legacy.(rest.Creater)
|
2024-04-04 20:02:51 +08:00
|
|
|
if !ok {
|
2024-04-11 19:41:05 +08:00
|
|
|
return nil, errDualWriterCreaterMissing
|
2024-04-04 20:02:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
created, err := d.Storage.Create(ctx, obj, createValidation, options)
|
|
|
|
|
if err != nil {
|
2024-04-09 16:54:09 +08:00
|
|
|
klog.FromContext(ctx).Error(err, "unable to create object in Storage", "mode", 3)
|
2024-04-04 20:02:51 +08:00
|
|
|
return created, err
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if _, err := legacy.Create(ctx, obj, createValidation, options); err != nil {
|
2024-04-09 16:54:09 +08:00
|
|
|
klog.FromContext(ctx).Error(err, "unable to create object in legacy storage", "mode", 3)
|
2024-04-04 20:02:51 +08:00
|
|
|
}
|
|
|
|
|
return created, nil
|
|
|
|
|
}
|
2024-04-09 22:08:20 +08:00
|
|
|
|
2024-04-12 20:21:10 +08:00
|
|
|
// Get overrides the behavior of the generic DualWriter and retrieves an object from Storage.
|
2024-04-09 22:08:20 +08:00
|
|
|
func (d *DualWriterMode3) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
|
2024-04-12 20:21:10 +08:00
|
|
|
return d.Storage.Get(ctx, name, &metav1.GetOptions{})
|
2024-04-09 22:08:20 +08:00
|
|
|
}
|