istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/schema/codegen/templates/clients.go.tmpl (about)

     1  // Code generated by pkg/config/schema/codegen/tools/collections.main.go. DO NOT EDIT.
     2  
     3  package {{.PackageName}}
     4  
     5  import (
     6  	"context"
     7  	"fmt"
     8  
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/runtime"
    11  	"k8s.io/apimachinery/pkg/runtime/schema"
    12  	"k8s.io/apimachinery/pkg/watch"
    13  	"k8s.io/client-go/tools/cache"
    14  
    15  	"istio.io/istio/pkg/config/schema/gvr"
    16  	"istio.io/istio/pkg/kube/informerfactory"
    17  	ktypes "istio.io/istio/pkg/kube/kubetypes"
    18  	"istio.io/istio/pkg/ptr"
    19  
    20  	apiistioioapiextensionsv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1"
    21  	apiistioioapinetworkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
    22  	apiistioioapinetworkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
    23  	apiistioioapisecurityv1beta1 "istio.io/client-go/pkg/apis/security/v1beta1"
    24  	apiistioioapitelemetryv1alpha1 "istio.io/client-go/pkg/apis/telemetry/v1alpha1"
    25  {{- range .Packages}}
    26  	{{.ImportName}} "{{.PackageName}}"
    27  {{- end}}
    28  )
    29  
    30  func GetWriteClient[T runtime.Object](c ClientGetter, namespace string) ktypes.WriteAPI[T] {
    31  	switch any(ptr.Empty[T]()).(type) {
    32  {{- range .Entries }}
    33  	{{- if not .Resource.Synthetic }}
    34  	case *{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}:
    35  		return  c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}namespace{{end}}).(ktypes.WriteAPI[T])
    36  	{{- end }}
    37  {{- end }}
    38    default:
    39      panic(fmt.Sprintf("Unknown type %T", ptr.Empty[T]()))
    40  	}
    41  }
    42  
    43  func GetClient[T, TL runtime.Object](c ClientGetter, namespace string) ktypes.ReadWriteAPI[T, TL] {
    44  	switch any(ptr.Empty[T]()).(type) {
    45  {{- range .Entries }}
    46  	{{- if not .Resource.Synthetic }}
    47  	case *{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}:
    48  		return  c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}namespace{{end}}).(ktypes.ReadWriteAPI[T, TL])
    49  	{{- end }}
    50  {{- end }}
    51    default:
    52      panic(fmt.Sprintf("Unknown type %T", ptr.Empty[T]()))
    53  	}
    54  }
    55  
    56  func gvrToObject(g schema.GroupVersionResource) runtime.Object {
    57  	switch g {
    58  {{- range .Entries }}
    59  	{{- if not .Resource.Synthetic }}
    60  	case gvr.{{ .Resource.Identifier }}:
    61  		return &{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}{}
    62  	{{- end }}
    63  {{- end }}
    64    default:
    65      panic(fmt.Sprintf("Unknown type %v", g))
    66  	}
    67  }
    68  
    69  func getInformerFiltered(c ClientGetter, opts ktypes.InformerOptions, g schema.GroupVersionResource) informerfactory.StartableInformer {
    70  	var l func(options metav1.ListOptions) (runtime.Object, error)
    71  	var w func(options metav1.ListOptions) (watch.Interface, error)
    72  
    73  	switch g {
    74  {{- range .Entries }}
    75  	{{- if not .Resource.Synthetic }}
    76  	case gvr.{{ .Resource.Identifier }}:
    77  		l = func(options metav1.ListOptions) (runtime.Object, error) {
    78  			return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}opts.Namespace{{end}}).List(context.Background(), options)
    79  		}
    80  		w = func(options metav1.ListOptions) (watch.Interface, error) {
    81  			return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}opts.Namespace{{end}}).Watch(context.Background(), options)
    82  		}
    83  	{{- end }}
    84  {{- end }}
    85    default:
    86      panic(fmt.Sprintf("Unknown type %v", g))
    87  	}
    88  	return c.Informers().InformerFor(g, opts, func() cache.SharedIndexInformer {
    89  		inf := cache.NewSharedIndexInformer(
    90  			&cache.ListWatch{
    91  				ListFunc: func(options metav1.ListOptions) (runtime.Object, error) {
    92  					options.FieldSelector = opts.FieldSelector
    93  					options.LabelSelector = opts.LabelSelector
    94  					return l(options)
    95  				},
    96  				WatchFunc: func(options metav1.ListOptions) (watch.Interface, error) {
    97  					options.FieldSelector = opts.FieldSelector
    98  					options.LabelSelector = opts.LabelSelector
    99  					return w(options)
   100  				},
   101  			},
   102  			gvrToObject(g),
   103  			0,
   104  			cache.Indexers{cache.NamespaceIndex: cache.MetaNamespaceIndexFunc},
   105  		)
   106  		setupInformer(opts, inf)
   107  		return inf
   108  	})
   109  }