共計 2151 個字符,預計需要花費 6 分鐘才能閱讀完成。
kubernetes 及 kubeadm 工作流的 Phase 數據結構是怎樣的,針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。
Phase 即工作流中的階段或步驟。創建一個 Phase 只需要實例化一個 Phase struct 類型的變量即可。
Phase 定義了某個步驟及該步驟下所采取的動作。
Phase 數結結構
Phase 數據結構定義位于 kubernetes\cmd\kubeadm\app\cmd\phases\workflow\phase.go。
type Phase struct {
Name string // phase 的名字,同一個 workflow 下的 phase 或同一個父 phase 下的子 phase 名字也必須唯一
Aliases []string // phase 的別名,可以有多個
Short string // phase 的簡短介紹
Long string // phase 的介紹
Example string // 使用示例,類似于 help 信息
Hidden bool // 該 phase 是否需要在工作流幫助信息中隱藏
Phases []Phase // 子 phase,有序排列
// RunAllSiblings allows to assign to a phase the responsibility to
// run all the sibling phases
// Nb. phase marked as RunAllSiblings can not have Run functions
RunAllSiblings bool
Run func(data RunData) error // phase 的回調函數
RunIf func(data RunData) (bool, error) // 條件檢測回調函數,在 Run 之前調用,決定是否要繼續調用 Run,如果 RunIf 返回(true,nil),那么 Run 將會被執行,否則不執行
// InheritFlags defines the list of flags that the cobra command generated for this phase should Inherit
// from local flags defined in the parent command / or additional flags defined in the phase runner.
// If the values is not set or empty, no flags will be assigned to the command
// Nb. global flags are automatically inherited by nested cobra command
InheritFlags []string
// LocalFlags defines the list of flags that should be assigned to the cobra command generated
// for this phase.
// Nb. if two or phases have the same local flags, please consider using local flags in the parent command
// or additional flags defined in the phase runner.
LocalFlags *pflag.FlagSet
// ArgsValidator defines the positional arg function to be used for validating args for this phase
// If not set a phase will adopt the args of the top level command.
ArgsValidator cobra.PositionalArgs
}
對外方法
Phase 只提供一個方法用于添加子 Phase,這也意味著一旦創建它,其屬性一般就不會修改,可以動態的添加子 Phase。
func (t *Phase) AppendPhase(phase Phase) {t.Phases = append(t.Phases, phase)
}
要點總結 phase 可以包含子 phase
通過 phase 的方法 func (t *Phase) AppendPhase(phase Phase) 可以把一個 phase 加入到另一個 phase 中,從而成為其子 phase。
一個 phase 的子 phase 存放于 Phase.Phases 的切片中,而且是按照添加的順序排列的,這也是子 phase 被執行的順序。
一個 phase 的子 phase 在其父 phase 執行后會立即執行。
關于 kubernetes 及 kubeadm 工作流的 Phase 數據結構是怎樣的問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注丸趣 TV 行業資訊頻道了解更多相關知識。