...

Source file src/helm.sh/helm/v3/pkg/releaseutil/manifest.go

Documentation: helm.sh/helm/v3/pkg/releaseutil

     1  /*
     2  Copyright The Helm Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package releaseutil
    18  
    19  import (
    20  	"fmt"
    21  	"regexp"
    22  	"strconv"
    23  	"strings"
    24  )
    25  
    26  // SimpleHead defines what the structure of the head of a manifest file
    27  type SimpleHead struct {
    28  	Version  string `json:"apiVersion"`
    29  	Kind     string `json:"kind,omitempty"`
    30  	Metadata *struct {
    31  		Name        string            `json:"name"`
    32  		Annotations map[string]string `json:"annotations"`
    33  	} `json:"metadata,omitempty"`
    34  }
    35  
    36  var sep = regexp.MustCompile("(?:^|\\s*\n)---\\s*")
    37  
    38  // SplitManifests takes a string of manifest and returns a map contains individual manifests
    39  func SplitManifests(bigFile string) map[string]string {
    40  	// Basically, we're quickly splitting a stream of YAML documents into an
    41  	// array of YAML docs. The file name is just a place holder, but should be
    42  	// integer-sortable so that manifests get output in the same order as the
    43  	// input (see `BySplitManifestsOrder`).
    44  	tpl := "manifest-%d"
    45  	res := map[string]string{}
    46  	// Making sure that any extra whitespace in YAML stream doesn't interfere in splitting documents correctly.
    47  	bigFileTmp := strings.TrimSpace(bigFile)
    48  	docs := sep.Split(bigFileTmp, -1)
    49  	var count int
    50  	for _, d := range docs {
    51  		if d == "" {
    52  			continue
    53  		}
    54  
    55  		d = strings.TrimSpace(d)
    56  		res[fmt.Sprintf(tpl, count)] = d
    57  		count = count + 1
    58  	}
    59  	return res
    60  }
    61  
    62  // BySplitManifestsOrder sorts by in-file manifest order, as provided in function `SplitManifests`
    63  type BySplitManifestsOrder []string
    64  
    65  func (a BySplitManifestsOrder) Len() int { return len(a) }
    66  func (a BySplitManifestsOrder) Less(i, j int) bool {
    67  	// Split `manifest-%d`
    68  	anum, _ := strconv.ParseInt(a[i][len("manifest-"):], 10, 0)
    69  	bnum, _ := strconv.ParseInt(a[j][len("manifest-"):], 10, 0)
    70  	return anum < bnum
    71  }
    72  func (a BySplitManifestsOrder) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
    73  

View as plain text