From a2dfbd6117b8ca2e65f58130909867cbf48c386b Mon Sep 17 00:00:00 2001 From: Antonio Ojea Date: Tue, 16 Aug 2022 10:05:41 +0200 Subject: [PATCH] Add IPAddress API to the REST registry ipaddress registry storage don't generate names for ipaddress --- pkg/registry/networking/ipaddress/doc.go | 17 +++ .../networking/ipaddress/storage/storage.go | 63 ++++++++++ pkg/registry/networking/ipaddress/strategy.go | 110 ++++++++++++++++++ .../networking/ipaddress/strategy_test.go | 17 +++ 4 files changed, 207 insertions(+) create mode 100644 pkg/registry/networking/ipaddress/doc.go create mode 100644 pkg/registry/networking/ipaddress/storage/storage.go create mode 100644 pkg/registry/networking/ipaddress/strategy.go create mode 100644 pkg/registry/networking/ipaddress/strategy_test.go diff --git a/pkg/registry/networking/ipaddress/doc.go b/pkg/registry/networking/ipaddress/doc.go new file mode 100644 index 00000000000..f9e67ffc9a4 --- /dev/null +++ b/pkg/registry/networking/ipaddress/doc.go @@ -0,0 +1,17 @@ +/* +Copyright 2022 The Kubernetes 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 ipaddress // import "k8s.io/kubernetes/pkg/registry/networking/ipaddress" diff --git a/pkg/registry/networking/ipaddress/storage/storage.go b/pkg/registry/networking/ipaddress/storage/storage.go new file mode 100644 index 00000000000..1ef77eba09b --- /dev/null +++ b/pkg/registry/networking/ipaddress/storage/storage.go @@ -0,0 +1,63 @@ +/* +Copyright 2022 The Kubernetes 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 storage + +import ( + "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" + "k8s.io/kubernetes/pkg/apis/networking" + "k8s.io/kubernetes/pkg/printers" + printersinternal "k8s.io/kubernetes/pkg/printers/internalversion" + printerstorage "k8s.io/kubernetes/pkg/printers/storage" + "k8s.io/kubernetes/pkg/registry/networking/ipaddress" +) + +// REST implements a RESTStorage for IPAddress against etcd +type REST struct { + *genericregistry.Store +} + +// NewREST returns a RESTStorage object that will work against endpoint slices. +func NewREST(optsGetter generic.RESTOptionsGetter) (*REST, error) { + store := &genericregistry.Store{ + NewFunc: func() runtime.Object { return &networking.IPAddress{} }, + NewListFunc: func() runtime.Object { return &networking.IPAddressList{} }, + DefaultQualifiedResource: networking.Resource("ipaddresses"), + SingularQualifiedResource: networking.Resource("ipaddress"), + + CreateStrategy: ipaddress.Strategy, + UpdateStrategy: ipaddress.Strategy, + DeleteStrategy: ipaddress.Strategy, + + TableConvertor: printerstorage.TableConvertor{TableGenerator: printers.NewTableGenerator().With(printersinternal.AddHandlers)}, + } + options := &generic.StoreOptions{RESTOptions: optsGetter} + if err := store.CompleteWithOptions(options); err != nil { + return nil, err + } + return &REST{store}, nil +} + +// Implement ShortNamesProvider. +var _ rest.ShortNamesProvider = &REST{} + +// ShortNames implements the ShortNamesProvider interface. Returns a list of short names for a resource. +func (r *REST) ShortNames() []string { + return []string{"ip"} +} diff --git a/pkg/registry/networking/ipaddress/strategy.go b/pkg/registry/networking/ipaddress/strategy.go new file mode 100644 index 00000000000..ef21e011e27 --- /dev/null +++ b/pkg/registry/networking/ipaddress/strategy.go @@ -0,0 +1,110 @@ +/* +Copyright 2022 The Kubernetes 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 ipaddress + +import ( + "context" + + "k8s.io/apimachinery/pkg/runtime" + "k8s.io/apimachinery/pkg/util/validation/field" + "k8s.io/apiserver/pkg/registry/rest" + "k8s.io/apiserver/pkg/storage/names" + "k8s.io/kubernetes/pkg/api/legacyscheme" + "k8s.io/kubernetes/pkg/apis/networking" + "k8s.io/kubernetes/pkg/apis/networking/validation" +) + +// ipAddressStrategy implements verification logic for Replication. +type ipAddressStrategy struct { + runtime.ObjectTyper + names.NameGenerator +} + +// noopNameGenerator does not generate names, it just returns the base. +type noopNameGenerator struct{} + +func (noopNameGenerator) GenerateName(base string) string { + return base +} + +// Strategy is the default logic that applies when creating and updating Replication IPAddress objects. +var Strategy = ipAddressStrategy{legacyscheme.Scheme, noopNameGenerator{}} + +// Strategy should implement rest.RESTCreateStrategy +var _ rest.RESTCreateStrategy = Strategy + +// Strategy should implement rest.RESTUpdateStrategy +var _ rest.RESTUpdateStrategy = Strategy + +// NamespaceScoped returns false because all IPAddresses is cluster scoped. +func (ipAddressStrategy) NamespaceScoped() bool { + return false +} + +// PrepareForCreate clears the status of an IPAddress before creation. +func (ipAddressStrategy) PrepareForCreate(ctx context.Context, obj runtime.Object) { + _ = obj.(*networking.IPAddress) + +} + +// PrepareForUpdate clears fields that are not allowed to be set by end users on update. +func (ipAddressStrategy) PrepareForUpdate(ctx context.Context, obj, old runtime.Object) { + newIPAddress := obj.(*networking.IPAddress) + oldIPAddress := old.(*networking.IPAddress) + + _, _ = newIPAddress, oldIPAddress +} + +// Validate validates a new IPAddress. +func (ipAddressStrategy) Validate(ctx context.Context, obj runtime.Object) field.ErrorList { + ipAddress := obj.(*networking.IPAddress) + err := validation.ValidateIPAddress(ipAddress) + return err +} + +// Canonicalize normalizes the object after validation. +func (ipAddressStrategy) Canonicalize(obj runtime.Object) { +} + +// AllowCreateOnUpdate is false for IPAddress; this means POST is needed to create one. +func (ipAddressStrategy) AllowCreateOnUpdate() bool { + return false +} + +// ValidateUpdate is the default update validation for an end user. +func (ipAddressStrategy) ValidateUpdate(ctx context.Context, new, old runtime.Object) field.ErrorList { + newIPAddress := new.(*networking.IPAddress) + oldIPAddress := old.(*networking.IPAddress) + errList := validation.ValidateIPAddress(newIPAddress) + errList = append(errList, validation.ValidateIPAddressUpdate(newIPAddress, oldIPAddress)...) + return errList +} + +// AllowUnconditionalUpdate is the default update policy for IPAddress objects. +func (ipAddressStrategy) AllowUnconditionalUpdate() bool { + return true +} + +// WarningsOnCreate returns warnings for the creation of the given object. +func (ipAddressStrategy) WarningsOnCreate(ctx context.Context, obj runtime.Object) []string { + return nil +} + +// WarningsOnUpdate returns warnings for the given update. +func (ipAddressStrategy) WarningsOnUpdate(ctx context.Context, obj, old runtime.Object) []string { + return nil +} diff --git a/pkg/registry/networking/ipaddress/strategy_test.go b/pkg/registry/networking/ipaddress/strategy_test.go new file mode 100644 index 00000000000..5b09bc32c22 --- /dev/null +++ b/pkg/registry/networking/ipaddress/strategy_test.go @@ -0,0 +1,17 @@ +/* +Copyright 2022 The Kubernetes 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 ipaddress