1

I am using Velero to create and backup and restore, Velero has controllers which get triggered when I can create the custom objects.

import veleroApi "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"

restoreObj := veleroApi.Restore{
    TypeMeta:   metav1.TypeMeta{},
    ObjectMeta: metav1.ObjectMeta{
        DeletionGracePeriodSeconds: &gracePeriodSeconds,
    },
    Spec:       veleroApi.RestoreSpec{
        BackupName:              "backup-name-20211101",
        RestorePVs:              &restorePV,
    },
    Status:     veleroApi.RestoreStatus{},
}

But how can I submit this custom object to the Kube API server?

I used API client to apply the changes:

apiClient.CoreV1().RESTClient().Patch(types.ApplyPatchType).Body(restoreObj).Do(context)

But I am getting:

unknown type used for body: {TypeMeta:{Kind:Restore APIVersion:velero.io/v1} ObjectMeta:{Name: GenerateName: Namespace:velero SelfLink: UID: ResourceVersion: Generation:0 CreationTimestamp:0001-01-01 00:00:00 +0000 UTC DeletionTimestamp:<nil> DeletionGracePeriodSeconds:0xc000256018 Labels:map[] Annotations:map[] OwnerReferences:[] Finalizers:[] ClusterName: ManagedFields:[]} Spec:{BackupName:backup-name-20211101 ScheduleName: IncludedNamespaces:[] ExcludedNamespaces:[] IncludedResources:[] ExcludedResources:[] NamespaceMapping:map[] LabelSelector:nil RestorePVs:0xc0007a9088 PreserveNodePorts:<nil> IncludeClusterResources:<nil> Hooks:{Resources:[]}} Status:{Phase: ValidationErrors:[] Warnings:0 Errors:0 FailureReason: StartTimestamp:<nil> CompletionTimestamp:<nil> Progress:<nil>}}
Vishrant
  • 15,456
  • 11
  • 71
  • 120
  • Same way you create any object, with a manifest matching the resource definition specification. The CRD for the custom objects needs to exist in the cluster. https://velero.io/docs/v1.7/api-types/restore/ – jordanm Nov 18 '21 at 16:19
  • Thanks, @jordanm, I could find API for creating objects like POD, PV, etc but I am not finding API to create/ submit the custom objects in `client-go` library. – Vishrant Nov 18 '21 at 16:24
  • The API is the kubernetes API. You `kubectl apply` a manifest matching the CRD. – jordanm Nov 18 '21 at 16:24
  • But I am not using `kubectl apply` but golang apis to interact with kube-api server – Vishrant Nov 18 '21 at 16:25
  • This can be treated as duplicate of https://stackoverflow.com/questions/63408493/create-get-a-custom-kubernetes-resource – Vishrant Nov 18 '21 at 23:02

1 Answers1

2

If you would like to create a client for custom object follow the following steps:

  1. Describe the custom resource for which you would like to create a rest client:
kubectl describe CustomResourceDefinition <custom resource definition name>

Note down the API and version and the Kind, as an example it would look like:

API Version:  apiextensions.k8s.io/v1
Kind:         CustomResourceDefinition

Here, apiextensions.k8s.io is API and v1 is the version.

  1. Check if the API version that you got from step 1 is in the list of APIs:
kubectl get --raw "/"
  1. Create the client:
func getClusterConfig() *rest.Config {
    config, err := rest.InClusterConfig()
    if err != nil {
        glog.Fatal(err.Error())
    }
    return config
}

func getRestClient() *rest.RESTClient {

    cfg := getClusterConfig()

    gv := schema.GroupVersion{Group: "<API>", Version: "<version>"}
    cfg.GroupVersion = &gv
    cfg.APIPath = "/apis" // you can verify the path from step 2

    var Scheme = runtime.NewScheme()
    var Codecs = serializer.NewCodecFactory(Scheme)
    cfg.NegotiatedSerializer = Codecs.WithoutConversion()

    restClient, err := rest.RESTClientFor(cfg)

    if err != nil {
        panic(err.Error())
    }
    return restClient
}

Alternatively, check the answer from kozmo here


For Velero you can reuse the client they have.

As an example take a look at this code:

restore, err := o.client.VeleroV1().Restores(restore.Namespace).Create(context.TODO(), restore, metav1.CreateOptions{})
Vishrant
  • 15,456
  • 11
  • 71
  • 120