Skip to content

Commit

Permalink
Fix statefulsets volumeClaimTemplates storageClassName after use Chan…
Browse files Browse the repository at this point in the history
…ging PV/PVC Storage Classes (#4375)

* fix statefulsets volumeClaimTemplates storageClassName after use Changing PV/PVC Storage Classes

Signed-off-by: Box-Cube <[email protected]>

* Fix (#4373)

Signed-off-by: Box-Cube <[email protected]>

* Fix StatefulSet volumeClaimTemplates storageClassName(#4373)

Signed-off-by: Box-Cube <[email protected]>

* Fix StatefulSet volumeClaimTemplates storageClassName(#4373)

Signed-off-by: Box-Cube <[email protected]>

* Fix StatefulSet volumeClaimTemplates storageClassName(#4373)

Signed-off-by: Box-Cube <[email protected]>

* Change the isStorageClassExist logic

Signed-off-by: Box-Cube <[email protected]>

* Fix StatefulSet volumeClaimTemplates storageClassName(#4373)

Signed-off-by: Box-Cube <[email protected]>
  • Loading branch information
Box-Cube authored Dec 16, 2021
1 parent e350ce5 commit 69f6c8d
Show file tree
Hide file tree
Showing 5 changed files with 258 additions and 45 deletions.
1 change: 1 addition & 0 deletions changelogs/unreleased/4375-Box-Cube
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix statefulsets volumeClaimTemplates storageClassName when use Changing PV/PVC Storage Classes
62 changes: 62 additions & 0 deletions pkg/builder/statefulset_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
Copyright 2021 the Velero contributors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package builder

import (
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// StatefulSetBuilder builds StatefulSet objects.
type StatefulSetBuilder struct {
object *appsv1.StatefulSet
}

// ForStatefulSet is the constructor for a StatefulSetBuilder.
func ForStatefulSet(ns, name string) *StatefulSetBuilder {
return &StatefulSetBuilder{
object: &appsv1.StatefulSet{
TypeMeta: metav1.TypeMeta{
APIVersion: appsv1.SchemeGroupVersion.String(),
Kind: "StatefulSet",
},
ObjectMeta: metav1.ObjectMeta{
Namespace: ns,
Name: name,
},
Spec: appsv1.StatefulSetSpec{
VolumeClaimTemplates: []corev1.PersistentVolumeClaim{},
},
},
}
}

// Result returns the built StatefulSet.
func (b *StatefulSetBuilder) Result() *appsv1.StatefulSet {
return b.object
}

// StorageClass sets the StatefulSet's VolumeClaimTemplates storage class name.
func (b *StatefulSetBuilder) StorageClass(names ...string) *StatefulSetBuilder {
for _, name := range names {
nameTmp := name
b.object.Spec.VolumeClaimTemplates = append(b.object.Spec.VolumeClaimTemplates,
corev1.PersistentVolumeClaim{Spec: corev1.PersistentVolumeClaimSpec{StorageClassName: &nameTmp}})
}
return b
}
29 changes: 28 additions & 1 deletion pkg/builder/storage_class_builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ import (

// StorageClassBuilder builds StorageClass objects.
type StorageClassBuilder struct {
object *storagev1api.StorageClass
object *storagev1api.StorageClass
objectSlice []*storagev1api.StorageClass
}

// ForStorageClass is the constructor for a StorageClassBuilder.
Expand Down Expand Up @@ -54,3 +55,29 @@ func (b *StorageClassBuilder) ObjectMeta(opts ...ObjectMetaOpt) *StorageClassBui

return b
}

// ForStorageClassSlice is the constructor for a storageClassSlice in StorageClassBuilder.
func ForStorageClassSlice(names ...string) *StorageClassBuilder {
var storageClassSlice []*storagev1api.StorageClass
for _, name := range names {
storageClass := &storagev1api.StorageClass{
TypeMeta: metav1.TypeMeta{
APIVersion: storagev1api.SchemeGroupVersion.String(),
Kind: "StorageClass",
},
ObjectMeta: metav1.ObjectMeta{
Name: name,
},
}
storageClassSlice = append(storageClassSlice, storageClass)
}

return &StorageClassBuilder{
objectSlice: storageClassSlice,
}
}

// SliceResult returns the built StorageClass slice.
func (b *StorageClassBuilder) SliceResult() []*storagev1api.StorageClass {
return b.objectSlice
}
76 changes: 59 additions & 17 deletions pkg/restore/change_storageclass_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,11 @@ import (

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
corev1client "k8s.io/client-go/kubernetes/typed/core/v1"
storagev1client "k8s.io/client-go/kubernetes/typed/storage/v1"

Expand Down Expand Up @@ -55,7 +58,7 @@ func NewChangeStorageClassAction(
// be run for.
func (a *ChangeStorageClassAction) AppliesTo() (velero.ResourceSelector, error) {
return velero.ResourceSelector{
IncludedResources: []string{"persistentvolumeclaims", "persistentvolumes"},
IncludedResources: []string{"persistentvolumeclaims", "persistentvolumes", "statefulsets"},
}, nil
}

Expand Down Expand Up @@ -87,33 +90,72 @@ func (a *ChangeStorageClassAction) Execute(input *velero.RestoreItemActionExecut
"name": obj.GetName(),
})

// use the unstructured helpers here since this code is for both PVs and PVCs, and the
// field names are the same for both types.
storageClass, _, err := unstructured.NestedString(obj.UnstructuredContent(), "spec", "storageClassName")
if err != nil {
return nil, errors.Wrap(err, "error getting item's spec.storageClassName")
// change StatefulSet volumeClaimTemplates storageClassName
if obj.GetKind() == "StatefulSet" {
sts := new(appsv1.StatefulSet)
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(obj.UnstructuredContent(), sts); err != nil {
return nil, err
}

if len(sts.Spec.VolumeClaimTemplates) > 0 {
for index, pvc := range sts.Spec.VolumeClaimTemplates {
exists, newStorageClass, err := a.isStorageClassExist(log, *pvc.Spec.StorageClassName, config)
if err != nil {
return nil, err
} else if !exists {
continue
}

log.Infof("Updating item's storage class name to %s", newStorageClass)
sts.Spec.VolumeClaimTemplates[index].Spec.StorageClassName = &newStorageClass
}

newObj, err := runtime.DefaultUnstructuredConverter.ToUnstructured(sts)
if err != nil {
return nil, errors.Wrap(err, "convert obj to StatefulSet failed")
}
obj.Object = newObj
}
} else {
// use the unstructured helpers here since this code is for both PVs and PVCs, and the
// field names are the same for both types.
storageClass, _, err := unstructured.NestedString(obj.UnstructuredContent(), "spec", "storageClassName")
if err != nil {
return nil, errors.Wrap(err, "error getting item's spec.storageClassName")
}

exists, newStorageClass, err := a.isStorageClassExist(log, storageClass, config)
if err != nil {
return nil, err
} else if !exists {
return velero.NewRestoreItemActionExecuteOutput(input.Item), nil
}

log.Infof("Updating item's storage class name to %s", newStorageClass)

if err := unstructured.SetNestedField(obj.UnstructuredContent(), newStorageClass, "spec", "storageClassName"); err != nil {
return nil, errors.Wrap(err, "unable to set item's spec.storageClassName")
}
}
return velero.NewRestoreItemActionExecuteOutput(obj), nil
}

func (a *ChangeStorageClassAction) isStorageClassExist(log *logrus.Entry, storageClass string, cm *corev1.ConfigMap) (exists bool, newStorageClass string, err error) {
if storageClass == "" {
log.Debug("Item has no storage class specified")
return velero.NewRestoreItemActionExecuteOutput(input.Item), nil
return false, "", nil
}

newStorageClass, ok := config.Data[storageClass]
newStorageClass, ok := cm.Data[storageClass]
if !ok {
log.Debugf("No mapping found for storage class %s", storageClass)
return velero.NewRestoreItemActionExecuteOutput(input.Item), nil
return false, "", nil
}

// validate that new storage class exists
if _, err := a.storageClassClient.Get(context.TODO(), newStorageClass, metav1.GetOptions{}); err != nil {
return nil, errors.Wrapf(err, "error getting storage class %s from API", newStorageClass)
return false, "", errors.Wrapf(err, "error getting storage class %s from API", newStorageClass)
}

log.Infof("Updating item's storage class name to %s", newStorageClass)

if err := unstructured.SetNestedField(obj.UnstructuredContent(), newStorageClass, "spec", "storageClassName"); err != nil {
return nil, errors.Wrap(err, "unable to set item's spec.storageClassName")
}

return velero.NewRestoreItemActionExecuteOutput(obj), nil
return true, newStorageClass, nil
}
Loading

0 comments on commit 69f6c8d

Please sign in to comment.