forked from alibaba/higress
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
32 changed files
with
2,544 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
## 介绍 | ||
|
||
此 SDK 用于使用 AssemblyScript 语言开发 Higress 的 Wasm 插件。 | ||
|
||
### 如何使用SDK | ||
|
||
创建一个新的 AssemblyScript 项目。 | ||
|
||
``` | ||
npm init | ||
npm install --save-dev assemblyscript | ||
npx asinit . | ||
``` | ||
|
||
在asconfig.json文件中,作为传递给asc编译器的选项之一,包含"use": "abort=abort_proc_exit"。 | ||
|
||
``` | ||
{ | ||
"options": { | ||
"use": "abort=abort_proc_exit" | ||
} | ||
} | ||
``` | ||
|
||
将`"@higress/proxy-wasm-assemblyscript-sdk": "^0.0.1"`和`"@higress/wasm-assemblyscript": "^0.0.3"`添加到你的依赖项中,然后运行`npm install`。 | ||
|
||
### 本地构建 | ||
|
||
``` | ||
npm run asbuild | ||
``` | ||
|
||
构建结果将在`build`文件夹中。其中,`debug.wasm`和`release.wasm`是已编译的文件,在生产环境中建议使用`release.wasm`。 | ||
|
||
注:如果需要插件带有 name section 信息需要带上`"debug": true`,编译参数解释详见[using-the-compiler](https://www.assemblyscript.org/compiler.html#using-the-compiler)。 | ||
|
||
```json | ||
"release": { | ||
"outFile": "build/release.wasm", | ||
"textFile": "build/release.wat", | ||
"sourceMap": true, | ||
"optimizeLevel": 3, | ||
"shrinkLevel": 0, | ||
"converge": false, | ||
"noAssert": false, | ||
"debug": true | ||
} | ||
``` | ||
|
||
### AssemblyScript 限制 | ||
|
||
此 SDK 使用的 AssemblyScript 版本为`0.27.29`,参考[AssemblyScript Status](https://www.assemblyscript.org/status.html)该版本尚未支持闭包、异常、迭代器等特性,并且JSON,正则表达式等功能还尚未在标准库中实现,暂时需要使用社区提供的实现。 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"targets": { | ||
"debug": { | ||
"outFile": "build/debug.wasm", | ||
"textFile": "build/debug.wat", | ||
"sourceMap": true, | ||
"debug": true | ||
}, | ||
"release": { | ||
"outFile": "build/release.wasm", | ||
"textFile": "build/release.wat", | ||
"sourceMap": true, | ||
"optimizeLevel": 3, | ||
"shrinkLevel": 0, | ||
"converge": false, | ||
"noAssert": false | ||
} | ||
}, | ||
"options": { | ||
"bindings": "esm", | ||
"use": "abort=abort_proc_exit" | ||
} | ||
} |
214 changes: 214 additions & 0 deletions
214
plugins/wasm-assemblyscript/assembly/cluster_wrapper.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,214 @@ | ||
import { | ||
log, | ||
LogLevelValues, | ||
get_property, | ||
WasmResultValues, | ||
} from "@higress/proxy-wasm-assemblyscript-sdk/assembly"; | ||
import { getRequestHost } from "./request_wrapper"; | ||
|
||
export abstract class Cluster { | ||
abstract clusterName(): string; | ||
abstract hostName(): string; | ||
} | ||
|
||
export class RouteCluster extends Cluster { | ||
host: string; | ||
constructor(host: string = "") { | ||
super(); | ||
this.host = host; | ||
} | ||
|
||
clusterName(): string { | ||
let result = get_property("cluster_name"); | ||
if (result.status != WasmResultValues.Ok) { | ||
log(LogLevelValues.error, "get route cluster failed"); | ||
return ""; | ||
} | ||
return String.UTF8.decode(result.returnValue); | ||
} | ||
|
||
hostName(): string { | ||
if (this.host != "") { | ||
return this.host; | ||
} | ||
return getRequestHost(); | ||
} | ||
} | ||
|
||
export class K8sCluster extends Cluster { | ||
serviceName: string; | ||
namespace: string; | ||
port: i64; | ||
version: string; | ||
host: string; | ||
|
||
constructor( | ||
serviceName: string, | ||
namespace: string, | ||
port: i64, | ||
version: string = "", | ||
host: string = "" | ||
) { | ||
super(); | ||
this.serviceName = serviceName; | ||
this.namespace = namespace; | ||
this.port = port; | ||
this.version = version; | ||
this.host = host; | ||
} | ||
|
||
clusterName(): string { | ||
let namespace = this.namespace != "" ? this.namespace : "default"; | ||
return `outbound|${this.port}|${this.version}|${this.serviceName}.${namespace}.svc.cluster.local`; | ||
} | ||
|
||
hostName(): string { | ||
if (this.host != "") { | ||
return this.host; | ||
} | ||
return `${this.serviceName}.${this.namespace}.svc.cluster.local`; | ||
} | ||
} | ||
|
||
export class NacosCluster extends Cluster { | ||
serviceName: string; | ||
group: string; | ||
namespaceID: string; | ||
port: i64; | ||
isExtRegistry: boolean; | ||
version: string; | ||
host: string; | ||
|
||
constructor( | ||
serviceName: string, | ||
namespaceID: string, | ||
port: i64, | ||
// use DEFAULT-GROUP by default | ||
group: string = "DEFAULT-GROUP", | ||
// set true if use edas/sae registry | ||
isExtRegistry: boolean = false, | ||
version: string = "", | ||
host: string = "" | ||
) { | ||
super(); | ||
this.serviceName = serviceName; | ||
this.group = group.replace("_", "-"); | ||
this.namespaceID = namespaceID; | ||
this.port = port; | ||
this.isExtRegistry = isExtRegistry; | ||
this.version = version; | ||
this.host = host; | ||
} | ||
|
||
clusterName(): string { | ||
let tail = "nacos" + (this.isExtRegistry ? "-ext" : ""); | ||
return `outbound|${this.port}|${this.version}|${this.serviceName}.${this.group}.${this.namespaceID}.${tail}`; | ||
} | ||
|
||
hostName(): string { | ||
if (this.host != "") { | ||
return this.host; | ||
} | ||
return this.serviceName; | ||
} | ||
} | ||
|
||
export class StaticIpCluster extends Cluster { | ||
serviceName: string; | ||
port: i64; | ||
host: string; | ||
|
||
constructor(serviceName: string, port: i64, host: string = "") { | ||
super() | ||
this.serviceName = serviceName; | ||
this.port = port; | ||
this.host = host; | ||
} | ||
|
||
clusterName(): string { | ||
return `outbound|${this.port}||${this.serviceName}.static`; | ||
} | ||
|
||
hostName(): string { | ||
if (this.host != "") { | ||
return this.host; | ||
} | ||
return this.serviceName; | ||
} | ||
} | ||
|
||
export class DnsCluster extends Cluster { | ||
serviceName: string; | ||
domain: string; | ||
port: i64; | ||
|
||
constructor(serviceName: string, domain: string, port: i64) { | ||
super(); | ||
this.serviceName = serviceName; | ||
this.domain = domain; | ||
this.port = port; | ||
} | ||
|
||
clusterName(): string { | ||
return `outbound|${this.port}||${this.serviceName}.dns`; | ||
} | ||
|
||
hostName(): string { | ||
return this.domain; | ||
} | ||
} | ||
|
||
export class ConsulCluster extends Cluster { | ||
serviceName: string; | ||
datacenter: string; | ||
port: i64; | ||
host: string; | ||
|
||
constructor( | ||
serviceName: string, | ||
datacenter: string, | ||
port: i64, | ||
host: string = "" | ||
) { | ||
super(); | ||
this.serviceName = serviceName; | ||
this.datacenter = datacenter; | ||
this.port = port; | ||
this.host = host; | ||
} | ||
|
||
clusterName(): string { | ||
return `outbound|${this.port}||${this.serviceName}.${this.datacenter}.consul`; | ||
} | ||
|
||
hostName(): string { | ||
if (this.host != "") { | ||
return this.host; | ||
} | ||
return this.serviceName; | ||
} | ||
} | ||
|
||
export class FQDNCluster extends Cluster { | ||
fqdn: string; | ||
host: string; | ||
port: i64; | ||
|
||
constructor(fqdn: string, port: i64, host: string = "") { | ||
super(); | ||
this.fqdn = fqdn; | ||
this.host = host; | ||
this.port = port; | ||
} | ||
|
||
clusterName(): string { | ||
return `outbound|${this.port}||${this.fqdn}`; | ||
} | ||
|
||
hostName(): string { | ||
if (this.host != "") { | ||
return this.host; | ||
} | ||
return this.fqdn; | ||
} | ||
} |
Oops, something went wrong.