github.com/GoogleContainerTools/skaffold@v1.39.18/pkg/skaffold/kubernetes/manifest/filter.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 manifest
    18  
    19  import (
    20  	"fmt"
    21  
    22  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    23  	k8syaml "sigs.k8s.io/yaml"
    24  )
    25  
    26  // Filter returns the manifest list that match any of the given `GroupKindSelector` items
    27  func (l *ManifestList) Filter(selectors ...GroupKindSelector) (ManifestList, error) {
    28  	if l == nil {
    29  		return nil, nil
    30  	}
    31  	var filtered ManifestList
    32  	for _, yByte := range *l {
    33  		// Convert yaml byte config to unstructured.Unstructured
    34  		jByte, err := k8syaml.YAMLToJSON(yByte)
    35  		if err != nil {
    36  			return nil, fmt.Errorf("yaml to json error: %w", err)
    37  		}
    38  		var obj unstructured.Unstructured
    39  		if err := obj.UnmarshalJSON(jByte); err != nil {
    40  			return nil, fmt.Errorf("unmarshaling config: %w", err)
    41  		}
    42  		gvk := obj.GroupVersionKind()
    43  		for _, w := range selectors {
    44  			if w.Matches(gvk.Group, gvk.Kind) {
    45  				filtered.Append(yByte)
    46  			}
    47  		}
    48  	}
    49  	return filtered, nil
    50  }
    51  
    52  // SelectResources returns the resources defined in the manifest list that match any of the given `GroupKindSelector` items
    53  func (l *ManifestList) SelectResources(selectors ...GroupKindSelector) ([]unstructured.Unstructured, error) {
    54  	if l == nil {
    55  		return nil, nil
    56  	}
    57  	var customResources []unstructured.Unstructured
    58  	for _, yByte := range *l {
    59  		// Convert yaml byte config to unstructured.Unstructured
    60  		jByte, err := k8syaml.YAMLToJSON(yByte)
    61  		if err != nil {
    62  			return nil, fmt.Errorf("yaml to json error: %w", err)
    63  		}
    64  		var obj unstructured.Unstructured
    65  		if err := obj.UnmarshalJSON(jByte); err != nil {
    66  			return nil, fmt.Errorf("unmarshaling config: %w", err)
    67  		}
    68  
    69  		gvk := obj.GroupVersionKind()
    70  		for _, w := range selectors {
    71  			if w.Matches(gvk.Group, gvk.Kind) {
    72  				customResources = append(customResources, obj)
    73  			}
    74  		}
    75  	}
    76  	return customResources, nil
    77  }