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

refactor: introduce converter function for default storage class #59

Merged
merged 1 commit into from
Sep 19, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions internal/adapter/converter/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import (
"k8s.io/kubernetes/pkg/apis/core"
)

func (converter *DockerAPIConverter) ConvertVolumeToPersistentVolume(volume volume.Volume, pvcConfigMap *corev1.ConfigMap) (*core.PersistentVolume, error) {
func (converter *DockerAPIConverter) ConvertVolumeToPersistentVolume(volume *volume.Volume, pvcConfigMap *corev1.ConfigMap) (core.PersistentVolume, error) {
creationDate, err := time.Parse(time.RFC3339, volume.CreatedAt)
if err != nil {
return nil, fmt.Errorf("unable to parse volume creation date: %w", err)
return core.PersistentVolume{}, fmt.Errorf("unable to parse volume creation date: %w", err)
}

var persistentVolumeClaimReference *core.ObjectReference
Expand All @@ -29,7 +29,7 @@ func (converter *DockerAPIConverter) ConvertVolumeToPersistentVolume(volume volu
}
}

return &core.PersistentVolume{
return core.PersistentVolume{
ObjectMeta: metav1.ObjectMeta{
Name: volume.Name,
CreationTimestamp: metav1.Time{
Expand Down
33 changes: 33 additions & 0 deletions internal/adapter/converter/storageclass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package converter

import (
"time"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/storage"
)

func BuildDefaultStorageClass(startTime time.Time) storage.StorageClass {
reclaimPolicy := core.PersistentVolumeReclaimRetain
volumeBindingMode := storage.VolumeBindingWaitForFirstConsumer

return storage.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "local",
Annotations: map[string]string{
"storageclass.kubernetes.io/is-default-class": "true",
},
CreationTimestamp: metav1.Time{
Time: startTime,
},
},
TypeMeta: metav1.TypeMeta{
Kind: "StorageClass",
APIVersion: "storage.k8s.io/v1",
},
Provisioner: "k2d.io/local",
ReclaimPolicy: &reclaimPolicy,
VolumeBindingMode: &volumeBindingMode,
}
}
8 changes: 4 additions & 4 deletions internal/adapter/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ func (adapter *KubeDockerAdapter) GetPersistentVolume(ctx context.Context, persi
)
}

persistentVolume, err := adapter.converter.ConvertVolumeToPersistentVolume(volume, boundPVCConfigMap)
persistentVolume, err := adapter.converter.ConvertVolumeToPersistentVolume(&volume, boundPVCConfigMap)
if err != nil {
return nil, fmt.Errorf("unable to convert Docker volume to PersistentVolume: %w", err)
}
Expand All @@ -64,7 +64,7 @@ func (adapter *KubeDockerAdapter) GetPersistentVolume(ctx context.Context, persi
},
}

err = adapter.ConvertK8SResource(persistentVolume, &versionedPersistentVolume)
err = adapter.ConvertK8SResource(&persistentVolume, &versionedPersistentVolume)
if err != nil {
return nil, fmt.Errorf("unable to convert internal object to versioned object: %w", err)
}
Expand Down Expand Up @@ -133,12 +133,12 @@ func (adapter *KubeDockerAdapter) listPersistentVolumes(ctx context.Context) (co
)
}

persistentVolume, err := adapter.converter.ConvertVolumeToPersistentVolume(*volume, boundPVCConfigMap)
persistentVolume, err := adapter.converter.ConvertVolumeToPersistentVolume(volume, boundPVCConfigMap)
if err != nil {
return core.PersistentVolumeList{}, fmt.Errorf("unable to convert Docker volume to PersistentVolume: %w", err)
}

persistentVolumes = append(persistentVolumes, *persistentVolume)
persistentVolumes = append(persistentVolumes, persistentVolume)

}

Expand Down
58 changes: 15 additions & 43 deletions internal/adapter/storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ import (
"context"
"fmt"

"github.com/portainer/k2d/internal/adapter/converter"
adaptererr "github.com/portainer/k2d/internal/adapter/errors"
"github.com/portainer/k2d/internal/k8s"
corev1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/kubernetes/pkg/apis/core"
"k8s.io/kubernetes/pkg/apis/storage"
)

Expand All @@ -18,28 +17,21 @@ func (adapter *KubeDockerAdapter) GetStorageClass(ctx context.Context, storageCl
return nil, adaptererr.ErrResourceNotFound
}

reclaimPolicy := corev1.PersistentVolumeReclaimPolicy("Retain")
volumeBindingMode := storagev1.VolumeBindingMode("WaitForFirstConsumer")

// TODO: create in the converter package
return &storagev1.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "local",
Annotations: map[string]string{
"storageclass.kubernetes.io/is-default-class": "true",
},
CreationTimestamp: metav1.Time{
Time: adapter.startTime,
},
},
defaultStorageClass := converter.BuildDefaultStorageClass(adapter.startTime)

versionedStorageClass := storagev1.StorageClass{
TypeMeta: metav1.TypeMeta{
Kind: "StorageClass",
APIVersion: "storage.k8s.io/v1",
},
Provisioner: "k2d.io/local",
ReclaimPolicy: &reclaimPolicy,
VolumeBindingMode: &volumeBindingMode,
}, nil
}

err := adapter.ConvertK8SResource(&defaultStorageClass, &versionedStorageClass)
if err != nil {
return nil, fmt.Errorf("unable to convert internal object to versioned object: %w", err)
}

return &versionedStorageClass, nil
}

func (adapter *KubeDockerAdapter) ListStorageClasses(ctx context.Context) (storagev1.StorageClassList, error) {
Expand Down Expand Up @@ -73,30 +65,10 @@ func (adapter *KubeDockerAdapter) GetStorageClassTable(ctx context.Context) (*me
}

func (adapter *KubeDockerAdapter) listStorageClasses(ctx context.Context) (storage.StorageClassList, error) {
storageClasses := []storage.StorageClass{}
defaultStorageClass := converter.BuildDefaultStorageClass(adapter.startTime)

reclaimPolicy := core.PersistentVolumeReclaimPolicy("Retain")
volumeBindingMode := storage.VolumeBindingMode("WaitForFirstConsumer")

// TODO: introduce a converter method to centralize the default storage class object creation
storageClasses = append(storageClasses, storage.StorageClass{
ObjectMeta: metav1.ObjectMeta{
Name: "local",
Annotations: map[string]string{
"storageclass.kubernetes.io/is-default-class": "true",
},
CreationTimestamp: metav1.Time{
Time: adapter.startTime,
},
},
TypeMeta: metav1.TypeMeta{
Kind: "StorageClass",
APIVersion: "storage.k8s.io/v1",
},
Provisioner: "k2d.io/local",
ReclaimPolicy: &reclaimPolicy,
VolumeBindingMode: &volumeBindingMode,
})
storageClasses := []storage.StorageClass{}
storageClasses = append(storageClasses, defaultStorageClass)

return storage.StorageClassList{
TypeMeta: metav1.TypeMeta{
Expand Down