sigs.k8s.io/external-dns@v0.14.1/source/gateway_tcproute.go (about)

     1  /*
     2  Copyright 2021 The Kubernetes 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  
    17  package source
    18  
    19  import (
    20  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    21  	"k8s.io/apimachinery/pkg/labels"
    22  	v1 "sigs.k8s.io/gateway-api/apis/v1"
    23  	"sigs.k8s.io/gateway-api/apis/v1alpha2"
    24  	informers "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions"
    25  	informers_v1a2 "sigs.k8s.io/gateway-api/pkg/client/informers/externalversions/apis/v1alpha2"
    26  )
    27  
    28  // NewGatewayTCPRouteSource creates a new Gateway TCPRoute source with the given config.
    29  func NewGatewayTCPRouteSource(clients ClientGenerator, config *Config) (Source, error) {
    30  	return newGatewayRouteSource(clients, config, "TCPRoute", func(factory informers.SharedInformerFactory) gatewayRouteInformer {
    31  		return &gatewayTCPRouteInformer{factory.Gateway().V1alpha2().TCPRoutes()}
    32  	})
    33  }
    34  
    35  type gatewayTCPRoute struct{ route v1alpha2.TCPRoute } // NOTE: Must update TypeMeta in List when changing the APIVersion.
    36  
    37  func (rt *gatewayTCPRoute) Object() kubeObject           { return &rt.route }
    38  func (rt *gatewayTCPRoute) Metadata() *metav1.ObjectMeta { return &rt.route.ObjectMeta }
    39  func (rt *gatewayTCPRoute) Hostnames() []v1.Hostname     { return nil }
    40  func (rt *gatewayTCPRoute) Protocol() v1.ProtocolType    { return v1.TCPProtocolType }
    41  func (rt *gatewayTCPRoute) RouteStatus() v1.RouteStatus  { return rt.route.Status.RouteStatus }
    42  
    43  type gatewayTCPRouteInformer struct {
    44  	informers_v1a2.TCPRouteInformer
    45  }
    46  
    47  func (inf gatewayTCPRouteInformer) List(namespace string, selector labels.Selector) ([]gatewayRoute, error) {
    48  	list, err := inf.TCPRouteInformer.Lister().TCPRoutes(namespace).List(selector)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	routes := make([]gatewayRoute, len(list))
    53  	for i, rt := range list {
    54  		// List results are supposed to be treated as read-only.
    55  		// We make a shallow copy since we're only interested in setting the TypeMeta.
    56  		clone := *rt
    57  		clone.TypeMeta = metav1.TypeMeta{
    58  			APIVersion: v1alpha2.GroupVersion.String(),
    59  			Kind:       "TCPRoute",
    60  		}
    61  		routes[i] = &gatewayTCPRoute{clone}
    62  	}
    63  	return routes, nil
    64  }