Skip to content

Commit

Permalink
feat: cluster describe use new backup crd (#5341)
Browse files Browse the repository at this point in the history
(cherry picked from commit 89ec847)
  • Loading branch information
fengluodb authored and ahjing99 committed Oct 12, 2023
1 parent 3455b45 commit d9cf5b9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 61 deletions.
3 changes: 3 additions & 0 deletions internal/cli/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,9 @@ func (o *ObjectsGetter) Get() (*ClusterObjects, error) {
if err = listResources(o.Dynamic, types.BackupPolicyGVR(), o.Namespace, dpListOpts, &objs.BackupPolicies); err != nil {
return nil, err
}
if err = listResources(o.Dynamic, types.BackupScheduleGVR(), o.Namespace, dpListOpts, &objs.BackupSchedules); err != nil {
return nil, err
}
var backups []dpv1alpha1.Backup
if err = listResources(o.Dynamic, types.BackupGVR(), o.Namespace, dpListOpts, &backups); err != nil {
return nil, err
Expand Down
5 changes: 3 additions & 2 deletions internal/cli/cluster/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ type ClusterObjects struct {
ConfigMaps *corev1.ConfigMapList
Events *corev1.EventList

BackupPolicies []dpv1alpha1.BackupPolicy
Backups []dpv1alpha1.Backup
BackupPolicies []dpv1alpha1.BackupPolicy
BackupSchedules []dpv1alpha1.BackupSchedule
Backups []dpv1alpha1.Backup
}

type ClusterInfo struct {
Expand Down
80 changes: 44 additions & 36 deletions internal/cli/cmd/cluster/describe.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
package cluster

import (
"context"
"fmt"
"io"
"strings"

"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
"k8s.io/client-go/dynamic"
Expand All @@ -39,7 +42,7 @@ import (
"github.com/apecloud/kubeblocks/internal/cli/printer"
"github.com/apecloud/kubeblocks/internal/cli/types"
"github.com/apecloud/kubeblocks/internal/cli/util"
dptypes "github.com/apecloud/kubeblocks/internal/dataprotection/types"
"github.com/apecloud/kubeblocks/internal/dataprotection/utils/boolptr"
)

var (
Expand Down Expand Up @@ -160,7 +163,11 @@ func (o *describeOptions) describeCluster(name string) error {
showImages(comps, o.Out)

// data protection info
showDataProtection(o.BackupPolicies, o.Backups, o.Out)
defaultBackupRepo, err := o.getDefaultBackupRepo()
if err != nil {
return err
}
showDataProtection(o.BackupPolicies, o.BackupSchedules, defaultBackupRepo, o.Out)

// events
showEvents(o.Cluster.Name, o.Cluster.Namespace, o.Out)
Expand All @@ -169,6 +176,23 @@ func (o *describeOptions) describeCluster(name string) error {
return nil
}

func (o *describeOptions) getDefaultBackupRepo() (string, error) {
backupRepoListObj, err := o.dynamic.Resource(types.BackupRepoGVR()).List(context.TODO(), metav1.ListOptions{})
if err != nil {
return printer.NoneString, err
}
for _, item := range backupRepoListObj.Items {
repo := dpv1alpha1.BackupRepo{}
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(item.Object, &repo); err != nil {
return printer.NoneString, err
}
if repo.Status.IsDefault {
return repo.Name, nil
}
}
return printer.NoneString, nil
}

func showCluster(c *appsv1alpha1.Cluster, out io.Writer) {
if c == nil {
return
Expand Down Expand Up @@ -225,44 +249,28 @@ func showEndpoints(c *appsv1alpha1.Cluster, svcList *corev1.ServiceList, out io.
tbl.Print()
}

func showDataProtection(backupPolicies []dpv1alpha1.BackupPolicy, backups []dpv1alpha1.Backup, out io.Writer) {
if len(backupPolicies) == 0 {
func showDataProtection(backupPolicies []dpv1alpha1.BackupPolicy, backupSchedules []dpv1alpha1.BackupSchedule, defaultBackupRepo string, out io.Writer) {
if len(backupPolicies) == 0 || len(backupSchedules) == 0 {
return
}
tbl := newTbl(out, "\nData Protection:", "AUTO-BACKUP", "BACKUP-SCHEDULE", "TYPE", "BACKUP-TTL", "LAST-SCHEDULE", "RECOVERABLE-TIME")
for _, policy := range backupPolicies {
if policy.Annotations[dptypes.DefaultBackupPolicyAnnotationKey] != "true" {
continue
tbl := newTbl(out, "\nData Protection:", "BACKUP-REPO", "AUTO-BACKUP", "BACKUP-SCHEDULE", "BACKUP-METHOD", "BACKUP-RETENTION")
for _, schedule := range backupSchedules {
backupRepo := defaultBackupRepo
for _, policy := range backupPolicies {
if policy.Name != schedule.Spec.BackupPolicyName {
continue
}
if policy.Spec.BackupRepoName != nil {
backupRepo = *policy.Spec.BackupRepoName
}
}
if policy.Status.Phase != dpv1alpha1.AvailablePhase {
continue
for _, schedulePolicy := range schedule.Spec.Schedules {
if !boolptr.IsSetToTrue(schedulePolicy.Enabled) {
continue
}

tbl.AddRow(backupRepo, "Enabled", schedulePolicy.CronExpression, schedulePolicy.BackupMethod, schedulePolicy.RetentionPeriod.String())
}
// ttlString := printer.NoneString
// backupSchedule := printer.NoneString
// backupType := printer.NoneString
// scheduleEnable := "Disabled"
// if policy.Spec.SchedulePolicy.Snapshot != nil {
// if policy.Spec.SchedulePolicy.Snapshot.Enable {
// scheduleEnable = "Enabled"
// backupSchedule = policy.Spec.SchedulePolicy.Snapshot.CronExpression
// backupType = string(dpv1alpha1.BackupTypeSnapshot)
// }
// }
// if policy.Spec.SchedulePolicy.Datafile != nil {
// if policy.Spec.SchedulePolicy.Datafile.Enable {
// scheduleEnable = "Enabled"
// backupSchedule = policy.Spec.SchedulePolicy.Datafile.CronExpression
// backupType = string(dpv1alpha1.BackupTypeDataFile)
// }
// }
// if policy.Spec.Retention != nil && policy.Spec.Retention.TTL != nil {
// ttlString = *policy.Spec.Retention.TTL
// }
// lastScheduleTime := printer.NoneString
// if policy.Status.LastScheduleTime != nil {
// lastScheduleTime = util.TimeFormat(policy.Status.LastScheduleTime)
// }
// tbl.AddRow(scheduleEnable, backupSchedule, backupType, ttlString, lastScheduleTime, getBackupRecoverableTime(backups))
}
tbl.Print()
}
Expand Down
27 changes: 4 additions & 23 deletions internal/cli/cmd/cluster/describe_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,10 @@ import (
"bytes"
"net/http"
"strings"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/cli-runtime/pkg/genericclioptions"
Expand Down Expand Up @@ -118,29 +116,12 @@ var _ = Describe("Expose", func() {
fakeBackupPolicies := []dpv1alpha1.BackupPolicy{
*testing.FakeBackupPolicy("backup-policy-test", "test-cluster"),
}
fakeBackups := []dpv1alpha1.Backup{
*testing.FakeBackup("backup-test"),
*testing.FakeBackup("backup-test2"),
fakeBackupSchedules := []dpv1alpha1.BackupSchedule{
*testing.FakeBackupSchedule("backup-schedule-test", "backup-policy-test"),
}
now := metav1.Now()
fakeBackups[0].Status = dpv1alpha1.BackupStatus{
Phase: dpv1alpha1.BackupPhaseCompleted,
TimeRange: &dpv1alpha1.BackupTimeRange{
Start: &now,
End: &now,
},
}
after := metav1.Time{Time: now.Add(time.Hour)}
fakeBackups[1].Status = dpv1alpha1.BackupStatus{
Phase: dpv1alpha1.BackupPhaseCompleted,
TimeRange: &dpv1alpha1.BackupTimeRange{
Start: &now,
End: &after,
},
}
showDataProtection(fakeBackupPolicies, fakeBackups, out)
strs := strings.Split(out.String(), "\n")

showDataProtection(fakeBackupPolicies, fakeBackupSchedules, "test-repository", out)
strs := strings.Split(out.String(), "\n")
Expect(strs).ShouldNot(BeEmpty())
})
})
31 changes: 31 additions & 0 deletions internal/cli/testing/fake.go
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,37 @@ func FakeBackup(backupName string) *dpv1alpha1.Backup {
return backup
}

func FakeBackupSchedule(backupScheduleName, backupPolicyName string) *dpv1alpha1.BackupSchedule {
backupSchedule := &dpv1alpha1.BackupSchedule{
TypeMeta: metav1.TypeMeta{
APIVersion: fmt.Sprintf("%s/%s", types.DPAPIGroup, types.DPAPIVersion),
Kind: types.KindBackupSchedule,
},
ObjectMeta: metav1.ObjectMeta{
Name: backupScheduleName,
Namespace: Namespace,
Labels: map[string]string{
constant.AppInstanceLabelKey: ClusterName,
},
},
Spec: dpv1alpha1.BackupScheduleSpec{
BackupPolicyName: backupPolicyName,
Schedules: []dpv1alpha1.SchedulePolicy{
{
Enabled: boolptr.True(),
CronExpression: "0 0 * * *",
BackupMethod: BackupMethodName,
RetentionPeriod: dpv1alpha1.RetentionPeriod("1d"),
},
},
},
Status: dpv1alpha1.BackupScheduleStatus{
Phase: dpv1alpha1.BackupSchedulePhaseAvailable,
},
}
return backupSchedule
}

func FakeBackupWithCluster(cluster *appsv1alpha1.Cluster, backupName string) *dpv1alpha1.Backup {
backup := &dpv1alpha1.Backup{
TypeMeta: metav1.TypeMeta{
Expand Down
1 change: 1 addition & 0 deletions internal/cli/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ const (
KindRestore = "Restore"
KindBackupPolicy = "BackupPolicy"
KindOps = "OpsRequest"
KindBackupSchedule = "BackupSchedule"
)

// K8S rbac API group
Expand Down

0 comments on commit d9cf5b9

Please sign in to comment.