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

feat: add rest storage of SyncClusterResources and TransformRule #42

Merged
merged 1 commit into from
May 15, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions pkg/registry/search/storage_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ import (

"github.com/KusionStack/karbour/pkg/apis/search"
"github.com/KusionStack/karbour/pkg/registry"
"github.com/KusionStack/karbour/pkg/registry/search/syncclusterresources"
"github.com/KusionStack/karbour/pkg/registry/search/transformrule"
"github.com/KusionStack/karbour/pkg/registry/search/uniresource"
"github.com/KusionStack/karbour/pkg/scheme"
"github.com/KusionStack/karbour/pkg/search/storage"
"github.com/KusionStack/karbour/pkg/search/storage/elasticsearch"
Expand Down Expand Up @@ -60,11 +63,25 @@ func (p RESTStorageProvider) NewRESTStorage(restOptionsGetter generic.RESTOption

func (p RESTStorageProvider) v1beta1Storage(restOptionsGetter generic.RESTOptionsGetter, searchStorageGetter storage.SearchStorageGetter) (map[string]rest.Storage, error) {
v1beta1Storage := map[string]rest.Storage{}
uniResourceStorage, err := NewUniResourceREST(searchStorageGetter)
uniResourceStorage, err := uniresource.NewREST(searchStorageGetter)
if err != nil {
return v1beta1Storage, err
return map[string]rest.Storage{}, err
}
v1beta1Storage["uniresources"] = uniResourceStorage

syncClusterResources, syncClusterResourcesStatus, err := syncclusterresources.NewREST(restOptionsGetter)
if err != nil {
return map[string]rest.Storage{}, err
}
v1beta1Storage["syncclusterresources"] = syncClusterResources
v1beta1Storage["syncclusterresources/status"] = syncClusterResourcesStatus

transformRule, err := transformrule.NewREST(restOptionsGetter)
if err != nil {
return map[string]rest.Storage{}, err
}
v1beta1Storage["transformrules"] = transformRule

return v1beta1Storage, nil
}

Expand Down
96 changes: 96 additions & 0 deletions pkg/registry/search/syncclusterresources/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
Copyright The Karbour Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package syncclusterresources

import (
"context"

"github.com/KusionStack/karbour/pkg/apis/search"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/generic"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
"sigs.k8s.io/structured-merge-diff/v4/fieldpath"
)

// NewREST returns a RESTStorage object that will work against API services.
func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, *StatusREST, error) {
store := &genericregistry.Store{
NewFunc: func() runtime.Object { return &search.SyncClustersResources{} },
NewListFunc: func() runtime.Object { return &search.SyncClustersResourcesList{} },
DefaultQualifiedResource: search.Resource("syncclusterresources"),
CreateStrategy: Strategy,
UpdateStrategy: Strategy,
DeleteStrategy: Strategy,
TableConvertor: rest.NewDefaultTableConvertor(search.Resource("syncclusterresources")),
}
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs}
if err := store.CompleteWithOptions(options); err != nil {
return nil, nil, err
}

statusStore := *store
statusStore.UpdateStrategy = StatusStartegy

return &REST{store}, &StatusREST{&statusStore}, nil
}

type REST struct {
*genericregistry.Store
}

// ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
func (r *REST) ShortNames() []string {
return []string{"scr"}
}

type StatusREST struct {
Store *genericregistry.Store
}

// New returns empty object.
func (r *StatusREST) New() runtime.Object {
return &search.SyncClustersResources{}
}

// Destroy cleans up resources on shutdown.
func (r *StatusREST) Destroy() {
// Given that underlying store is shared with REST,
// we don't destroy it here explicitly.
}

// Get retrieves the object from the storage. It is required to support Patch.
func (r *StatusREST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
return r.Store.Get(ctx, name, options)
}

// Update alters the status subset of an object.
func (r *StatusREST) Update(ctx context.Context, name string, objInfo rest.UpdatedObjectInfo, createValidation rest.ValidateObjectFunc, updateValidation rest.ValidateObjectUpdateFunc, forceAllowCreate bool, options *metav1.UpdateOptions) (runtime.Object, bool, error) {
// We are explicitly setting forceAllowCreate to false in the call to the underlying storage because
// subresources should never allow create on update.
return r.Store.Update(ctx, name, objInfo, createValidation, updateValidation, false, options)
}

// GetResetFields implements rest.ResetFieldsStrategy
func (r *StatusREST) GetResetFields() map[fieldpath.APIVersion]*fieldpath.Set {
return r.Store.GetResetFields()
}

func (r *StatusREST) ConvertToTable(ctx context.Context, object runtime.Object, tableOptions runtime.Object) (*metav1.Table, error) {
return r.Store.ConvertToTable(ctx, object, tableOptions)
}
115 changes: 115 additions & 0 deletions pkg/registry/search/syncclusterresources/strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
Copyright The Karbour Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package syncclusterresources

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/storage/names"

"github.com/KusionStack/karbour/pkg/apis/search"
"github.com/KusionStack/karbour/pkg/scheme"
)

var Strategy = strategy{scheme.Scheme, names.SimpleNameGenerator}

