简体   繁体   中英

How to create custom objects in Kubernetes?

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>}}

There is a Velero client.

As an example take a look at this code :

restore, err := o.client.VeleroV1().Restores(restore.Namespace).Create(context.TODO(), restore, metav1.CreateOptions{})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM