add typemeta to kustomization

This commit is contained in:
Jingfang Liu
2018-08-16 11:23:36 -07:00
parent 7050a45134
commit 6221bed190
3 changed files with 52 additions and 12 deletions

View File

@@ -35,8 +35,23 @@ import (
) )
var ( var (
kustomizationFields = []string{"resources", "bases", "namePrefix", "namespace", "crds", "commonLabels", "commonAnnotations", "patches", "configMapGenerator", "secretGenerator", "vars", "imageTags"} // These field names are the exact kustomization fields
recognizedFields = regexp.MustCompile("^(" + strings.Join(kustomizationFields, "|") + "):") kustomizationFields = []string{
"APIVersion",
"Kind",
"Resources",
"Bases",
"NamePrefix",
"Namespace",
"Crds",
"CommonLabels",
"CommonAnnotations",
"Patches",
"ConfigMapGenerator",
"SecretGenerator",
"Vars",
"ImageTags",
}
) )
// commentedField records the comment associated with a kustomization field // commentedField records the comment associated with a kustomization field
@@ -51,6 +66,10 @@ func (cf *commentedField) appendComment(comment []byte) {
cf.comment = append(cf.comment, comment...) cf.comment = append(cf.comment, comment...)
} }
func squash(x [][]byte) []byte {
return bytes.Join(x, []byte(``))
}
type kustomizationFile struct { type kustomizationFile struct {
path string path string
fsys fs.FileSystem fsys fs.FileSystem
@@ -129,20 +148,20 @@ func stringInSlice(str string, list []string) bool {
func (mf *kustomizationFile) parseCommentedFields(content []byte) error { func (mf *kustomizationFile) parseCommentedFields(content []byte) error {
buffer := bytes.NewBuffer(content) buffer := bytes.NewBuffer(content)
var comments [][]byte var comments [][]byte
var currentfield string
line, err := buffer.ReadBytes('\n') line, err := buffer.ReadBytes('\n')
for err == nil { for err == nil {
if isCommentOrBlankLine(line) { if isCommentOrBlankLine(line) {
comments = append(comments, line) comments = append(comments, line)
} else if recognizedFields.Match(line) { } else {
fields := recognizedFields.FindSubmatch(line) matched, field := findMatchedField(line)
currentfield = string(fields[1]) if matched {
mf.originalFields = append(mf.originalFields, &commentedField{field: currentfield, comment: bytes.Join(comments, []byte(``))}) mf.originalFields = append(mf.originalFields, &commentedField{field: field, comment: squash(comments)})
comments = [][]byte{} comments = [][]byte{}
} else if len(comments) > 0 { } else if len(comments) > 0 {
mf.originalFields[len(mf.originalFields)-1].appendComment(bytes.Join(comments, []byte(``))) mf.originalFields[len(mf.originalFields)-1].appendComment(squash(comments))
comments = [][]byte{} comments = [][]byte{}
}
} }
line, err = buffer.ReadBytes('\n') line, err = buffer.ReadBytes('\n')
} }
@@ -199,6 +218,17 @@ func isCommentOrBlankLine(line []byte) bool {
return len(s) == 0 || bytes.HasPrefix(s, []byte(`#`)) return len(s) == 0 || bytes.HasPrefix(s, []byte(`#`))
} }
func findMatchedField(line []byte) (bool, string) {
for _, field := range kustomizationFields {
// (?i) is for case insensitive regexp matching
r := regexp.MustCompile("^(" + "(?i)" + field + "):")
if r.Match(line) {
return true, field
}
}
return false, ""
}
// marshalField marshal a given field of a kustomization object into yaml format. // marshalField marshal a given field of a kustomization object into yaml format.
// If the field wasn't in the original kustomization.yaml file or wasn't added, // If the field wasn't in the original kustomization.yaml file or wasn't added,
// an empty []byte is returned. // an empty []byte is returned.

View File

@@ -147,6 +147,8 @@ resources:
# See which field this comment goes into # See which field this comment goes into
- service.yaml - service.yaml
APIVersion: v1beta1
kind: kustomization.yaml
# something you may want to keep # something you may want to keep
vars: vars:
@@ -158,7 +160,7 @@ vars:
kind: Service kind: Service
name: my-service name: my-service
bases: BASES:
- ../namespaces - ../namespaces
# some descriptions for the patches # some descriptions for the patches
@@ -180,6 +182,8 @@ resources:
- pod.yaml - pod.yaml
- service.yaml - service.yaml
apiVersion: v1beta1
kind: kustomization.yaml
# something you may want to keep # something you may want to keep
vars: vars:

View File

@@ -17,8 +17,14 @@ limitations under the License.
// Package types holds struct definitions that should find a better home. // Package types holds struct definitions that should find a better home.
package types package types
import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// Kustomization holds the information needed to generate customized k8s api resources. // Kustomization holds the information needed to generate customized k8s api resources.
type Kustomization struct { type Kustomization struct {
metav1.TypeMeta `json:",inline" yaml:",inline"`
// NamePrefix will prefix the names of all resources mentioned in the kustomization // NamePrefix will prefix the names of all resources mentioned in the kustomization
// file including generated configmaps and secrets. // file including generated configmaps and secrets.
NamePrefix string `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"` NamePrefix string `json:"namePrefix,omitempty" yaml:"namePrefix,omitempty"`