istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pkg/config/schema/codegen/templates/crdclient.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  	"fmt"
     7  	"context"
     8  
     9  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    10  	"k8s.io/apimachinery/pkg/runtime"
    11  	"k8s.io/apimachinery/pkg/types"
    12  
    13  	"istio.io/istio/pkg/config"
    14  	"istio.io/istio/pkg/config/schema/gvk"
    15  	"istio.io/istio/pkg/kube"
    16  
    17  	apiistioioapiextensionsv1alpha1 "istio.io/client-go/pkg/apis/extensions/v1alpha1"
    18  	apiistioioapinetworkingv1alpha3 "istio.io/client-go/pkg/apis/networking/v1alpha3"
    19  	apiistioioapinetworkingv1beta1 "istio.io/client-go/pkg/apis/networking/v1beta1"
    20  	apiistioioapisecurityv1beta1 "istio.io/client-go/pkg/apis/security/v1beta1"
    21  	apiistioioapitelemetryv1alpha1 "istio.io/client-go/pkg/apis/telemetry/v1alpha1"
    22  {{- range .Packages}}
    23  	{{.ImportName}} "{{.PackageName}}"
    24  {{- end}}
    25  )
    26  
    27  func create(c kube.Client, cfg config.Config, objMeta metav1.ObjectMeta) (metav1.Object, error) {
    28  	switch cfg.GroupVersionKind {
    29  {{- range .Entries }}
    30  	{{- if and (not .Resource.Synthetic) (not .Resource.Builtin) }}
    31  	case gvk.{{.Resource.Identifier}}:
    32  		return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}cfg.Namespace{{end}}).Create(context.TODO(), &{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}{
    33  			ObjectMeta: objMeta,
    34  			Spec:       *(cfg.Spec.(*{{ .ClientImport }}.{{.SpecType}})),
    35  		}, metav1.CreateOptions{})
    36  	{{- end }}
    37  {{- end }}
    38  	default:
    39  		return nil, fmt.Errorf("unsupported type: %v", cfg.GroupVersionKind)
    40  	}
    41  }
    42  
    43  func update(c kube.Client, cfg config.Config, objMeta metav1.ObjectMeta) (metav1.Object, error) {
    44  	switch cfg.GroupVersionKind {
    45  {{- range .Entries }}
    46  	{{- if and (not .Resource.Synthetic) (not .Resource.Builtin) }}
    47  	case gvk.{{.Resource.Identifier}}:
    48  		return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}cfg.Namespace{{end}}).Update(context.TODO(), &{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}{
    49  			ObjectMeta: objMeta,
    50  			Spec:       *(cfg.Spec.(*{{ .ClientImport }}.{{.SpecType}})),
    51  		}, metav1.UpdateOptions{})
    52  	{{- end }}
    53  {{- end }}
    54  	default:
    55  		return nil, fmt.Errorf("unsupported type: %v", cfg.GroupVersionKind)
    56  	}
    57  }
    58  
    59  func updateStatus(c kube.Client, cfg config.Config, objMeta metav1.ObjectMeta) (metav1.Object, error) {
    60  	switch cfg.GroupVersionKind {
    61  {{- range .Entries }}
    62  	{{- if and (not .Resource.Synthetic) (not .Resource.Builtin) (not (eq .StatusType "")) }}
    63  	case gvk.{{.Resource.Identifier}}:
    64  		return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}cfg.Namespace{{end}}).UpdateStatus(context.TODO(), &{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}{
    65  			ObjectMeta: objMeta,
    66  			Status:       *(cfg.Status.(*{{ .StatusImport }}.{{.StatusType}})),
    67  		}, metav1.UpdateOptions{})
    68  	{{- end }}
    69  {{- end }}
    70  	default:
    71  		return nil, fmt.Errorf("unsupported type: %v", cfg.GroupVersionKind)
    72  	}
    73  }
    74  
    75  func patch(c kube.Client, orig config.Config, origMeta metav1.ObjectMeta, mod config.Config, modMeta metav1.ObjectMeta, typ types.PatchType) (metav1.Object, error) {
    76  	if orig.GroupVersionKind != mod.GroupVersionKind {
    77  		return nil, fmt.Errorf("gvk mismatch: %v, modified: %v", orig.GroupVersionKind, mod.GroupVersionKind)
    78  	}
    79  	switch orig.GroupVersionKind {
    80  {{- range .Entries }}
    81  	{{- if and (not .Resource.Synthetic) (not .Resource.Builtin) }}
    82  	case gvk.{{.Resource.Identifier}}:
    83  		oldRes := &{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}{
    84  				ObjectMeta: origMeta,
    85  				Spec:       *(orig.Spec.(*{{ .ClientImport }}.{{.SpecType}})),
    86  		}
    87  		modRes := &{{ .IstioAwareClientImport }}.{{ .Resource.Kind }}{
    88  			ObjectMeta: modMeta,
    89  			Spec:       *(mod.Spec.(*{{ .ClientImport }}.{{.SpecType}})),
    90  		}
    91  		patchBytes, err := genPatchBytes(oldRes, modRes, typ)
    92  		if err != nil {
    93  				return nil, err
    94  		}
    95  		return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}orig.Namespace{{end}}).
    96  				Patch(context.TODO(), orig.Name, typ, patchBytes, metav1.PatchOptions{FieldManager: "pilot-discovery"})
    97  	{{- end }}
    98  {{- end }}
    99  	default:
   100  		return nil, fmt.Errorf("unsupported type: %v", orig.GroupVersionKind)
   101  	}
   102  }
   103  
   104  
   105  func delete(c kube.Client, typ config.GroupVersionKind, name, namespace string, resourceVersion *string) error {
   106  	var deleteOptions metav1.DeleteOptions
   107  	if resourceVersion != nil {
   108  		deleteOptions.Preconditions = &metav1.Preconditions{ResourceVersion: resourceVersion}
   109  	}
   110  	switch typ {
   111  {{- range .Entries }}
   112  	{{- if and (not .Resource.Synthetic) (not .Resource.Builtin) }}
   113  	case gvk.{{.Resource.Identifier}}:
   114  		return c.{{.ClientGetter}}().{{ .ClientGroupPath }}().{{ .ClientTypePath }}({{if not .Resource.ClusterScoped}}namespace{{end}}).Delete(context.TODO(), name, deleteOptions)
   115  	{{- end }}
   116  {{- end }}
   117  	default:
   118  		return fmt.Errorf("unsupported type: %v", typ)
   119  	}
   120  }
   121  
   122  
   123  var translationMap = map[config.GroupVersionKind]func(r runtime.Object) config.Config{
   124  {{- range .Entries }}
   125  	{{- if and (not .Resource.Synthetic) }}
   126  	gvk.{{.Resource.Identifier}}: func(r runtime.Object) config.Config {
   127  		obj := r.(*{{ .IstioAwareClientImport }}.{{.Resource.Kind}})
   128  		return config.Config{
   129  		  Meta: config.Meta{
   130  		  	GroupVersionKind:  gvk.{{.Resource.Identifier}},
   131  		  	Name:              obj.Name,
   132  		  	Namespace:         obj.Namespace,
   133  		  	Labels:            obj.Labels,
   134  		  	Annotations:       obj.Annotations,
   135  		  	ResourceVersion:   obj.ResourceVersion,
   136  		  	CreationTimestamp: obj.CreationTimestamp.Time,
   137  		  	OwnerReferences:   obj.OwnerReferences,
   138  		  	UID:               string(obj.UID),
   139  		  	Generation:        obj.Generation,
   140  		  },
   141  			Spec:   {{ if not .Resource.Specless }}&obj.Spec{{ else }}obj{{ end }},
   142        {{- if not (eq .StatusType "") }}
   143  		  Status: &obj.Status,
   144        {{- end }}
   145  		}
   146  	},
   147  	{{- end }}
   148  {{- end }}
   149  }