kubernetes / client-go

Go client for Kubernetes.
Apache License 2.0
8.79k stars 2.91k forks source link

How to perform pre-verification when using dynamic client to create resources? #1226

Closed mark8s closed 1 year ago

mark8s commented 1 year ago

Hello everyone, I have a question.

When I was using client-go dynamic client, I found a phenomenon, the following code:

_, err = r.KubeDynamicClient.Resource(mapping.Resource).Namespace(namespace).Create(ctx, &unstruct, metav1.CreateOptions{})

I use the above code to create a crd resource. This crd resource is actually problematic. It has a wrong owner references in it, pointing to a cross-cluster crd resource (ShortCutRun). Its resource content is:

apiVersion: networking.istio.io/v1beta1
kind: DestinationRule
metadata:
   name: reviews
   namespace: bookinfo
   ownerReferences:
   - apiVersion: xxx
     kind: ShortCutRun
     name: canary-shortcutrun-sample
spec:
   host: reviews
   subsets:
   - labels:
       version: v1
     name: v1
   - labels:
       version: v2
     name: v2
   trafficPolicy:
     loadBalancer:
       simple: RANDOM

Therefore, when I use DynamicClient to create this resource, after execution, this resource (DestinationRule) does not appear in the cluster. The final reason is the wrong ownerreferences configuration. However, DynamicClient does not return any error, it returns a nil. It seems that the create or patch interface of DynamicClient has not been verified for resources?

I just want to ask now, is there any way or solution to solve this problem?

Does anyone know? Hope to get help.

k8s-triage-robot commented 1 year ago

The Kubernetes project currently lacks enough contributors to adequately respond to all issues.

This bot triages un-triaged issues according to the following rules:

You can:

Please send feedback to sig-contributor-experience at kubernetes/community.

/lifecycle stale