Skip to content

Commit

Permalink
Convert Pod Volume Restore resource/controller to the Kubebuilder fra…
Browse files Browse the repository at this point in the history
…mework

Convert Pod Volume Restore resource/controller to the Kubebuilder framework

Fixes vmware-tanzu#4134

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
  • Loading branch information
ywk253100 committed Apr 25, 2022
1 parent 3ec96e2 commit 80ab6da
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 537 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/4655-ywk253100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert Pod Volume Restore resource/controller to the Kubebuilder framework
34 changes: 33 additions & 1 deletion config/crd/v1/bases/velero.io_podvolumerestores.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,37 @@ spec:
singular: podvolumerestore
scope: Namespaced
versions:
- name: v1
- additionalPrinterColumns:
- description: Namespace of the pod containing the volume to be restored
jsonPath: .spec.pod.namespace
name: Namespace
type: string
- description: Name of the pod containing the volume to be restored
jsonPath: .spec.pod.name
name: Pod
type: string
- description: Name of the volume to be restored
jsonPath: .spec.volume
name: Volume
type: string
- description: Pod Volume Restore status such as New/InProgress
jsonPath: .status.phase
name: Status
type: string
- description: Pod Volume Restore status such as New/InProgress
format: int64
jsonPath: .status.progress.totalBytes
name: TotalBytes
type: integer
- description: Pod Volume Restore status such as New/InProgress
format: int64
jsonPath: .status.progress.bytesDone
name: BytesDone
type: integer
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
properties:
Expand Down Expand Up @@ -136,6 +166,8 @@ spec:
type: object
served: true
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
Expand Down
5 changes: 5 additions & 0 deletions config/crd/v1/crds/crds.go

Large diffs are not rendered by default.

26 changes: 26 additions & 0 deletions config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,24 @@ metadata:
creationTimestamp: null
name: velero-perms
rules:
- apiGroups:
- ""
resources:
- persistentvolumerclaims
verbs:
- get
- apiGroups:
- ""
resources:
- persistentvolumes
verbs:
- get
- apiGroups:
- ""
resources:
- pods
verbs:
- get
- apiGroups:
- velero.io
resources:
Expand Down Expand Up @@ -55,7 +73,11 @@ rules:
- apiGroups:
- velero.io
resources:
<<<<<<< HEAD
- podvolumebackups
=======
- podvolumerestores
>>>>>>> aabf9a9a (Convert Pod Volume Restore resource/controller to the Kubebuilder framework)
verbs:
- create
- delete
Expand All @@ -67,6 +89,7 @@ rules:
- apiGroups:
- velero.io
resources:
<<<<<<< HEAD
- podvolumebackups/status
verbs:
- get
Expand All @@ -88,6 +111,9 @@ rules:
- velero.io
resources:
- schedules/status
=======
- podvolumerestores/status
>>>>>>> aabf9a9a (Convert Pod Volume Restore resource/controller to the Kubebuilder framework)
verbs:
- get
- patch
Expand Down
1 change: 1 addition & 0 deletions hack/update-generated-crd-code.sh
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ ${GOPATH}/src/k8s.io/code-generator/generate-groups.sh \
# Generate manifests e.g. CRD, RBAC etc.
controller-gen \
crd:crdVersions=v1\
rbac:roleName=velero-perms \
paths=./pkg/apis/velero/v1/... \
rbac:roleName=velero-perms \
paths=./pkg/controller/... \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,20 @@ type PodVolumeRestoreStatus struct {
Progress PodVolumeOperationProgress `json:"progress,omitempty"`
}

// TODO(2.0) After converting all resources to use the runtime-controller client, the genclient and k8s:deepcopy markers will no longer be needed and should be removed.
// +genclient
// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root
// +kubebuilder:object:generate=true
// +kubebuilder:storageversion
// +kubebuilder:subresource:status
// +kubebuilder:printcolumn:name="Namespace",type="string",JSONPath=".spec.pod.namespace",description="Namespace of the pod containing the volume to be restored"
// +kubebuilder:printcolumn:name="Pod",type="string",JSONPath=".spec.pod.name",description="Name of the pod containing the volume to be restored"
// +kubebuilder:printcolumn:name="Volume",type="string",JSONPath=".spec.volume",description="Name of the volume to be restored"
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase",description="Pod Volume Restore status such as New/InProgress"
// +kubebuilder:printcolumn:name="TotalBytes",type="integer",format="int64",JSONPath=".status.progress.totalBytes",description="Pod Volume Restore status such as New/InProgress"
// +kubebuilder:printcolumn:name="BytesDone",type="integer",format="int64",JSONPath=".status.progress.bytesDone",description="Pod Volume Restore status such as New/InProgress"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

