Skip to content

Commit 967c039

Browse files
authored
fix: skip snapshot fetching for local snapshot restore (#1229)
1 parent 476fb52 commit 967c039

File tree

2 files changed

+14
-20
lines changed

2 files changed

+14
-20
lines changed

pkg/resources/provisioning.cattle.io/v1/cluster/validator.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -810,6 +810,12 @@ func (p *provisioningAdmitter) validateETCDSnapshotRestore(request *admission.Re
810810
return admission.ResponseAllowed(), nil
811811
}
812812

813+
// For "none" restore mode, the snapshot metadata isn't needed,
814+
// so we can allow restores using just the snapshot file name
815+
if newRestore.RestoreRKEConfig == "none" {
816+
return admission.ResponseAllowed(), nil
817+
}
818+
813819
snap, err := p.etcdSnapshotCache.Get(newCluster.Namespace, newRestore.Name)
814820
if apierrors.IsNotFound(err) {
815821
return admission.ResponseBadRequest(
@@ -818,21 +824,13 @@ func (p *provisioningAdmitter) validateETCDSnapshotRestore(request *admission.Re
818824
return nil, fmt.Errorf("failed to get etcd snapshot %s/%s: %w", newCluster.Namespace, newRestore.Name, err)
819825
}
820826

821-
var clusterSpec *v1.ClusterSpec
822-
var decodeErr error
823-
// Only parse snapshot metadata if the restore mode requires it.
824-
if newRestore.RestoreRKEConfig != "none" {
825-
clusterSpec, decodeErr = snapshotutil.ParseSnapshotClusterSpecOrError(snap)
826-
if decodeErr != nil {
827-
return admission.ResponseBadRequest(
828-
fmt.Sprintf("invalid ETCD snapshot metadata for %s/%s: %v", snap.Namespace, snap.Name, decodeErr)), nil
829-
}
827+
clusterSpec, decodeErr := snapshotutil.ParseSnapshotClusterSpecOrError(snap)
828+
if decodeErr != nil {
829+
return admission.ResponseBadRequest(
830+
fmt.Sprintf("invalid ETCD snapshot metadata for %s/%s: %v", snap.Namespace, snap.Name, decodeErr)), nil
830831
}
831832

832833
switch newRestore.RestoreRKEConfig {
833-
case "none":
834-
return admission.ResponseAllowed(), nil
835-
836834
case "kubernetesVersion":
837835
if clusterSpec.KubernetesVersion == "" {
838836
return admission.ResponseBadRequest("snapshot metadata missing KubernetesVersion for kubernetesVersion restore"), nil

pkg/resources/provisioning.cattle.io/v1/cluster/validator_test.go

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3112,14 +3112,10 @@ func TestValidateETCDSnapshotRestore(t *testing.T) {
31123112
expectedDenyMsg: fmt.Sprintf("invalid ETCD snapshot metadata for %s/%s", testNamespace, invalidMetadataSnapshotName),
31133113
},
31143114
{
3115-
name: "should allow restore mode 'none' even with invalid metadata",
3116-
request: baseRequest(),
3117-
oldCluster: baseCluster(),
3118-
newCluster: withRestore(baseCluster(), "none", invalidMetadataSnapshotName),
3119-
mockSetup: func(mockCache *fake.MockCacheInterface[*rkev1.ETCDSnapshot]) {
3120-
mockCache.EXPECT().Get(testNamespace, invalidMetadataSnapshotName).
3121-
Return(invalidMetadataSnapshot, nil)
3122-
},
3115+
name: "should allow restore mode 'none' without fetching snapshot",
3116+
request: baseRequest(),
3117+
oldCluster: baseCluster(),
3118+
newCluster: withRestore(baseCluster(), "none", invalidMetadataSnapshotName),
31233119
expectAllowed: true,
31243120
},
31253121
{

0 commit comments

Comments
 (0)