Reputation: 41
Sorry I'm quite a noob at Go so hope this isn't a stupid question. I know pointers in a general sense but struggling with Go semantics.
I can't get this to work:
func DeleteOldCronJob(c client.Client, ctx context.Context, namespace string, name string) error {
cronJob := batchv1beta1.CronJob{}
key := client.ObjectKey{
Namespace: namespace,
Name: name,
}
return DeleteOldAny(c, ctx, name, key, &cronJob)
}
func DeleteOldAny(c client.Client, ctx context.Context, name string, key client.ObjectKey, resource interface{}) error {
err := c.Get(ctx, key, resource)
if err == nil {
err := c.Delete(ctx, resource)
if err != nil {
return err
}
} else {
return err
}
return nil
}
I get an error:
interface {} does not implement "k8s.io/apimachinery/pkg/runtime".Object (missing DeepCopyObject method)
The point is so that I can reuse DeleteOldAny on multiple different types, to make my codebase more compact (I could just copy+paste DeleteOldCronjob and change the type). As far as I read, pointers to interfaces in Go are usually wrong. Also, the k8s type I'm importing is just a struct. So, I thought since it's a struct not an interface I should pass resource a a pointer like:
err := c.Get(ctx, key, &resource)
But that gives me another error:
*interface {} is pointer to interface, not interface
So I'm a bit stuck. Am I doomed to copy+paste the same function for each type or is it a simple syntax mistake I'm making?
Upvotes: 1
Views: 1047
Reputation: 119
func (client.Reader).Get(ctx context.Context, key types.NamespacedName, obj client.Object) error
Get retrieves an obj for the given object key from the Kubernetes Cluster. obj must be a struct pointer so that obj can be updated with the response returned by the Server.
so the idea is right to have modular approach and stop reusing the code .
But implementation is wrong , the obj would be the resource you are trying to fetch from cluster which should be passed in the function as a struct pointer.
err := c.Get(ctx, key, &resource)
here resource should be a struct as Get , delete etc. expects as pointer to the respective object to be passed .
Upvotes: 1
Reputation: 41
Pretty much the answer is to use the unstructured package.
I don't think you can do client.Get()
without a concrete type, which is ridiculous. Go really is a stiff and verbose language, isn't it?
Upvotes: 0