@@ -21,6 +21,7 @@ import (
21
21
metaV1 "k8s.io/apimachinery/pkg/apis/meta/v1"
22
22
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
23
23
"k8s.io/apimachinery/pkg/runtime"
24
+ "k8s.io/apimachinery/pkg/util/intstr"
24
25
"k8s.io/apimachinery/pkg/util/validation"
25
26
"k8s.io/cli-runtime/pkg/resource"
26
27
)
@@ -104,16 +105,40 @@ func k8sServiceFromInfo(info *resource.Info) (*Service, error) {
104
105
serviceCtx .Resource .Type = svcObj .Spec .Type
105
106
serviceCtx .Resource .Selectors = matchLabelSelectorToStrLabels (svcObj .Spec .Selector )
106
107
serviceCtx .Resource .ExposeExternally = (svcObj .Spec .Type == v1 .ServiceTypeLoadBalancer || svcObj .Spec .Type == v1 .ServiceTypeNodePort )
107
- serviceCtx .Resource .ExposeToCluster = false
108
108
109
+ prometheusPort , prometheusPortValid := exposedPrometheusScrapePort (svcObj .Annotations )
109
110
for _ , p := range svcObj .Spec .Ports {
110
- n := SvcNetworkAttr {Port : int (p .Port ), TargetPort : p .TargetPort , Protocol : p .Protocol }
111
+ n := SvcNetworkAttr {Port : int (p .Port ), TargetPort : p .TargetPort , Protocol : p .Protocol , name : p .Name }
112
+ n .exposeToCluster = prometheusPortValid && n .equals (prometheusPort )
111
113
serviceCtx .Resource .Network = append (serviceCtx .Resource .Network , n )
112
114
}
113
115
114
116
return & serviceCtx , nil
115
117
}
116
118
119
+ const defaultPrometheusScrapePort = 9090
120
+
121
+ func exposedPrometheusScrapePort (annotations map [string ]string ) (* intstr.IntOrString , bool ) {
122
+ scrapeOn := false
123
+ scrapePort := intstr .FromInt (defaultPrometheusScrapePort )
124
+ for k , v := range annotations {
125
+ if strings .Contains (k , "prometheus" ) {
126
+ if strings .HasSuffix (k , "/scrape" ) && v == "true" {
127
+ scrapeOn = true
128
+ } else if strings .HasSuffix (k , "/port" ) {
129
+ scrapePort = intstr .Parse (v )
130
+ }
131
+ }
132
+ }
133
+
134
+ return & scrapePort , scrapeOn
135
+ }
136
+
137
+ func (port * SvcNetworkAttr ) equals (intStrPort * intstr.IntOrString ) bool {
138
+ return (port .name != "" && port .name == intStrPort .StrVal ) ||
139
+ (port .Port > 0 && port .Port == int (intStrPort .IntVal ))
140
+ }
141
+
117
142
// ocRouteFromInfo updates servicesToExpose based on an OpenShift Route object
118
143
func ocRouteFromInfo (info * resource.Info , toExpose servicesToExpose ) error {
119
144
routeObj := parseResourceFromInfo [ocroutev1.Route ](info )
@@ -123,12 +148,12 @@ func ocRouteFromInfo(info *resource.Info, toExpose servicesToExpose) error {
123
148
124
149
exposedServicesInNamespace , ok := toExpose [routeObj .Namespace ]
125
150
if ! ok {
126
- toExpose [routeObj .Namespace ] = map [string ]bool {}
151
+ toExpose [routeObj .Namespace ] = map [string ][] * intstr. IntOrString {}
127
152
exposedServicesInNamespace = toExpose [routeObj .Namespace ]
128
153
}
129
- exposedServicesInNamespace [ routeObj .Spec .To .Name ] = false
154
+ appendToSliceInMap ( exposedServicesInNamespace , routeObj .Spec .To .Name , & routeObj . Spec . Port . TargetPort )
130
155
for _ , backend := range routeObj .Spec .AlternateBackends {
131
- exposedServicesInNamespace [ backend .Name ] = false
156
+ appendToSliceInMap ( exposedServicesInNamespace , backend .Name , & routeObj . Spec . Port . TargetPort )
132
157
}
133
158
134
159
return nil
@@ -143,13 +168,14 @@ func k8sIngressFromInfo(info *resource.Info, toExpose servicesToExpose) error {
143
168
144
169
exposedServicesInNamespace , ok := toExpose [ingressObj .Namespace ]
145
170
if ! ok {
146
- toExpose [ingressObj .Namespace ] = map [string ]bool {}
171
+ toExpose [ingressObj .Namespace ] = map [string ][] * intstr. IntOrString {}
147
172
exposedServicesInNamespace = toExpose [ingressObj .Namespace ]
148
173
}
149
174
150
175
defaultBackend := ingressObj .Spec .DefaultBackend
151
176
if defaultBackend != nil && defaultBackend .Service != nil {
152
- exposedServicesInNamespace [defaultBackend .Service .Name ] = false
177
+ portToAppend := portFromServiceBackendPort (& defaultBackend .Service .Port )
178
+ appendToSliceInMap (exposedServicesInNamespace , defaultBackend .Service .Name , portToAppend )
153
179
}
154
180
155
181
for ruleIdx := range ingressObj .Spec .Rules {
@@ -158,7 +184,8 @@ func k8sIngressFromInfo(info *resource.Info, toExpose servicesToExpose) error {
158
184
for pathIdx := range rule .HTTP .Paths {
159
185
svc := rule .HTTP .Paths [pathIdx ].Backend .Service
160
186
if svc != nil {
161
- exposedServicesInNamespace [svc .Name ] = false
187
+ portToAppend := portFromServiceBackendPort (& svc .Port )
188
+ appendToSliceInMap (exposedServicesInNamespace , svc .Name , portToAppend )
162
189
}
163
190
}
164
191
}
@@ -167,6 +194,14 @@ func k8sIngressFromInfo(info *resource.Info, toExpose servicesToExpose) error {
167
194
return nil
168
195
}
169
196
197
+ func portFromServiceBackendPort (sbp * networkv1.ServiceBackendPort ) * intstr.IntOrString {
198
+ res := intstr .FromInt32 (sbp .Number )
199
+ if sbp .Number == 0 {
200
+ res = intstr .FromString (sbp .Name )
201
+ }
202
+ return & res
203
+ }
204
+
170
205
func parseDeployResource (podSpec * v1.PodTemplateSpec , obj metaV1.Object , resourceCtx * Resource ) {
171
206
resourceCtx .Resource .Name = obj .GetName ()
172
207
resourceCtx .Resource .Namespace = obj .GetNamespace ()
0 commit comments