type PodVolumeRestore struct {
metav1.TypeMeta `json:",inline"`
Expand All @@ -98,6 +110,7 @@ type PodVolumeRestore struct {
}

// +k8s:deepcopy-gen:interfaces=k8s.io/apimachinery/pkg/runtime.Object
// +kubebuilder:object:root

// PodVolumeRestoreList is a list of PodVolumeRestores.
type PodVolumeRestoreList struct {
Expand Down
25 changes: 4 additions & 21 deletions pkg/cmd/cli/restic/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/manager"

"github.com/vmware-tanzu/velero/internal/credentials"
"github.com/vmware-tanzu/velero/internal/util/managercontroller"
velerov1api "github.com/vmware-tanzu/velero/pkg/apis/velero/v1"
"github.com/vmware-tanzu/velero/pkg/buildinfo"
"github.com/vmware-tanzu/velero/pkg/client"
Expand Down Expand Up @@ -226,31 +225,15 @@ func (s *resticServer) run() {
s.logger.Fatal(err, "unable to create controller", "controller", controller.PodVolumeBackup)
}

restoreController := controller.NewPodVolumeRestoreController(
s.logger,
s.veleroInformerFactory.Velero().V1().PodVolumeRestores(),
s.veleroClient.VeleroV1(),
s.podInformer,
s.kubeInformerFactory.Core().V1().PersistentVolumeClaims(),
s.kubeInformerFactory.Core().V1().PersistentVolumes(),
s.mgr.GetClient(),
os.Getenv("NODE_NAME"),
credentialFileStore,
)
if err = controller.NewPodVolumeRestoreReconciler(s.logger, s.mgr.GetClient(), os.Getenv("NODE_NAME"),
credentialFileStore).SetupWithManager(s.mgr); err != nil {
s.logger.WithError(err).Fatal("Unable to create the pod volume restore controller")
}

go s.veleroInformerFactory.Start(s.ctx.Done())
go s.kubeInformerFactory.Start(s.ctx.Done())
go s.podInformer.Run(s.ctx.Done())

// TODO(2.0): presuming all controllers and resources are converted to runtime-controller
// by v2.0, the block from this line and including the `s.mgr.Start() will be
// deprecated, since the manager auto-starts all the caches. Until then, we need to start the
// cache for them manually.

// Adding the controllers to the manager will register them as a (runtime-controller) runnable,
// so the manager will ensure the cache is started and ready before all controller are started
s.mgr.Add(managercontroller.Runnable(restoreController, 1))

s.logger.Info("Controllers starting...")

if err := s.mgr.Start(ctrl.SetupSignalHandler()); err != nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/controller/pod_volume_backup_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,10 +123,17 @@ func (r *PodVolumeBackupReconciler) Reconcile(ctx context.Context, req ctrl.Requ
pvb.Status.Phase = velerov1api.PodVolumeBackupPhaseInProgress
pvb.Status.StartTimestamp = &metav1.Time{Time: r.Clock.Now()}

<<<<<<< HEAD
var pod corev1.Pod
podNamespacedName := client.ObjectKey{
Namespace: pvb.Spec.Pod.Namespace,
Name: pvb.Spec.Pod.Name,
=======
volumeDir, err := kube.GetVolumeDirectoryDeprecated(log, pod, req.Spec.Volume, c.pvcLister, c.pvLister, c.kbClient)
if err != nil {
log.WithError(err).Error("Error getting volume directory name")
return c.fail(req, errors.Wrap(err, "error getting volume directory name").Error(), log)
>>>>>>> aabf9a9a (Convert Pod Volume Restore resource/controller to the Kubebuilder framework)
}
if err := r.Client.Get(ctx, podNamespacedName, &pod); err != nil {
return r.updateStatusToFailed(ctx, &pvb, err, fmt.Sprintf("getting pod %s/%s", pvb.Spec.Pod.Namespace, pvb.Spec.Pod.Name))
Expand Down
Loading

0 comments on commit 80ab6da

Please sign in to comment.