跳到內容
開源

安裝工具如何選擇和安裝元件

安裝工具從 catalog API 挑選元件、排定順序、裝好後檢查時要遵守的規則,以及用來測試安裝工具的案例。

安裝工具讀取某個 catalog 版本的 API,再決定要安裝哪些元件。這一頁列出挑選的規則。遵守這些規則的安裝工具,用同一份設定會挑出同樣的元件。每條規則在 conformance/select/ 都有一個測試案例,用法見本頁最後一節。

  • 一個 catalog 版本的 API:catalog.json 和各元件的檔案。
  • profile,也就是 single-node 或 multi-node。
  • 發行版,也就是 k3s、rke2 或 kubesolo。
  • 叢集節點的平台,例如 linux/amd64。
  • 使用者寫的 enable 和 disable 清單。每一項可以是元件 ID(例如 victoria-logs),也可以是 capability(例如 logs)。

API 存放的是原始資料。安裝工具根據這些資料,替每個元件算出兩份清單,下面的規則都會用到。

testedOn 列出元件通過測試的發行版,以及在各發行版上通過測試的平台。對 catalog.json 的 distributions 裡的每個發行版,取出元件量測資料中 distribution 相同、distributionVersion 符合固定版本的紀錄。某個平台上最新一次通過測試的紀錄,若 sourceDigest 與元件目前的值相同,且日期不早於最新一次失敗的紀錄,就把這個平台列入 testedOn。同一天有通過和失敗的紀錄時,以通過為準。沒有任何平台符合條件的發行版不列入,所以 testedOn 可以是空清單。

installOrder 列出在某個 profile 下安裝元件時會安裝的所有元件:先列出需要的元件,最後是元件本身。依序處理 requires 的每一項。{component: <id>} 指定該元件。{capability: <capability>} 則從 provides.capability 相符的元件中,依 ID 順序取第一個在這個 profile 是 recommended 的元件;沒有時,取第一個 optional 的元件。先放入各指定元件的安裝順序,略過重複的元件,最後加上元件本身。元件在某個 profile 是 unsupported 時,該 profile 沒有 installOrder。

名稱是某個元件的 ID 時,指的就是那個元件。其他名稱都當作 capability,對到元件的 provides.capability。安裝工具從提供這個 capability、在這個 profile 不是 unsupported 的元件裡挑一個。挑法是依下列順序排好,取第一個。

  1. testedOn 在這個發行版底下列有這個平台的元件排前面。
  2. 接著是這個 profile recommended 的元件。
  3. 最後依 ID 排序。

名稱對不到任何 capability 或元件 ID 時,不論寫在 enable 還是 disable,都會出錯。

安裝工具依序完成下面幾步。

  1. 先取這個 profile 所有 recommended 的元件,這些是預設元件。安裝工具可以用 ID 或 capability 再加上自己需要的元件,這些元件也算預設元件。

  2. 加入 enable 列出的元件,這些是啟用的元件。

  3. 拿掉 disable 列出的元件,這些是停用的元件。寫元件 ID 就拿掉那個元件,寫 capability 就拿掉這個 capability 底下所有已選的元件。名稱存在但沒對到已選的元件時,什麼都不做。

  4. 拿掉在這裡裝不了的元件,這些是排除的元件。每個排除的元件都要附上原因。測試案例用下表的代碼表示原因,要怎麼跟使用者說明,由安裝工具自己決定。

    情況代碼是否排除
    distributions 沒有列這個發行版distributions一律排除。啟用的元件另外顯示 left-out 警告
    這個 profile 的 defaults 是 unsupportedunsupported一律排除。啟用的元件另外顯示 left-out 警告

    testedOn 沒有在這個發行版底下列出這個平台的已選元件,照樣保留,並顯示 untested 警告。從沒在這裡測過的元件、最近一次在這裡測試失敗的元件,以及自上次通過測試後內容有變動的元件,都是這樣處理。

  5. 依 ID 順序走過已選的元件,取每個元件在這個 profile 的 installOrder,排出安裝順序。installOrder 先列它需要的元件,最後是它自己。結果裡還沒有的 ID 就依序加進去。結果裡沒被選到的元件,是因為其他元件需要才安裝的。

    installOrder 裡需要的元件,可能已停用、被排除或沒測過:

    需要的元件結果
    已停用,或因為 distributions、unsupported 排除出錯,代碼是 needs-disabled
    testedOn 沒有在這個發行版底下列出這個平台照樣安裝需要的元件,並顯示 untested 警告

    例如 VictoriaLogs 需要 VictoriaMetrics operator,而 operator 只在 k3s 測過。在 rke2 上,不管 VictoriaLogs 是預設元件還是啟用的元件,都會連 operator 一起裝,並提醒 operator 在 rke2 沒測過。

