github.com/imran-kn/cilium-fork@v1.6.9/pkg/policy/trace/yaml.go (about) 1 // Copyright 2017 Authors of Cilium 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package trace 16 17 import ( 18 "bytes" 19 "fmt" 20 "io/ioutil" 21 "os" 22 23 k8sConst "github.com/cilium/cilium/pkg/k8s/apis/cilium.io" 24 "github.com/cilium/cilium/pkg/labels" 25 26 "k8s.io/api/core/v1" 27 "k8s.io/api/extensions/v1beta1" 28 "k8s.io/client-go/kubernetes/scheme" 29 ) 30 31 const ( 32 // DefaultNamespace represents the default Kubernetes namespace. 33 DefaultNamespace = "default" 34 ) 35 36 // GetLabelsFromYaml iterates through the provided YAML file and for each 37 // section in the YAML, returns the labels or an error if the labels could not 38 // be parsed. 39 func GetLabelsFromYaml(file string) ([][]string, error) { 40 reader, err := os.Open(file) 41 42 if err != nil { 43 return nil, err 44 } 45 defer reader.Close() 46 47 byteArr, err := ioutil.ReadAll(reader) 48 if err != nil { 49 return nil, err 50 } 51 52 splitYamlLabels := [][]string{} 53 yamlDocs := bytes.Split(byteArr, []byte("---")) 54 for _, v := range yamlDocs { 55 yamlLabels := []string{} 56 57 // Ignore empty documents, e.g., file starting with --- or ---\n 58 if len(v) < 2 { 59 continue 60 } 61 62 obj, _, err := scheme.Codecs.UniversalDeserializer().Decode(v, nil, nil) 63 if err != nil { 64 return nil, err 65 } 66 switch obj.(type) { 67 case *v1beta1.Deployment: 68 deployment := obj.(*v1beta1.Deployment) 69 var ns string 70 if deployment.Namespace != "" { 71 ns = deployment.Namespace 72 } else { 73 ns = DefaultNamespace 74 } 75 yamlLabels = append(yamlLabels, labels.GenerateK8sLabelString(k8sConst.PodNamespaceLabel, ns)) 76 77 for k, v := range deployment.Spec.Template.Labels { 78 yamlLabels = append(yamlLabels, labels.GenerateK8sLabelString(k, v)) 79 } 80 case *v1.ReplicationController: 81 controller := obj.(*v1.ReplicationController) 82 var ns string 83 if controller.Namespace != "" { 84 ns = controller.Namespace 85 } else { 86 ns = DefaultNamespace 87 } 88 yamlLabels = append(yamlLabels, labels.GenerateK8sLabelString(k8sConst.PodNamespaceLabel, ns)) 89 90 for k, v := range controller.Spec.Template.Labels { 91 yamlLabels = append(yamlLabels, labels.GenerateK8sLabelString(k, v)) 92 } 93 case *v1beta1.ReplicaSet: 94 rep := obj.(*v1beta1.ReplicaSet) 95 var ns string 96 if rep.Namespace != "" { 97 ns = rep.Namespace 98 } else { 99 ns = DefaultNamespace 100 } 101 yamlLabels = append(yamlLabels, labels.GenerateK8sLabelString(k8sConst.PodNamespaceLabel, ns)) 102 103 for k, v := range rep.Spec.Template.Labels { 104 yamlLabels = append(yamlLabels, labels.GenerateK8sLabelString(k, v)) 105 } 106 default: 107 return nil, fmt.Errorf("unsupported type provided in YAML file: %T", obj) 108 } 109 splitYamlLabels = append(splitYamlLabels, yamlLabels) 110 } 111 return splitYamlLabels, nil 112 }