Skip to content

Commit

Permalink
feature: add rollback for promote feature
Browse files Browse the repository at this point in the history
Signed-off-by: zhouhaoA1 <[email protected]>
  • Loading branch information
zhouhaoA1 committed Mar 7, 2024
1 parent 0a73c7b commit 872d906
Show file tree
Hide file tree
Showing 26 changed files with 1,269 additions and 365 deletions.
34 changes: 18 additions & 16 deletions cmd/clustertree/cluster-manager/app/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (

"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/util/uuid"
"k8s.io/client-go/discovery"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/cache"
Expand All @@ -22,6 +23,7 @@ import (
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/mcs"
podcontrollers "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/pod"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote"
_ "github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/pv"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/pvc"
Expand Down Expand Up @@ -162,11 +164,11 @@ func run(ctx context.Context, opts *options.Options) error {
return err
}

//discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
//if err != nil {
// klog.Errorf("Unable to create discoveryClient: %v", err)
// return err
//}
discoveryClient, err := discovery.NewDiscoveryClientForConfig(config)
if err != nil {
klog.Errorf("Unable to create discoveryClient: %v", err)
return err
}

// add cluster controller
clusterController := clusterManager.ClusterController{
Expand Down Expand Up @@ -269,17 +271,17 @@ func run(ctx context.Context, opts *options.Options) error {
}
}

//promotePolicyController := promote.PromotePolicyController{
// RootClient: mgr.GetClient(),
// RootClientSet: rootClient,
// RootDynamicClient: dynamicClient,
// RootDiscoveryClient: discoveryClient,
// GlobalLeafManager: globalleafManager,
// PromotePolicyOptions: opts.PromotePolicyOptions,
//}
//if err = promotePolicyController.SetupWithManager(mgr); err != nil {
// return fmt.Errorf("error starting %s: %v", promote.PromotePolicyControllerName, err)
//}
promotePolicyController := promote.PromotePolicyController{
RootClient: mgr.GetClient(),
RootClientSet: rootClient,
RootDynamicClient: dynamicClient,
RootDiscoveryClient: discoveryClient,
GlobalLeafManager: globalleafManager,
PromotePolicyOptions: opts.PromotePolicyOptions,
}
if err = promotePolicyController.SetupWithManager(mgr); err != nil {
return fmt.Errorf("error starting %s: %v", promote.PromotePolicyControllerName, err)
}

// init commonController
for i, gvr := range controllers.SYNC_GVRS {
Expand Down
6 changes: 6 additions & 0 deletions deploy/crds/kosmos.io_promotepolicies.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,16 @@ spec:
type: string
nullable: true
type: array
rollback:
description: Rollback set true, then rollback from the backup
nullable: true
type: string
type: object
status:
description: PromotePolicyStatus defines the observed state of promotePolicy
properties:
backedupFile:
type: string
completionTimestamp:
description: CompletionTimestamp records the time a sync was completed.
Completion time is recorded even on failed sync. The server's time
Expand Down
13 changes: 13 additions & 0 deletions pkg/apis/kosmos/v1alpha1/promotepolicy_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ type PromotePolicySpec struct {
// +optional
// +nullable
ExcludedNamespaceScopedResources []string `json:"excludedNamespaceScopedResources,omitempty"`

// Rollback set true, then rollback from the backup
// +optional
// +nullable
Rollback string `json:"rollback,omitempty"`
}

// PromotePolicyPhase is a string representation of the lifecycle phase
Expand Down Expand Up @@ -65,6 +70,12 @@ const (
// PromotePolicyPhaseFailedRestore means restore has failed
PromotePolicyPhaseFailedRestore PromotePolicyPhase = "FailedRestore"

// PromotePolicyPhaseFailedRollback means rollback has failed
PromotePolicyPhaseFailedRollback PromotePolicyPhase = "FailedRollback"

// PromotePolicyPhaseRolledback means rollback has successed
PromotePolicyPhaseRolledback PromotePolicyPhase = "RolledBack"

// PromotePolicyPhaseCompleted means the sync has run successfully
PromotePolicyPhaseCompleted PromotePolicyPhase = "Completed"
)
Expand Down Expand Up @@ -118,6 +129,8 @@ type PromotePolicyStatus struct {
// +optional
// +nullable
Progress *PromotePolicyProgress `json:"progress,omitempty"`

BackedupFile string `json:"backedupFile,omitempty"`
}

// +genclient
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"time"

"github.com/google/go-cmp/cmp"
"github.com/pkg/errors"
corev1 "k8s.io/api/core/v1"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -149,12 +148,13 @@ func (r *LeafPodReconciler) SetupWithManager(mgr manager.Manager) error {
return skipFunc(createEvent.Object)
},
UpdateFunc: func(updateEvent event.UpdateEvent) bool {
pod1 := updateEvent.ObjectOld.(*corev1.Pod)
pod2 := updateEvent.ObjectNew.(*corev1.Pod)
if !skipFunc(updateEvent.ObjectNew) {
return false
}
return !cmp.Equal(pod1.Status, pod2.Status)
return skipFunc(updateEvent.ObjectNew)
//pod1 := updateEvent.ObjectOld.(*corev1.Pod)
//pod2 := updateEvent.ObjectNew.(*corev1.Pod)
//if !skipFunc(updateEvent.ObjectNew) {
// return false
//}
//return !cmp.Equal(pod1.Status, pod2.Status)
},
DeleteFunc: func(deleteEvent event.DeleteEvent) bool {
return skipFunc(deleteEvent.Object)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package backup

import (
"archive/tar"
"fmt"
"time"

"github.com/pkg/errors"
Expand All @@ -18,6 +17,7 @@ import (
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/client"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/discovery"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/requests"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/types"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/utils/archive"
)

Expand Down Expand Up @@ -68,8 +68,8 @@ func (ib *itemBackupper) backupItemInternal(obj runtime.Unstructured, groupResou
namespace := metadata.GetNamespace()
name := metadata.GetName()

key := requests.ItemKey{
Resource: resourceKey(obj),
key := types.ItemKey{
Resource: groupResource.String(),
Namespace: namespace,
Name: name,
}
Expand Down Expand Up @@ -189,10 +189,3 @@ func resourceVersion(obj runtime.Unstructured) string {
gvk := obj.GetObjectKind().GroupVersionKind()
return gvk.Version
}

// resourceKey returns a string representing the object's GroupVersionKind (e.g.
// apps/v1/Deployment).
func resourceKey(obj runtime.Unstructured) string {
gvk := obj.GetObjectKind().GroupVersionKind()
return fmt.Sprintf("%s/%s", gvk.GroupVersion().String(), gvk.Kind)
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"k8s.io/apimachinery/pkg/runtime"

"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/requests"
"github.com/kosmos.io/kosmos/pkg/clustertree/cluster-manager/controllers/promote/types"
)

// BackupItemAction is an actor that performs an operation on an individual item being backed up.
Expand Down Expand Up @@ -34,7 +35,7 @@ func registerBackupActions() (map[string]BackupItemAction, error) {
return actionMap, nil
}

func registerBackupItemAction(actionsMap map[string]BackupItemAction, initializer requests.HandlerInitializer) error {
func registerBackupItemAction(actionsMap map[string]BackupItemAction, initializer types.HandlerInitializer) error {
instance, err := initializer()
if err != nil {
return errors.WithMessage(err, "init backup action instance error")
Expand Down
Loading

0 comments on commit 872d906

Please sign in to comment.