1

我在看https://godoc.org/k8s.io/api/core/v1#Secret

type Secret struct {
    metav1.TypeMeta `json:",inline"`
    // Standard object's metadata.
    // More info: https://git.k8s.io/community/contributors/devel/api-conventions.md#metadata
    // +optional
    metav1.ObjectMeta `json:"metadata,omitempty" protobuf:"bytes,1,opt,name=metadata"`

    // Data contains the secret data. Each key must consist of alphanumeric
    // characters, '-', '_' or '.'. The serialized form of the secret data is a
    // base64 encoded string, representing the arbitrary (possibly non-string)
    // data value here. Described in https://tools.ietf.org/html/rfc4648#section-4
    // +optional
    Data map[string][]byte `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"`

    // stringData allows specifying non-binary secret data in string form.
    // It is provided as a write-only convenience method.
    // All keys and values are merged into the data field on write, overwriting any existing values.
    // It is never output when reading from the API.
    // +k8s:conversion-gen=false
    // +optional
    StringData map[string]string `json:"stringData,omitempty" protobuf:"bytes,4,rep,name=stringData"`

    // Used to facilitate programmatic handling of secret data.
    // +optional
    Type SecretType `json:"type,omitempty" protobuf:"bytes,3,opt,name=type,casttype=SecretType"`
}

举 个Data map[string][]byte `json:"data,omitempty" protobuf:"bytes,2,rep,name=data"` 例子,我知道Data是名称,map[string][]byte是类型,接下来的第三件事是什么?它是做什么的,什么时候需要包含这第三件事?

4

1 回答 1

4

这些json:"data,omitempty" protobuf:"bytes,2,rep,name=data"被称为结构标签。关于该主题的一些有用链接是:

  • 官方语言规范在这里
  • 这里有一些非常知名的
  • 一个教程如何在这里创建自定义的。

标签是结构定义的一部分,允许您告诉结构如何存储数据、创建映射、进行验证等。事实上,您会在处理数据的 Go 包中看到很多标签。

于 2019-04-05T23:43:31.907 回答