Skip to content

Commit

Permalink
Minor patches to ANP CRD types
Browse files Browse the repository at this point in the history
- Nameport.protocol is no longer a pointer --> consistent with k8s core.servicePort
- ExternalEndPoint.ip is a string ---> so it is readable to end user.
- Add "omitempty" to all fields ---> we potentially can have some fields unset.
- added webhook methods ---> allows ExternalEntity to hookup to webhook service.
  • Loading branch information
Su Wang authored and Dyanngg committed Jun 3, 2020
1 parent ad0342b commit d6b52dd
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 54 deletions.
27 changes: 12 additions & 15 deletions pkg/apis/core/v1alpha1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,52 +28,49 @@ 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.
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 {
metav1.TypeMeta `json:",inline"`
// +optional
metav1.ListMeta `json:"metadata,omitempty"`

Items []ExternalEntity `json:"items"`
Items []ExternalEntity `json:"items,omitempty"`
}
82 changes: 82 additions & 0 deletions pkg/apis/core/v1alpha1/webhook.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 6 additions & 39 deletions pkg/apis/core/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d6b52dd

Please sign in to comment.