Skip to content

Commit

Permalink
feat: support external service (#1306)
Browse files Browse the repository at this point in the history
  • Loading branch information
lingsamuel authored Oct 20, 2022
1 parent 7a89a0a commit 5c79821
Show file tree
Hide file tree
Showing 19 changed files with 1,880 additions and 125 deletions.
75 changes: 53 additions & 22 deletions pkg/kube/apisix/apis/config/v2/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,17 @@ type ApisixRouteHTTP struct {
// Backends represents potential backends to proxy after the route
// rule matched. When number of backends are more than one, traffic-split
// plugin in APISIX will be used to split traffic based on the backend weight.
Backends []ApisixRouteHTTPBackend `json:"backends,omitempty" yaml:"backends,omitempty"`
Backends []ApisixRouteHTTPBackend `json:"backends,omitempty" yaml:"backends,omitempty"`
// Upstreams refer to ApisixUpstream CRD
Upstreams []ApisixRouteUpstreamReference `json:"upstreams,omitempty" yaml:"upstreams,omitempty"`

Websocket bool `json:"websocket" yaml:"websocket"`
PluginConfigName string `json:"plugin_config_name,omitempty" yaml:"plugin_config_name,omitempty"`
Plugins []ApisixRoutePlugin `json:"plugins,omitempty" yaml:"plugins,omitempty"`
Authentication ApisixRouteAuthentication `json:"authentication,omitempty" yaml:"authentication,omitempty"`
}

// ApisixRouteHTTPBackend represents a HTTP backend (a Kuberentes Service).
// ApisixRouteHTTPBackend represents an HTTP backend (a Kubernetes Service).
type ApisixRouteHTTPBackend struct {
// The name (short) of the service, note cross namespace is forbidden,
// so be sure the ApisixRoute and Service are in the same namespace.
Expand All @@ -93,6 +96,13 @@ type ApisixRouteHTTPBackend struct {
Subset string `json:"subset,omitempty" yaml:"subset,omitempty"`
}

// ApisixRouteUpstreamReference contains a ApisixUpstream CRD reference
type ApisixRouteUpstreamReference struct {
Name string `json:"name,omitempty" yaml:"name"`
// +optional
Weight *int `json:"weight,omitempty" yaml:"weight"`
}

// ApisixRouteHTTPMatch represents the match condition for hitting this route.
type ApisixRouteHTTPMatch struct {
// URI path predicates, at least one path should be
Expand Down Expand Up @@ -166,6 +176,20 @@ type ApisixRoutePlugin struct {
// any plugins.
type ApisixRoutePluginConfig map[string]interface{}

func (p ApisixRoutePluginConfig) DeepCopyInto(out *ApisixRoutePluginConfig) {
b, _ := json.Marshal(&p)
_ = json.Unmarshal(b, out)
}

func (p *ApisixRoutePluginConfig) DeepCopy() *ApisixRoutePluginConfig {
if p == nil {
return nil
}
out := new(ApisixRoutePluginConfig)
p.DeepCopyInto(out)
return out
}

// ApisixRouteAuthentication is the authentication-related
// configuration in ApisixRoute.
type ApisixRouteAuthentication struct {
Expand All @@ -189,20 +213,6 @@ type ApisixRouteAuthenticationJwtAuth struct {
Cookie string `json:"cookie,omitempty" yaml:"cookie,omitempty"`
}

func (p ApisixRoutePluginConfig) DeepCopyInto(out *ApisixRoutePluginConfig) {
b, _ := json.Marshal(&p)
_ = json.Unmarshal(b, out)
}

func (p *ApisixRoutePluginConfig) DeepCopy() *ApisixRoutePluginConfig {
if p == nil {
return nil
}
out := new(ApisixRoutePluginConfig)
p.DeepCopyInto(out)
return out
}

// ApisixRouteStream is the configuration for level 4 route
type ApisixRouteStream struct {
// The rule name, cannot be empty.
Expand Down Expand Up @@ -238,7 +248,6 @@ type ApisixRouteStreamBackend struct {
}

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

// ApisixRouteList contains a list of ApisixRoute.
type ApisixRouteList struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
Expand All @@ -250,7 +259,6 @@ type ApisixRouteList struct {
// +genclient:nonNamespaced
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:subresource:status

// ApisixClusterConfig is the Schema for the ApisixClusterConfig resource.
// An ApisixClusterConfig is used to identify an APISIX cluster, it's a
// ClusterScoped resource so the name is unique.
Expand Down Expand Up @@ -310,7 +318,6 @@ type ApisixClusterAdminConfig struct {
}

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

// ApisixClusterConfigList contains a list of ApisixClusterConfig.
type ApisixClusterConfigList struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
Expand All @@ -322,7 +329,6 @@ type ApisixClusterConfigList struct {
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:subresource:status

// ApisixConsumer is the Schema for the ApisixConsumer resource.
// An ApisixConsumer is used to identify a consumer.
type ApisixConsumer struct {
Expand Down Expand Up @@ -418,7 +424,6 @@ type ApisixConsumerHMACAuthValue struct {
}

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

// ApisixConsumerList contains a list of ApisixConsumer.
type ApisixConsumerList struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
Expand All @@ -443,13 +448,18 @@ type ApisixUpstream struct {

// ApisixUpstreamSpec describes the specification of ApisixUpstream.
type ApisixUpstreamSpec struct {
// ExternalNodes contains external nodes the Upstream should use
// If this field is set, the upstream will use these nodes directly without any further resolves
// +optional
ExternalNodes []ApisixUpstreamExternalNode `json:"externalNodes,omitempty" yaml:"externalNodes,omitempty"`

ApisixUpstreamConfig `json:",inline" yaml:",inline"`

PortLevelSettings []PortLevelSettings `json:"portLevelSettings,omitempty" yaml:"portLevelSettings,omitempty"`
}

// ApisixUpstreamConfig contains rich features on APISIX Upstream, for instance
// load balancer, health check and etc.
// load balancer, health check, etc.
type ApisixUpstreamConfig struct {
// LoadBalancer represents the load balancer configuration for Kubernetes Service.
// The default strategy is round robin.
Expand Down Expand Up @@ -483,6 +493,27 @@ type ApisixUpstreamConfig struct {
Subsets []ApisixUpstreamSubset `json:"subsets,omitempty" yaml:"subsets,omitempty"`
}

// ApisixUpstreamExternalType is the external service type
type ApisixUpstreamExternalType string

const (
// ExternalTypeDomain type is a domain
// +k8s:deepcopy-gen=false
ExternalTypeDomain ApisixUpstreamExternalType = "Domain"

// ExternalTypeService type is a K8s ExternalName service
// +k8s:deepcopy-gen=false
ExternalTypeService ApisixUpstreamExternalType = "Service"
)

// ApisixUpstreamExternalNode is the external node conf
type ApisixUpstreamExternalNode struct {
Name string `json:"name,omitempty" yaml:"name"`
Type ApisixUpstreamExternalType `json:"type,omitempty" yaml:"type"`
// +optional
Weight *int `json:"weight,omitempty" yaml:"weight"`
}

// ApisixUpstreamSubset defines a single endpoints group of one Service.
type ApisixUpstreamSubset struct {
// Name is the name of subset.
Expand Down
72 changes: 72 additions & 0 deletions pkg/kube/apisix/apis/config/v2/zz_generated.deepcopy.go

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

12 changes: 10 additions & 2 deletions pkg/kube/apisix_upstream.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ package kube
import (
"errors"

"k8s.io/apimachinery/pkg/labels"

"github.com/apache/apisix-ingress-controller/pkg/config"
configv2 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2"
configv2beta3 "github.com/apache/apisix-ingress-controller/pkg/kube/apisix/apis/config/v2beta3"
Expand All @@ -28,9 +30,11 @@ import (
// it aims at to be compatible with different ApisixUpstream versions.
type ApisixUpstreamLister interface {
// V2beta3 gets the ApisixUpstream in apisix.apache.org/v2beta3.
V2beta3(string, string) (ApisixUpstream, error)
V2beta3(namespace, name string) (ApisixUpstream, error)
// V2 gets the ApisixUpstream in apisix.apache.org/v2.
V2(string, string) (ApisixUpstream, error)
V2(namespace, name string) (ApisixUpstream, error)
// ListV2 gets v2.ApisixUpstreams
ListV2(namespace string) ([]*configv2.ApisixUpstream, error)
}

// ApisixUpstreamInformer is an encapsulation for the informer of ApisixUpstream,
Expand Down Expand Up @@ -120,6 +124,10 @@ func (l *apisixUpstreamLister) V2(namespace, name string) (ApisixUpstream, error
}, nil
}

func (l *apisixUpstreamLister) ListV2(namespace string) ([]*configv2.ApisixUpstream, error) {
return l.v2Lister.ApisixUpstreams(namespace).List(labels.Everything())
}

// MustNewApisixUpstream creates a kube.ApisixUpstream object according to the
// type of obj.
func MustNewApisixUpstream(obj interface{}) ApisixUpstream {
Expand Down
Loading

0 comments on commit 5c79821

Please sign in to comment.