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

     1  /*
     2  Copyright 2022 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  // NewGatewayGRPCRouteSource creates a new Gateway GRPCRoute source with the given config.
    29  func NewGatewayGRPCRouteSource(clients ClientGenerator, config *Config) (Source, error) {
    30  	return newGatewayRouteSource(clients, config, "GRPCRoute", func(factory informers.SharedInformerFactory) gatewayRouteInformer {
    31  		return &gatewayGRPCRouteInformer{factory.Gateway().V1alpha2().GRPCRoutes()}
    32  	})
    33  }
    34  
    35  type gatewayGRPCRoute struct{ route v1alpha2.GRPCRoute } // NOTE: Must update TypeMeta in List when changing the APIVersion.
    36  
    37  func (rt *gatewayGRPCRoute) Object() kubeObject           { return &rt.route }
    38  func (rt *gatewayGRPCRoute) Metadata() *metav1.ObjectMeta { return &rt.route.ObjectMeta }
    39  func (rt *gatewayGRPCRoute) Hostnames() []v1.Hostname     { return rt.route.Spec.Hostnames }
    40  func (rt *gatewayGRPCRoute) Protocol() v1.ProtocolType    { return v1.HTTPSProtocolType }
    41  func (rt *gatewayGRPCRoute) RouteStatus() v1.RouteStatus  { return rt.route.Status.RouteStatus }
    42  
    43  type gatewayGRPCRouteInformer struct {
    44  	informers_v1a2.GRPCRouteInformer
    45  }
    46  
    47  func (inf gatewayGRPCRouteInformer) List(namespace string, selector labels.Selector) ([]gatewayRoute, error) {
    48  	list, err := inf.GRPCRouteInformer.Lister().GRPCRoutes(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:       "GRPCRoute",
    60  		}
    61  		routes[i] = &gatewayGRPCRoute{clone}
    62  	}
    63  	return routes, nil
    64  }