...

Source file src/sigs.k8s.io/cli-utils/pkg/object/unstructured_set.go

Documentation: sigs.k8s.io/cli-utils/pkg/object

     1  // Copyright 2021 The Kubernetes Authors.
     2  // SPDX-License-Identifier: Apache-2.0
     3  //
     4  
     5  package object
     6  
     7  import (
     8  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
     9  )
    10  
    11  // UnstructuredSet is an ordered list of Unstructured that acts like an
    12  // unordered set for comparison purposes.
    13  type UnstructuredSet []*unstructured.Unstructured
    14  
    15  // UnstructuredSetEquals returns true if the slice of objects in setA equals
    16  // the slice of objects in setB.
    17  func UnstructuredSetEquals(setA []*unstructured.Unstructured, setB []*unstructured.Unstructured) bool {
    18  	return UnstructuredSet(setA).Equal(UnstructuredSet(setB))
    19  }
    20  
    21  func (setA UnstructuredSet) Equal(setB UnstructuredSet) bool {
    22  	mapA := make(map[string]string, len(setA))
    23  	for _, a := range setA {
    24  		jsonBytes, err := a.MarshalJSON()
    25  		if err != nil {
    26  			mapA[string(jsonBytes)] = err.Error()
    27  		} else {
    28  			mapA[string(jsonBytes)] = ""
    29  		}
    30  	}
    31  	mapB := make(map[string]string, len(setB))
    32  	for _, b := range setB {
    33  		jsonBytes, err := b.MarshalJSON()
    34  		if err != nil {
    35  			mapB[string(jsonBytes)] = err.Error()
    36  		} else {
    37  			mapB[string(jsonBytes)] = ""
    38  		}
    39  	}
    40  	if len(mapA) != len(mapB) {
    41  		return false
    42  	}
    43  	for b, errB := range mapB {
    44  		if errA, exists := mapA[b]; !exists {
    45  			if !exists {
    46  				return false
    47  			}
    48  			if errA != errB {
    49  				return false
    50  			}
    51  		}
    52  	}
    53  	return true
    54  }
    55  

View as plain text