diff --git a/pkg/apis/core/v1alpha1/types.go b/pkg/apis/core/v1alpha1/types.go index 178533efafd..c5b28568323 100644 --- a/pkg/apis/core/v1alpha1/types.go +++ b/pkg/apis/core/v1alpha1/types.go @@ -28,27 +28,27 @@ type ExternalEntity struct { // Standard metadata of the object. metav1.ObjectMeta `json:"metadata,omitempty"` // Desired state of the external entity. - Spec ExternalEntitySpec `json:"spec"` + Spec ExternalEntitySpec `json:"spec,omitempty"` } // ExternalEntitySpec defines the desired state for ExternalEntity. type ExternalEntitySpec struct { // Endpoints is a list of external endpoints associated with this entity. - Endpoints []ExternalEndpoint `json:"endpoints"` + Endpoints []Endpoint `json:"endpoints,omitempty"` // ExternalNode is the opaque identifier of the agent/controller responsible // for additional computation of this external entity. - ExternalNode string `json:"externalNode"` + ExternalNode string `json:"externalNode,omitempty"` } -// ExternalEndpoint refers to an endpoint associated with the ExternalEntity. -type ExternalEndpoint struct { +// Endpoint refers to an endpoint associated with the ExternalEntity. +type Endpoint struct { // IP associated with this endpoint. - IP IPAddress `json:"ip"` + IP string `json:"ip,omitempty"` // Name identifies this endpoint. Could be the interface name in case of VMs. // +optional - Name string `json:"name"` + Name string `json:"name,omitempty"` // Ports maintain the list of named ports. - Ports []NamedPort `json:"ports"` + Ports []NamedPort `json:"ports,omitempty"` } // NamedPort describes the port and protocol to match in a rule. @@ -56,18 +56,15 @@ type NamedPort struct { // The protocol (TCP, UDP, or SCTP) which traffic must match. // If not specified, this field defaults to TCP. // +optional - Protocol *v1.Protocol `json:"protocol"` + Protocol v1.Protocol `json:"protocol,omitempty"` // The port on the given protocol. // +optional - Port int32 `json:"port"` + Port int32 `json:"port,omitempty"` // Name associated with the Port. // +optional - Name string `json:"name"` + Name string `json:"name,omitempty"` } -// IPAddress describes a single IP address. Either an IPv4 or IPv6 address must be set. -type IPAddress []byte - // +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object type ExternalEntityList struct { @@ -75,5 +72,5 @@ type ExternalEntityList struct { // +optional metav1.ListMeta `json:"metadata,omitempty"` - Items []ExternalEntity `json:"items"` + Items []ExternalEntity `json:"items,omitempty"` } diff --git a/pkg/apis/core/v1alpha1/webhook.go b/pkg/apis/core/v1alpha1/webhook.go new file mode 100644 index 00000000000..f576e53622b --- /dev/null +++ b/pkg/apis/core/v1alpha1/webhook.go @@ -0,0 +1,82 @@ +// Copyright 2020 Antrea 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 v1alpha1 + +import ( + "fmt" + "reflect" + + "k8s.io/apimachinery/pkg/runtime" +) + +// WebhookImpl implements webhook validator of a resource. +type WebhookImpl interface { + Default(in *ExternalEntity) + ValidateCreate(in *ExternalEntity) error + ValidateUpdate(in *ExternalEntity, old runtime.Object) error + ValidateDelete(in *ExternalEntity) error +} + +var ( + externalEntityWebhook WebhookImpl +) + +// RegisterWebhook registers webhook implementation of a resource. +func RegisterWebhook(in runtime.Object, webhook WebhookImpl) error { + switch in.(type) { + case *ExternalEntity: + if externalEntityWebhook != nil { + return fmt.Errorf("externalEntityWebhook already registered") + } + externalEntityWebhook = webhook + default: + return fmt.Errorf("unknown type %s to register webhook", reflect.TypeOf(in).Elem().Name()) + } + return nil +} + +// Default implements webhook Defaulter. +func (in *ExternalEntity) Default() { + if externalEntityWebhook != nil { + externalEntityWebhook.Default(in) + } + return +} + +// ValidateCreate implements webhook Validator. +func (in *ExternalEntity) ValidateCreate() error { + if externalEntityWebhook != nil { + return externalEntityWebhook.ValidateCreate(in) + } + return nil +} + +// ValidateUpdate implements webhook Validator. +func (in *ExternalEntity) ValidateUpdate(old runtime.Object) error { + if externalEntityWebhook != nil { + return externalEntityWebhook.ValidateUpdate(in, old) + } + + return nil +} + +// ValidateDelete implements webhook Validator. +func (in *ExternalEntity) ValidateDelete() error { + if externalEntityWebhook != nil { + return externalEntityWebhook.ValidateDelete(in) + } + return nil +} diff --git a/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go b/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go index 8209d90eb42..670754f1ac4 100644 --- a/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go +++ b/pkg/apis/core/v1alpha1/zz_generated.deepcopy.go @@ -19,34 +19,26 @@ package v1alpha1 import ( - v1 "k8s.io/api/core/v1" runtime "k8s.io/apimachinery/pkg/runtime" ) // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in *ExternalEndpoint) DeepCopyInto(out *ExternalEndpoint) { +func (in *Endpoint) DeepCopyInto(out *Endpoint) { *out = *in - if in.IP != nil { - in, out := &in.IP, &out.IP - *out = make(IPAddress, len(*in)) - copy(*out, *in) - } if in.Ports != nil { in, out := &in.Ports, &out.Ports *out = make([]NamedPort, len(*in)) - for i := range *in { - (*in)[i].DeepCopyInto(&(*out)[i]) - } + copy(*out, *in) } return } -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new ExternalEndpoint. -func (in *ExternalEndpoint) DeepCopy() *ExternalEndpoint { +// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new Endpoint. +func (in *Endpoint) DeepCopy() *Endpoint { if in == nil { return nil } - out := new(ExternalEndpoint) + out := new(Endpoint) in.DeepCopyInto(out) return out } @@ -116,7 +108,7 @@ func (in *ExternalEntitySpec) DeepCopyInto(out *ExternalEntitySpec) { *out = *in if in.Endpoints != nil { in, out := &in.Endpoints, &out.Endpoints - *out = make([]ExternalEndpoint, len(*in)) + *out = make([]Endpoint, len(*in)) for i := range *in { (*in)[i].DeepCopyInto(&(*out)[i]) } @@ -134,34 +126,9 @@ func (in *ExternalEntitySpec) DeepCopy() *ExternalEntitySpec { return out } -// DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. -func (in IPAddress) DeepCopyInto(out *IPAddress) { - { - in := &in - *out = make(IPAddress, len(*in)) - copy(*out, *in) - return - } -} - -// DeepCopy is an autogenerated deepcopy function, copying the receiver, creating a new IPAddress. -func (in IPAddress) DeepCopy() IPAddress { - if in == nil { - return nil - } - out := new(IPAddress) - in.DeepCopyInto(out) - return *out -} - // DeepCopyInto is an autogenerated deepcopy function, copying the receiver, writing into out. in must be non-nil. func (in *NamedPort) DeepCopyInto(out *NamedPort) { *out = *in - if in.Protocol != nil { - in, out := &in.Protocol, &out.Protocol - *out = new(v1.Protocol) - **out = **in - } return }