新聞中心
作者 |?阿里云智能事業(yè)群技術(shù)專家 牛秋霖(冬島)
導(dǎo)讀:通過(guò)前面的一系列文章你已經(jīng)知道如何基于 kubectl 來(lái)操作 Knative 的各種資源。但是如果想要在項(xiàng)目中集成 Knative 僅僅使用 kubectl 這種命令的方式是不夠的,還需要在代碼中基于 Knative Serving SDK 進(jìn)行集成開(kāi)發(fā)。本篇文章中,阿里云智能事業(yè)群技術(shù)專家冬島將從 Knative Serving SDK 入手,介紹如何基于 Knative SDK 進(jìn)行 serverless 開(kāi)發(fā)。
Golang Context
在正式開(kāi)始介紹 Knative Serving SDK 之前,我們先簡(jiǎn)單介紹一下 Golang Context 的機(jī)理。因?yàn)樵?Knative Serving 中 client、Informer 的初始化和信息傳遞完全是基于 Golang Context 實(shí)現(xiàn)的。
Golang 是從 1.7 版本開(kāi)始引入的 Context ,Golang 的 Context 可以很好的簡(jiǎn)化多個(gè) goroutine 之間以及請(qǐng)求域間的數(shù)據(jù)傳遞、取消信號(hào)和截至?xí)r間等相關(guān)操作。Context 主要有兩個(gè)作用:
- 傳輸必要的數(shù)據(jù);
- 進(jìn)行協(xié)調(diào)控制,比如終止 goroutein、設(shè)置超時(shí)時(shí)間等。
Context 定義
Context 本身是一個(gè)接口。
type Context interface {
Deadline() (deadline time.Time, ok bool)
Done() <-chan struct{}
Err() error
Value(key interface{}) interface{}
}
這個(gè)接口中定義了四個(gè)方法,下面分別介紹:
- Deadline 方法是獲取設(shè)置的截止時(shí)間的意思,到了這個(gè)時(shí)間點(diǎn),Context 會(huì)自動(dòng)發(fā)起取消請(qǐng)求;
- Done 方法返回一個(gè)只讀的 chan,如果該方法返回的 chan 可以讀取,則意味著 parent Context 已經(jīng)發(fā)起了取消請(qǐng)求, 此時(shí)應(yīng)該做清理操作,然后退出 goroutine 并釋放資源;
- Err 方法返回取消的錯(cuò)誤原因;
- Value 方法獲取該 Context 上綁定的值,是一個(gè)鍵值對(duì)。所以要通過(guò)一個(gè) Key 才可以獲取對(duì)應(yīng)的值,這個(gè)值是線程安全的。
關(guān)于 Context 主要記住一點(diǎn):可以通過(guò) Value 傳遞數(shù)據(jù),Value 是一個(gè)鍵值對(duì)結(jié)構(gòu)。更多詳細(xì)的介紹可以參見(jiàn)下面這些文章:
- Concurrency Patterns in Go
- How to correctly use context.Context in Go 1.7
- Using context cancellation in Go
- Go Context
Knative Serving client 源碼淺析
在 Context 的這些特性中,Knative Serving 中重度依賴的是 Value 功能。以 ?Service 的 Informer 初始化為例進(jìn)行說(shuō)明,這里可以看到源碼。
Informer “構(gòu)造函數(shù)”是在 init 函數(shù)中自動(dòng)注冊(cè)到 injection.Default 中的。當(dāng) Informer “構(gòu)造函數(shù)”被調(diào)用之后會(huì)自動(dòng)把生成的 Informer 注入到 Context 中 context.WithValue(ctx, Key{}, inf), inf.Informer()
。
從上圖中可以看到:Informer 初始化的時(shí)候需要調(diào)用 factory,而 factory 本身是從 ?Context 中獲取的。下面我們?cè)倏纯?factory 是怎么初始化的。factory 的初始化。
上圖可以發(fā)現(xiàn),factory 也是把“構(gòu)造函數(shù)”注冊(cè)到 injection.Default 中,并會(huì)將生成的 SharedInformerFactory 注入到 Context 中。而且 factory 中使用的 client (鏈接 kube-apiserver 使用的對(duì)象)也是從 Context 獲取到的。
可以說(shuō) Knative Serving SDK 初始化的過(guò)程是面向 Context 編程的。關(guān)鍵對(duì)象是自動(dòng)注入到 Context,在使用的時(shí)候從 Context 中取出。
順帶提一點(diǎn),Knative Serving 的日志對(duì)象也是在 Context 保存的,當(dāng)需要打印日志的時(shí)候先通過(guò) logger := logging.FromContext(ctx)
從 Context 中拿到 logger,然后就可以使用了。這樣做的好處是可以通過(guò)管理 logger 對(duì)象,比如做 trace 功能。
如下所示是基于 logger 打印出來(lái)的日志,可以看到對(duì)于同一個(gè)請(qǐng)求的處理是可以通過(guò) traceID 進(jìn)行追蹤的。下面這段日志都是對(duì) 577f8de5-cec9-4c17-84f7-f08d39f40127
這個(gè) ?trace 的處理。
{"level":"info","ts":"2019-08-28T20:24:39.871+0800","caller":"controller/service.go:67","msg":"Reconcile: default/helloworld-go","knative.dev/traceid":"be5ec711-6ca3-493c-80ed-dddfa21fd576","knative.dev/key":"default/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.871+0800","caller":"controller/controller.go:339","msg":"Reconcile succeeded. Time taken: 487.347μs.","knative.dev/traceid":"90653eda-644b-4b1e-8bdb-4a1a7a7ff0d8","knative.dev/key":"eci-test/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.871+0800","caller":"controller/service.go:106","msg":"service: default/helloworld-go route: default/helloworld-go ","knative.dev/traceid":"be5ec711-6ca3-493c-80ed-dddfa21fd576","knative.dev/key":"default/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.872+0800","caller":"controller/service.go:67","msg":"Reconcile: eci-test/helloworld-go","knative.dev/traceid":"22f6c77d-8365-4773-bd78-e011ccb2fa3d","knative.dev/key":"eci-test/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.872+0800","caller":"controller/service.go:116","msg":"service: default/helloworld-go revisions: 1 ","knative.dev/traceid":"be5ec711-6ca3-493c-80ed-dddfa21fd576","knative.dev/key":"default/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.872+0800","caller":"controller/service.go:118","msg":"service: default/helloworld-go revision: default/helloworld-go-cgt65 ","knative.dev/traceid":"be5ec711-6ca3-493c-80ed-dddfa21fd576","knative.dev/key":"default/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.872+0800","caller":"controller/controller.go:339","msg":"Reconcile succeeded. Time taken: 416.527μs.","knative.dev/traceid":"be5ec711-6ca3-493c-80ed-dddfa21fd576","knative.dev/key":"default/helloworld-go"}
{"level":"info","ts":"2019-08-28T20:24:39.872+0800","caller":"controller/service.go:106","msg
使用 Knative Serving SDK
介紹完 Knative Serving client 的初始化過(guò)程,下面我們看一下應(yīng)該如何在代碼中用 Knative Serving SDK 進(jìn)行編碼。
示例參見(jiàn):https://github.com/knative-sample/serving-controller/blob/v0.1/cmd/app/app.go
這個(gè)示例中首先使用配置初始化 *zap.SugaredLogger
?對(duì)象,然后基于 ctx := signals.NewContext()
生成一個(gè) Context。signals.NewContext()
?作用是監(jiān)聽(tīng) SIGINT 信號(hào),也就是處理 CTRL+c 指令。這里用到了 Context 接口的 Done 函數(shù)機(jī)制。
構(gòu)造 Informer
接著使用 ctx, informers := injection.Default.SetupInformers(ctx, cfg)
構(gòu)造出所有的 informer,然后調(diào)用下面這段代碼執(zhí)行注入,把 informer 注入到 Context 中。
// Start all of the informers and wait for them to sync.
logger.Info("Starting informers.")
if err := controller.StartInformers(ctx.Done(), informers...); err != nil {
logger.Fatalw("Failed to start informers", err)
}
從 Context 中獲取 Informer
實(shí)例代碼: https://github.com/knative-sample/serving-controller/blob/v0.1/pkg/controller/controller.go
如上所示,所有的 informer 都是從 Context 中獲取的。
最后 Controller 初始化一個(gè) Reconciler 接口,接口的定義如下, 里面只有一個(gè) Reconcile 函數(shù)。這個(gè)使用方式和 sigs.k8s.io/controller-runtime
使用的邏輯是一樣的。如果你之前寫(xiě)過(guò) Operator 之類(lèi)的功能,對(duì)這個(gè)操作應(yīng)該不會(huì)陌生。
// Reconciler is the interface that controller implementations are expected
// to implement, so that the shared controller.Impl can drive work through it.
type Reconciler interface {
Reconcile(ctx context.Context, key string) error
}
在 Reconcile 中調(diào)用 Knative API
代碼示例: https://github.com/knative-sample/serving-controller/blob/v0.1/pkg/controller/service.go
現(xiàn)在就可以在 Reconcile 中通過(guò) c.serviceLister.Services(namespace).Get(name)
這種方式直接操作 Seving 資源了。
至此,我們已經(jīng)把基于 Knative Seving 開(kāi)發(fā) Serverless 應(yīng)用的關(guān)鍵脈梳理了一遍。更詳細(xì)的代碼示例請(qǐng)參見(jiàn):https://github.com/knative-sample/serving-controller ,這里面有完整可以運(yùn)行的代碼。
小結(jié)
本文從 Knative Serving client 的初始化過(guò)程開(kāi)始展開(kāi),介紹了 Knative informer 的設(shè)計(jì)以及使用方法。通過(guò)本文你可以了解到:
- Knative Serving client 的設(shè)計(jì)思路
- 如何基于 Knative Serving SDK 進(jìn)行二次開(kāi)發(fā)
- 通過(guò) Knative Serving 學(xué)習(xí)到如何面向 Context 編程
- Knative Serving 集成開(kāi)發(fā)示例: https://github.com/knative-sample/serving-controller
歡迎加入 Knative 交流群
掃描下方二維碼添加小助手,拉你進(jìn)群!
另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)cdcxhl.cn,海內(nèi)外云服務(wù)器15元起步,三天無(wú)理由+7*72小時(shí)售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國(guó)服務(wù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡(jiǎn)單易用、服務(wù)可用性高、性價(jià)比高”等特點(diǎn)與優(yōu)勢(shì),專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場(chǎng)景需求。
新聞標(biāo)題:KnativeServing進(jìn)階:KnativeServingSDK-創(chuàng)新互聯(lián)
標(biāo)題路徑:http://www.dlmjj.cn/article/coidog.html