Skip to content
This repository has been archived by the owner on Oct 20, 2023. It is now read-only.

Commit

Permalink
apis/specs: Add traffic specs for v1alpha4
Browse files Browse the repository at this point in the history
Adds TCPRoute and UDPRoute specs to v1alpha4 api
version.

Signed-off-by: Shashank Ram <shashr2204@gmail.com>
  • Loading branch information
shashankram committed Dec 15, 2020
1 parent 468046e commit c8f89f6
Show file tree
Hide file tree
Showing 34 changed files with 2,471 additions and 14 deletions.
96 changes: 83 additions & 13 deletions crds/specs.yaml
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
metadata:
name: httproutegroups.specs.smi-spec.io
spec:
spec:
group: specs.smi-spec.io
scope: Namespaced
names:
scope: Namespaced
names:
kind: HTTPRouteGroup
shortNames:
shortNames:
- htr
plural: httproutegroups
singular: httproutegroup
version: v1alpha3
version: v1alpha4
versions:
- name: v1alpha3
- name: v1alpha4
served: true
storage: true
- name: v1alpha3
served: false
storage: false
- name: v1alpha2
served: false
storage: false
Expand Down Expand Up @@ -72,25 +75,92 @@ spec:
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
metadata:
name: tcproutes.specs.smi-spec.io
spec:
spec:
group: specs.smi-spec.io
scope: Namespaced
names:
scope: Namespaced
names:
kind: TCPRoute
shortNames:
shortNames:
- tr
plural: tcproutes
singular: tcproute
version: v1alpha3
version: v1alpha4
versions:
- name: v1alpha4
served: true
storage: true
- name: v1alpha3
served: false
storage: false
- name: v1alpha2
served: false
storage: false
- name: v1alpha1
served: false
storage: false
validation:
openAPIV3Schema:
properties:
spec:
required:
- matches
properties:
matches:
description: Match conditions of this route.
type: object
required:
- ports
properties:
ports:
description: Port numbers to match TCP traffic.
type: array
items:
type: integer
---
apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
name: udproutes.specs.smi-spec.io
spec:
group: specs.smi-spec.io
scope: Namespaced
names:
kind: UDPRoute
shortNames:
- ur
plural: udproutes
singular: udproute
version: v1alpha4
versions:
- name: v1alpha4
served: true
storage: true
- name: v1alpha3
served: false
storage: false
- name: v1alpha2
served: false
storage: false
- name: v1alpha1
served: false
storage: false
validation:
openAPIV3Schema:
properties:
spec:
required:
- matches
properties:
matches:
description: Match conditions of this route.
type: object
required:
- ports
properties:
ports:
description: Port numbers to match UDP traffic.
type: array
items:
type: integer
2 changes: 1 addition & 1 deletion hack/update-codegen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function generate_client() {
}

echo "##### Generating specs client ######"
generate_client "specs" "v1alpha1,v1alpha2,v1alpha3"
generate_client "specs" "v1alpha1,v1alpha2,v1alpha3,v1alpha4"

echo ""
echo "###### Generating split client ######"
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/specs/v1alpha4/doc.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// +k8s:deepcopy-gen=package
// +groupName=specs.smi-spec.io

package v1alpha4
34 changes: 34 additions & 0 deletions pkg/apis/specs/v1alpha4/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package v1alpha4

import (
"encoding/json"
"errors"
)

// UnmarshalJSON converts a given array of single value maps to one map
func (h *httpHeaders) UnmarshalJSON(b []byte) error {
*h = make(map[string]string)
var temp []map[string]string
if err := json.Unmarshal(b, &temp); err != nil {
return err
}

for _, m := range temp {
if len(m) > 1 {
return errors.New("incorrect length of keyval")
}
for key, val := range m {
(*h)[key] = val
}
}
return nil
}

// MarshalJSON converts a given map to array of single value maps
func (h httpHeaders) MarshalJSON() ([]byte, error) {
var returnArr []map[string]string
for key, val := range h {
returnArr = append(returnArr, map[string]string{key: val})
}
return json.Marshal(returnArr)
}
83 changes: 83 additions & 0 deletions pkg/apis/specs/v1alpha4/http_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package v1alpha4

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// HTTPRouteGroup is used to describe HTTP/1 and HTTP/2 traffic.
// It enumerates the routes that can be served by an application.
type HTTPRouteGroup struct {
metav1.TypeMeta `json:",inline"`

// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec HTTPRouteGroupSpec `json:"spec"`
}

// HTTPRouteGroupSpec is the specification for a HTTPRouteGroup
type HTTPRouteGroupSpec struct {
// Routes for inbound traffic
Matches []HTTPMatch `json:"matches,omitempty"`
}

