github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/schema/util/yaml.go (about)

     1  /*
     2  Copyright 2019 The Skaffold 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 util
    18  
    19  import (
    20  	"encoding/json"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"sigs.k8s.io/kustomize/kyaml/yaml"
    24  )
    25  
    26  // UnmarshalClusterVolumes provides a helper function to
    27  // for a custom unmarshler to deal with
    28  // https://github.com/GoogleContainerTools/skaffold/issues/4175
    29  func UnmarshalClusterVolumes(value *yaml.Node) (volumes []v1.Volume, remaining []byte, result error) {
    30  	clusterMap := make(map[string]interface{})
    31  
    32  	value.Decode(clusterMap)
    33  
    34  	if vMap, hasVolumes := clusterMap["volumes"]; hasVolumes {
    35  		volumes = []v1.Volume{}
    36  		volumesBuff, err := json.Marshal(vMap)
    37  
    38  		if err != nil {
    39  			result = err
    40  			return
    41  		}
    42  
    43  		if err := json.Unmarshal(volumesBuff, &volumes); err != nil {
    44  			result = err
    45  			return
    46  		}
    47  
    48  		delete(clusterMap, "volumes")
    49  	}
    50  
    51  	// Remarshal the remaining values
    52  	remaining, result = yaml.Marshal(clusterMap)
    53  
    54  	return
    55  }
    56  
    57  // UnmarshalKanikoArtifact provides a helper function to
    58  // for a custom unmarshaller to deal with
    59  // https://github.com/GoogleContainerTools/skaffold/issues/4175
    60  func UnmarshalKanikoArtifact(value *yaml.Node) (mounts []v1.VolumeMount, remaining []byte, result error) {
    61  	kaMap := make(map[string]interface{})
    62  
    63  	value.Decode(kaMap)
    64  
    65  	if vMap, hasVolumes := kaMap["volumeMounts"]; hasVolumes {
    66  		mounts = []v1.VolumeMount{}
    67  		volumesBuff, err := json.Marshal(vMap)
    68  
    69  		if err != nil {
    70  			result = err
    71  			return
    72  		}
    73  
    74  		if err := json.Unmarshal(volumesBuff, &mounts); err != nil {
    75  			result = err
    76  			return
    77  		}
    78  
    79  		delete(kaMap, "volumeMounts")
    80  	}
    81  
    82  	// Remarshal the remaining values
    83  	remaining, result = yaml.Marshal(kaMap)
    84  	return
    85  }