github.com/verrazzano/verrazzano@v1.7.0/application-operator/controllers/loggingtrait/loggingtrait_controller_helper.go (about)

     1  // Copyright (c) 2021, 2022, Oracle and/or its affiliates.
     2  // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl.
     3  
     4  package loggingtrait
     5  
     6  import (
     7  	"encoding/json"
     8  	"reflect"
     9  
    10  	"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
    11  )
    12  
    13  // struct2Unmarshal - Struct to unstructured
    14  func struct2Unmarshal(obj interface{}) (unstructured.Unstructured, error) {
    15  	marshal, err := json.Marshal(obj)
    16  	var c unstructured.Unstructured
    17  	c.UnmarshalJSON(marshal)
    18  	return c, err
    19  }
    20  
    21  // appendSliceOfInterface - Append two slices of interfaces in to one slice without duplicates
    22  func appendSliceOfInterface(aSlice []interface{}, bSlice []interface{}) []interface{} {
    23  
    24  	res := make([]interface{}, 0)
    25  	res = append(res, bSlice...)
    26  
    27  	for _, k := range aSlice {
    28  		jndex := -1
    29  		for j, l := range bSlice {
    30  			if reflect.DeepEqual(k, l) {
    31  				jndex = j
    32  			}
    33  		}
    34  		if jndex == -1 {
    35  			res = append(res, k)
    36  		}
    37  	}
    38  
    39  	return res
    40  
    41  }
    42  
    43  // locateContainersField locate the containers field
    44  func locateContainersField(res *unstructured.Unstructured) (bool, []string) {
    45  	var containersFieldPath []string
    46  	var ok = false
    47  	kind := res.GetKind()
    48  
    49  	switch kind {
    50  	case "Pod":
    51  		containersFieldPath = []string{"spec", "containers"}
    52  		ok = true
    53  	case "ContainerizedWorkload":
    54  		containersFieldPath = []string{"spec", "containers"}
    55  		ok = true
    56  	case "Deployment":
    57  		containersFieldPath = []string{"spec", "template", "spec", "containers"}
    58  		ok = true
    59  	case "StatefulSet":
    60  		containersFieldPath = []string{"spec", "template", "spec", "containers"}
    61  		ok = true
    62  	case "DaemonSet":
    63  		containersFieldPath = []string{"spec", "template", "spec", "containers"}
    64  		ok = true
    65  	}
    66  
    67  	return ok, containersFieldPath
    68  }
    69  
    70  // locateVolumesField locate the volumes field
    71  func locateVolumesField(res *unstructured.Unstructured) (bool, []string) {
    72  	var volumeFieldPath []string
    73  	var ok = false
    74  	kind := res.GetKind()
    75  
    76  	switch kind {
    77  	case "Pod":
    78  		volumeFieldPath = []string{"spec", "volumes"}
    79  		ok = true
    80  	case "ContainerizedWorkload":
    81  		volumeFieldPath = []string{"spec", "volumes"}
    82  		ok = true
    83  	case "Deployment":
    84  		volumeFieldPath = []string{"spec", "template", "spec", "volumes"}
    85  		ok = true
    86  	case "StatefulSet":
    87  		volumeFieldPath = []string{"spec", "template", "spec", "volumes"}
    88  		ok = true
    89  	case "DaemonSet":
    90  		volumeFieldPath = []string{"spec", "template", "spec", "volumes"}
    91  		ok = true
    92  	}
    93  
    94  	return ok, volumeFieldPath
    95  }