挑選的結果有三部分:

  • 依安裝順序排好的元件,以及各自的安裝原因:default、enabled 或 dependency。
  • 排除的元件和原因。
  • 警告。

每個 installOrder 都已經把元件排在它需要的元件之後,所以結果也是這樣。enable 和 disable 怎麼排列,都不影響結果。

遇到下列錯誤時,安裝工具會停下來,什麼都不裝。kind 是測試案例裡的錯誤名稱。

kind情況要告訴使用者
unknown名稱對不到任何 capability 或元件 ID這個名稱
needs-disabled已選的元件需要的元件已停用,或因為 distributions、unsupported 排除兩個元件的名稱

依結果的順序安裝元件,並移除結果裡沒有的已裝元件。每個元件的 releases、固定內容的 digest,以及安裝工具會讀取的其他欄位,見元件檔案格式。

元件裝好後,執行每項 health 檢查。有 condition 的檢查執行一次 kubectl wait,引數依序是 --for=condition=<condition>、resource 指定的物件、-n <namespace>(有命名空間時才加上),最後是 --timeout=<timeout>。單一物件寫成 <resource>/<name>,用 selector 選取的物件寫成 <resource> -l <key>=<value>,…,all: true 則寫成 <resource> --all。執行 kubectl wait 之前,先等物件出現,因為找不到物件時 kubectl wait 會立刻失敗。Deployment、DaemonSet 和 StatefulSet 的檢查沒有 condition,它們在 rollout 完成時才算就緒,所以改執行 kubectl rollout status <resource>/<name>,用 selector 選取時執行 kubectl rollout status <resource> -l <key>=<value>,…,並加上 -n <namespace> 和 --timeout=<timeout>。kubectl 會比對 observedGeneration 和 generation,所以舊一輪 rollout 的 Pod 不會讓新的 rollout 被當成就緒。<resource> 依 kind 查下表:

kind<resource>
APIServiceapiservices.apiregistration.k8s.io
DaemonSetdaemonsets.apps
Deploymentdeployments.apps
GatewayClassgatewayclasses.gateway.networking.k8s.io
Node.longhorn.ionodes.longhorn.io
Podpods
StatefulSetstatefulsets.apps

例如 Longhorn 的 nodes-ready 檢查會執行:

kubectl wait --for=condition=Ready nodes.longhorn.io --all -n longhorn-system --timeout=60s

安裝工具可以用自己的等待時間取代 timeout。檢查失敗時,先顯示物件的原因和訊息(和 kubectl describe 顯示的相同),再顯示它的 advice。advice 說明該怎麼處理,不說明哪裡出錯。

上面每條規則在 conformance/select/ 都有一個案例。這個目錄在儲存庫裡,也包含在 bundle.tar.gz 中,所以固定某個 catalog commit 或版本的安裝工具,讀到的就是那個版本的案例。格式定義在 schema/v1/select-case.schema.json。

description: A default not tested on the distribution is installed with a warning.
components: # each component with only the fields selection reads, and the testedOn
# and installOrder that the installer works out
- id: traefik
provides: {capability: ingress}
defaults: {single-node: recommended, multi-node: recommended}
distributions: [k3s, rke2, kubesolo]
testedOn:
- {distribution: k3s, platforms: [linux/amd64]}
- {distribution: rke2, platforms: [linux/amd64]}
- {distribution: kubesolo, platforms: [linux/amd64]}
- id: victoria-logs
provides: {capability: logs}
defaults: {single-node: recommended, multi-node: recommended}
distributions: [k3s, rke2, kubesolo]
testedOn:
- {distribution: k3s, platforms: [linux/amd64]}
- {distribution: kubesolo, platforms: [linux/amd64]}
config:
profile: multi-node
distribution: rke2
platform: linux/amd64
# add: what the installer adds to the defaults
# enable, disable: the person's lists
want:
components: # in install order
- {id: traefik, why: default}
- {id: victoria-logs, why: default}
warnings: # in any order
- {kind: untested, component: victoria-logs, distribution: rke2}
# leftOut: {<id>: {reason: …, …}}
# error: {kind: unknown, name: …}, in place of the rest

執行案例的步驟如下。

  1. 把 components 當作 catalog,直接使用其中的 testedOn 和 installOrder。每個元件的 capability 是它的 provides.capability。某個 profile 沒有 installOrder 時,只裝元件本身。
  2. 依 config 挑選元件。
  3. 和 want 比對。want 沒寫的清單或對照表就是空的。原因、警告和錯誤都比對代碼和欄位,不比對安裝工具顯示的文字。