Skip to content

Commit

Permalink
feat(gateway): impl gateway crd and opt route&domain crd (#313)
Browse files Browse the repository at this point in the history
* feat(gateway): impl gateway crd and opt route&domain crd

* feat(gateway):update manifests

* feat(gateway): update app route

* feat(gateway): update bucket route

* feat(gateway): opt controller
  • Loading branch information
skyoct authored Sep 15, 2022
1 parent ab8c969 commit b3cb361
Show file tree
Hide file tree
Showing 14 changed files with 586 additions and 151 deletions.
37 changes: 24 additions & 13 deletions controllers/gateway/api/v1/domain_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,39 @@ import (
// DomainSpec defines the desired state of Domain
type DomainSpec struct {

// Preferred 是提供域名使用位置的推荐项,是字符串类型,长度大于1小于10,必须存在
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=10
// Domain是域名,必须存在,匹配域名规则
// +kubebuilder:validation:Pattern="^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$"
// +kubebuilder:validation:Required
Preferred string `json:"preferred"`
Domain string `json:"domain"`

// Region 是域名设定的解析区域,是字符串类型,长度大于1小于10,可选存在
// +kubebuilder:validation:MinLength=1
// +kubebuilder:validation:MaxLength=10
// +kubebuilder:validation:Optional
Region string `json:"region,omitempty"`
// BackendType是后端服务类型,必须存在APP;bucket;WEBSITE
// +kubebuilder:validation:Enum=app;bucket;website
// +kubebuilder:validation:Required
BackendType BackendType `json:"backendType"`

// Domain 是域名,是字符串类型,规则匹配域名规则,必须存在
// +kubebuilder:validation:Pattern="^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\\.)+[a-zA-Z]{2,6}$"
// Region 是区域 必须存在 由字符数组和-组成
// +kubebuilder:validation:Pattern="^[a-zA-Z0-9-]+$"
// +kubebuilder:validation:Required
Domain string `json:"domain"`
Region string `json:"region"`

// CertConfigRef 是字符串类型,是configMap的引用
// Cluster 是网关集群配置 必须存在
// +kubebuilder:validation:Required
Cluster ClusterSpec `json:"cluster"`

// CertConfigRef 是字符串类型,是configMap的引用,可选存在
// +kubebuilder:validation:Optional
CertConfigRef string `json:"certConfigRef"`
}

// ClusterSpec 是集群的规格
type ClusterSpec struct {
// url是集群的url,必须存在
Url string `json:"url"`

// key是集群的key,必须存在
Key string `json:"key"`
}

// DomainStatus defines the observed state of Domain
type DomainStatus struct {
// CertConfigRef 是字符串类型,是configMap的引用
Expand Down
54 changes: 30 additions & 24 deletions controllers/gateway/api/v1/gateway_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,23 @@ import (
// EDIT THIS FILE! THIS IS SCAFFOLDING FOR YOU TO OWN!
// NOTE: json tags are required. Any new fields you add must have json tags for the fields to be serialized.

// BackendType 是后端服务类型
type BackendType string

const (
APP BackendType = "app"
BUCKET BackendType = "bucket"
WEBSITE BackendType = "website"
)

// RouteState 是路由的状态
type RouteState string

const (
PREPARING RouteState = "preparing"
CREATED RouteState = "created"
)

// GatewaySpec defines the desired state of Gateway
type GatewaySpec struct {
// INSERT ADDITIONAL SPEC FIELDS - desired state of cluster
Expand All @@ -33,7 +50,7 @@ type GatewaySpec struct {
// AppId是应用id,字母数字组成,长度5至16位,必须存在
// +kubebuilder:validation:Pattern="^[a-zA-Z0-9]{5,16}$"
// +kubebuilder:validation:Required
AppId string `json:"appId"`
AppId string `json:"appid"`

// Buckets是存储桶, 是一个数组,可选存在
// +kubebuilder:validation:Optional
Expand All @@ -49,37 +66,26 @@ type GatewayStatus struct {
// INSERT ADDITIONAL STATUS FIELD - define observed state of cluster
// Important: Run "make" to regenerate code after modifying this file

// AppDomain 是应用域名,必须存在
// +kubebuilder:validation:Required
AppDomain string `json:"appDomain"`
// AppRoute 是应用路由
AppRoute *GatewayRoute `json:"appRoute,omitempty"`

// BucketDomains 是存储桶域名列表,是一个数组,可选存在
// +kubebuilder:validation:Optional
BucketDomains []string `json:"bucketDomains,omitempty"`
// BucketRoutes 是存储桶路由
BucketRoutes map[string]*GatewayRoute `json:"bucketRoutes,omitempty"`

// WebsiteDomains 是静态站点域名列表,是一个数组,可选存在
// +kubebuilder:validation:Optional
WebsiteDomains []string `json:"websiteDomains,omitempty"`
// WebsiteRoutes 是静态站点路由
WebsiteRoutes map[string]*GatewayRoute `json:"websiteRoutes,omitempty"`
}

// BucketDomain 是存储桶位的域名配置
type BucketDomain struct {
// Name 是存储桶名称,必须存在
// +kubebuilder:validation:Required
Name string `json:"name"`

// Domain 是存储桶域名,必须存在
type GatewayRoute struct {
// DomainName 是域名名称,必须存在
// +kubebuilder:validation:Required
Domain string `json:"domain"`
}
DomainName string `json:"domainName"`

// WebsiteDomain 是静态站点的域名配置
type WebsiteDomain struct {
// Name 是静态站点名称,必须存在
// DomainNamespace 是域名所在的命名空间,必须存在
// +kubebuilder:validation:Required
Name string `json:"name"`
DomainNamespace string `json:"domainNamespace"`

// Domain 是静态站点域名,必须存在
// Domain 是域名,必须存在
// +kubebuilder:validation:Required
Domain string `json:"domain"`
}
Expand Down
8 changes: 8 additions & 0 deletions controllers/gateway/api/v1/route_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ type RouteSpec struct {
// +kubebuilder:validation:Required
Backend Backend `json:"backend"`

// DomainName 是域名名称,必须存在
// +kubebuilder:validation:Required
DomainName string `json:"domainName"`

// DomainNamespace 是域名所在的命名空间,必须存在
// +kubebuilder:validation:Required
DomainNamespace string `json:"domainNamespace"`

// CertConfigRef 是证书配置,可选存在
// +kubebuilder:validation:Optional
CertConfigRef string `json:"certConfigRef,omitempty"`
Expand Down
87 changes: 59 additions & 28 deletions controllers/gateway/api/v1/zz_generated.deepcopy.go

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

2 changes: 1 addition & 1 deletion controllers/gateway/apisix/route.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ func (r *RouteClient) Put(id string, data map[string]interface{}) error {

// Delete 删除路由
func (r *RouteClient) Delete(id string) error {
url := r.client.baseURL + "routes/" + id
url := r.client.baseURL + "/routes/" + id
return r.client.Delete(url, "routes")
}
29 changes: 0 additions & 29 deletions controllers/gateway/apisix/route_test.go
Original file line number Diff line number Diff line change
@@ -1,30 +1 @@
package apisix

import "testing"

func TestCreateRoute(t *testing.T) {
// 创建RouteClient
routeClient := NewRouteClient("http://localhost:9180", "edd1c9f034335f136f87ad84b625c8f1")
routeData := map[string]interface{}{
"uri": "/hello",
"upstream": map[string]interface{}{
"type": "roundrobin",
"nodes": map[string]interface{}{
"baidu.com": 1,
},
},
"host": "test.com",
}
err := routeClient.PutRoute("test", routeData)
if err != nil {
panic(err)
}
}

func TestDeleteRoute(t *testing.T) {
routeClient := NewRouteClient("http://localhost:9180", "edd1c9f034335f136f87ad84b625c8f1")
err := routeClient.DeleteRoute("test")
if err != nil {
panic(err)
}
}
39 changes: 27 additions & 12 deletions controllers/gateway/config/crd/bases/gateway.laf.dev_domains.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -35,27 +35,42 @@ spec:
spec:
description: DomainSpec defines the desired state of Domain
properties:
backendType:
description: BackendType是后端服务类型,必须存在APP;bucket;WEBSITE
enum:
- app
- bucket
- website
type: string
certConfigRef:
description: CertConfigRef 是字符串类型,是configMap的引用
description: CertConfigRef 是字符串类型,是configMap的引用,可选存在
type: string
cluster:
description: Cluster 是网关集群配置 必须存在
properties:
key:
description: key是集群的key,必须存在
type: string
url:
description: url是集群的url,必须存在
type: string
required:
- key
- url
type: object
domain:
description: Domain 是域名,是字符串类型,规则匹配域名规则,必须存在
description: Domain是域名,必须存在,匹配域名规则
pattern: ^([a-zA-Z0-9]([a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?\.)+[a-zA-Z]{2,6}$
type: string
preferred:
description: Preferred 是提供域名使用位置的推荐项,是字符串类型,长度大于1小于10,必须存在
maxLength: 10
minLength: 1
type: string
region:
description: Region 是域名设定的解析区域,是字符串类型,长度大于1小于10,可选存在
maxLength: 10
minLength: 1
description: Region 是区域 必须存在 由字符数组和-组成
pattern: ^[a-zA-Z0-9-]+$
type: string
required:
- certConfigRef
- backendType
- cluster
- domain
- preferred
- region
type: object
status:
description: DomainStatus defines the observed state of Domain
Expand Down
Loading

0 comments on commit b3cb361

Please sign in to comment.