1. Chart.yaml 文件与 Chart 对象

有关 chart 的元信息存储在名为 Chart.yaml 的特殊文件中。

[!NOTE|style:flat] 每个 Chart 都必须有 Chart.yaml 文件,路径 mychart/Chart.yaml

1.1. 支持的字段

name: string
home: string
sources: []string
version: string
description: string
keywords: []string
maintainers: []*Maintainer
icon: string
apiVersion: string
condition: string
tags: string
appVersion: string
deprecated: bool
annotations: map[string]string
kubeVersion: string
dependencies: []*Dependency
type: string

字段说明:

  • name: Chart 的名称,必须项。
  • home: 指向相关项目页面的 URL 或者 git repo 或联系人地址
  • sources: 指向这个 Chart 的源码 URL
  • version: 这个 Chart 的版本,符合 SemVer2 版本规范,必须项。
  • description: 这个 Chart 的描述信息。用来描述这个 Chart 的作用的字段, helm search 的时候提供介绍
  • keywords: 关键字,是字符串列表。便于检索
  • maintainers: 维护者的姓名、URL、电子邮件等信息组合
  • icon: icon 图标文件的 URL。Chart 的图标或者 Logo,便于在一些基于 Web 的应用市场显示。
  • apiVersion: 这个 Chart 的 API 版本,必须。早期的 Helm2 的 v1,Helm3 后为 v2。
  • condition: 检查是否启用这个 Chart
  • tags: 用于检查 Chart 是否需要启用的 tags 列表
  • appVersion: Chart 中实际应用程序的版本
  • deprecated: 不管这个图表是否已被废弃
  • annotations: 注解是 Helm 附加说明,可用于其他应用程序的检查
  • kubeVersion: 运行支持的 Kubernetes 版本,是一个 SemVer2 的版本格式。SemVer2 详情见 语义版本文档
  • dependencies: Chart 的依赖列表,详情见 Chart 依赖管理
  • type: 指定 Chart 类型,是应用类型 Chart 还是 库类型 Chart(application or library)

1.2. 特殊字段说明

1.2.1. annotations

按名称键入的注释列表 (可选)

annotations:
  category: Database

1.2.2. appVersion

表示应用的版本,核心是 Image 镜像的版本。

请注意,appVersion 字段与 version 字段无关。这是一种指定应用程序版本的方法。例如,drupal chart 可能有一个 appVersion: 8.2.1,表示 chart 中包含的 Drupal 版本(默认情况下)是 8.2.1。该字段是信息标识,对 chart 版本没有影响。

1.2.3. type

表示 Chart 的类型,application 表示这是一个应用类型的 Chart,public 表示这是一个公共模板类型的 Chart。

实际需要安装不是的 Chart 的 tpye 配置

type: application

如果该 Chart 作为库 Chart,需要设置的 library

type: library

例如 bitnami 提供的 common 的库 Chart 的配置

1.2.4. version

表示 Chart 模板的版本

version: 8.8.16

[!NOTE|style:flat] 注意:

  • version: 定义的 Chart 模板的版本,范围是当前 Chart 模板相关的文件。
  • appVersion: 定义的是需要安装的服务版本,范围是需要部署的核心 Image 镜像对应的版本。
  • apiVersion: 定义的是 Helm API 的版本,早期的 Helm2 的 v1,Helm3后为 v2。

1.2.5. deprecated

chart 是否已弃用 (可选, boolean)

deprecated: false

在管理 chart tepo 库中的 chart 时,有时需要弃用 chart。Chart.yaml 的 deprecated 字段可用于将 chart 标记为已弃用。如果存储库中最新版本的 chart 标记为已弃用,则整个 chart 被视为已弃用。chart 名称稍后可以通过发布未标记为已弃用的较新版本来重新使用。废弃 chart 的工作流程根据 helm/charts 项目的工作流程如下:

  • 更新 chart 的 Chart.yaml 以将 chart 标记为启用,并且更新版本
  • 在 chart Repository 中发布新的 chart 版本
  • 从源代码库中删除 chart(例如 git)

1.3. Chart 对象

在 templates 模板中可以通过 .Chart 对象来引用 Chart.yaml 中的配置。

[!NOTE|style:flat] 注意: 引用 Chart.yaml 中的内容时需要首字母大写。

{{ .Chart.Name }}-{{ .Chart.AppVersion }}

1.4. 示例

如下 bitnami 的 MySQL Chart 的 Chart.yaml 文件

annotations:
  category: Database
apiVersion: v2
appVersion: 8.0.27
dependencies:
  - name: common
    repository: https://charts.bitnami.com/bitnami
    tags:
      - bitnami-common
    version: 1.x.x
description: Chart to create a Highly available MySQL cluster
engine: gotpl
home: https://github.com/bitnami/charts/tree/master/bitnami/mysql
icon: https://bitnami.com/assets/stacks/mysql/img/mysql-stack-220x234.png
keywords:
  - mysql
  - database
  - sql
  - cluster
  - high availability
maintainers:
  - email: containers@bitnami.com
    name: Bitnami
name: mysql
sources:
  - https://github.com/bitnami/bitnami-docker-mysql
  - https://mysql.com
