github.com/pyroscope-io/pyroscope@v0.37.3-0.20230725203016-5f6947968bd0/pkg/scrape/discovery/kubernetes/ingress_adaptor.go (about) 1 // Copyright 2016 The Prometheus Authors 2 // Copyright 2021 The Pyroscope 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 package kubernetes 17 18 import ( 19 v1 "k8s.io/api/networking/v1" 20 "k8s.io/api/networking/v1beta1" 21 ) 22 23 // ingressAdaptor is an adaptor for the different Ingress versions 24 type ingressAdaptor interface { 25 name() string 26 namespace() string 27 labels() map[string]string 28 annotations() map[string]string 29 tlsHosts() []string 30 ingressClassName() *string 31 rules() []ingressRuleAdaptor 32 } 33 34 type ingressRuleAdaptor interface { 35 paths() []string 36 host() string 37 } 38 39 // Adaptor for networking.k8s.io/v1 40 type ingressAdaptorV1 struct { 41 ingress *v1.Ingress 42 } 43 44 func newIngressAdaptorFromV1(ingress *v1.Ingress) ingressAdaptor { 45 return &ingressAdaptorV1{ingress: ingress} 46 } 47 48 func (i *ingressAdaptorV1) name() string { return i.ingress.Name } 49 func (i *ingressAdaptorV1) namespace() string { return i.ingress.Namespace } 50 func (i *ingressAdaptorV1) labels() map[string]string { return i.ingress.Labels } 51 func (i *ingressAdaptorV1) annotations() map[string]string { return i.ingress.Annotations } 52 func (i *ingressAdaptorV1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } 53 54 func (i *ingressAdaptorV1) tlsHosts() []string { 55 var hosts []string 56 for _, tls := range i.ingress.Spec.TLS { 57 hosts = append(hosts, tls.Hosts...) 58 } 59 return hosts 60 } 61 62 func (i *ingressAdaptorV1) rules() []ingressRuleAdaptor { 63 var rules []ingressRuleAdaptor 64 for _, rule := range i.ingress.Spec.Rules { 65 rules = append(rules, newIngressRuleAdaptorFromV1(rule)) 66 } 67 return rules 68 } 69 70 type ingressRuleAdaptorV1 struct { 71 rule v1.IngressRule 72 } 73 74 func newIngressRuleAdaptorFromV1(rule v1.IngressRule) ingressRuleAdaptor { 75 return &ingressRuleAdaptorV1{rule: rule} 76 } 77 78 func (i *ingressRuleAdaptorV1) paths() []string { 79 rv := i.rule.IngressRuleValue 80 if rv.HTTP == nil { 81 return nil 82 } 83 paths := make([]string, len(rv.HTTP.Paths)) 84 for n, p := range rv.HTTP.Paths { 85 paths[n] = p.Path 86 } 87 return paths 88 } 89 90 func (i *ingressRuleAdaptorV1) host() string { return i.rule.Host } 91 92 // Adaptor for networking.k8s.io/v1beta1 93 type ingressAdaptorV1Beta1 struct { 94 ingress *v1beta1.Ingress 95 } 96 97 func newIngressAdaptorFromV1beta1(ingress *v1beta1.Ingress) ingressAdaptor { 98 return &ingressAdaptorV1Beta1{ingress: ingress} 99 } 100 101 func (i *ingressAdaptorV1Beta1) name() string { return i.ingress.Name } 102 func (i *ingressAdaptorV1Beta1) namespace() string { return i.ingress.Namespace } 103 func (i *ingressAdaptorV1Beta1) labels() map[string]string { return i.ingress.Labels } 104 func (i *ingressAdaptorV1Beta1) annotations() map[string]string { return i.ingress.Annotations } 105 func (i *ingressAdaptorV1Beta1) ingressClassName() *string { return i.ingress.Spec.IngressClassName } 106 107 func (i *ingressAdaptorV1Beta1) tlsHosts() []string { 108 var hosts []string 109 for _, tls := range i.ingress.Spec.TLS { 110 hosts = append(hosts, tls.Hosts...) 111 } 112 return hosts 113 } 114 115 func (i *ingressAdaptorV1Beta1) rules() []ingressRuleAdaptor { 116 var rules []ingressRuleAdaptor 117 for _, rule := range i.ingress.Spec.Rules { 118 rules = append(rules, newIngressRuleAdaptorFromV1Beta1(rule)) 119 } 120 return rules 121 } 122 123 type ingressRuleAdaptorV1Beta1 struct { 124 rule v1beta1.IngressRule 125 } 126 127 func newIngressRuleAdaptorFromV1Beta1(rule v1beta1.IngressRule) ingressRuleAdaptor { 128 return &ingressRuleAdaptorV1Beta1{rule: rule} 129 } 130 131 func (i *ingressRuleAdaptorV1Beta1) paths() []string { 132 rv := i.rule.IngressRuleValue 133 if rv.HTTP == nil { 134 return nil 135 } 136 paths := make([]string, len(rv.HTTP.Paths)) 137 for n, p := range rv.HTTP.Paths { 138 paths[n] = p.Path 139 } 140 return paths 141 } 142 143 func (i *ingressRuleAdaptorV1Beta1) host() string { return i.rule.Host }