// HTTPMatch defines an individual route for HTTP traffic
type HTTPMatch struct {
// Name is the name of the match for referencing in a TrafficTarget
Name string `json:"name,omitempty"`

// Methods for inbound traffic as defined in RFC 7231
// https://tools.ietf.org/html/rfc7231#section-4
Methods []string `json:"methods,omitempty"`

// PathRegex is a regular expression defining the route
PathRegex string `json:"pathRegex,omitempty"`

// Headers is a list of headers used to match HTTP traffic
Headers httpHeaders `json:"headers,omitempty"`
}

// httpHeaders is a map of key/value pairs which match HTTP header name and value
type httpHeaders map[string]string

// HTTPRouteMethod are methods allowed by the route
type HTTPRouteMethod string

const (
// HTTPRouteMethodAll is a wildcard for all HTTP methods
HTTPRouteMethodAll HTTPRouteMethod = "*"
// HTTPRouteMethodGet HTTP GET method
HTTPRouteMethodGet HTTPRouteMethod = "GET"
// HTTPRouteMethodHead HTTP HEAD method
HTTPRouteMethodHead HTTPRouteMethod = "HEAD"
// HTTPRouteMethodPut HTTP PUT method
HTTPRouteMethodPut HTTPRouteMethod = "PUT"
// HTTPRouteMethodPost HTTP POST method
HTTPRouteMethodPost HTTPRouteMethod = "POST"
// HTTPRouteMethodDelete HTTP DELETE method
HTTPRouteMethodDelete HTTPRouteMethod = "DELETE"
// HTTPRouteMethodConnect HTTP CONNECT method
HTTPRouteMethodConnect HTTPRouteMethod = "CONNECT"
// HTTPRouteMethodOptions HTTP OPTIONS method
HTTPRouteMethodOptions HTTPRouteMethod = "OPTIONS"
// HTTPRouteMethodTrace HTTP TRACE method
HTTPRouteMethodTrace HTTPRouteMethod = "TRACE"
// HTTPRouteMethodPatch HTTP PATCH method
HTTPRouteMethodPatch HTTPRouteMethod = "PATCH"
)

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// HTTPRouteGroupList satisfy K8s code gen requirements
type HTTPRouteGroupList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []HTTPRouteGroup `json:"items"`
}
50 changes: 50 additions & 0 deletions pkg/apis/specs/v1alpha4/register.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package v1alpha4

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"

ts "github.com/servicemeshinterface/smi-sdk-go/pkg/apis/specs"
)

// SchemeGroupVersion is the identifier for the API which includes
// the name of the group and the version of the API
var SchemeGroupVersion = schema.GroupVersion{
Group: ts.GroupName,
Version: "v1alpha4",
}

// Kind takes an unqualified kind and returns back a Group qualified GroupKind
func Kind(kind string) schema.GroupKind {
return SchemeGroupVersion.WithKind(kind).GroupKind()
}

// Resource takes an unqualified resource and returns a Group qualified GroupResource
func Resource(resource string) schema.GroupResource {
return SchemeGroupVersion.WithResource(resource).GroupResource()
}

var (
// SchemeBuilder collects functions that add things to a scheme. It's to allow
// code to compile without explicitly referencing generated types. You should
// declare one in each package that will have generated deep copy or conversion
// functions.
SchemeBuilder = runtime.NewSchemeBuilder(addKnownTypes)

// AddToScheme applies all the stored functions to the scheme. A non-nil error
// indicates that one function failed and the attempt was abandoned.
AddToScheme = SchemeBuilder.AddToScheme
)

// Adds the list of known types to Scheme.
func addKnownTypes(scheme *runtime.Scheme) error {
scheme.AddKnownTypes(SchemeGroupVersion,
&HTTPRouteGroup{},
&HTTPRouteGroupList{},
&TCPRoute{},
&TCPRouteList{},
)
metav1.AddToGroupVersion(scheme, SchemeGroupVersion)
return nil
}
46 changes: 46 additions & 0 deletions pkg/apis/specs/v1alpha4/tcp_route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package v1alpha4

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// +genclient
// +genclient:noStatus
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// TCPRoute is used to describe TCP inbound connections
type TCPRoute struct {
metav1.TypeMeta `json:",inline"`

// Standard object's metadata.
// More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
// +optional
metav1.ObjectMeta `json:"metadata,omitempty"`

Spec TCPRouteSpec `json:"spec,omitempty"`
}

// TCPRouteSpec is the specification of a TCPRoute
type TCPRouteSpec struct {
// Route match for inbound traffic
Matches TCPMatch `json:"matches,omitempty"`
}

// TCPMatch defines an individual route for TCP traffic
type TCPMatch struct {
// Name is the name of the match for referencing in a TrafficTarget
Name string `json:"name,omitempty"`

// Ports to allow inbound traffic on
Ports []int `json:"ports,omitempty"`
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object

// TCPRouteList satisfy K8s code gen requirements
type TCPRouteList struct {
metav1.TypeMeta `json:",inline"`
metav1.ListMeta `json:"metadata"`

Items []TCPRoute `json:"items"`
}
Loading

0 comments on commit c8f89f6

Please sign in to comment.