Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check for snapshot existence before delete #548

Merged
merged 2 commits into from
Aug 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
18 changes: 7 additions & 11 deletions client/clientset/versioned/typed/stash/v1alpha1/util/recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,9 @@ func TryUpdateRecovery(c cs.StashV1alpha1Interface, meta metav1.ObjectMeta, tran
}

func SetRecoveryStats(c cs.StashV1alpha1Interface, recovery *api.Recovery, path string, d time.Duration, phase api.RecoveryPhase) (*api.Recovery, error) {
out, _, err := PatchRecovery(c, recovery, func(in *api.Recovery) *api.Recovery {
out, err := UpdateRecoveryStatus(c, recovery, func(in *api.RecoveryStatus) *api.RecoveryStatus {
found := false
for _, stats := range in.Status.Stats {
for _, stats := range in.Stats {
if stats.Path == path {
found = true
stats.Duration = d.String()
Expand All @@ -100,7 +100,7 @@ func SetRecoveryStats(c cs.StashV1alpha1Interface, recovery *api.Recovery, path
})
}
return in
})
}, api.EnableStatusSubresource)
return out, err
}

Expand All @@ -114,16 +114,12 @@ func UpdateRecoveryStatus(
return nil, errors.Errorf("invalid value passed for useSubresource: %v", useSubresource)
}

apply := func(x *api.Recovery, copy bool) *api.Recovery {
apply := func(x *api.Recovery) *api.Recovery {
out := &api.Recovery{
TypeMeta: x.TypeMeta,
ObjectMeta: x.ObjectMeta,
Spec: x.Spec,
}
if copy {
out.Status = *transform(in.Status.DeepCopy())
} else {
out.Status = *transform(&in.Status)
Status: *transform(in.Status.DeepCopy()),
}
return out
}
Expand All @@ -134,7 +130,7 @@ func UpdateRecoveryStatus(
err = wait.PollImmediate(kutil.RetryInterval, kutil.RetryTimeout, func() (bool, error) {
attempt++
var e2 error
result, e2 = c.Recoveries(in.Namespace).UpdateStatus(apply(cur, false))
result, e2 = c.Recoveries(in.Namespace).UpdateStatus(apply(cur))
if kerr.IsConflict(e2) {
latest, e3 := c.Recoveries(in.Namespace).Get(in.Name, metav1.GetOptions{})
switch {
Expand All @@ -158,6 +154,6 @@ func UpdateRecoveryStatus(
return
}

result, _, err = PatchRecoveryObject(c, in, apply(in, true))
result, _, err = PatchRecoveryObject(c, in, apply(in))
return
}
2 changes: 1 addition & 1 deletion hack/docker/setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ source "$REPO_ROOT/hack/libbuild/common/public_image.sh"

APPSCODE_ENV=${APPSCODE_ENV:-dev}
IMG=stash
RESTIC_VER=${RESTIC_VER:-0.9.1}
RESTIC_VER=${RESTIC_VER:-0.8.3}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reverts back to 0.8.3 because of restic/restic#1934

RESTIC_BRANCH=${RESTIC_BRANCH:-stash-0.4.2}

DIST=$REPO_ROOT/dist
Expand Down
55 changes: 38 additions & 17 deletions pkg/registry/snapshot/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import (
stash "github.com/appscode/stash/apis/stash/v1alpha1"
"github.com/appscode/stash/client/clientset/versioned"
"github.com/appscode/stash/pkg/util"
"github.com/pkg/errors"
apierrors "k8s.io/apimachinery/pkg/api/errors"
kerr "k8s.io/apimachinery/pkg/api/errors"
metainternalversion "k8s.io/apimachinery/pkg/apis/meta/internalversion"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
Expand Down Expand Up @@ -55,20 +56,21 @@ func (r *REST) GroupVersionKind(containingGV schema.GroupVersion) schema.GroupVe
func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions) (runtime.Object, error) {
ns, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, errors.New("missing namespace")
}
if len(name) < 9 {
return nil, errors.New("invalid snapshot name")
return nil, apierrors.NewBadRequest("missing namespace")
}

repoName, snapshotId, err := util.GetRepoNameAndSnapshotID(name)
if err != nil {
return nil, err
return nil, apierrors.NewBadRequest(err.Error())
}

repo, err := r.stashClient.StashV1alpha1().Repositories(ns).Get(repoName, metav1.GetOptions{})
if err != nil {
return nil, errors.New("respective repository not found. error:" + err.Error())
if kerr.IsNotFound(err) {
return nil, apierrors.NewNotFound(stash.Resource(stash.ResourceSingularRepository), repoName)
} else {
return nil, apierrors.NewInternalError(err)
}
}

snapshots := make([]repositories.Snapshot, 0)
Expand All @@ -78,11 +80,11 @@ func (r *REST) Get(ctx context.Context, name string, options *metav1.GetOptions)
snapshots, err = r.GetSnapshots(repo, []string{snapshotId})
}
if err != nil {
return nil, err
return nil, apierrors.NewInternalError(err)
}

if len(snapshots) == 0 {
return nil, errors.New("no resource found")
return nil, apierrors.NewNotFound(repositories.Resource(repov1alpha1.ResourceSingularSnapshot), name)
}

snapshot := &repositories.Snapshot{}
Expand All @@ -97,12 +99,12 @@ func (r *REST) NewList() runtime.Object {
func (r *REST) List(ctx context.Context, options *metainternalversion.ListOptions) (runtime.Object, error) {
ns, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, errors.New("missing namespace")
return nil, apierrors.NewBadRequest("missing namespace")
}

repos, err := r.stashClient.StashV1alpha1().Repositories(ns).List(metav1.ListOptions{})
if err != nil {
return nil, err
return nil, apierrors.NewInternalError(err)
}

var selectedRepos []stash.Repository
Expand All @@ -127,12 +129,12 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption
if repo.Spec.Backend.Local != nil {
snapshots, err = r.getSnapshotsFromSidecar(&repo, nil)
if err != nil {
return nil, err
return nil, apierrors.NewInternalError(err)
}
} else {
snapshots, err = r.GetSnapshots(&repo, nil)
if err != nil {
return nil, err
return nil, apierrors.NewInternalError(err)
}
}
snapshotList.Items = append(snapshotList.Items, snapshots...)
Expand All @@ -146,24 +148,43 @@ func (r *REST) List(ctx context.Context, options *metainternalversion.ListOption
func (r *REST) Delete(ctx context.Context, name string, options *metav1.DeleteOptions) (runtime.Object, bool, error) {
ns, ok := apirequest.NamespaceFrom(ctx)
if !ok {
return nil, false, errors.New("missing namespace")
return nil, false, apierrors.NewBadRequest("missing namespace")
}
repoName, snapshotId, err := util.GetRepoNameAndSnapshotID(name)
if err != nil {
return nil, false, err
return nil, false, apierrors.NewBadRequest(err.Error())
}
repo, err := r.stashClient.StashV1alpha1().Repositories(ns).Get(repoName, metav1.GetOptions{})
if err != nil {
return nil, false, errors.New("respective repository not found. error:" + err.Error())
if kerr.IsNotFound(err) {
return nil, false, apierrors.NewNotFound(stash.Resource(stash.ResourceSingularRepository), repoName)
} else {
return nil, false, apierrors.NewInternalError(err)
}
}

// first, check if the snapshot exist
snapshots := make([]repositories.Snapshot, 0)
if repo.Spec.Backend.Local != nil {
snapshots, err = r.getSnapshotsFromSidecar(repo, []string{snapshotId})
} else {
snapshots, err = r.GetSnapshots(repo, []string{snapshotId})
}

if err != nil {
return nil, false, apierrors.NewInternalError(err)
} else if len(snapshots) == 0 {
return nil, false, apierrors.NewNotFound(repositories.Resource(repov1alpha1.ResourceSingularSnapshot), name)
}

// delete snapshot
if repo.Spec.Backend.Local != nil {
err = r.forgetSnapshotsFromSidecar(repo, []string{snapshotId})
} else {
err = r.ForgetSnapshots(repo, []string{snapshotId})
}
if err != nil {
return nil, false, err
return nil, false, apierrors.NewInternalError(err)
}

return nil, true, nil
Expand Down
13 changes: 11 additions & 2 deletions pkg/util/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -564,9 +564,18 @@ func GetRepoNameAndSnapshotID(snapshotName string) (repoName, snapshotId string,
err = errors.New("invalid snapshot name")
return
}
snapshotId = snapshotName[len(snapshotName)-SnapshotIDLength:]
tokens := strings.SplitN(snapshotName, "-", 2)
if len(tokens) < 2 {
err = errors.New("invalid snapshot name")
return
}
snapshotId = tokens[len(tokens)-1]
if len(snapshotId) != SnapshotIDLength {
err = errors.New("invalid snapshot name")
return
}

repoName = strings.TrimSuffix(snapshotName, snapshotName[len(snapshotName)-SnapshotIDLengthWithDashPrefix:])
repoName = strings.TrimSuffix(snapshotName, "-"+snapshotId)
return
}

Expand Down