...

Source file src/helm.sh/helm/v3/pkg/storage/driver/util.go

Documentation: helm.sh/helm/v3/pkg/storage/driver

     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 driver // import "helm.sh/helm/v3/pkg/storage/driver"
    18  
    19  import (
    20  	"bytes"
    21  	"compress/gzip"
    22  	"encoding/base64"
    23  	"encoding/json"
    24  	"io"
    25  
    26  	rspb "helm.sh/helm/v3/pkg/release"
    27  )
    28  
    29  var b64 = base64.StdEncoding
    30  
    31  var magicGzip = []byte{0x1f, 0x8b, 0x08}
    32  
    33  var systemLabels = []string{"name", "owner", "status", "version", "createdAt", "modifiedAt"}
    34  
    35  // encodeRelease encodes a release returning a base64 encoded
    36  // gzipped string representation, or error.
    37  func encodeRelease(rls *rspb.Release) (string, error) {
    38  	b, err := json.Marshal(rls)
    39  	if err != nil {
    40  		return "", err
    41  	}
    42  	var buf bytes.Buffer
    43  	w, err := gzip.NewWriterLevel(&buf, gzip.BestCompression)
    44  	if err != nil {
    45  		return "", err
    46  	}
    47  	if _, err = w.Write(b); err != nil {
    48  		return "", err
    49  	}
    50  	w.Close()
    51  
    52  	return b64.EncodeToString(buf.Bytes()), nil
    53  }
    54  
    55  // decodeRelease decodes the bytes of data into a release
    56  // type. Data must contain a base64 encoded gzipped string of a
    57  // valid release, otherwise an error is returned.
    58  func decodeRelease(data string) (*rspb.Release, error) {
    59  	// base64 decode string
    60  	b, err := b64.DecodeString(data)
    61  	if err != nil {
    62  		return nil, err
    63  	}
    64  
    65  	// For backwards compatibility with releases that were stored before
    66  	// compression was introduced we skip decompression if the
    67  	// gzip magic header is not found
    68  	if len(b) > 3 && bytes.Equal(b[0:3], magicGzip) {
    69  		r, err := gzip.NewReader(bytes.NewReader(b))
    70  		if err != nil {
    71  			return nil, err
    72  		}
    73  		defer r.Close()
    74  		b2, err := io.ReadAll(r)
    75  		if err != nil {
    76  			return nil, err
    77  		}
    78  		b = b2
    79  	}
    80  
    81  	var rls rspb.Release
    82  	// unmarshal release object bytes
    83  	if err := json.Unmarshal(b, &rls); err != nil {
    84  		return nil, err
    85  	}
    86  	return &rls, nil
    87  }
    88  
    89  // Checks if label is system
    90  func isSystemLabel(key string) bool {
    91  	for _, v := range GetSystemLabels() {
    92  		if key == v {
    93  			return true
    94  		}
    95  	}
    96  	return false
    97  }
    98  
    99  // Removes system labels from labels map
   100  func filterSystemLabels(lbs map[string]string) map[string]string {
   101  	result := make(map[string]string)
   102  	for k, v := range lbs {
   103  		if !isSystemLabel(k) {
   104  			result[k] = v
   105  		}
   106  	}
   107  	return result
   108  }
   109  
   110  // Checks if labels array contains system labels
   111  func ContainsSystemLabels(lbs map[string]string) bool {
   112  	for k := range lbs {
   113  		if isSystemLabel(k) {
   114  			return true
   115  		}
   116  	}
   117  	return false
   118  }
   119  
   120  func GetSystemLabels() []string {
   121  	return systemLabels
   122  }
   123  

View as plain text