version: 8.8.16

2. 源码

源码地址:https://github.com/helm/helm/blob/v3.7.2/pkg/chart/metadata.go

源码内容

/*
Copyright The Helm Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package chart

import (
    "strings"
    "unicode"

    "github.com/Masterminds/semver/v3"
)

// Maintainer describes a Chart maintainer.
type Maintainer struct {
    // Name is a user name or organization name
    Name string `json:"name,omitempty"`
    // Email is an optional email address to contact the named maintainer
    Email string `json:"email,omitempty"`
    // URL is an optional URL to an address for the named maintainer
    URL string `json:"url,omitempty"`
}

// Validate checks valid data and sanitizes string characters.
func (m *Maintainer) Validate() error {
    m.Name = sanitizeString(m.Name)
    m.Email = sanitizeString(m.Email)
    m.URL = sanitizeString(m.URL)
    return nil
}

// Metadata for a Chart file. This models the structure of a Chart.yaml file.
type Metadata struct {
    // The name of the chart. Required.
    Name string `json:"name,omitempty"`
    // The URL to a relevant project page, git repo, or contact person
    Home string `json:"home,omitempty"`
    // Source is the URL to the source code of this chart
    Sources []string `json:"sources,omitempty"`
    // A SemVer 2 conformant version string of the chart. Required.
    Version string `json:"version,omitempty"`
    // A one-sentence description of the chart
    Description string `json:"description,omitempty"`
    // A list of string keywords
    Keywords []string `json:"keywords,omitempty"`
    // A list of name and URL/email address combinations for the maintainer(s)
    Maintainers []*Maintainer `json:"maintainers,omitempty"`
    // The URL to an icon file.
    Icon string `json:"icon,omitempty"`
    // The API Version of this chart. Required.
    APIVersion string `json:"apiVersion,omitempty"`
    // The condition to check to enable chart
    Condition string `json:"condition,omitempty"`
    // The tags to check to enable chart
    Tags string `json:"tags,omitempty"`
    // The version of the application enclosed inside of this chart.
    AppVersion string `json:"appVersion,omitempty"`
    // Whether or not this chart is deprecated
    Deprecated bool `json:"deprecated,omitempty"`
    // Annotations are additional mappings uninterpreted by Helm,
    // made available for inspection by other applications.
    Annotations map[string]string `json:"annotations,omitempty"`
    // KubeVersion is a SemVer constraint specifying the version of Kubernetes required.
    KubeVersion string `json:"kubeVersion,omitempty"`
    // Dependencies are a list of dependencies for a chart.
    Dependencies []*Dependency `json:"dependencies,omitempty"`
    // Specifies the chart type: application or library
    Type string `json:"type,omitempty"`
}

// Validate checks the metadata for known issues and sanitizes string
// characters.
func (md *Metadata) Validate() error {
    if md == nil {
        return ValidationError("chart.metadata is required")
    }

    md.Name = sanitizeString(md.Name)
    md.Description = sanitizeString(md.Description)
    md.Home = sanitizeString(md.Home)
    md.Icon = sanitizeString(md.Icon)
    md.Condition = sanitizeString(md.Condition)
    md.Tags = sanitizeString(md.Tags)
    md.AppVersion = sanitizeString(md.AppVersion)
    md.KubeVersion = sanitizeString(md.KubeVersion)
    for i := range md.Sources {
        md.Sources[i] = sanitizeString(md.Sources[i])
    }
    for i := range md.Keywords {
        md.Keywords[i] = sanitizeString(md.Keywords[i])
    }

    if md.APIVersion == "" {
        return ValidationError("chart.metadata.apiVersion is required")
    }
    if md.Name == "" {
        return ValidationError("chart.metadata.name is required")
    }
    if md.Version == "" {
        return ValidationError("chart.metadata.version is required")
    }
    if !isValidSemver(md.Version) {
        return ValidationErrorf("chart.metadata.version %q is invalid", md.Version)
    }
    if !isValidChartType(md.Type) {
        return ValidationError("chart.metadata.type must be application or library")
    }

    for _, m := range md.Maintainers {
        if err := m.Validate(); err != nil {
            return err
        }
    }

    // Aliases need to be validated here to make sure that the alias name does
    // not contain any illegal characters.
    for _, dependency := range md.Dependencies {
        if err := dependency.Validate(); err != nil {
            return err
        }
    }
    return nil
}

func isValidChartType(in string) bool {
    switch in {
    case "", "application", "library":
        return true
    }
    return false
}

func isValidSemver(v string) bool {
    _, err := semver.NewVersion(v)
    return err == nil
}

// sanitizeString normalize spaces and removes non-printable characters.
func sanitizeString(str string) string {
    return strings.Map(func(r rune) rune {
        if unicode.IsSpace(r) {
            return ' '
        }
        if unicode.IsPrint(r) {
            return r
        }
        return -1
    }, str)
}
Copyright © 温玉 2021 | 浙ICP备2020032454号 all right reserved,powered by Gitbook该文件修订时间: 2022-06-22 02:06:22

results matching ""

    No results matching ""