diff --git a/go.mod b/go.mod index c7476edb..8215fb67 100644 --- a/go.mod +++ b/go.mod @@ -4,11 +4,9 @@ go 1.22.6 toolchain go1.22.6 require ( - github.com/kubernetes-incubator/external-storage v0.20.4-openstorage-rc7 github.com/libopenstorage/autopilot-api v1.3.0 github.com/libopenstorage/openstorage v9.4.47+incompatible github.com/libopenstorage/operator v0.0.0-20230801044606-e27dec4914d4 - github.com/libopenstorage/stork v1.4.1-0.20230610103146-72cf75320066 github.com/openshift/api v0.0.0-20230503133300-8bbcb7ca7183 github.com/openshift/client-go v0.0.0-20210112165513-ebc401615f47 // TODO: Vendor from pb-1874 branch. Need to change it to master. @@ -157,7 +155,6 @@ require ( ) replace ( - github.com/kubernetes-incubator/external-storage => github.com/libopenstorage/external-storage v0.25.1-openstorage-rc1 github.com/libopenstorage/autopilot-api => github.com/libopenstorage/autopilot-api v0.6.1-0.20210301232050-ca2633c6e114 github.com/portworx/torpedo => github.com/portworx/torpedo v0.0.0-20230206190621-4ccdccff9ded helm.sh/helm/v3 => helm.sh/helm/v3 v3.10.3 diff --git a/k8s/externalstorage/externalstorage.go b/k8s/externalstorage/externalstorage.go deleted file mode 100644 index 4d4a2321..00000000 --- a/k8s/externalstorage/externalstorage.go +++ /dev/null @@ -1,149 +0,0 @@ -package externalstorage - -import ( - "fmt" - "os" - "sync" - - snapclient "github.com/kubernetes-incubator/external-storage/snapshot/pkg/client" - "github.com/portworx/sched-ops/k8s/common" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" -) - -var ( - instance Ops - once sync.Once -) - -// Ops is an interface to perform operations on external storage resources. -type Ops interface { - SnapshotOps - - // SetConfig sets the config and resets the client - SetConfig(config *rest.Config) -} - -// Instance returns a singleton instance of the client. -func Instance() Ops { - once.Do(func() { - if instance == nil { - instance = &Client{} - } - }) - return instance -} - -// SetInstance replaces the instance with the provided one. Should be used only for testing purposes. -func SetInstance(i Ops) { - instance = i -} - -// New builds a new client for the given config. -func New(client rest.Interface) *Client { - return &Client{ - snap: client, - } -} - -// NewForConfig builds a new client for the given config. -func NewForConfig(c *rest.Config) (*Client, error) { - snap, _, err := snapclient.NewClient(c) - if err != nil { - return nil, err - } - - return &Client{ - snap: snap, - }, nil -} - -// NewInstanceFromConfigFile returns new instance of client by using given -// config file -func NewInstanceFromConfigFile(config string) (Ops, error) { - newInstance := &Client{} - err := newInstance.loadClientFromKubeconfig(config) - if err != nil { - return nil, err - } - return newInstance, nil -} - -// Client is a wrapper for the external-storage client. -type Client struct { - config *rest.Config - snap rest.Interface -} - -// SetConfig sets the config and resets the client -func (c *Client) SetConfig(cfg *rest.Config) { - c.config = cfg - c.snap = nil -} - -// initClient the k8s client if uninitialized -func (c *Client) initClient() error { - if c.snap != nil { - return nil - } - - return c.setClient() -} - -// setClient instantiates a client. -func (c *Client) setClient() error { - var err error - - if c.config != nil { - err = c.loadClient() - } else { - kubeconfig := os.Getenv("KUBECONFIG") - if len(kubeconfig) > 0 { - err = c.loadClientFromKubeconfig(kubeconfig) - } else { - err = c.loadClientFromServiceAccount() - } - - } - - return err -} - -// loadClientFromServiceAccount loads a k8s client from a ServiceAccount specified in the pod running px -func (c *Client) loadClientFromServiceAccount() error { - config, err := rest.InClusterConfig() - if err != nil { - return err - } - - c.config = config - return c.loadClient() -} - -func (c *Client) loadClientFromKubeconfig(kubeconfig string) error { - config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) - if err != nil { - return err - } - - c.config = config - return c.loadClient() -} - -func (c *Client) loadClient() error { - if c.config == nil { - return fmt.Errorf("rest config is not provided") - } - - var err error - err = common.SetRateLimiter(c.config) - if err != nil { - return err - } - c.snap, _, err = snapclient.NewClient(c.config) - if err != nil { - return err - } - - return nil -} diff --git a/k8s/externalstorage/externalstorage_test.go b/k8s/externalstorage/externalstorage_test.go deleted file mode 100644 index ea497b32..00000000 --- a/k8s/externalstorage/externalstorage_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package externalstorage - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestInstance(t *testing.T) { - Instance() - - require.NotNil(t, instance, "instance should be initialized") -} diff --git a/k8s/externalstorage/snapshot.go b/k8s/externalstorage/snapshot.go deleted file mode 100644 index 314b9ee1..00000000 --- a/k8s/externalstorage/snapshot.go +++ /dev/null @@ -1,272 +0,0 @@ -package externalstorage - -import ( - "context" - "fmt" - "time" - - snapv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1" - schederrors "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - corev1 "k8s.io/api/core/v1" -) - -// SnapshotOps is an interface to perform k8s VolumeSnapshot operations -type SnapshotOps interface { - // GetSnapshot returns the snapshot for given name and namespace - GetSnapshot(name string, namespace string) (*snapv1.VolumeSnapshot, error) - // ListSnapshots lists all snapshots in the given namespace - ListSnapshots(namespace string) (*snapv1.VolumeSnapshotList, error) - // CreateSnapshot creates the given snapshot - CreateSnapshot(*snapv1.VolumeSnapshot) (*snapv1.VolumeSnapshot, error) - // UpdateSnapshot updates the given snapshot - UpdateSnapshot(*snapv1.VolumeSnapshot) (*snapv1.VolumeSnapshot, error) - // DeleteSnapshot deletes the given snapshot - DeleteSnapshot(name string, namespace string) error - // ValidateSnapshot validates the given snapshot. - ValidateSnapshot(name string, namespace string, retry bool, timeout, retryInterval time.Duration) error - // GetVolumeForSnapshot returns the volumeID for the given snapshot - GetVolumeForSnapshot(name string, namespace string) (string, error) - // GetSnapshotStatus returns the status of the given snapshot - GetSnapshotStatus(name string, namespace string) (*snapv1.VolumeSnapshotStatus, error) - // GetSnapshotData returns the snapshot for given name - GetSnapshotData(name string) (*snapv1.VolumeSnapshotData, error) - // CreateSnapshotData creates the given volume snapshot data object - CreateSnapshotData(*snapv1.VolumeSnapshotData) (*snapv1.VolumeSnapshotData, error) - // DeleteSnapshotData deletes the given snapshot - DeleteSnapshotData(name string) error - // ValidateSnapshotData validates the given snapshot data object - ValidateSnapshotData(name string, retry bool, timeout, retryInterval time.Duration) error -} - -// CreateSnapshot creates the given snapshot -func (c *Client) CreateSnapshot(snap *snapv1.VolumeSnapshot) (*snapv1.VolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - var result snapv1.VolumeSnapshot - if err := c.snap.Post(). - Name(snap.Metadata.Name). - Resource(snapv1.VolumeSnapshotResourcePlural). - Namespace(snap.Metadata.Namespace). - Body(snap). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - return &result, nil -} - -// UpdateSnapshot updates the given snapshot -func (c *Client) UpdateSnapshot(snap *snapv1.VolumeSnapshot) (*snapv1.VolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - - var result snapv1.VolumeSnapshot - if err := c.snap.Put(). - Name(snap.Metadata.Name). - Resource(snapv1.VolumeSnapshotResourcePlural). - Namespace(snap.Metadata.Namespace). - Body(snap). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - return &result, nil -} - -// DeleteSnapshot deletes the given snapshot -func (c *Client) DeleteSnapshot(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.snap.Delete(). - Name(name). - Resource(snapv1.VolumeSnapshotResourcePlural). - Namespace(namespace). - Do(context.TODO()).Error() -} - -// ValidateSnapshot validates the given snapshot. -func (c *Client) ValidateSnapshot(name string, namespace string, retry bool, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - status, err := c.GetSnapshotStatus(name, namespace) - if err != nil { - return "", true, err - } - - for _, condition := range status.Conditions { - if condition.Type == snapv1.VolumeSnapshotConditionReady && condition.Status == corev1.ConditionTrue { - return "", false, nil - } else if condition.Type == snapv1.VolumeSnapshotConditionError && condition.Status == corev1.ConditionTrue { - return "", true, &schederrors.ErrSnapshotFailed{ - ID: name, - Cause: fmt.Sprintf("Snapshot Status %v", status), - } - } - } - - return "", true, &schederrors.ErrSnapshotNotReady{ - ID: name, - Cause: fmt.Sprintf("Snapshot Status %v", status), - } - } - - if retry { - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - } else { - if _, _, err := t(); err != nil { - return err - } - } - - return nil -} - -// ValidateSnapshotData validates the given snapshot data object -func (c *Client) ValidateSnapshotData(name string, retry bool, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - - t := func() (interface{}, bool, error) { - snapData, err := c.GetSnapshotData(name) - if err != nil { - return "", true, err - } - - for _, condition := range snapData.Status.Conditions { - if condition.Status == corev1.ConditionTrue { - if condition.Type == snapv1.VolumeSnapshotDataConditionReady { - return "", false, nil - } else if condition.Type == snapv1.VolumeSnapshotDataConditionError { - return "", true, &schederrors.ErrSnapshotDataFailed{ - ID: name, - Cause: fmt.Sprintf("SnapshotData Status %v", snapData.Status), - } - } - } - } - - return "", true, &schederrors.ErrSnapshotDataNotReady{ - ID: name, - Cause: fmt.Sprintf("SnapshotData Status %v", snapData.Status), - } - } - - if retry { - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - } else { - if _, _, err := t(); err != nil { - return err - } - } - - return nil -} - -// GetVolumeForSnapshot returns the volumeID for the given snapshot -func (c *Client) GetVolumeForSnapshot(name string, namespace string) (string, error) { - snapshot, err := c.GetSnapshot(name, namespace) - if err != nil { - return "", err - } - - return snapshot.Metadata.Name, nil -} - -// GetSnapshot returns the snapshot for given name and namespace -func (c *Client) GetSnapshot(name string, namespace string) (*snapv1.VolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - - var result snapv1.VolumeSnapshot - if err := c.snap.Get(). - Name(name). - Resource(snapv1.VolumeSnapshotResourcePlural). - Namespace(namespace). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - - return &result, nil -} - -// ListSnapshots lists all snapshots in the given namespace -func (c *Client) ListSnapshots(namespace string) (*snapv1.VolumeSnapshotList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - - var result snapv1.VolumeSnapshotList - if err := c.snap.Get(). - Resource(snapv1.VolumeSnapshotResourcePlural). - Namespace(namespace). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - - return &result, nil -} - -// GetSnapshotStatus returns the status of the given snapshot -func (c *Client) GetSnapshotStatus(name string, namespace string) (*snapv1.VolumeSnapshotStatus, error) { - snapshot, err := c.GetSnapshot(name, namespace) - if err != nil { - return nil, err - } - - return &snapshot.Status, nil -} - -// GetSnapshotData returns the snapshot for given name -func (c *Client) GetSnapshotData(name string) (*snapv1.VolumeSnapshotData, error) { - if err := c.initClient(); err != nil { - return nil, err - } - - var result snapv1.VolumeSnapshotData - if err := c.snap.Get(). - Name(name). - Resource(snapv1.VolumeSnapshotDataResourcePlural). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - - return &result, nil -} - -// CreateSnapshotData creates the given volume snapshot data object -func (c *Client) CreateSnapshotData(snapData *snapv1.VolumeSnapshotData) (*snapv1.VolumeSnapshotData, error) { - if err := c.initClient(); err != nil { - return nil, err - } - - var result snapv1.VolumeSnapshotData - if err := c.snap.Post(). - Name(snapData.Metadata.Name). - Resource(snapv1.VolumeSnapshotDataResourcePlural). - Body(snapData). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - return &result, nil -} - -// DeleteSnapshotData deletes the given snapshot -func (c *Client) DeleteSnapshotData(name string) error { - if err := c.initClient(); err != nil { - return err - } - return c.snap.Delete(). - Name(name). - Resource(snapv1.VolumeSnapshotDataResourcePlural). - Do(context.TODO()).Error() -} diff --git a/k8s/stork/action.go b/k8s/stork/action.go deleted file mode 100644 index f80a359c..00000000 --- a/k8s/stork/action.go +++ /dev/null @@ -1,64 +0,0 @@ -package stork - -import ( - "context" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ActionOps is an interface to manage Action Object -type ActionOps interface { - // CreateAction creates a Action - CreateAction(*storkv1alpha1.Action) (*storkv1alpha1.Action, error) - // GetAction gets the Action - GetAction(string, string) (*storkv1alpha1.Action, error) - // ListActions lists all the Actions - ListActions(namespace string) (*storkv1alpha1.ActionList, error) - // UpdateAction updates the Action - UpdateAction(*storkv1alpha1.Action) (*storkv1alpha1.Action, error) - // DeleteAction deletes the Action - DeleteAction(string, string) error -} - -// CreateAction creates a Action -func (c *Client) CreateAction(action *storkv1alpha1.Action) (*storkv1alpha1.Action, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Actions(action.Namespace).Create(context.TODO(), action, metav1.CreateOptions{}) -} - -// GetAction gets the Action -func (c *Client) GetAction(name string, namespace string) (*storkv1alpha1.Action, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Actions(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListActions lists all the Actions -func (c *Client) ListActions(namespace string) (*storkv1alpha1.ActionList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Actions(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// UpdateAction updates the Action -func (c *Client) UpdateAction(action *storkv1alpha1.Action) (*storkv1alpha1.Action, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Actions(action.Namespace).Update(context.TODO(), action, metav1.UpdateOptions{}) -} - -// DeleteAction deletes the Action -func (c *Client) DeleteAction(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().Actions(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} diff --git a/k8s/stork/applicationbackuprestore.go b/k8s/stork/applicationbackuprestore.go deleted file mode 100644 index 6b0b8921..00000000 --- a/k8s/stork/applicationbackuprestore.go +++ /dev/null @@ -1,396 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ApplicationBackupRestoreOps is an interface to perfrom k8s Application Backup -// and Restore operations -type ApplicationBackupRestoreOps interface { - // CreateApplicationBackup creates the ApplicationBackup - CreateApplicationBackup(*storkv1alpha1.ApplicationBackup) (*storkv1alpha1.ApplicationBackup, error) - // GetApplicationBackup gets the ApplicationBackup - GetApplicationBackup(string, string) (*storkv1alpha1.ApplicationBackup, error) - // ListApplicationBackups lists all the ApplicationBackups - ListApplicationBackups(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ApplicationBackupList, error) - // UpdateApplicationBackup updates the ApplicationBackup - UpdateApplicationBackup(*storkv1alpha1.ApplicationBackup) (*storkv1alpha1.ApplicationBackup, error) - // DeleteApplicationBackup deletes the ApplicationBackup - DeleteApplicationBackup(string, string) error - // ValidateApplicationBackup validates the ApplicationBackup - ValidateApplicationBackup(string, string, time.Duration, time.Duration) error - // WatchApplicationBackup watch the ApplicationBackup - WatchApplicationBackup(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error - // CreateApplicationRestore creates the ApplicationRestore - CreateApplicationRestore(*storkv1alpha1.ApplicationRestore) (*storkv1alpha1.ApplicationRestore, error) - // GetApplicationRestore gets the ApplicationRestore - GetApplicationRestore(string, string) (*storkv1alpha1.ApplicationRestore, error) - // ListApplicationRestores lists all the ApplicationRestores - ListApplicationRestores(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ApplicationRestoreList, error) - // UpdateApplicationRestore updates the ApplicationRestore - UpdateApplicationRestore(*storkv1alpha1.ApplicationRestore) (*storkv1alpha1.ApplicationRestore, error) - // DeleteApplicationRestore deletes the ApplicationRestore - DeleteApplicationRestore(string, string) error - // ValidateApplicationRestore validates the ApplicationRestore - ValidateApplicationRestore(string, string, time.Duration, time.Duration) error - // WatchApplicationRestore watch the ApplicationRestore - WatchApplicationRestore(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error - // GetApplicationBackupSchedule gets the ApplicationBackupSchedule - GetApplicationBackupSchedule(string, string) (*storkv1alpha1.ApplicationBackupSchedule, error) - // CreateApplicationBackupSchedule creates an ApplicationBackupSchedule - CreateApplicationBackupSchedule(*storkv1alpha1.ApplicationBackupSchedule) (*storkv1alpha1.ApplicationBackupSchedule, error) - // UpdateApplicationBackupSchedule updates the ApplicationBackupSchedule - UpdateApplicationBackupSchedule(*storkv1alpha1.ApplicationBackupSchedule) (*storkv1alpha1.ApplicationBackupSchedule, error) - // ListApplicationBackupSchedules lists all the ApplicationBackupSchedules - ListApplicationBackupSchedules(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ApplicationBackupScheduleList, error) - // DeleteApplicationBackupSchedule deletes the ApplicationBackupSchedule - DeleteApplicationBackupSchedule(string, string) error - // ValidateApplicationBackupSchedule validates the given ApplicationBackupSchedule. It checks the status of each of - // the backups triggered for this schedule and returns a map of successfull backups. The key of the - // map will be the schedule type and value will be list of backups for that schedule type. - // The caller is expected to validate if the returned map has all backups expected at that point of time - ValidateApplicationBackupSchedule(string, string, int, time.Duration, time.Duration) ( - map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledApplicationBackupStatus, error) - // WatchApplicationBackupSchedule watch the ApplicationBackupSchedule objects - WatchApplicationBackupSchedule(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error -} - -// CreateApplicationBackup creates the ApplicationBackup -func (c *Client) CreateApplicationBackup(backup *storkv1alpha1.ApplicationBackup) (*storkv1alpha1.ApplicationBackup, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackups(backup.Namespace).Create(context.TODO(), backup, metav1.CreateOptions{}) -} - -// GetApplicationBackup gets the ApplicationBackup -func (c *Client) GetApplicationBackup(name string, namespace string) (*storkv1alpha1.ApplicationBackup, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackups(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListApplicationBackups lists all the ApplicationBackups -func (c *Client) ListApplicationBackups(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ApplicationBackupList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackups(namespace).List(context.TODO(), filterOptions) -} - -// DeleteApplicationBackup deletes the ApplicationBackup -func (c *Client) DeleteApplicationBackup(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ApplicationBackups(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// UpdateApplicationBackup updates the ApplicationBackup -func (c *Client) UpdateApplicationBackup(backup *storkv1alpha1.ApplicationBackup) (*storkv1alpha1.ApplicationBackup, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackups(backup.Namespace).Update(context.TODO(), backup, metav1.UpdateOptions{}) -} - -// ValidateApplicationBackup validates the ApplicationBackup -func (c *Client) ValidateApplicationBackup(name, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - applicationbackup, err := c.GetApplicationBackup(name, namespace) - if err != nil { - return "", true, err - } - - if applicationbackup.Status.Status == storkv1alpha1.ApplicationBackupStatusSuccessful { - return "", false, nil - } - - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: applicationbackup.Name, - Cause: fmt.Sprintf("Application backup failed . Error: %v .Expected status: %v Actual status: %v", err, storkv1alpha1.ApplicationBackupStatusSuccessful, applicationbackup.Status.Status), - Type: applicationbackup, - } - - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - return nil -} - -// GetApplicationRestore gets the ApplicationRestore -func (c *Client) GetApplicationRestore(name string, namespace string) (*storkv1alpha1.ApplicationRestore, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRestores(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListApplicationRestores lists all the ApplicationRestores -func (c *Client) ListApplicationRestores(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ApplicationRestoreList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRestores(namespace).List(context.TODO(), filterOptions) -} - -// CreateApplicationRestore creates the ApplicationRestore -func (c *Client) CreateApplicationRestore(restore *storkv1alpha1.ApplicationRestore) (*storkv1alpha1.ApplicationRestore, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRestores(restore.Namespace).Create(context.TODO(), restore, metav1.CreateOptions{}) -} - -// DeleteApplicationRestore deletes the ApplicationRestore -func (c *Client) DeleteApplicationRestore(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ApplicationRestores(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateApplicationRestore validates the ApplicationRestore -func (c *Client) ValidateApplicationRestore(name, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - applicationrestore, err := c.stork.StorkV1alpha1().ApplicationRestores(namespace).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return "", true, err - } - - if applicationrestore.Status.Status == storkv1alpha1.ApplicationRestoreStatusSuccessful || - applicationrestore.Status.Status == storkv1alpha1.ApplicationRestoreStatusPartialSuccess { - return "", false, nil - } - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: applicationrestore.Name, - Cause: fmt.Sprintf("Application restore failed . Error: %v .Expected status: %v/%v Actual status: %v", - err, - storkv1alpha1.ApplicationRestoreStatusSuccessful, - storkv1alpha1.ApplicationRestoreStatusPartialSuccess, - applicationrestore.Status.Status), - Type: applicationrestore, - } - } - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - return nil -} - -// UpdateApplicationRestore updates the ApplicationRestore -func (c *Client) UpdateApplicationRestore(restore *storkv1alpha1.ApplicationRestore) (*storkv1alpha1.ApplicationRestore, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRestores(restore.Namespace).Update(context.TODO(), restore, metav1.UpdateOptions{}) -} - -// GetApplicationBackupSchedule gets the ApplicationBackupSchedule -func (c *Client) GetApplicationBackupSchedule(name string, namespace string) (*storkv1alpha1.ApplicationBackupSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackupSchedules(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListApplicationBackupSchedules lists all the ApplicationBackupSchedules -func (c *Client) ListApplicationBackupSchedules(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ApplicationBackupScheduleList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackupSchedules(namespace).List(context.TODO(), filterOptions) -} - -// CreateApplicationBackupSchedule creates an ApplicationBackupSchedule -func (c *Client) CreateApplicationBackupSchedule(applicationBackupSchedule *storkv1alpha1.ApplicationBackupSchedule) (*storkv1alpha1.ApplicationBackupSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackupSchedules(applicationBackupSchedule.Namespace).Create(context.TODO(), applicationBackupSchedule, metav1.CreateOptions{}) -} - -// UpdateApplicationBackupSchedule updates the ApplicationBackupSchedule -func (c *Client) UpdateApplicationBackupSchedule(applicationBackupSchedule *storkv1alpha1.ApplicationBackupSchedule) (*storkv1alpha1.ApplicationBackupSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationBackupSchedules(applicationBackupSchedule.Namespace).Update(context.TODO(), applicationBackupSchedule, metav1.UpdateOptions{}) -} - -// DeleteApplicationBackupSchedule deletes the ApplicationBackupSchedule -func (c *Client) DeleteApplicationBackupSchedule(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ApplicationBackupSchedules(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateApplicationBackupSchedule validates the given ApplicationBackupSchedule. It checks the status of each of -// the backups triggered for this schedule and returns a map of successfull backups. The key of the -// map will be the schedule type and value will be list of backups for that schedule type. -// The caller is expected to validate if the returned map has all backups expected at that point of time -func (c *Client) ValidateApplicationBackupSchedule(name string, namespace string, expectedSuccess int, timeout, retryInterval time.Duration) ( - map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledApplicationBackupStatus, error) { - if err := c.initClient(); err != nil { - return nil, err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetApplicationBackupSchedule(name, namespace) - if err != nil { - return nil, true, err - } - - if len(resp.Status.Items) == 0 { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("0 backups have yet run for the backup schedule"), - Type: resp, - } - } - - failedBackups := make([]string, 0) - pendingBackups := make([]string, 0) - success := 0 - for _, backupStatuses := range resp.Status.Items { - // The check below assumes that the status will not have a failed - // backup if the last one succeeded so just get the last status - if len(backupStatuses) > 0 { - status := backupStatuses[len(backupStatuses)-1] - if status == nil { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: "ApplicationBackupSchedule has an empty backup in it's most recent status", - Type: resp, - } - } - - if status.Status == storkv1alpha1.ApplicationBackupStatusSuccessful { - success++ - continue - } - - if status.Status == storkv1alpha1.ApplicationBackupStatusFailed { - failedBackups = append(failedBackups, - fmt.Sprintf("backup: %s failed. status: %v", status.Name, status.Status)) - } else { - pendingBackups = append(pendingBackups, - fmt.Sprintf("backup: %s is not done. status: %v", status.Name, status.Status)) - } - } - } - - if len(failedBackups) > 0 { - return nil, false, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("ApplicationBackupSchedule failed as one or more backups have failed. %s", - failedBackups), - Type: resp, - } - } - - if success == expectedSuccess { - return resp.Status.Items, false, nil - } - - if len(pendingBackups) > 0 { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("ApplicationBackupSchedule has certain backups pending: %s", - pendingBackups), - Type: resp, - } - } - - return resp.Status.Items, false, nil - } - - ret, err := task.DoRetryWithTimeout(t, timeout, retryInterval) - if err != nil { - return nil, err - } - - backups, ok := ret.(map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledApplicationBackupStatus) - if !ok { - return nil, fmt.Errorf("invalid type when checking backup schedules: %v", backups) - } - - return backups, nil -} - -// WatchApplicationBackup sets up a watcher that listens for changes on application backups -func (c *Client) WatchApplicationBackup(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().ApplicationBackups(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for application backups") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.ApplicationBackup{}, "", fn, listOptions) - return nil -} - -// WatchApplicationRestore sets up a watcher that listens for changes on application restores -func (c *Client) WatchApplicationRestore(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().ApplicationRestores(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for application restores") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.ApplicationRestore{}, "", fn, listOptions) - return nil -} - -// WatchApplicationBackupSchedule sets up a watcher that listens for changes on applicationbackup schedules -func (c *Client) WatchApplicationBackupSchedule(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().ApplicationBackupSchedules(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for application backup schedules") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.ApplicationBackupSchedule{}, "", fn, listOptions) - return nil -} diff --git a/k8s/stork/applicationclone.go b/k8s/stork/applicationclone.go deleted file mode 100644 index 7514743f..00000000 --- a/k8s/stork/applicationclone.go +++ /dev/null @@ -1,117 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ApplicationCloneOps is an interface to perform k8s Application Clone operations -type ApplicationCloneOps interface { - // CreateApplicationClone creates the ApplicationClone - CreateApplicationClone(*storkv1alpha1.ApplicationClone) (*storkv1alpha1.ApplicationClone, error) - // GetApplicationClone gets the ApplicationClone - GetApplicationClone(string, string) (*storkv1alpha1.ApplicationClone, error) - // ListApplicationClones lists all the ApplicationClones - ListApplicationClones(string) (*storkv1alpha1.ApplicationCloneList, error) - // UpdateApplicationClone updates the ApplicationClone - UpdateApplicationClone(*storkv1alpha1.ApplicationClone) (*storkv1alpha1.ApplicationClone, error) - // DeleteApplicationClone deletes the ApplicationClone - DeleteApplicationClone(string, string) error - // ValidateApplicationClone validates the ApplicationClone - ValidateApplicationClone(string, string, time.Duration, time.Duration) error - // WatchApplicationClone watch the ApplicationClone - WatchApplicationClone(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error -} - -// CreateApplicationClone creates the ApplicationClone -func (c *Client) CreateApplicationClone(clone *storkv1alpha1.ApplicationClone) (*storkv1alpha1.ApplicationClone, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationClones(clone.Namespace).Create(context.TODO(), clone, metav1.CreateOptions{}) -} - -// GetApplicationClone gets the ApplicationClone -func (c *Client) GetApplicationClone(name string, namespace string) (*storkv1alpha1.ApplicationClone, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationClones(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListApplicationClones lists all the ApplicationClones -func (c *Client) ListApplicationClones(namespace string) (*storkv1alpha1.ApplicationCloneList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationClones(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// DeleteApplicationClone deletes the ApplicationClone -func (c *Client) DeleteApplicationClone(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ApplicationClones(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// UpdateApplicationClone updates the ApplicationClone -func (c *Client) UpdateApplicationClone(clone *storkv1alpha1.ApplicationClone) (*storkv1alpha1.ApplicationClone, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationClones(clone.Namespace).Update(context.TODO(), clone, metav1.UpdateOptions{}) -} - -// ValidateApplicationClone validates the ApplicationClone -func (c *Client) ValidateApplicationClone(name, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - applicationclone, err := c.stork.StorkV1alpha1().ApplicationClones(namespace).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return "", true, err - } - - if applicationclone.Status.Status == storkv1alpha1.ApplicationCloneStatusSuccessful { - return "", false, nil - } - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: applicationclone.Name, - Cause: fmt.Sprintf("Application Clone failed . Error: %v .Expected status: %v Actual status: %v", err, storkv1alpha1.ApplicationCloneStatusSuccessful, applicationclone.Status.Status), - Type: applicationclone, - } - } - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - return nil -} - -// WatchApplicationClone sets up a watcher that listens for changes on application backups -func (c *Client) WatchApplicationClone(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().ApplicationClones(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for application clones") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.ApplicationClone{}, "", fn, listOptions) - return nil -} diff --git a/k8s/stork/applicationregistration.go b/k8s/stork/applicationregistration.go deleted file mode 100644 index eb888376..00000000 --- a/k8s/stork/applicationregistration.go +++ /dev/null @@ -1,94 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ApplicationRegistrationOps is an interface to perform k8s ApplicationRegistration operations -type ApplicationRegistrationOps interface { - // CreateApplicationRegistration creates the ApplicationRegistration - CreateApplicationRegistration(*storkv1alpha1.ApplicationRegistration) (*storkv1alpha1.ApplicationRegistration, error) - // GetApplicationRegistration gets the ApplicationRegistration - GetApplicationRegistration(string) (*storkv1alpha1.ApplicationRegistration, error) - // ListApplicationRegistrations lists all the ApplicationRegistrations - ListApplicationRegistrations() (*storkv1alpha1.ApplicationRegistrationList, error) - // UpdateApplicationRegistration updates the ApplicationRegistration - UpdateApplicationRegistration(*storkv1alpha1.ApplicationRegistration) (*storkv1alpha1.ApplicationRegistration, error) - // DeleteApplicationRegistration deletes the ApplicationRegistration - DeleteApplicationRegistration(string) error - // ValidateApplicationRegistration validates the ApplicationRegistration - ValidateApplicationRegistration(string, time.Duration, time.Duration) error -} - -// CreateApplicationRegistration creates the ApplicationRegistration -func (c *Client) CreateApplicationRegistration(applicationRegistration *storkv1alpha1.ApplicationRegistration) (*storkv1alpha1.ApplicationRegistration, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRegistrations().Create(context.TODO(), applicationRegistration, metav1.CreateOptions{}) -} - -// GetApplicationRegistration gets the ApplicationRegistration -func (c *Client) GetApplicationRegistration(name string) (*storkv1alpha1.ApplicationRegistration, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRegistrations().Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListApplicationRegistrations lists all the ApplicationRegistrations -func (c *Client) ListApplicationRegistrations() (*storkv1alpha1.ApplicationRegistrationList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRegistrations().List(context.TODO(), metav1.ListOptions{}) - -} - -// UpdateApplicationRegistration updates the ApplicationRegistration -func (c *Client) UpdateApplicationRegistration(applicationRegistration *storkv1alpha1.ApplicationRegistration) (*storkv1alpha1.ApplicationRegistration, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ApplicationRegistrations().Update(context.TODO(), applicationRegistration, metav1.UpdateOptions{}) -} - -// DeleteApplicationRegistration deletes the ApplicationRegistration -func (c *Client) DeleteApplicationRegistration(name string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ApplicationRegistrations().Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateApplicationRegistration validates the ApplicationRegistration -func (c *Client) ValidateApplicationRegistration(name string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetApplicationRegistration(name) - if err != nil { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("ApplicationRegistration failed . Error: %v", err), - Type: resp, - } - } - return "", false, nil - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - return nil -} diff --git a/k8s/stork/backuplocation.go b/k8s/stork/backuplocation.go deleted file mode 100644 index 96760bb5..00000000 --- a/k8s/stork/backuplocation.go +++ /dev/null @@ -1,121 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// BackupLocationOps is an interface to perform k8s BackupLocation operations -type BackupLocationOps interface { - // CreateBackupLocation creates the BackupLocation - CreateBackupLocation(*storkv1alpha1.BackupLocation) (*storkv1alpha1.BackupLocation, error) - // GetBackupLocation gets the BackupLocation - GetBackupLocation(string, string) (*storkv1alpha1.BackupLocation, error) - // ListBackupLocations lists all the BackupLocations - ListBackupLocations(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.BackupLocationList, error) - // UpdateBackupLocation updates the BackupLocation - UpdateBackupLocation(*storkv1alpha1.BackupLocation) (*storkv1alpha1.BackupLocation, error) - // DeleteBackupLocation deletes the BackupLocation - DeleteBackupLocation(string, string) error - // ValidateBackupLocation validates the BackupLocation - ValidateBackupLocation(string, string, time.Duration, time.Duration) error -} - -// CreateBackupLocation creates the BackupLocation -func (c *Client) CreateBackupLocation(backupLocation *storkv1alpha1.BackupLocation) (*storkv1alpha1.BackupLocation, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().BackupLocations(backupLocation.Namespace).Create(context.TODO(), backupLocation, metav1.CreateOptions{}) -} - -// GetBackupLocation gets the BackupLocation -func (c *Client) GetBackupLocation(name string, namespace string) (*storkv1alpha1.BackupLocation, error) { - if err := c.initClient(); err != nil { - return nil, err - } - backupLocation, err := c.stork.StorkV1alpha1().BackupLocations(namespace).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return nil, err - } - - // TODO: use secrets/corev1 client instead of clientset - err = backupLocation.UpdateFromSecret(c.kube) - if err != nil { - return nil, err - } - err = backupLocation.UpdateFromClusterSecret(c.kube) - if err != nil { - return nil, err - } - return backupLocation, nil -} - -// ListBackupLocations lists all the BackupLocations -func (c *Client) ListBackupLocations(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.BackupLocationList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - backupLocations, err := c.stork.StorkV1alpha1().BackupLocations(namespace).List(context.TODO(), filterOptions) - if err != nil { - return nil, err - } - for i := range backupLocations.Items { - err = backupLocations.Items[i].UpdateFromSecret(c.kube) - if err != nil { - return nil, err - } - err = backupLocations.Items[i].UpdateFromClusterSecret(c.kube) - if err != nil { - return nil, err - } - } - return backupLocations, nil -} - -// UpdateBackupLocation updates the BackupLocation -func (c *Client) UpdateBackupLocation(backupLocation *storkv1alpha1.BackupLocation) (*storkv1alpha1.BackupLocation, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().BackupLocations(backupLocation.Namespace).Update(context.TODO(), backupLocation, metav1.UpdateOptions{}) -} - -// DeleteBackupLocation deletes the BackupLocation -func (c *Client) DeleteBackupLocation(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().BackupLocations(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateBackupLocation validates the BackupLocation -func (c *Client) ValidateBackupLocation(name, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetBackupLocation(name, namespace) - if err != nil { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("BackupLocation failed . Error: %v", err), - Type: resp, - } - } - return "", false, nil - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - return nil -} diff --git a/k8s/stork/clusterdomains.go b/k8s/stork/clusterdomains.go deleted file mode 100644 index f7843c44..00000000 --- a/k8s/stork/clusterdomains.go +++ /dev/null @@ -1,203 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ClusterDomainsOps is an interface to perform k8s ClusterDomains operations -type ClusterDomainsOps interface { - // CreateClusterDomainsStatus creates the ClusterDomainStatus - CreateClusterDomainsStatus(*storkv1alpha1.ClusterDomainsStatus) (*storkv1alpha1.ClusterDomainsStatus, error) - // GetClusterDomainsStatus gets the ClusterDomainsStatus - GetClusterDomainsStatus(string) (*storkv1alpha1.ClusterDomainsStatus, error) - // UpdateClusterDomainsStatus updates the ClusterDomainsStatus - UpdateClusterDomainsStatus(*storkv1alpha1.ClusterDomainsStatus) (*storkv1alpha1.ClusterDomainsStatus, error) - // DeleteClusterDomainsStatus deletes the ClusterDomainsStatus - DeleteClusterDomainsStatus(string) error - // ListClusterDomainStatuses lists ClusterDomainsStatus - ListClusterDomainStatuses() (*storkv1alpha1.ClusterDomainsStatusList, error) - // ValidateClusterDomainsStatus validates the ClusterDomainsStatus - ValidateClusterDomainsStatus(string, map[string]bool, time.Duration, time.Duration) error - // CreateClusterDomainUpdate creates the ClusterDomainUpdate - CreateClusterDomainUpdate(*storkv1alpha1.ClusterDomainUpdate) (*storkv1alpha1.ClusterDomainUpdate, error) - // GetClusterDomainUpdate gets the ClusterDomainUpdate - GetClusterDomainUpdate(string) (*storkv1alpha1.ClusterDomainUpdate, error) - // UpdateClusterDomainUpdate updates the ClusterDomainUpdate - UpdateClusterDomainUpdate(*storkv1alpha1.ClusterDomainUpdate) (*storkv1alpha1.ClusterDomainUpdate, error) - // DeleteClusterDomainUpdate deletes the ClusterDomainUpdate - DeleteClusterDomainUpdate(string) error - // ValidateClusterDomainUpdate validates ClusterDomainUpdate - ValidateClusterDomainUpdate(string, time.Duration, time.Duration) error - // ListClusterDomainUpdates lists ClusterDomainUpdates - ListClusterDomainUpdates() (*storkv1alpha1.ClusterDomainUpdateList, error) -} - -// CreateClusterDomainsStatus creates the ClusterDomainStatus -func (c *Client) CreateClusterDomainsStatus(clusterDomainsStatus *storkv1alpha1.ClusterDomainsStatus) (*storkv1alpha1.ClusterDomainsStatus, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainsStatuses().Create(context.TODO(), clusterDomainsStatus, metav1.CreateOptions{}) -} - -// GetClusterDomainsStatus gets the ClusterDomainsStatus -func (c *Client) GetClusterDomainsStatus(name string) (*storkv1alpha1.ClusterDomainsStatus, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainsStatuses().Get(context.TODO(), name, metav1.GetOptions{}) -} - -// UpdateClusterDomainsStatus updates the ClusterDomainsStatus -func (c *Client) UpdateClusterDomainsStatus(clusterDomainsStatus *storkv1alpha1.ClusterDomainsStatus) (*storkv1alpha1.ClusterDomainsStatus, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainsStatuses().Update(context.TODO(), clusterDomainsStatus, metav1.UpdateOptions{}) -} - -// DeleteClusterDomainsStatus deletes the ClusterDomainsStatus -func (c *Client) DeleteClusterDomainsStatus(name string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ClusterDomainsStatuses().Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateClusterDomainsStatus validates the ClusterDomainsStatus -func (c *Client) ValidateClusterDomainsStatus(name string, domainMap map[string]bool, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - cds, err := c.GetClusterDomainsStatus(name) - if err != nil { - return "", true, err - } - - for _, domainInfo := range cds.Status.ClusterDomainInfos { - isActive, _ := domainMap[domainInfo.Name] - if isActive { - if domainInfo.State != storkv1alpha1.ClusterDomainActive { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: domainInfo.Name, - Cause: fmt.Sprintf("ClusterDomainsStatus mismatch. For domain %v "+ - "expected to be active found inactive", domainInfo.Name), - Type: cds, - } - } - } else { - if domainInfo.State != storkv1alpha1.ClusterDomainInactive { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: domainInfo.Name, - Cause: fmt.Sprintf("ClusterDomainsStatus mismatch. For domain %v "+ - "expected to be inactive found active", domainInfo.Name), - Type: cds, - } - } - } - } - - return "", false, nil - - } - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - - return nil - -} - -// ListClusterDomainStatuses lists ClusterDomainsStatus -func (c *Client) ListClusterDomainStatuses() (*storkv1alpha1.ClusterDomainsStatusList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainsStatuses().List(context.TODO(), metav1.ListOptions{}) -} - -// CreateClusterDomainUpdate creates the ClusterDomainUpdate -func (c *Client) CreateClusterDomainUpdate(clusterDomainUpdate *storkv1alpha1.ClusterDomainUpdate) (*storkv1alpha1.ClusterDomainUpdate, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainUpdates().Create(context.TODO(), clusterDomainUpdate, metav1.CreateOptions{}) -} - -// GetClusterDomainUpdate gets the ClusterDomainUpdate -func (c *Client) GetClusterDomainUpdate(name string) (*storkv1alpha1.ClusterDomainUpdate, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainUpdates().Get(context.TODO(), name, metav1.GetOptions{}) -} - -// UpdateClusterDomainUpdate updates the ClusterDomainUpdate -func (c *Client) UpdateClusterDomainUpdate(clusterDomainUpdate *storkv1alpha1.ClusterDomainUpdate) (*storkv1alpha1.ClusterDomainUpdate, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainUpdates().Update(context.TODO(), clusterDomainUpdate, metav1.UpdateOptions{}) -} - -// DeleteClusterDomainUpdate deletes the ClusterDomainUpdate -func (c *Client) DeleteClusterDomainUpdate(name string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ClusterDomainUpdates().Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateClusterDomainUpdate validates ClusterDomainUpdate -func (c *Client) ValidateClusterDomainUpdate(name string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetClusterDomainUpdate(name) - if err != nil { - return "", true, err - } - - if resp.Status.Status == storkv1alpha1.ClusterDomainUpdateStatusSuccessful { - return "", false, nil - } else if resp.Status.Status == storkv1alpha1.ClusterDomainUpdateStatusFailed { - return "", false, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("ClusterDomainUpdate Status %v, Reason: %v", resp.Status.Status, resp.Status.Reason), - Type: resp, - } - } - - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("ClusterDomainUpdate Status %v, Reason: %v", resp.Status.Status, resp.Status.Reason), - Type: resp, - } - } - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - - return nil -} - -// ListClusterDomainUpdates lists ClusterDomainUpdates -func (c *Client) ListClusterDomainUpdates() (*storkv1alpha1.ClusterDomainUpdateList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterDomainUpdates().List(context.TODO(), metav1.ListOptions{}) -} diff --git a/k8s/stork/clusterpair.go b/k8s/stork/clusterpair.go deleted file mode 100644 index 9bde413b..00000000 --- a/k8s/stork/clusterpair.go +++ /dev/null @@ -1,129 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ClusterPairOps is an interface to perfrom k8s ClusterPair operations -type ClusterPairOps interface { - // CreateClusterPair creates the ClusterPair - CreateClusterPair(*storkv1alpha1.ClusterPair) (*storkv1alpha1.ClusterPair, error) - // GetClusterPair gets the ClusterPair - GetClusterPair(string, string) (*storkv1alpha1.ClusterPair, error) - // ListClusterPairs gets all the ClusterPairs - ListClusterPairs(string) (*storkv1alpha1.ClusterPairList, error) - // UpdateClusterPair updates the ClusterPair - UpdateClusterPair(*storkv1alpha1.ClusterPair) (*storkv1alpha1.ClusterPair, error) - // DeleteClusterPair deletes the ClusterPair - DeleteClusterPair(string, string) error - // ValidateClusterPair validates clusterpair status - ValidateClusterPair(string, string, time.Duration, time.Duration) error - // WatchClusterPair watch the ClusterPair object - WatchClusterPair(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error -} - -// CreateClusterPair creates the ClusterPair -func (c *Client) CreateClusterPair(pair *storkv1alpha1.ClusterPair) (*storkv1alpha1.ClusterPair, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterPairs(pair.Namespace).Create(context.TODO(), pair, metav1.CreateOptions{}) -} - -// GetClusterPair gets the ClusterPair -func (c *Client) GetClusterPair(name string, namespace string) (*storkv1alpha1.ClusterPair, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterPairs(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListClusterPairs gets all the ClusterPairs -func (c *Client) ListClusterPairs(namespace string) (*storkv1alpha1.ClusterPairList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterPairs(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// UpdateClusterPair updates the ClusterPair -func (c *Client) UpdateClusterPair(pair *storkv1alpha1.ClusterPair) (*storkv1alpha1.ClusterPair, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ClusterPairs(pair.Namespace).Update(context.TODO(), pair, metav1.UpdateOptions{}) -} - -// DeleteClusterPair deletes the ClusterPair -func (c *Client) DeleteClusterPair(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ClusterPairs(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateClusterPair validates clusterpair status -func (c *Client) ValidateClusterPair(name string, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - clusterPair, err := c.GetClusterPair(name, namespace) - if err != nil { - return "", true, err - } - - if clusterPair.Status.SchedulerStatus == storkv1alpha1.ClusterPairStatusReady && - (clusterPair.Status.StorageStatus == storkv1alpha1.ClusterPairStatusReady || - clusterPair.Status.StorageStatus == storkv1alpha1.ClusterPairStatusNotProvided) { - return "", false, nil - } else if clusterPair.Status.SchedulerStatus == storkv1alpha1.ClusterPairStatusError || - clusterPair.Status.StorageStatus == storkv1alpha1.ClusterPairStatusError { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("Storage Status: %v \t Scheduler Status: %v", clusterPair.Status.StorageStatus, clusterPair.Status.SchedulerStatus), - Type: clusterPair, - } - } - - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("Storage Status: %v \t Scheduler Status: %v", clusterPair.Status.StorageStatus, clusterPair.Status.SchedulerStatus), - Type: clusterPair, - } - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - - return nil -} - -// WatchClusterPair sets up a watcher that listens for changes on cluster pair objects -func (c *Client) WatchClusterPair(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().ClusterPairs(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for cluster pair") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.ClusterPair{}, "", fn, listOptions) - return nil -} diff --git a/k8s/stork/groupsnapshot.go b/k8s/stork/groupsnapshot.go deleted file mode 100644 index 61f5d90f..00000000 --- a/k8s/stork/groupsnapshot.go +++ /dev/null @@ -1,191 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - snapv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1" - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - schederrors "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - corev1 "k8s.io/api/core/v1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// GroupSnapshotOps is an interface to perform k8s GroupVolumeSnapshot operations -type GroupSnapshotOps interface { - // GetGroupSnapshot returns the group snapshot for the given name and namespace - GetGroupSnapshot(name, namespace string) (*storkv1alpha1.GroupVolumeSnapshot, error) - // ListGroupSnapshots lists all group snapshots for the given namespace - ListGroupSnapshots(namespace string) (*storkv1alpha1.GroupVolumeSnapshotList, error) - // CreateGroupSnapshot creates the given group snapshot - CreateGroupSnapshot(*storkv1alpha1.GroupVolumeSnapshot) (*storkv1alpha1.GroupVolumeSnapshot, error) - // UpdateGroupSnapshot updates the given group snapshot - UpdateGroupSnapshot(*storkv1alpha1.GroupVolumeSnapshot) (*storkv1alpha1.GroupVolumeSnapshot, error) - // DeleteGroupSnapshot deletes the group snapshot with the given name and namespace - DeleteGroupSnapshot(name, namespace string) error - // ValidateGroupSnapshot checks if the group snapshot with given name and namespace is in ready state - // If retry is true, the validation will be retried with given timeout and retry internal - ValidateGroupSnapshot(name, namespace string, retry bool, timeout, retryInterval time.Duration) error - // GetSnapshotsForGroupSnapshot returns all child snapshots for the group snapshot - GetSnapshotsForGroupSnapshot(name, namespace string) ([]*snapv1.VolumeSnapshot, error) -} - -// GetGroupSnapshot returns the group snapshot for the given name and namespace -func (c *Client) GetGroupSnapshot(name, namespace string) (*storkv1alpha1.GroupVolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().GroupVolumeSnapshots(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListGroupSnapshots lists all group snapshots for the given namespace -func (c *Client) ListGroupSnapshots(namespace string) (*storkv1alpha1.GroupVolumeSnapshotList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().GroupVolumeSnapshots(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// CreateGroupSnapshot creates the given group snapshot -func (c *Client) CreateGroupSnapshot(snap *storkv1alpha1.GroupVolumeSnapshot) (*storkv1alpha1.GroupVolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().GroupVolumeSnapshots(snap.Namespace).Create(context.TODO(), snap, metav1.CreateOptions{}) -} - -// UpdateGroupSnapshot updates the given group snapshot -func (c *Client) UpdateGroupSnapshot(snap *storkv1alpha1.GroupVolumeSnapshot) (*storkv1alpha1.GroupVolumeSnapshot, error) { - return c.stork.StorkV1alpha1().GroupVolumeSnapshots(snap.Namespace).Update(context.TODO(), snap, metav1.UpdateOptions{}) -} - -// DeleteGroupSnapshot deletes the group snapshot with the given name and namespace -func (c *Client) DeleteGroupSnapshot(name, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().GroupVolumeSnapshots(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateGroupSnapshot checks if the group snapshot with given name and namespace is in ready state -// If retry is true, the validation will be retried with given timeout and retry internal -func (c *Client) ValidateGroupSnapshot(name, namespace string, retry bool, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - snap, err := c.GetGroupSnapshot(name, namespace) - if err != nil { - return "", true, err - } - - if len(snap.Status.VolumeSnapshots) == 0 { - return "", true, schederrors.ErrSnapshotNotReady{ - ID: name, - Cause: fmt.Sprintf("group snapshot has 0 child snapshots yet"), - } - } - - if snap.Status.Stage == storkv1alpha1.GroupSnapshotStageFinal { - if snap.Status.Status == storkv1alpha1.GroupSnapshotSuccessful { - // Perform extra check that all child snapshots are also ready - notDoneChildSnaps := make([]string, 0) - for _, childSnap := range snap.Status.VolumeSnapshots { - conditions := childSnap.Conditions - if len(conditions) == 0 { - notDoneChildSnaps = append(notDoneChildSnaps, childSnap.VolumeSnapshotName) - continue - } - - lastCondition := conditions[0] - if lastCondition.Status != corev1.ConditionTrue || lastCondition.Type != snapv1.VolumeSnapshotConditionReady { - notDoneChildSnaps = append(notDoneChildSnaps, childSnap.VolumeSnapshotName) - continue - } - } - - if len(notDoneChildSnaps) > 0 { - return "", false, schederrors.ErrSnapshotFailed{ - ID: name, - Cause: fmt.Sprintf("group snapshot is marked as successfull "+ - " but following child volumesnapshots are in pending or error state: %s", notDoneChildSnaps), - } - } - - return "", false, nil - } - - if snap.Status.Status == storkv1alpha1.GroupSnapshotFailed { - return "", false, schederrors.ErrSnapshotFailed{ - ID: name, - Cause: fmt.Sprintf("group snapshot is in failed state"), - } - } - } - - return "", true, schederrors.ErrSnapshotNotReady{ - ID: name, - Cause: fmt.Sprintf("stage: %s status: %s", snap.Status.Stage, snap.Status.Status), - } - } - - if retry { - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - } else { - if _, _, err := t(); err != nil { - return err - } - } - - return nil -} - -// GetSnapshotsForGroupSnapshot returns all child snapshots for the group snapshot -func (c *Client) GetSnapshotsForGroupSnapshot(name, namespace string) ([]*snapv1.VolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - snap, err := c.GetGroupSnapshot(name, namespace) - if err != nil { - return nil, err - } - - if len(snap.Status.VolumeSnapshots) == 0 { - return nil, fmt.Errorf("group snapshot: [%s] %s does not have any volume snapshots", namespace, name) - } - - snapshots := make([]*snapv1.VolumeSnapshot, 0) - for _, snapStatus := range snap.Status.VolumeSnapshots { - snap, err := c.getSnapshot(snapStatus.VolumeSnapshotName, namespace) - if err != nil { - return nil, err - } - - snapshots = append(snapshots, snap) - } - - return snapshots, nil -} - -func (c *Client) getSnapshot(name string, namespace string) (*snapv1.VolumeSnapshot, error) { - if err := c.initClient(); err != nil { - return nil, err - } - - var result snapv1.VolumeSnapshot - if err := c.snap.Get(). - Name(name). - Resource(snapv1.VolumeSnapshotResourcePlural). - Namespace(namespace). - Do(context.TODO()).Into(&result); err != nil { - return nil, err - } - - return &result, nil -} diff --git a/k8s/stork/migration.go b/k8s/stork/migration.go deleted file mode 100644 index dd8f11be..00000000 --- a/k8s/stork/migration.go +++ /dev/null @@ -1,290 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// MigrationOps is an interface to perform k8s Migration operations -type MigrationOps interface { - // CreateMigration creates the Migration - CreateMigration(*storkv1alpha1.Migration) (*storkv1alpha1.Migration, error) - // GetMigration gets the Migration - GetMigration(string, string) (*storkv1alpha1.Migration, error) - // ListMigrations lists all the Migrations - ListMigrations(string) (*storkv1alpha1.MigrationList, error) - // UpdateMigration updates the Migration - UpdateMigration(*storkv1alpha1.Migration) (*storkv1alpha1.Migration, error) - // DeleteMigration deletes the Migration - DeleteMigration(string, string) error - // ValidateMigration validate the Migration status - ValidateMigration(string, string, time.Duration, time.Duration) error - // GetMigrationSchedule gets the MigrationSchedule - GetMigrationSchedule(string, string) (*storkv1alpha1.MigrationSchedule, error) - // CreateMigrationSchedule creates a MigrationSchedule - CreateMigrationSchedule(*storkv1alpha1.MigrationSchedule) (*storkv1alpha1.MigrationSchedule, error) - // UpdateMigrationSchedule updates the MigrationSchedule - UpdateMigrationSchedule(*storkv1alpha1.MigrationSchedule) (*storkv1alpha1.MigrationSchedule, error) - // ListMigrationSchedules lists all the MigrationSchedules - ListMigrationSchedules(string) (*storkv1alpha1.MigrationScheduleList, error) - // DeleteMigrationSchedule deletes the MigrationSchedule - DeleteMigrationSchedule(string, string) error - // ValidateMigrationSchedule validates the given MigrationSchedule. It checks the status of each of - // the migrations triggered for this schedule and returns a map of successfull migrations. The key of the - // map will be the schedule type and value will be list of migrations for that schedule type. - // The caller is expected to validate if the returned map has all migrations expected at that point of time - ValidateMigrationSchedule(string, string, time.Duration, time.Duration) ( - map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledMigrationStatus, error) - // WatchMigration watch the Migration object - WatchMigration(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error - // WatchMigrationSchedule watch the MigrationSchedule object - WatchMigrationSchedule(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error -} - -// GetMigration gets the Migration -func (c *Client) GetMigration(name string, namespace string) (*storkv1alpha1.Migration, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Migrations(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListMigrations lists all the Migrations -func (c *Client) ListMigrations(namespace string) (*storkv1alpha1.MigrationList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Migrations(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// CreateMigration creates the Migration -func (c *Client) CreateMigration(migration *storkv1alpha1.Migration) (*storkv1alpha1.Migration, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Migrations(migration.Namespace).Create(context.TODO(), migration, metav1.CreateOptions{}) -} - -// DeleteMigration deletes the Migration -func (c *Client) DeleteMigration(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().Migrations(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// UpdateMigration updates the Migration -func (c *Client) UpdateMigration(migration *storkv1alpha1.Migration) (*storkv1alpha1.Migration, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Migrations(migration.Namespace).Update(context.TODO(), migration, metav1.UpdateOptions{}) -} - -// ValidateMigration validate the Migration status -func (c *Client) ValidateMigration(name string, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetMigration(name, namespace) - if err != nil { - return "", true, err - } - - if resp.Status.Status == storkv1alpha1.MigrationStatusSuccessful { - return "", false, nil - } else if resp.Status.Status == storkv1alpha1.MigrationStatusFailed { - return "", false, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("Migration Status %v", resp.Status.Status), - Type: resp, - } - } - - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("Migration Status %v", resp.Status.Status), - Type: resp, - } - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - - return nil -} - -// GetMigrationSchedule gets the MigrationSchedule -func (c *Client) GetMigrationSchedule(name string, namespace string) (*storkv1alpha1.MigrationSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().MigrationSchedules(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListMigrationSchedules lists all the MigrationSchedules -func (c *Client) ListMigrationSchedules(namespace string) (*storkv1alpha1.MigrationScheduleList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().MigrationSchedules(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// CreateMigrationSchedule creates a MigrationSchedule -func (c *Client) CreateMigrationSchedule(migrationSchedule *storkv1alpha1.MigrationSchedule) (*storkv1alpha1.MigrationSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().MigrationSchedules(migrationSchedule.Namespace).Create(context.TODO(), migrationSchedule, metav1.CreateOptions{}) -} - -// UpdateMigrationSchedule updates the MigrationSchedule -func (c *Client) UpdateMigrationSchedule(migrationSchedule *storkv1alpha1.MigrationSchedule) (*storkv1alpha1.MigrationSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().MigrationSchedules(migrationSchedule.Namespace).Update(context.TODO(), migrationSchedule, metav1.UpdateOptions{}) -} - -// DeleteMigrationSchedule deletes the MigrationSchedule -func (c *Client) DeleteMigrationSchedule(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().MigrationSchedules(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateMigrationSchedule validates the given MigrationSchedule. It checks the status of each of -// the migrations triggered for this schedule and returns a map of successfull migrations. The key of the -// map will be the schedule type and value will be list of migrations for that schedule type. -// The caller is expected to validate if the returned map has all migrations expected at that point of time -func (c *Client) ValidateMigrationSchedule(name string, namespace string, timeout, retryInterval time.Duration) ( - map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledMigrationStatus, error) { - if err := c.initClient(); err != nil { - return nil, err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetMigrationSchedule(name, namespace) - if err != nil { - return nil, true, err - } - - if len(resp.Status.Items) == 0 { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("0 migrations have yet run for the migration schedule"), - Type: resp, - } - } - - failedMigrations := make([]string, 0) - pendingMigrations := make([]string, 0) - for _, migrationStatuses := range resp.Status.Items { - // The check below assumes that the status will not have a failed migration if the last one succeeded - // so just get the last status - if len(migrationStatuses) > 0 { - status := migrationStatuses[len(migrationStatuses)-1] - if status == nil { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: "MigrationSchedule has an empty migration in it's most recent status", - Type: resp, - } - } - - if status.Status == storkv1alpha1.MigrationStatusSuccessful { - continue - } - - if status.Status == storkv1alpha1.MigrationStatusFailed { - failedMigrations = append(failedMigrations, - fmt.Sprintf("migration: %s failed. status: %v", status.Name, status.Status)) - } else { - pendingMigrations = append(pendingMigrations, - fmt.Sprintf("migration: %s is not done. status: %v", status.Name, status.Status)) - } - } - } - - if len(failedMigrations) > 0 { - return nil, false, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("MigrationSchedule failed as one or more migrations have failed. %s", - failedMigrations), - Type: resp, - } - } - - if len(pendingMigrations) > 0 { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("MigrationSchedule has certain migrations pending: %s", - pendingMigrations), - Type: resp, - } - } - - return resp.Status.Items, false, nil - } - - ret, err := task.DoRetryWithTimeout(t, timeout, retryInterval) - if err != nil { - return nil, err - } - - migrations, ok := ret.(map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledMigrationStatus) - if !ok { - return nil, fmt.Errorf("invalid type when checking migration schedules: %v", migrations) - } - - return migrations, nil -} - -// WatchMigration sets up a watcher that listens for changes on migration objects -func (c *Client) WatchMigration(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().Migrations(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for migration") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.Migration{}, "", fn, listOptions) - return nil -} - -// WatchMigrationSchedule sets up a watcher that listens for changes on migration schedule objects -func (c *Client) WatchMigrationSchedule(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().MigrationSchedules(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for migrationschedules") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.MigrationSchedule{}, "", fn, listOptions) - return nil -} diff --git a/k8s/stork/namespacedschedulepolicy.go b/k8s/stork/namespacedschedulepolicy.go deleted file mode 100644 index a35c6981..00000000 --- a/k8s/stork/namespacedschedulepolicy.go +++ /dev/null @@ -1,64 +0,0 @@ -package stork - -import ( - "context" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// NamespacedSchedulePolicyOps is an interface to manage NamespacedSchedulePolicy Object -type NamespacedSchedulePolicyOps interface { - // CreateNamespacedSchedulePolicy creates a NamespacedSchedulePolicy - CreateNamespacedSchedulePolicy(*storkv1alpha1.NamespacedSchedulePolicy) (*storkv1alpha1.NamespacedSchedulePolicy, error) - // GetNamespacedSchedulePolicy gets the NamespacedSchedulePolicy - GetNamespacedSchedulePolicy(string, string) (*storkv1alpha1.NamespacedSchedulePolicy, error) - // ListNamespacedSchedulePolicies lists all the NamespacedSchedulePolicies - ListNamespacedSchedulePolicies(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.NamespacedSchedulePolicyList, error) - // UpdateNamespacedSchedulePolicy updates the NamespacedSchedulePolicy - UpdateNamespacedSchedulePolicy(*storkv1alpha1.NamespacedSchedulePolicy) (*storkv1alpha1.NamespacedSchedulePolicy, error) - // DeleteNamespacedSchedulePolicy deletes the NamespacedSchedulePolicy - DeleteNamespacedSchedulePolicy(string, string) error -} - -// CreateNamespacedSchedulePolicy creates a NamespacedSchedulePolicy -func (c *Client) CreateNamespacedSchedulePolicy(schedulePolicy *storkv1alpha1.NamespacedSchedulePolicy) (*storkv1alpha1.NamespacedSchedulePolicy, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().NamespacedSchedulePolicies(schedulePolicy.Namespace).Create(context.TODO(), schedulePolicy, metav1.CreateOptions{}) -} - -// GetNamespacedSchedulePolicy gets the NamespacedSchedulePolicy -func (c *Client) GetNamespacedSchedulePolicy(name string, namespace string) (*storkv1alpha1.NamespacedSchedulePolicy, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().NamespacedSchedulePolicies(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListNamespacedSchedulePolicies lists all the NamespacedSchedulePolicies -func (c *Client) ListNamespacedSchedulePolicies(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.NamespacedSchedulePolicyList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().NamespacedSchedulePolicies(namespace).List(context.TODO(), filterOptions) -} - -// UpdateNamespacedSchedulePolicy updates the NamespacedSchedulePolicy -func (c *Client) UpdateNamespacedSchedulePolicy(schedulePolicy *storkv1alpha1.NamespacedSchedulePolicy) (*storkv1alpha1.NamespacedSchedulePolicy, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().NamespacedSchedulePolicies(schedulePolicy.Namespace).Update(context.TODO(), schedulePolicy, metav1.UpdateOptions{}) -} - -// DeleteNamespacedSchedulePolicy deletes the NamespacedSchedulePolicy -func (c *Client) DeleteNamespacedSchedulePolicy(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().NamespacedSchedulePolicies(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} diff --git a/k8s/stork/platformcredential.go b/k8s/stork/platformcredential.go deleted file mode 100644 index 9c500fb5..00000000 --- a/k8s/stork/platformcredential.go +++ /dev/null @@ -1,113 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// PlatformCredentialOps is an interface to perform k8s PlatformCredential operations -type PlatformCredentialOps interface { - // CreatePlatformCredential creates the PlatformCredential - CreatePlatformCredential(*storkv1alpha1.PlatformCredential) (*storkv1alpha1.PlatformCredential, error) - // GetPlatformCredential gets the PlatformCredential - GetPlatformCredential(string, string) (*storkv1alpha1.PlatformCredential, error) - // ListPlatformCredential lists all the PlatformCredential - ListPlatformCredential(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.PlatformCredentialList, error) - // UpdatePlatformCredential updates the PlatformCredential - UpdatePlatformCredential(*storkv1alpha1.PlatformCredential) (*storkv1alpha1.PlatformCredential, error) - // DeletePlatformCredential deletes the PlatformCredential - DeletePlatformCredential(string, string) error - // ValidatePlatformCredential validates the PlatformCredential - ValidatePlatformCredential(string, string, time.Duration, time.Duration) error -} - -// CreatePlatformCredential creates the PlatformCredential -func (c *Client) CreatePlatformCredential(platformcredential *storkv1alpha1.PlatformCredential) (*storkv1alpha1.PlatformCredential, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().PlatformCredentials(platformcredential.Namespace).Create(context.TODO(), platformcredential, metav1.CreateOptions{}) -} - -// GetPlatformCredential gets the PlatformCredential -func (c *Client) GetPlatformCredential(name string, namespace string) (*storkv1alpha1.PlatformCredential, error) { - if err := c.initClient(); err != nil { - return nil, err - } - platformCredential, err := c.stork.StorkV1alpha1().PlatformCredentials(namespace).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return nil, err - } - - // TODO: use secrets/corev1 client instead of clientset - err = platformCredential.UpdateFromSecret(c.kube) - if err != nil { - return nil, err - } - return platformCredential, nil -} - -// ListPlatformCredential lists all the PlatformCredentials -func (c *Client) ListPlatformCredential(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.PlatformCredentialList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - platformCredentials, err := c.stork.StorkV1alpha1().PlatformCredentials(namespace).List(context.TODO(), filterOptions) - if err != nil { - return nil, err - } - for i := range platformCredentials.Items { - err = platformCredentials.Items[i].UpdateFromSecret(c.kube) - if err != nil { - return nil, err - } - } - return platformCredentials, nil -} - -// UpdatePlatformCredential updates the PlatformCredential -func (c *Client) UpdatePlatformCredential(platformCredential *storkv1alpha1.PlatformCredential) (*storkv1alpha1.PlatformCredential, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().PlatformCredentials(platformCredential.Namespace).Update(context.TODO(), platformCredential, metav1.UpdateOptions{}) -} - -// DeletePlatformCredential deletes the PlatformCredential -func (c *Client) DeletePlatformCredential(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().PlatformCredentials(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidatePlatformCredential validates the platformCredential -func (c *Client) ValidatePlatformCredential(name, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetPlatformCredential(name, namespace) - if err != nil { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("PlatformCredential failed . Error: %v", err), - Type: resp, - } - } - return "", false, nil - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - return nil -} diff --git a/k8s/stork/resourcetransformation.go b/k8s/stork/resourcetransformation.go deleted file mode 100644 index 22ef5bfc..00000000 --- a/k8s/stork/resourcetransformation.go +++ /dev/null @@ -1,113 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// ResourceTransformOps is an interface to perform k8s ResourceTransformOps operations -type ResourceTransformOps interface { - // CreateResourceTransformation creates the ResourceTransformation - CreateResourceTransformation(*storkv1alpha1.ResourceTransformation) (*storkv1alpha1.ResourceTransformation, error) - // GetResourceTransformation gets the ResourceTransformation - GetResourceTransformation(string, string) (*storkv1alpha1.ResourceTransformation, error) - // ListResourceTransformations lists all the ResourceTransformations - ListResourceTransformations(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ResourceTransformationList, error) - // UpdateResourceTransformation updates the ResourceTransformation - UpdateResourceTransformation(*storkv1alpha1.ResourceTransformation) (*storkv1alpha1.ResourceTransformation, error) - // DeleteResourceTransformation deletes the ResourceTransformation - DeleteResourceTransformation(string, string) error - // ValidateResourceTransformation validates resource transformation status - ValidateResourceTransformation(string, string, time.Duration, time.Duration) error -} - -// CreateResourceTransformation creates the ResourceTransformation CR -func (c *Client) CreateResourceTransformation(ResourceTransformation *storkv1alpha1.ResourceTransformation) (*storkv1alpha1.ResourceTransformation, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ResourceTransformations(ResourceTransformation.Namespace).Create(context.TODO(), ResourceTransformation, metav1.CreateOptions{}) -} - -// GetResourceTransformation gets the ResourceTransformation CR -func (c *Client) GetResourceTransformation(name string, namespace string) (*storkv1alpha1.ResourceTransformation, error) { - if err := c.initClient(); err != nil { - return nil, err - } - ResourceTransformation, err := c.stork.StorkV1alpha1().ResourceTransformations(namespace).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return nil, err - } - return ResourceTransformation, nil -} - -// ListResourceTransformations lists all the ResourceTransformations CR -func (c *Client) ListResourceTransformations(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.ResourceTransformationList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - ResourceTransformations, err := c.stork.StorkV1alpha1().ResourceTransformations(namespace).List(context.TODO(), filterOptions) - if err != nil { - return nil, err - } - return ResourceTransformations, nil -} - -// UpdateResourceTransformation updates the ResourceTransformation CR -func (c *Client) UpdateResourceTransformation(ResourceTransformation *storkv1alpha1.ResourceTransformation) (*storkv1alpha1.ResourceTransformation, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().ResourceTransformations(ResourceTransformation.Namespace).Update(context.TODO(), ResourceTransformation, metav1.UpdateOptions{}) -} - -// DeleteResourceTransformation deletes the ResourceTransformation CR -func (c *Client) DeleteResourceTransformation(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().ResourceTransformations(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateResourceTransformation validates ResourceTransformation CR status -func (c *Client) ValidateResourceTransformation(name string, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - transform, err := c.GetResourceTransformation(name, namespace) - if err != nil { - return "", true, err - } - - if transform.Status.Status == storkv1alpha1.ResourceTransformationStatusReady { - return "", false, nil - } else if transform.Status.Status == storkv1alpha1.ResourceTransformationStatusFailed { - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("Status: %v \t Resource Spec: %v", transform.Status.Status, transform.Status.Resources), - Type: transform, - } - } - - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("Status: %v", transform.Status.Status), - Type: transform, - } - } - - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - - return nil -} diff --git a/k8s/stork/rule.go b/k8s/stork/rule.go deleted file mode 100644 index 068dbcca..00000000 --- a/k8s/stork/rule.go +++ /dev/null @@ -1,64 +0,0 @@ -package stork - -import ( - "context" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// RuleOps is an interface to perform operations for k8s stork rule -type RuleOps interface { - // GetRule fetches the given stork rule - GetRule(name, namespace string) (*storkv1alpha1.Rule, error) - // CreateRule creates the given stork rule - CreateRule(rule *storkv1alpha1.Rule) (*storkv1alpha1.Rule, error) - // UpdateRule updates the given stork rule - UpdateRule(rule *storkv1alpha1.Rule) (*storkv1alpha1.Rule, error) - // DeleteRule deletes the given stork rule - DeleteRule(name, namespace string) error - // ListRules returns the list of rules - ListRules(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.RuleList, error) -} - -// GetRule fetches the given stork rule -func (c *Client) GetRule(name, namespace string) (*storkv1alpha1.Rule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Rules(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// CreateRule creates the given stork rule -func (c *Client) CreateRule(rule *storkv1alpha1.Rule) (*storkv1alpha1.Rule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Rules(rule.GetNamespace()).Create(context.TODO(), rule, metav1.CreateOptions{}) -} - -// UpdateRule updates the given stork rule -func (c *Client) UpdateRule(rule *storkv1alpha1.Rule) (*storkv1alpha1.Rule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Rules(rule.GetNamespace()).Update(context.TODO(), rule, metav1.UpdateOptions{}) -} - -// DeleteRule deletes the given stork rule -func (c *Client) DeleteRule(name, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().Rules(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ListRules returns the list of rules -func (c *Client) ListRules(namespace string, filterOptions metav1.ListOptions) (*storkv1alpha1.RuleList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().Rules(namespace).List(context.TODO(), filterOptions) -} diff --git a/k8s/stork/schedulepolicy.go b/k8s/stork/schedulepolicy.go deleted file mode 100644 index 20156f88..00000000 --- a/k8s/stork/schedulepolicy.go +++ /dev/null @@ -1,64 +0,0 @@ -package stork - -import ( - "context" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// SchedulePolicyOps is an interface to manage SchedulePolicy Object -type SchedulePolicyOps interface { - // CreateSchedulePolicy creates a SchedulePolicy - CreateSchedulePolicy(*storkv1alpha1.SchedulePolicy) (*storkv1alpha1.SchedulePolicy, error) - // GetSchedulePolicy gets the SchedulePolicy - GetSchedulePolicy(string) (*storkv1alpha1.SchedulePolicy, error) - // ListSchedulePolicies lists all the SchedulePolicies - ListSchedulePolicies() (*storkv1alpha1.SchedulePolicyList, error) - // UpdateSchedulePolicy updates the SchedulePolicy - UpdateSchedulePolicy(*storkv1alpha1.SchedulePolicy) (*storkv1alpha1.SchedulePolicy, error) - // DeleteSchedulePolicy deletes the SchedulePolicy - DeleteSchedulePolicy(string) error -} - -// CreateSchedulePolicy creates a SchedulePolicy -func (c *Client) CreateSchedulePolicy(schedulePolicy *storkv1alpha1.SchedulePolicy) (*storkv1alpha1.SchedulePolicy, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().SchedulePolicies().Create(context.TODO(), schedulePolicy, metav1.CreateOptions{}) -} - -// GetSchedulePolicy gets the SchedulePolicy -func (c *Client) GetSchedulePolicy(name string) (*storkv1alpha1.SchedulePolicy, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().SchedulePolicies().Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListSchedulePolicies lists all the SchedulePolicies -func (c *Client) ListSchedulePolicies() (*storkv1alpha1.SchedulePolicyList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().SchedulePolicies().List(context.TODO(), metav1.ListOptions{}) -} - -// UpdateSchedulePolicy updates the SchedulePolicy -func (c *Client) UpdateSchedulePolicy(schedulePolicy *storkv1alpha1.SchedulePolicy) (*storkv1alpha1.SchedulePolicy, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().SchedulePolicies().Update(context.TODO(), schedulePolicy, metav1.UpdateOptions{}) -} - -// DeleteSchedulePolicy deletes the SchedulePolicy -func (c *Client) DeleteSchedulePolicy(name string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().SchedulePolicies().Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} diff --git a/k8s/stork/snapshotschedule.go b/k8s/stork/snapshotschedule.go deleted file mode 100644 index ab7de513..00000000 --- a/k8s/stork/snapshotschedule.go +++ /dev/null @@ -1,180 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - snapv1 "github.com/kubernetes-incubator/external-storage/snapshot/pkg/apis/crd/v1" - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// SnapshotScheduleOps is an interface to perform k8s VolumeSnapshotSchedule operations -type SnapshotScheduleOps interface { - // GetSnapshotSchedule gets the SnapshotSchedule - GetSnapshotSchedule(string, string) (*storkv1alpha1.VolumeSnapshotSchedule, error) - // CreateSnapshotSchedule creates a SnapshotSchedule - CreateSnapshotSchedule(*storkv1alpha1.VolumeSnapshotSchedule) (*storkv1alpha1.VolumeSnapshotSchedule, error) - // UpdateSnapshotSchedule updates the SnapshotSchedule - UpdateSnapshotSchedule(*storkv1alpha1.VolumeSnapshotSchedule) (*storkv1alpha1.VolumeSnapshotSchedule, error) - // ListSnapshotSchedules lists all the SnapshotSchedules - ListSnapshotSchedules(string) (*storkv1alpha1.VolumeSnapshotScheduleList, error) - // DeleteSnapshotSchedule deletes the SnapshotSchedule - DeleteSnapshotSchedule(string, string) error - // ValidateSnapshotSchedule validates the given SnapshotSchedule. It checks the status of each of - // the snapshots triggered for this schedule and returns a map of successfull snapshots. The key of the - // map will be the schedule type and value will be list of snapshots for that schedule type. - // The caller is expected to validate if the returned map has all snapshots expected at that point of time - ValidateSnapshotSchedule(string, string, time.Duration, time.Duration) ( - map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledVolumeSnapshotStatus, error) - // sets up a watcher that listens for changes on volume snapshot schedules - WatchVolumeSnapshotSchedule(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error -} - -// CreateSnapshotSchedule creates a SnapshotSchedule -func (c *Client) CreateSnapshotSchedule(snapshotSchedule *storkv1alpha1.VolumeSnapshotSchedule) (*storkv1alpha1.VolumeSnapshotSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotSchedules(snapshotSchedule.Namespace).Create(context.TODO(), snapshotSchedule, metav1.CreateOptions{}) -} - -// GetSnapshotSchedule gets the SnapshotSchedule -func (c *Client) GetSnapshotSchedule(name string, namespace string) (*storkv1alpha1.VolumeSnapshotSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotSchedules(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListSnapshotSchedules lists all the SnapshotSchedules -func (c *Client) ListSnapshotSchedules(namespace string) (*storkv1alpha1.VolumeSnapshotScheduleList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotSchedules(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// UpdateSnapshotSchedule updates the SnapshotSchedule -func (c *Client) UpdateSnapshotSchedule(snapshotSchedule *storkv1alpha1.VolumeSnapshotSchedule) (*storkv1alpha1.VolumeSnapshotSchedule, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotSchedules(snapshotSchedule.Namespace).Update(context.TODO(), snapshotSchedule, metav1.UpdateOptions{}) -} - -// DeleteSnapshotSchedule deletes the SnapshotSchedule -func (c *Client) DeleteSnapshotSchedule(name string, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().VolumeSnapshotSchedules(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{ - PropagationPolicy: &deleteForegroundPolicy, - }) -} - -// ValidateSnapshotSchedule validates the given SnapshotSchedule. It checks the status of each of -// the snapshots triggered for this schedule and returns a map of successfull snapshots. The key of the -// map will be the schedule type and value will be list of snapshots for that schedule type. -// The caller is expected to validate if the returned map has all snapshots expected at that point of time -func (c *Client) ValidateSnapshotSchedule(name string, namespace string, timeout, retryInterval time.Duration) ( - map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledVolumeSnapshotStatus, error) { - if err := c.initClient(); err != nil { - return nil, err - } - t := func() (interface{}, bool, error) { - resp, err := c.GetSnapshotSchedule(name, namespace) - if err != nil { - return nil, true, err - } - - if len(resp.Status.Items) == 0 { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("0 snapshots have yet run for the snapshot schedule"), - Type: resp, - } - } - - failedSnapshots := make([]string, 0) - pendingSnapshots := make([]string, 0) - for _, snapshotStatuses := range resp.Status.Items { - if len(snapshotStatuses) > 0 { - status := snapshotStatuses[len(snapshotStatuses)-1] - if status == nil { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: "SnapshotSchedule has an empty migration in it's most recent status", - Type: resp, - } - } - - if status.Status == snapv1.VolumeSnapshotConditionReady { - continue - } - - if status.Status == snapv1.VolumeSnapshotConditionError { - failedSnapshots = append(failedSnapshots, - fmt.Sprintf("snapshot: %s failed. status: %v", status.Name, status.Status)) - } else { - pendingSnapshots = append(pendingSnapshots, - fmt.Sprintf("snapshot: %s is not done. status: %v", status.Name, status.Status)) - } - } - } - - if len(failedSnapshots) > 0 { - return nil, false, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("SnapshotSchedule failed as one or more snapshots have failed. %s", - failedSnapshots), - Type: resp, - } - } - - if len(pendingSnapshots) > 0 { - return nil, true, &errors.ErrFailedToValidateCustomSpec{ - Name: name, - Cause: fmt.Sprintf("SnapshotSchedule has certain snapshots pending: %s", - pendingSnapshots), - Type: resp, - } - } - - return resp.Status.Items, false, nil - } - - ret, err := task.DoRetryWithTimeout(t, timeout, retryInterval) - if err != nil { - return nil, err - } - - snapshots, ok := ret.(map[storkv1alpha1.SchedulePolicyType][]*storkv1alpha1.ScheduledVolumeSnapshotStatus) - if !ok { - return nil, fmt.Errorf("invalid type when checking snapshot schedules: %v", snapshots) - } - - return snapshots, nil -} - -// WatchVolumeSnapshotSchedule sets up a watcher that listens for changes on volume snapshot schedules -func (c *Client) WatchVolumeSnapshotSchedule(namespace string, fn WatchFunc, listOptions metav1.ListOptions) error { - if err := c.initClient(); err != nil { - return err - } - - listOptions.Watch = true - watchInterface, err := c.stork.StorkV1alpha1().VolumeSnapshotSchedules(namespace).Watch(context.TODO(), listOptions) - if err != nil { - logrus.WithError(err).Error("error invoking the watch api for snapshot schedules") - return err - } - - // fire off watch function - go c.handleWatch(watchInterface, &storkv1alpha1.VolumeSnapshotSchedule{}, "", fn, listOptions) - return nil -} diff --git a/k8s/stork/stork.go b/k8s/stork/stork.go deleted file mode 100644 index 78997423..00000000 --- a/k8s/stork/stork.go +++ /dev/null @@ -1,298 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "os" - "sync" - "time" - - snapclient "github.com/kubernetes-incubator/external-storage/snapshot/pkg/client" - storkv1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - storkclientset "github.com/libopenstorage/stork/pkg/client/clientset/versioned" - "github.com/portworx/sched-ops/k8s/common" - "github.com/portworx/sched-ops/task" - "github.com/sirupsen/logrus" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" - "k8s.io/apimachinery/pkg/runtime" - "k8s.io/apimachinery/pkg/watch" - "k8s.io/client-go/kubernetes" - "k8s.io/client-go/rest" - "k8s.io/client-go/tools/clientcmd" -) - -var ( - instance Ops - once sync.Once - - deleteForegroundPolicy = metav1.DeletePropagationForeground -) - -// Ops is an interface to Stork operations. -type Ops interface { - SnapshotScheduleOps - GroupSnapshotOps - RuleOps - ClusterPairOps - MigrationOps - ClusterDomainsOps - SchedulePolicyOps - ActionOps - NamespacedSchedulePolicyOps - BackupLocationOps - ApplicationBackupRestoreOps - ApplicationCloneOps - VolumeSnapshotRestoreOps - ApplicationRegistrationOps - ResourceTransformOps - PlatformCredentialOps - - // SetConfig sets the config and resets the client - SetConfig(config *rest.Config) - // WatchStorkResources sets up and return resource watch - WatchStorkResources(string, runtime.Object) (watch.Interface, error) -} - -// Instance returns a singleton instance of the client. -func Instance() Ops { - once.Do(func() { - if instance == nil { - instance = &Client{} - } - }) - return instance -} - -// SetInstance replaces the instance with the provided one. Should be used only for testing purposes. -func SetInstance(i Ops) { - instance = i -} - -// New creates a new stork client. -func New(kube kubernetes.Interface, stork storkclientset.Interface, snap rest.Interface) *Client { - return &Client{ - kube: kube, - stork: stork, - snap: snap, - } -} - -// NewForConfig creates a new stork client for the given config. -func NewForConfig(c *rest.Config) (*Client, error) { - kclient, err := kubernetes.NewForConfig(c) - if err != nil { - return nil, err - } - - storkClient, err := storkclientset.NewForConfig(c) - if err != nil { - return nil, err - } - - return &Client{ - kube: kclient, - stork: storkClient, - }, nil -} - -// NewInstanceFromConfigFile returns new instance of client by using given -// config file -func NewInstanceFromConfigFile(config string) (Ops, error) { - newInstance := &Client{} - err := newInstance.loadClientFromKubeconfig(config) - if err != nil { - return nil, err - } - return newInstance, nil -} - -// Client is a wrapper for the stork operator client. -type Client struct { - config *rest.Config - kube kubernetes.Interface - stork storkclientset.Interface - snap rest.Interface -} - -// SetConfig sets the config and resets the client -func (c *Client) SetConfig(cfg *rest.Config) { - c.config = cfg - - c.kube = nil - c.stork = nil - c.snap = nil -} - -// initClient initialize stork clients. -func (c *Client) initClient() error { - if c.stork != nil && c.kube != nil { - return nil - } - - return c.setClient() -} - -// setClient instantiates a client. -func (c *Client) setClient() error { - var err error - - if c.config != nil { - err = c.loadClient() - } else { - kubeconfig := os.Getenv("KUBECONFIG") - if len(kubeconfig) > 0 { - err = c.loadClientFromKubeconfig(kubeconfig) - } else { - err = c.loadClientFromServiceAccount() - } - - } - - return err -} - -// loadClientFromServiceAccount loads a k8s client from a ServiceAccount specified in the pod running px -func (c *Client) loadClientFromServiceAccount() error { - config, err := rest.InClusterConfig() - if err != nil { - return err - } - - c.config = config - return c.loadClient() -} - -func (c *Client) loadClientFromKubeconfig(kubeconfig string) error { - config, err := clientcmd.BuildConfigFromFlags("", kubeconfig) - if err != nil { - return err - } - - c.config = config - return c.loadClient() -} - -func (c *Client) loadClient() error { - if c.config == nil { - return fmt.Errorf("rest config is not provided") - } - - var err error - err = common.SetRateLimiter(c.config) - if err != nil { - return err - } - c.kube, err = kubernetes.NewForConfig(c.config) - if err != nil { - return err - } - - c.stork, err = storkclientset.NewForConfig(c.config) - if err != nil { - return err - } - - c.snap, _, err = snapclient.NewClient(c.config) - if err != nil { - return err - } - - return nil -} - -// WatchStorkResources sets up and return resource watch -func (c *Client) WatchStorkResources(namespace string, object runtime.Object) (watch.Interface, error) { - if err := c.initClient(); err != nil { - return nil, err - } - listOptions := metav1.ListOptions{ - Watch: true, - } - var watchInterface watch.Interface - - var err error - if _, ok := object.(*storkv1.ApplicationBackupList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ApplicationBackups(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.ApplicationBackupScheduleList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ApplicationBackupSchedules(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.ApplicationRestoreList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ApplicationRestores(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.ApplicationCloneList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ApplicationClones(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.ClusterPairList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ClusterPairs(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.ClusterDomainsStatusList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ClusterDomainsStatuses().Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.ApplicationBackupList); ok { - watchInterface, err = c.stork.StorkV1alpha1().ApplicationBackups(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.MigrationList); ok { - watchInterface, err = c.stork.StorkV1alpha1().Migrations(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.MigrationScheduleList); ok { - watchInterface, err = c.stork.StorkV1alpha1().MigrationSchedules(namespace).Watch(context.TODO(), listOptions) - } else if _, ok := object.(*storkv1.VolumeSnapshotRestoreList); ok { - watchInterface, err = c.stork.StorkV1alpha1().VolumeSnapshotRestores(namespace).Watch(context.TODO(), listOptions) - } else { - return nil, fmt.Errorf("unsupported object, %v", object) - } - - if err != nil { - return nil, err - } - return watchInterface, nil -} - -// WatchFunc is a callback provided to the Watch functions -// which is invoked when the given object is changed. -type WatchFunc func(object runtime.Object) error - -// handleWatch is internal function that handles the watch. On channel shutdown (ie. stop watch), -// it'll attempt to reestablish its watch function. -func (c *Client) handleWatch( - watchInterface watch.Interface, - object runtime.Object, - namespace string, - fn WatchFunc, - listOptions metav1.ListOptions) { - defer watchInterface.Stop() - for { - select { - case event, more := <-watchInterface.ResultChan(): - if !more { - logrus.Debug("Kubernetes watch closed (attempting to re-establish)") - - t := func() (interface{}, bool, error) { - var err error - if _, ok := object.(*storkv1.ApplicationBackup); ok { - err = c.WatchApplicationBackup(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.ApplicationRestore); ok { - err = c.WatchApplicationRestore(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.ApplicationBackupSchedule); ok { - err = c.WatchApplicationBackupSchedule(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.ApplicationClone); ok { - err = c.WatchApplicationClone(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.ClusterPair); ok { - err = c.WatchClusterPair(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.Migration); ok { - err = c.WatchMigration(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.MigrationSchedule); ok { - err = c.WatchMigrationSchedule(namespace, fn, listOptions) - } else if _, ok := object.(*storkv1.VolumeSnapshotSchedule); ok { - err = c.WatchVolumeSnapshotSchedule(namespace, fn, listOptions) - } else { - return "", false, fmt.Errorf("unsupported object: %v given to handle watch", object) - } - return "", true, err - } - - if _, err := task.DoRetryWithTimeout(t, 10*time.Minute, 10*time.Second); err != nil { - logrus.WithError(err).Error("Could not re-establish the watch") - } else { - logrus.Debug("watch re-established") - } - return - } - - fn(event.Object) - } - } -} diff --git a/k8s/stork/stork_test.go b/k8s/stork/stork_test.go deleted file mode 100644 index dc1bfcc5..00000000 --- a/k8s/stork/stork_test.go +++ /dev/null @@ -1,13 +0,0 @@ -package stork - -import ( - "testing" - - "github.com/stretchr/testify/require" -) - -func TestInstance(t *testing.T) { - Instance() - - require.NotNil(t, instance, "instance should be initialized") -} diff --git a/k8s/stork/volumesnapshot.go b/k8s/stork/volumesnapshot.go deleted file mode 100644 index 649f50c4..00000000 --- a/k8s/stork/volumesnapshot.go +++ /dev/null @@ -1,98 +0,0 @@ -package stork - -import ( - "context" - "fmt" - "time" - - storkv1alpha1 "github.com/libopenstorage/stork/pkg/apis/stork/v1alpha1" - "github.com/portworx/sched-ops/k8s/errors" - "github.com/portworx/sched-ops/task" - metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" -) - -// VolumeSnapshotRestoreOps is interface to perform isnapshot restore using CRD -type VolumeSnapshotRestoreOps interface { - // CreateVolumeSnapshotRestore restore snapshot to pvc specifed in CRD, if no pvcs defined we restore to - // parent volumes - CreateVolumeSnapshotRestore(snap *storkv1alpha1.VolumeSnapshotRestore) (*storkv1alpha1.VolumeSnapshotRestore, error) - // UpdateVolumeSnapshotRestore updates given volumesnapshorestore CRD - UpdateVolumeSnapshotRestore(snap *storkv1alpha1.VolumeSnapshotRestore) (*storkv1alpha1.VolumeSnapshotRestore, error) - // GetVolumeSnapshotRestore returns details of given restore crd status - GetVolumeSnapshotRestore(name, namespace string) (*storkv1alpha1.VolumeSnapshotRestore, error) - // ListVolumeSnapshotRestore return list of volumesnapshotrestores in given namespaces - ListVolumeSnapshotRestore(namespace string) (*storkv1alpha1.VolumeSnapshotRestoreList, error) - // DeleteVolumeSnapshotRestore delete given volumesnapshotrestore CRD - DeleteVolumeSnapshotRestore(name, namespace string) error - // ValidateVolumeSnapshotRestore validates given volumesnapshotrestore CRD - ValidateVolumeSnapshotRestore(name, namespace string, timeout, retry time.Duration) error -} - -// CreateVolumeSnapshotRestore restore snapshot to pvc specifed in CRD, if no pvcs defined we restore to -// parent volumes -func (c *Client) CreateVolumeSnapshotRestore(snapRestore *storkv1alpha1.VolumeSnapshotRestore) (*storkv1alpha1.VolumeSnapshotRestore, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotRestores(snapRestore.Namespace).Create(context.TODO(), snapRestore, metav1.CreateOptions{}) -} - -// UpdateVolumeSnapshotRestore updates given volumesnapshorestore CRD -func (c *Client) UpdateVolumeSnapshotRestore(snapRestore *storkv1alpha1.VolumeSnapshotRestore) (*storkv1alpha1.VolumeSnapshotRestore, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotRestores(snapRestore.Namespace).Update(context.TODO(), snapRestore, metav1.UpdateOptions{}) -} - -// GetVolumeSnapshotRestore returns details of given restore crd status -func (c *Client) GetVolumeSnapshotRestore(name, namespace string) (*storkv1alpha1.VolumeSnapshotRestore, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotRestores(namespace).Get(context.TODO(), name, metav1.GetOptions{}) -} - -// ListVolumeSnapshotRestore return list of volumesnapshotrestores in given namespaces -func (c *Client) ListVolumeSnapshotRestore(namespace string) (*storkv1alpha1.VolumeSnapshotRestoreList, error) { - if err := c.initClient(); err != nil { - return nil, err - } - return c.stork.StorkV1alpha1().VolumeSnapshotRestores(namespace).List(context.TODO(), metav1.ListOptions{}) -} - -// DeleteVolumeSnapshotRestore delete given volumesnapshotrestore CRD -func (c *Client) DeleteVolumeSnapshotRestore(name, namespace string) error { - if err := c.initClient(); err != nil { - return err - } - return c.stork.StorkV1alpha1().VolumeSnapshotRestores(namespace).Delete(context.TODO(), name, metav1.DeleteOptions{}) -} - -// ValidateVolumeSnapshotRestore validates given volumesnapshotrestore CRD -func (c *Client) ValidateVolumeSnapshotRestore(name, namespace string, timeout, retryInterval time.Duration) error { - if err := c.initClient(); err != nil { - return err - } - t := func() (interface{}, bool, error) { - snapRestore, err := c.stork.StorkV1alpha1().VolumeSnapshotRestores(namespace).Get(context.TODO(), name, metav1.GetOptions{}) - if err != nil { - return "", true, err - } - - if snapRestore.Status.Status == storkv1alpha1.VolumeSnapshotRestoreStatusSuccessful { - return "", false, nil - } - return "", true, &errors.ErrFailedToValidateCustomSpec{ - Name: snapRestore.Name, - Cause: fmt.Sprintf("VolumeSnapshotRestore failed . Error: %v .Expected status: %v Actual status: %v", - err, storkv1alpha1.VolumeSnapshotRestoreStatusSuccessful, snapRestore.Status.Status), - Type: snapRestore, - } - } - if _, err := task.DoRetryWithTimeout(t, timeout, retryInterval); err != nil { - return err - } - - return nil -}