// GetAttrs returns labels.Set, fields.Set, and error in case the given runtime.Object is not a Fischer
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
apiserver, ok := obj.(*search.SyncClustersResources)
if !ok {
return nil, nil, fmt.Errorf("given object is not a Fischer")
}
return labels.Set(apiserver.ObjectMeta.Labels), SelectableFields(apiserver), nil
}

// SelectableFields returns a field set that represents the object.
func SelectableFields(obj *search.SyncClustersResources) fields.Set {
return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false)
}

type strategy struct {
runtime.ObjectTyper
names.NameGenerator
}

func (strategy) NamespaceScoped() bool {
return false
}

func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
obj.(*search.SyncClustersResources).Status = search.SyncClustersResourcesStatus{}
}

func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
obj.(*search.SyncClustersResources).Status = old.(*search.SyncClustersResources).Status
}

func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
return field.ErrorList{}
}

// WarningsOnCreate returns warnings for the creation of the given object.
func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
return nil
}

func (strategy) AllowCreateOnUpdate() bool {
return false
}

func (strategy) AllowUnconditionalUpdate() bool {
return false
}

func (strategy) Canonicalize(obj runtime.Object) {
}

func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return field.ErrorList{}
}

// WarningsOnUpdate returns warnings for the given update.
func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}

type statusStrategy struct {
strategy
}

var StatusStartegy = statusStrategy{Strategy}

// PrepareForUpdate clears fields that are not allowed to be set by end users on update of status
func (statusStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
obj.(*search.SyncClustersResources).Spec = old.(*search.SyncClustersResources).Spec
}

// ValidateUpdate is the default update validation for an end user updating status
func (statusStrategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return field.ErrorList{}
}

// WarningsOnUpdate returns warnings for the given update.
func (statusStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}
52 changes: 52 additions & 0 deletions pkg/registry/search/transformrule/storage.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
Copyright The Karbour Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transformrule

import (
"github.com/KusionStack/karbour/pkg/apis/search"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apiserver/pkg/registry/generic"
genericregistry "k8s.io/apiserver/pkg/registry/generic/registry"
"k8s.io/apiserver/pkg/registry/rest"
)

// NewREST returns a RESTStorage object that will work against API services.
func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, error) {
store := &genericregistry.Store{
NewFunc: func() runtime.Object { return &search.TransformRule{} },
NewListFunc: func() runtime.Object { return &search.TransformRuleList{} },
DefaultQualifiedResource: search.Resource("transformrules"),
CreateStrategy: Strategy,
UpdateStrategy: Strategy,
DeleteStrategy: Strategy,
TableConvertor: rest.NewDefaultTableConvertor(search.Resource("transformrules")),
}
options := &generic.StoreOptions{RESTOptions: optsGetter, AttrFunc: GetAttrs}
if err := store.CompleteWithOptions(options); err != nil {
return nil, err
}
return &REST{store}, nil
}

type REST struct {
*genericregistry.Store
}

// ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource.
func (r *REST) ShortNames() []string {
return []string{"tfr"}
}
92 changes: 92 additions & 0 deletions pkg/registry/search/transformrule/strategy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright The Karbour Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package transformrule

import (
"context"
"fmt"

"k8s.io/apimachinery/pkg/fields"
"k8s.io/apimachinery/pkg/labels"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/util/validation/field"
"k8s.io/apiserver/pkg/registry/generic"
"k8s.io/apiserver/pkg/storage/names"

"github.com/KusionStack/karbour/pkg/apis/search"
"github.com/KusionStack/karbour/pkg/scheme"
)

var Strategy = strategy{scheme.Scheme, names.SimpleNameGenerator}

// GetAttrs returns labels.Set, fields.Set, and error in case the given runtime.Object is not a Fischer
func GetAttrs(obj runtime.Object) (labels.Set, fields.Set, error) {
apiserver, ok := obj.(*search.TransformRule)
if !ok {
return nil, nil, fmt.Errorf("given object is not a Fischer")
}
return labels.Set(apiserver.ObjectMeta.Labels), SelectableFields(apiserver), nil
}

// SelectableFields returns a field set that represents the object.
func SelectableFields(obj *search.TransformRule) fields.Set {
return generic.ObjectMetaFieldsSet(&obj.ObjectMeta, false)
}

type strategy struct {
runtime.ObjectTyper
names.NameGenerator
}

func (strategy) NamespaceScoped() bool {
return false
}

func (strategy) PrepareForCreate(ctx context.Context, obj runtime.Object) {
}

func (strategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) {
}

func (strategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList {
return field.ErrorList{}
}

// WarningsOnCreate returns warnings for the creation of the given object.
func (strategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string {
return nil
}

func (strategy) AllowCreateOnUpdate() bool {
return false
}

func (strategy) AllowUnconditionalUpdate() bool {
return false
}

func (strategy) Canonicalize(obj runtime.Object) {
}

func (strategy) ValidateUpdate(ctx context.Context, obj, old runtime.Object) field.ErrorList {
return field.ErrorList{}
}

// WarningsOnUpdate returns warnings for the given update.
func (strategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string {
return nil
}
Loading