Skip to content

Commit

Permalink
Merge pull request #87 from BuddhiWathsala/master
Browse files Browse the repository at this point in the history
Fix pvc creation segmentation fault error
  • Loading branch information
pcnfernando authored Aug 29, 2019
2 parents 3b6c9ae + 84b7ca9 commit 4b18b34
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 38 deletions.
51 changes: 18 additions & 33 deletions pkg/controller/siddhiprocess/artifact/artifacts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"k8s.io/apimachinery/pkg/api/resource"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/intstr"
Expand Down Expand Up @@ -229,9 +228,7 @@ func (k *KubeClient) CreateNATS(namespace string) error {
func (k *KubeClient) CreateOrUpdatePVC(
name string,
namespace string,
accessModes []corev1.PersistentVolumeAccessMode,
storage resource.Quantity,
storageClassName string,
pvcSpec corev1.PersistentVolumeClaimSpec,
owner metav1.Object,
) error {
pvc := &corev1.PersistentVolumeClaim{}
Expand All @@ -241,20 +238,22 @@ func (k *KubeClient) CreateOrUpdatePVC(
pvc,
)
if err != nil && apierrors.IsNotFound(err) {
accessGranted := false
for _, accessMode := range accessModes {
if accessMode == corev1.ReadWriteOnce {
accessGranted = true
break
if pvcSpec.AccessModes != nil {
accessGranted := false
for _, accessMode := range pvcSpec.AccessModes {
if accessMode == corev1.ReadWriteOnce {
accessGranted = true
break
}
if accessMode == corev1.ReadWriteMany {
accessGranted = true
break
}
}
if accessMode == corev1.ReadWriteMany {
accessGranted = true
break
if !accessGranted {
return errors.New("Restricted access mode in " + name)
}
}
if !accessGranted {
return errors.New("Restricted access mode in " + name)
}
pvc = &corev1.PersistentVolumeClaim{
TypeMeta: metav1.TypeMeta{
APIVersion: corev1.SchemeGroupVersion.String(),
Expand All @@ -264,22 +263,14 @@ func (k *KubeClient) CreateOrUpdatePVC(
Name: name,
Namespace: namespace,
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: accessModes,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: storage,
},
},
StorageClassName: &storageClassName,
},
Spec: pvcSpec,
}
controllerutil.SetControllerReference(owner, pvc, k.Scheme)
_, err = controllerutil.CreateOrUpdate(
context.TODO(),
k.Client,
pvc,
PVCMutateFunc(accessModes, storage, storageClassName),
PVCMutateFunc(pvcSpec),
)
}
return err
Expand Down Expand Up @@ -529,17 +520,11 @@ func ConfigMapMutateFunc(data map[string]string) controllerutil.MutateFn {

// PVCMutateFunc is the function for update k8s persistence volumes claims gracefully
func PVCMutateFunc(
accessModes []corev1.PersistentVolumeAccessMode,
storage resource.Quantity,
class string,
pvcSpec corev1.PersistentVolumeClaimSpec,
) controllerutil.MutateFn {
return func(obj runtime.Object) error {
pvc := obj.(*corev1.PersistentVolumeClaim)
pvc.Spec.AccessModes = accessModes
pvc.Spec.Resources.Requests = corev1.ResourceList{
corev1.ResourceStorage: storage,
}
pvc.Spec.StorageClassName = &class
pvc.Spec = pvcSpec
return nil
}
}
Expand Down
12 changes: 10 additions & 2 deletions pkg/controller/siddhiprocess/artifact/artifacts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,17 @@ func TestCreateOrUpdatePVC(t *testing.T) {
corev1.ReadWriteOnce,
corev1.ReadWriteMany,
}
storage := resource.MustParse("1Gi")
storageClassName := "standard"
err := kubeClient.CreateOrUpdatePVC(name, namespace, accessModes, storage, storageClassName, sampleDeployment)
pvcSpec := corev1.PersistentVolumeClaimSpec{
AccessModes: accessModes,
StorageClassName: &storageClassName,
Resources: corev1.ResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: *resource.NewQuantity(1*1024*1024*1024, resource.BinarySI),
},
},
}
err := kubeClient.CreateOrUpdatePVC(name, namespace, pvcSpec, sampleDeployment)
if err != nil {
t.Error(err)
}
Expand Down
4 changes: 1 addition & 3 deletions pkg/controller/siddhiprocess/deploymanager/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,7 @@ func (d *DeployManager) Deploy() (operationResult controllerutil.OperationResult
err = d.KubeClient.CreateOrUpdatePVC(
pvcName,
d.SiddhiProcess.Namespace,
d.SiddhiProcess.Spec.PVC.AccessModes,
d.SiddhiProcess.Spec.PVC.Resources.Requests[corev1.ResourceStorage],
*d.SiddhiProcess.Spec.PVC.StorageClassName,
d.SiddhiProcess.Spec.PVC,
d.SiddhiProcess,
)
if err != nil {
Expand Down

0 comments on commit 4b18b34

Please sign in to comment.