Skip to content

Commit

Permalink
Refactor schedule controller with kubebuilder
Browse files Browse the repository at this point in the history
Refactor schedule controller with kubebuilder

fixes vmware-tanzu#4671

Signed-off-by: Wenkai Yin(尹文开) <yinw@vmware.com>
  • Loading branch information
ywk253100 committed Mar 17, 2022
1 parent 3c49ec4 commit edb5cc5
Show file tree
Hide file tree
Showing 12 changed files with 327 additions and 323 deletions.
1 change: 0 additions & 1 deletion .github/auto-assignees.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ reviewers:

groups:
maintainers:
- zubron
- dsu-igeek
- jenting
- sseago
Expand Down
1 change: 1 addition & 0 deletions changelogs/unreleased/4748-ywk253100
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Refactor schedule controller with kubebuilder
24 changes: 23 additions & 1 deletion config/crd/v1/bases/velero.io_schedules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,27 @@ spec:
singular: schedule
scope: Namespaced
versions:
- name: v1
- additionalPrinterColumns:
- description: Name of the schedule
jsonPath: .metadata.name
name: Name
type: string
- description: Status of the schedule
jsonPath: .status.phase
name: Status
type: string
- description: A Cron expression defining when to run the Backup
jsonPath: .spec.schedule
name: Schedule
type: string
- description: The last time a Backup was run for this schedule
jsonPath: .status.lastBackup
name: LastBackup
type: date
- jsonPath: .metadata.creationTimestamp
name: Age
type: date
name: v1
schema:
openAPIV3Schema:
description: Schedule is a Velero resource that represents a pre-scheduled
Expand Down Expand Up @@ -393,6 +413,8 @@ spec:
type: object
served: true
storage: true
subresources:
status: {}
status:
acceptedNames:
kind: ""
Expand Down
2 changes: 1 addition & 1 deletion config/crd/v1/crds/crds.go

Large diffs are not rendered by default.

28 changes: 27 additions & 1 deletion config/rbac/role.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,14 @@ apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
creationTimestamp: null
name: manager-role
name: velero-perms
rules:
- apiGroups:
- velero.io
resources:
- backups
verbs:
- create
- apiGroups:
- velero.io
resources:
Expand Down Expand Up @@ -46,6 +52,26 @@ rules:
- get
- patch
- update
- apiGroups:
- velero.io
resources:
- schedules
verbs:
- create
- delete
- get
- list
- patch
- update
- watch
- apiGroups:
- velero.io
resources:
- schedules/status
verbs:
- get
- patch
- update
- apiGroups:
- velero.io
resources:
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 @@ -49,6 +49,7 @@ ${GOPATH}/src/k8s.io/code-generator/generate-groups.sh \
controller-gen \
crd:crdVersions=v1\
paths=./pkg/apis/velero/v1/... \
rbac:roleName=velero-perms \
paths=./pkg/controller/... \
output:crd:artifacts:config=config/crd/v1/bases

Expand Down
11 changes: 11 additions & 0 deletions pkg/apis/velero/v1/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ type ScheduleStatus struct {
ValidationErrors []string `json:"validationErrors,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="Name",type="string",JSONPath=".metadata.name",description="Name of the schedule"
// +kubebuilder:printcolumn:name="Status",type="string",JSONPath=".status.phase",description="Status of the schedule"
// +kubebuilder:printcolumn:name="Schedule",type="string",JSONPath=".spec.schedule",description="A Cron expression defining when to run the Backup"
// +kubebuilder:printcolumn:name="LastBackup",type="date",JSONPath=".status.lastBackup",description="The last time a Backup was run for this schedule"
// +kubebuilder:printcolumn:name="Age",type="date",JSONPath=".metadata.creationTimestamp"

// Schedule is a Velero resource that represents a pre-scheduled or
// periodic Backup that should be run.
Expand All @@ -96,6 +106,7 @@ type Schedule struct {
}

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

// ScheduleList is a list of Schedules.
type ScheduleList struct {
Expand Down
21 changes: 4 additions & 17 deletions pkg/cmd/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -654,22 +654,6 @@ func (s *server) runControllers(defaultVolumeSnapshotLocations map[string]string
}
}

scheduleControllerRunInfo := func() controllerRunInfo {
scheduleController := controller.NewScheduleController(
s.namespace,
s.veleroClient.VeleroV1(),
s.veleroClient.VeleroV1(),
s.sharedInformerFactory.Velero().V1().Schedules(),
s.logger,
s.metrics,
)

return controllerRunInfo{
controller: scheduleController,
numWorkers: defaultControllerWorkers,
}
}

gcControllerRunInfo := func() controllerRunInfo {
gcController := controller.NewGCController(
s.logger,
Expand Down Expand Up @@ -771,7 +755,6 @@ func (s *server) runControllers(defaultVolumeSnapshotLocations map[string]string
enabledControllers := map[string]func() controllerRunInfo{
controller.BackupSync: backupSyncControllerRunInfo,
controller.Backup: backupControllerRunInfo,
controller.Schedule: scheduleControllerRunInfo,
controller.GarbageCollection: gcControllerRunInfo,
controller.BackupDeletion: deletionControllerRunInfo,
controller.Restore: restoreControllerRunInfo,
Expand Down Expand Up @@ -843,6 +826,10 @@ func (s *server) runControllers(defaultVolumeSnapshotLocations map[string]string
s.logger.Fatal(err, "unable to create controller", "controller", controller.BackupStorageLocation)
}

if err := controller.NewScheduleReconciler(s.namespace, s.logger, s.mgr.GetClient(), s.metrics).SetupWithManager(s.mgr); err != nil {
s.logger.Fatal(err, "unable to create controller", "controller", controller.Schedule)
}

if _, ok := enabledRuntimeControllers[controller.ServerStatusRequest]; ok {
r := controller.ServerStatusRequestReconciler{
Scheme: s.mgr.GetScheme(),
Expand Down
Loading

0 comments on commit edb5cc5

Please sign in to comment.