discovery: Remove ingress & endpoint slice adaptors (#16413)

* Remove ingress & endpoint slice adaptors
* fix ci

Signed-off-by: zhengkezhou1 <madzhou1@gmail.com>
This commit is contained in:
Zhengke Zhou 2025-04-09 17:25:53 +08:00 committed by GitHub
parent caa1b9abb7
commit c884dd16ac
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 94 additions and 418 deletions

View File

@ -16,7 +16,6 @@ package kubernetes
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"log/slog" "log/slog"
"net" "net"
"strconv" "strconv"
@ -106,13 +105,13 @@ func NewEndpointSlice(l *slog.Logger, eps cache.SharedIndexInformer, svc, pod, n
// LabelServiceName so this operation doesn't have to iterate over all // LabelServiceName so this operation doesn't have to iterate over all
// endpoint objects. // endpoint objects.
for _, obj := range e.endpointSliceStore.List() { for _, obj := range e.endpointSliceStore.List() {
esa, err := e.getEndpointSliceAdaptor(obj) es, ok := obj.(*v1.EndpointSlice)
if err != nil { if !ok {
e.logger.Error("converting to EndpointSlice object failed", "err", err) e.logger.Error("converting to EndpointSlice object failed", "err", err)
continue continue
} }
if lv, exists := esa.labels()[esa.labelServiceName()]; exists && lv == svc.Name { if lv, exists := es.Labels[v1.LabelServiceName]; exists && lv == svc.Name {
e.enqueue(esa.get()) e.enqueue(es)
} }
} }
} }
@ -229,27 +228,17 @@ func (e *EndpointSlice) process(ctx context.Context, ch chan<- []*targetgroup.Gr
return true return true
} }
esa, err := e.getEndpointSliceAdaptor(o) if es, ok := o.(*v1.EndpointSlice); ok {
if err != nil { send(ctx, ch, e.buildEndpointSlice(*es))
e.logger.Error("converting to EndpointSlice object failed", "err", err) } else {
return true e.logger.Error("received unexpected object", "object", o)
return false
} }
send(ctx, ch, e.buildEndpointSlice(esa))
return true return true
} }
func (e *EndpointSlice) getEndpointSliceAdaptor(o interface{}) (endpointSliceAdaptor, error) { func endpointSliceSource(ep v1.EndpointSlice) string {
switch endpointSlice := o.(type) { return endpointSliceSourceFromNamespaceAndName(ep.Namespace, ep.Name)
case *v1.EndpointSlice:
return newEndpointSliceAdaptorFromV1(endpointSlice), nil
default:
return nil, fmt.Errorf("received unexpected object: %v", o)
}
}
func endpointSliceSource(ep endpointSliceAdaptor) string {
return endpointSliceSourceFromNamespaceAndName(ep.namespace(), ep.name())
} }
func endpointSliceSourceFromNamespaceAndName(namespace, name string) string { func endpointSliceSourceFromNamespaceAndName(namespace, name string) string {
@ -274,95 +263,95 @@ const (
endpointSliceEndpointTopologyLabelPresentPrefix = metaLabelPrefix + "endpointslice_endpoint_topology_present_" endpointSliceEndpointTopologyLabelPresentPrefix = metaLabelPrefix + "endpointslice_endpoint_topology_present_"
) )
func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgroup.Group { func (e *EndpointSlice) buildEndpointSlice(eps v1.EndpointSlice) *targetgroup.Group {
tg := &targetgroup.Group{ tg := &targetgroup.Group{
Source: endpointSliceSource(eps), Source: endpointSliceSource(eps),
} }
tg.Labels = model.LabelSet{ tg.Labels = model.LabelSet{
namespaceLabel: lv(eps.namespace()), namespaceLabel: lv(eps.Namespace),
endpointSliceAddressTypeLabel: lv(eps.addressType()), endpointSliceAddressTypeLabel: lv(string(eps.AddressType)),
} }
addObjectMetaLabels(tg.Labels, eps.getObjectMeta(), RoleEndpointSlice) addObjectMetaLabels(tg.Labels, eps.ObjectMeta, RoleEndpointSlice)
e.addServiceLabels(eps, tg) e.addServiceLabels(eps, tg)
type podEntry struct { type podEntry struct {
pod *apiv1.Pod pod *apiv1.Pod
servicePorts []endpointSlicePortAdaptor servicePorts []v1.EndpointPort
} }
seenPods := map[string]*podEntry{} seenPods := map[string]*podEntry{}
add := func(addr string, ep endpointSliceEndpointAdaptor, port endpointSlicePortAdaptor) { add := func(addr string, ep v1.Endpoint, port v1.EndpointPort) {
a := addr a := addr
if port.port() != nil { if port.Port != nil {
a = net.JoinHostPort(addr, strconv.FormatUint(uint64(*port.port()), 10)) a = net.JoinHostPort(addr, strconv.FormatUint(uint64(*port.Port), 10))
} }
target := model.LabelSet{ target := model.LabelSet{
model.AddressLabel: lv(a), model.AddressLabel: lv(a),
} }
if port.name() != nil { if port.Name != nil {
target[endpointSlicePortNameLabel] = lv(*port.name()) target[endpointSlicePortNameLabel] = lv(*port.Name)
} }
if port.protocol() != nil { if port.Protocol != nil {
target[endpointSlicePortProtocolLabel] = lv(*port.protocol()) target[endpointSlicePortProtocolLabel] = lv(string(*port.Protocol))
} }
if port.port() != nil { if port.Port != nil {
target[endpointSlicePortLabel] = lv(strconv.FormatUint(uint64(*port.port()), 10)) target[endpointSlicePortLabel] = lv(strconv.FormatUint(uint64(*port.Port), 10))
} }
if port.appProtocol() != nil { if port.AppProtocol != nil {
target[endpointSlicePortAppProtocol] = lv(*port.appProtocol()) target[endpointSlicePortAppProtocol] = lv(*port.AppProtocol)
} }
if ep.conditions().ready() != nil { if ep.Conditions.Ready != nil {
target[endpointSliceEndpointConditionsReadyLabel] = lv(strconv.FormatBool(*ep.conditions().ready())) target[endpointSliceEndpointConditionsReadyLabel] = lv(strconv.FormatBool(*ep.Conditions.Ready))
} }
if ep.conditions().serving() != nil { if ep.Conditions.Serving != nil {
target[endpointSliceEndpointConditionsServingLabel] = lv(strconv.FormatBool(*ep.conditions().serving())) target[endpointSliceEndpointConditionsServingLabel] = lv(strconv.FormatBool(*ep.Conditions.Serving))
} }
if ep.conditions().terminating() != nil { if ep.Conditions.Terminating != nil {
target[endpointSliceEndpointConditionsTerminatingLabel] = lv(strconv.FormatBool(*ep.conditions().terminating())) target[endpointSliceEndpointConditionsTerminatingLabel] = lv(strconv.FormatBool(*ep.Conditions.Terminating))
} }
if ep.hostname() != nil { if ep.Hostname != nil {
target[endpointSliceEndpointHostnameLabel] = lv(*ep.hostname()) target[endpointSliceEndpointHostnameLabel] = lv(*ep.Hostname)
} }
if ep.targetRef() != nil { if ep.TargetRef != nil {
target[model.LabelName(endpointSliceAddressTargetKindLabel)] = lv(ep.targetRef().Kind) target[model.LabelName(endpointSliceAddressTargetKindLabel)] = lv(ep.TargetRef.Kind)
target[model.LabelName(endpointSliceAddressTargetNameLabel)] = lv(ep.targetRef().Name) target[model.LabelName(endpointSliceAddressTargetNameLabel)] = lv(ep.TargetRef.Name)
} }
if ep.nodename() != nil { if ep.NodeName != nil {
target[endpointSliceEndpointNodenameLabel] = lv(*ep.nodename()) target[endpointSliceEndpointNodenameLabel] = lv(*ep.NodeName)
} }
if ep.zone() != nil { if ep.Zone != nil {
target[model.LabelName(endpointSliceEndpointZoneLabel)] = lv(*ep.zone()) target[model.LabelName(endpointSliceEndpointZoneLabel)] = lv(*ep.Zone)
} }
for k, v := range ep.topology() { for k, v := range ep.DeprecatedTopology {
ln := strutil.SanitizeLabelName(k) ln := strutil.SanitizeLabelName(k)
target[model.LabelName(endpointSliceEndpointTopologyLabelPrefix+ln)] = lv(v) target[model.LabelName(endpointSliceEndpointTopologyLabelPrefix+ln)] = lv(v)
target[model.LabelName(endpointSliceEndpointTopologyLabelPresentPrefix+ln)] = presentValue target[model.LabelName(endpointSliceEndpointTopologyLabelPresentPrefix+ln)] = presentValue
} }
if e.withNodeMetadata { if e.withNodeMetadata {
if ep.targetRef() != nil && ep.targetRef().Kind == "Node" { if ep.TargetRef != nil && ep.TargetRef.Kind == "Node" {
target = addNodeLabels(target, e.nodeInf, e.logger, &ep.targetRef().Name) target = addNodeLabels(target, e.nodeInf, e.logger, &ep.TargetRef.Name)
} else { } else {
target = addNodeLabels(target, e.nodeInf, e.logger, ep.nodename()) target = addNodeLabels(target, e.nodeInf, e.logger, ep.NodeName)
} }
} }
pod := e.resolvePodRef(ep.targetRef()) pod := e.resolvePodRef(ep.TargetRef)
if pod == nil { if pod == nil {
// This target is not a Pod, so don't continue with Pod specific logic. // This target is not a Pod, so don't continue with Pod specific logic.
tg.Targets = append(tg.Targets, target) tg.Targets = append(tg.Targets, target)
@ -383,12 +372,12 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
containers := append(pod.Spec.Containers, pod.Spec.InitContainers...) containers := append(pod.Spec.Containers, pod.Spec.InitContainers...)
for i, c := range containers { for i, c := range containers {
for _, cport := range c.Ports { for _, cport := range c.Ports {
if port.port() == nil { if port.Port == nil {
continue continue
} }
if *port.port() == cport.ContainerPort { if *port.Port == cport.ContainerPort {
ports := strconv.FormatUint(uint64(*port.port()), 10) ports := strconv.FormatUint(uint64(*port.Port), 10)
isInit := i >= len(pod.Spec.Containers) isInit := i >= len(pod.Spec.Containers)
target[podContainerNameLabel] = lv(c.Name) target[podContainerNameLabel] = lv(c.Name)
@ -408,9 +397,9 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
tg.Targets = append(tg.Targets, target) tg.Targets = append(tg.Targets, target)
} }
for _, ep := range eps.endpoints() { for _, ep := range eps.Endpoints {
for _, port := range eps.ports() { for _, port := range eps.Ports {
for _, addr := range ep.addresses() { for _, addr := range ep.Addresses {
add(addr, ep, port) add(addr, ep, port)
} }
} }
@ -429,10 +418,10 @@ func (e *EndpointSlice) buildEndpointSlice(eps endpointSliceAdaptor) *targetgrou
for _, cport := range c.Ports { for _, cport := range c.Ports {
hasSeenPort := func() bool { hasSeenPort := func() bool {
for _, eport := range pe.servicePorts { for _, eport := range pe.servicePorts {
if eport.port() == nil { if eport.Port == nil {
continue continue
} }
if cport.ContainerPort == *eport.port() { if cport.ContainerPort == *eport.Port {
return true return true
} }
} }
@ -479,16 +468,16 @@ func (e *EndpointSlice) resolvePodRef(ref *apiv1.ObjectReference) *apiv1.Pod {
return obj.(*apiv1.Pod) return obj.(*apiv1.Pod)
} }
func (e *EndpointSlice) addServiceLabels(esa endpointSliceAdaptor, tg *targetgroup.Group) { func (e *EndpointSlice) addServiceLabels(esa v1.EndpointSlice, tg *targetgroup.Group) {
var ( var (
found bool found bool
name string name string
) )
ns := esa.namespace() ns := esa.Namespace
// Every EndpointSlice object has the Service they belong to in the // Every EndpointSlice object has the Service they belong to in the
// kubernetes.io/service-name label. // kubernetes.io/service-name label.
name, found = esa.labels()[esa.labelServiceName()] name, found = esa.Labels[v1.LabelServiceName]
if !found { if !found {
return return
} }

View File

@ -1,190 +0,0 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
corev1 "k8s.io/api/core/v1"
v1 "k8s.io/api/discovery/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// endpointSliceAdaptor is an adaptor for the different EndpointSlice versions.
type endpointSliceAdaptor interface {
get() interface{}
getObjectMeta() metav1.ObjectMeta
name() string
namespace() string
addressType() string
endpoints() []endpointSliceEndpointAdaptor
ports() []endpointSlicePortAdaptor
labels() map[string]string
labelServiceName() string
}
type endpointSlicePortAdaptor interface {
name() *string
port() *int32
protocol() *string
appProtocol() *string
}
type endpointSliceEndpointAdaptor interface {
addresses() []string
hostname() *string
nodename() *string
zone() *string
conditions() endpointSliceEndpointConditionsAdaptor
targetRef() *corev1.ObjectReference
topology() map[string]string
}
type endpointSliceEndpointConditionsAdaptor interface {
ready() *bool
serving() *bool
terminating() *bool
}
// Adaptor for k8s.io/api/discovery/v1.
type endpointSliceAdaptorV1 struct {
endpointSlice *v1.EndpointSlice
}
func newEndpointSliceAdaptorFromV1(endpointSlice *v1.EndpointSlice) endpointSliceAdaptor {
return &endpointSliceAdaptorV1{endpointSlice: endpointSlice}
}
func (e *endpointSliceAdaptorV1) get() interface{} {
return e.endpointSlice
}
func (e *endpointSliceAdaptorV1) getObjectMeta() metav1.ObjectMeta {
return e.endpointSlice.ObjectMeta
}
func (e *endpointSliceAdaptorV1) name() string {
return e.endpointSlice.ObjectMeta.Name
}
func (e *endpointSliceAdaptorV1) namespace() string {
return e.endpointSlice.ObjectMeta.Namespace
}
func (e *endpointSliceAdaptorV1) addressType() string {
return string(e.endpointSlice.AddressType)
}
func (e *endpointSliceAdaptorV1) endpoints() []endpointSliceEndpointAdaptor {
eps := make([]endpointSliceEndpointAdaptor, 0, len(e.endpointSlice.Endpoints))
for i := 0; i < len(e.endpointSlice.Endpoints); i++ {
eps = append(eps, newEndpointSliceEndpointAdaptorFromV1(e.endpointSlice.Endpoints[i]))
}
return eps
}
func (e *endpointSliceAdaptorV1) ports() []endpointSlicePortAdaptor {
ports := make([]endpointSlicePortAdaptor, 0, len(e.endpointSlice.Ports))
for i := 0; i < len(e.endpointSlice.Ports); i++ {
ports = append(ports, newEndpointSlicePortAdaptorFromV1(e.endpointSlice.Ports[i]))
}
return ports
}
func (e *endpointSliceAdaptorV1) labels() map[string]string {
return e.endpointSlice.Labels
}
func (e *endpointSliceAdaptorV1) labelServiceName() string {
return v1.LabelServiceName
}
type endpointSliceEndpointAdaptorV1 struct {
endpoint v1.Endpoint
}
func newEndpointSliceEndpointAdaptorFromV1(endpoint v1.Endpoint) endpointSliceEndpointAdaptor {
return &endpointSliceEndpointAdaptorV1{endpoint: endpoint}
}
func (e *endpointSliceEndpointAdaptorV1) addresses() []string {
return e.endpoint.Addresses
}
func (e *endpointSliceEndpointAdaptorV1) hostname() *string {
return e.endpoint.Hostname
}
func (e *endpointSliceEndpointAdaptorV1) nodename() *string {
return e.endpoint.NodeName
}
func (e *endpointSliceEndpointAdaptorV1) zone() *string {
return e.endpoint.Zone
}
func (e *endpointSliceEndpointAdaptorV1) conditions() endpointSliceEndpointConditionsAdaptor {
return newEndpointSliceEndpointConditionsAdaptorFromV1(e.endpoint.Conditions)
}
func (e *endpointSliceEndpointAdaptorV1) targetRef() *corev1.ObjectReference {
return e.endpoint.TargetRef
}
func (e *endpointSliceEndpointAdaptorV1) topology() map[string]string {
return e.endpoint.DeprecatedTopology
}
type endpointSliceEndpointConditionsAdaptorV1 struct {
endpointConditions v1.EndpointConditions
}
func newEndpointSliceEndpointConditionsAdaptorFromV1(endpointConditions v1.EndpointConditions) endpointSliceEndpointConditionsAdaptor {
return &endpointSliceEndpointConditionsAdaptorV1{endpointConditions: endpointConditions}
}
func (e *endpointSliceEndpointConditionsAdaptorV1) ready() *bool {
return e.endpointConditions.Ready
}
func (e *endpointSliceEndpointConditionsAdaptorV1) serving() *bool {
return e.endpointConditions.Serving
}
func (e *endpointSliceEndpointConditionsAdaptorV1) terminating() *bool {
return e.endpointConditions.Terminating
}
type endpointSlicePortAdaptorV1 struct {
endpointPort v1.EndpointPort
}
func newEndpointSlicePortAdaptorFromV1(port v1.EndpointPort) endpointSlicePortAdaptor {
return &endpointSlicePortAdaptorV1{endpointPort: port}
}
func (e *endpointSlicePortAdaptorV1) name() *string {
return e.endpointPort.Name
}
func (e *endpointSlicePortAdaptorV1) port() *int32 {
return e.endpointPort.Port
}
func (e *endpointSlicePortAdaptorV1) protocol() *string {
val := string(*e.endpointPort.Protocol)
return &val
}
func (e *endpointSlicePortAdaptorV1) appProtocol() *string {
return e.endpointPort.AppProtocol
}

View File

@ -1,50 +0,0 @@
// Copyright 2020 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
"testing"
"github.com/stretchr/testify/require"
v1 "k8s.io/api/discovery/v1"
)
func Test_EndpointSliceAdaptor_v1(t *testing.T) {
t.Parallel()
endpointSlice := makeEndpointSliceV1()
adaptor := newEndpointSliceAdaptorFromV1(endpointSlice)
require.Equal(t, endpointSlice.ObjectMeta.Name, adaptor.name())
require.Equal(t, endpointSlice.ObjectMeta.Namespace, adaptor.namespace())
require.Equal(t, endpointSlice.AddressType, v1.AddressType(adaptor.addressType()))
require.Equal(t, endpointSlice.Labels, adaptor.labels())
require.Equal(t, "testendpoints", endpointSlice.Labels[v1.LabelServiceName])
for i, endpointAdaptor := range adaptor.endpoints() {
require.Equal(t, endpointSlice.Endpoints[i].Addresses, endpointAdaptor.addresses())
require.Equal(t, endpointSlice.Endpoints[i].Hostname, endpointAdaptor.hostname())
require.Equal(t, endpointSlice.Endpoints[i].Conditions.Ready, endpointAdaptor.conditions().ready())
require.Equal(t, endpointSlice.Endpoints[i].Conditions.Serving, endpointAdaptor.conditions().serving())
require.Equal(t, endpointSlice.Endpoints[i].Conditions.Terminating, endpointAdaptor.conditions().terminating())
require.Equal(t, endpointSlice.Endpoints[i].TargetRef, endpointAdaptor.targetRef())
require.Equal(t, endpointSlice.Endpoints[i].DeprecatedTopology, endpointAdaptor.topology())
}
for i, portAdaptor := range adaptor.ports() {
require.Equal(t, endpointSlice.Ports[i].Name, portAdaptor.name())
require.Equal(t, endpointSlice.Ports[i].Port, portAdaptor.port())
require.EqualValues(t, endpointSlice.Ports[i].Protocol, portAdaptor.protocol())
require.Equal(t, endpointSlice.Ports[i].AppProtocol, portAdaptor.appProtocol())
}
}

View File

@ -121,21 +121,18 @@ func (i *Ingress) process(ctx context.Context, ch chan<- []*targetgroup.Group) b
return true return true
} }
var ia ingressAdaptor if ingress, ok := o.(*v1.Ingress); ok {
switch ingress := o.(type) { send(ctx, ch, i.buildIngress(*ingress))
case *v1.Ingress: } else {
ia = newIngressAdaptorFromV1(ingress)
default:
i.logger.Error("converting to Ingress object failed", "err", i.logger.Error("converting to Ingress object failed", "err",
fmt.Errorf("received unexpected object: %v", o)) fmt.Errorf("received unexpected object: %v", o))
return true return true
} }
send(ctx, ch, i.buildIngress(ia))
return true return true
} }
func ingressSource(s ingressAdaptor) string { func ingressSource(s v1.Ingress) string {
return ingressSourceFromNamespaceAndName(s.namespace(), s.name()) return ingressSourceFromNamespaceAndName(s.Namespace, s.Name)
} }
func ingressSourceFromNamespaceAndName(namespace, name string) string { func ingressSourceFromNamespaceAndName(namespace, name string) string {
@ -149,15 +146,15 @@ const (
ingressClassNameLabel = metaLabelPrefix + "ingress_class_name" ingressClassNameLabel = metaLabelPrefix + "ingress_class_name"
) )
func ingressLabels(ingress ingressAdaptor) model.LabelSet { func ingressLabels(ingress v1.Ingress) model.LabelSet {
// Each label and annotation will create two key-value pairs in the map. // Each label and annotation will create two key-value pairs in the map.
ls := make(model.LabelSet) ls := make(model.LabelSet)
ls[namespaceLabel] = lv(ingress.namespace()) ls[namespaceLabel] = lv(ingress.Namespace)
if cls := ingress.ingressClassName(); cls != nil { if cls := ingress.Spec.IngressClassName; cls != nil {
ls[ingressClassNameLabel] = lv(*cls) ls[ingressClassNameLabel] = lv(*cls)
} }
addObjectMetaLabels(ls, ingress.getObjectMeta(), RoleIngress) addObjectMetaLabels(ls, ingress.ObjectMeta, RoleIngress)
return ls return ls
} }
@ -177,19 +174,39 @@ func pathsFromIngressPaths(ingressPaths []string) []string {
return paths return paths
} }
func (i *Ingress) buildIngress(ingress ingressAdaptor) *targetgroup.Group { func rulePaths(rule v1.IngressRule) []string {
rv := rule.IngressRuleValue
if rv.HTTP == nil {
return nil
}
paths := make([]string, len(rv.HTTP.Paths))
for n, p := range rv.HTTP.Paths {
paths[n] = p.Path
}
return paths
}
func tlsHosts(ingressTLS []v1.IngressTLS) []string {
var hosts []string
for _, tls := range ingressTLS {
hosts = append(hosts, tls.Hosts...)
}
return hosts
}
func (i *Ingress) buildIngress(ingress v1.Ingress) *targetgroup.Group {
tg := &targetgroup.Group{ tg := &targetgroup.Group{
Source: ingressSource(ingress), Source: ingressSource(ingress),
} }
tg.Labels = ingressLabels(ingress) tg.Labels = ingressLabels(ingress)
for _, rule := range ingress.rules() { for _, rule := range ingress.Spec.Rules {
scheme := "http" scheme := "http"
paths := pathsFromIngressPaths(rule.paths()) paths := pathsFromIngressPaths(rulePaths(rule))
out: out:
for _, pattern := range ingress.tlsHosts() { for _, pattern := range tlsHosts(ingress.Spec.TLS) {
if matchesHostnamePattern(pattern, rule.host()) { if matchesHostnamePattern(pattern, rule.Host) {
scheme = "https" scheme = "https"
break out break out
} }
@ -197,9 +214,9 @@ func (i *Ingress) buildIngress(ingress ingressAdaptor) *targetgroup.Group {
for _, path := range paths { for _, path := range paths {
tg.Targets = append(tg.Targets, model.LabelSet{ tg.Targets = append(tg.Targets, model.LabelSet{
model.AddressLabel: lv(rule.host()), model.AddressLabel: lv(rule.Host),
ingressSchemeLabel: lv(scheme), ingressSchemeLabel: lv(scheme),
ingressHostLabel: lv(rule.host()), ingressHostLabel: lv(rule.Host),
ingressPathLabel: lv(path), ingressPathLabel: lv(path),
}) })
} }

View File

@ -1,90 +0,0 @@
// Copyright 2016 The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubernetes
import (
v1 "k8s.io/api/networking/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
// ingressAdaptor is an adaptor for the different Ingress versions.
type ingressAdaptor interface {
getObjectMeta() metav1.ObjectMeta
name() string
namespace() string
labels() map[string]string
annotations() map[string]string
tlsHosts() []string
ingressClassName() *string
rules() []ingressRuleAdaptor
}
type ingressRuleAdaptor interface {
paths() []string
host() string
}
// Adaptor for networking.k8s.io/v1.
type ingressAdaptorV1 struct {
ingress *v1.Ingress
}
func newIngressAdaptorFromV1(ingress *v1.Ingress) ingressAdaptor {
return &ingressAdaptorV1{ingress: ingress}
}
func (i *ingressAdaptorV1) getObjectMeta() metav1.ObjectMeta { return i.ingress.ObjectMeta }
func (i *ingressAdaptorV1) name() string { return i.ingress.Name }
func (i *ingressAdaptorV1) namespace() string { return i.ingress.Namespace }
func (i *ingressAdaptorV1) labels() map[string]string { return i.ingress.Labels }
func (i *ingressAdaptorV1) annotations() map[string]string { return i.ingress.Annotations }
func (i *ingressAdaptorV1) ingressClassName() *string { return i.ingress.Spec.IngressClassName }
func (i *ingressAdaptorV1) tlsHosts() []string {
var hosts []string
for _, tls := range i.ingress.Spec.TLS {
hosts = append(hosts, tls.Hosts...)
}
return hosts
}
func (i *ingressAdaptorV1) rules() []ingressRuleAdaptor {
var rules []ingressRuleAdaptor
for _, rule := range i.ingress.Spec.Rules {
rules = append(rules, newIngressRuleAdaptorFromV1(rule))
}
return rules
}
type ingressRuleAdaptorV1 struct {
rule v1.IngressRule
}
func newIngressRuleAdaptorFromV1(rule v1.IngressRule) ingressRuleAdaptor {
return &ingressRuleAdaptorV1{rule: rule}
}
func (i *ingressRuleAdaptorV1) paths() []string {
rv := i.rule.IngressRuleValue
if rv.HTTP == nil {
return nil
}
paths := make([]string, len(rv.HTTP.Paths))
for n, p := range rv.HTTP.Paths {
paths[n] = p.Path
}
return paths
}
func (i *ingressRuleAdaptorV1) host() string { return i.rule.Host }