diff --git a/internal/adapter/adapter.go b/internal/adapter/adapter.go index 6e8e3e1..788acd9 100644 --- a/internal/adapter/adapter.go +++ b/internal/adapter/adapter.go @@ -14,11 +14,13 @@ import ( "github.com/portainer/k2d/internal/config" "github.com/portainer/k2d/internal/types" "go.uber.org/zap" + storagev1 "k8s.io/api/storage/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/kubernetes/pkg/apis/apps" appsv1 "k8s.io/kubernetes/pkg/apis/apps/v1" "k8s.io/kubernetes/pkg/apis/core" corev1 "k8s.io/kubernetes/pkg/apis/core/v1" + "k8s.io/kubernetes/pkg/apis/storage" ) type ( @@ -106,6 +108,8 @@ func NewKubeDockerAdapter(options *KubeDockerAdapterOptions) (*KubeDockerAdapter appsv1.AddToScheme(scheme) core.AddToScheme(scheme) corev1.AddToScheme(scheme) + storage.AddToScheme(scheme) + storagev1.AddToScheme(scheme) return &KubeDockerAdapter{ cli: cli, diff --git a/internal/adapter/persistentvolume.go b/internal/adapter/persistentvolume.go index 528f3c9..e175bc8 100644 --- a/internal/adapter/persistentvolume.go +++ b/internal/adapter/persistentvolume.go @@ -52,13 +52,25 @@ func (adapter *KubeDockerAdapter) GetPersistentVolume(ctx context.Context, persi return &versionedPersistentVolume, nil } -func (adapter *KubeDockerAdapter) ListPersistentVolumes(ctx context.Context) (core.PersistentVolumeList, error) { +func (adapter *KubeDockerAdapter) ListPersistentVolumes(ctx context.Context) (corev1.PersistentVolumeList, error) { persistentVolumes, err := adapter.listPersistentVolumes(ctx) if err != nil { - return core.PersistentVolumeList{}, fmt.Errorf("unable to list nodes: %w", err) + return corev1.PersistentVolumeList{}, fmt.Errorf("unable to list nodes: %w", err) } - return persistentVolumes, nil + versionedPersistentVolumeList := corev1.PersistentVolumeList{ + TypeMeta: metav1.TypeMeta{ + Kind: "PersistentVolumeList", + APIVersion: "v1", + }, + } + + err = adapter.ConvertK8SResource(&persistentVolumes, &versionedPersistentVolumeList) + if err != nil { + return corev1.PersistentVolumeList{}, fmt.Errorf("unable to convert internal PersistentVolumeList to versioned PersistentVolumeList: %w", err) + } + + return versionedPersistentVolumeList, nil } func (adapter *KubeDockerAdapter) GetPersistentVolumeTable(ctx context.Context) (*metav1.Table, error) { diff --git a/internal/adapter/persistentvolumeclaim.go b/internal/adapter/persistentvolumeclaim.go index d9cc7a2..5d197ee 100644 --- a/internal/adapter/persistentvolumeclaim.go +++ b/internal/adapter/persistentvolumeclaim.go @@ -143,13 +143,25 @@ func (adapter *KubeDockerAdapter) updatePersistentVolumeClaimFromVolume(persiste return &persistentVolumeClaim, nil } -func (adapter *KubeDockerAdapter) ListPersistentVolumeClaims(ctx context.Context, namespaceName string) (core.PersistentVolumeClaimList, error) { +func (adapter *KubeDockerAdapter) ListPersistentVolumeClaims(ctx context.Context, namespaceName string) (corev1.PersistentVolumeClaimList, error) { persistentVolumeClaims, err := adapter.listPersistentVolumeClaims(ctx, namespaceName) if err != nil { - return core.PersistentVolumeClaimList{}, fmt.Errorf("unable to list persistent volume claims: %w", err) + return corev1.PersistentVolumeClaimList{}, fmt.Errorf("unable to list persistent volume claims: %w", err) } - return *persistentVolumeClaims, nil + versionedPersistentVolumeClaimList := corev1.PersistentVolumeClaimList{ + TypeMeta: metav1.TypeMeta{ + Kind: "PersistentVolumeClaimList", + APIVersion: "v1", + }, + } + + err = adapter.ConvertK8SResource(&persistentVolumeClaims, &versionedPersistentVolumeClaimList) + if err != nil { + return corev1.PersistentVolumeClaimList{}, fmt.Errorf("unable to convert internal PersistentVolumeClaimList to versioned PersistentVolumeClaimList: %w", err) + } + + return versionedPersistentVolumeClaimList, nil } func (adapter *KubeDockerAdapter) GetPersistentVolumeClaimTable(ctx context.Context, namespaceName string) (*metav1.Table, error) { @@ -158,13 +170,13 @@ func (adapter *KubeDockerAdapter) GetPersistentVolumeClaimTable(ctx context.Cont return &metav1.Table{}, fmt.Errorf("unable to list persistent volume claim: %w", err) } - return k8s.GenerateTable(persistentVolumeClaims) + return k8s.GenerateTable(&persistentVolumeClaims) } -func (adapter *KubeDockerAdapter) listPersistentVolumeClaims(ctx context.Context, namespaceName string) (*core.PersistentVolumeClaimList, error) { +func (adapter *KubeDockerAdapter) listPersistentVolumeClaims(ctx context.Context, namespaceName string) (core.PersistentVolumeClaimList, error) { configMaps, err := adapter.ListConfigMaps(namespaceName) if err != nil { - return nil, fmt.Errorf("unable to list configmaps: %w", err) + return core.PersistentVolumeClaimList{}, fmt.Errorf("unable to list configmaps: %w", err) } persistentVolumeClaims := core.PersistentVolumeClaimList{ @@ -175,25 +187,21 @@ func (adapter *KubeDockerAdapter) listPersistentVolumeClaims(ctx context.Context } for _, configMap := range configMaps.Items { - persistentVolumeClaimConfigMap, err := adapter.GetConfigMap(configMap.Labels[k2dtypes.PersistentVolumeClaimLabelKey], namespaceName) + persistentVolumeLabelKey := configMap.Labels[k2dtypes.PersistentVolumeClaimLabelKey] - if err != nil { - return nil, fmt.Errorf("unable to get persistent volume claim: %w", err) - } + if persistentVolumeLabelKey != "" { + persistentvolumeClaimLastAppliedConfigLabelKey := configMap.Labels[k2dtypes.PersistentVolumeClaimLastAppliedConfigLabelKey] - if persistentVolumeClaimConfigMap == nil { - continue - } + if persistentvolumeClaimLastAppliedConfigLabelKey != "" { + persistentVolumeClaim, err := adapter.updatePersistentVolumeClaimFromVolume(persistentvolumeClaimLastAppliedConfigLabelKey, &configMap) + if err != nil { + return core.PersistentVolumeClaimList{}, fmt.Errorf("unable to update persistent volume claim from volume: %w", err) + } - if persistentVolumeClaimConfigMap.Labels[k2dtypes.PersistentVolumeClaimLastAppliedConfigLabelKey] != "" { - persistentVolumeClaim, err := adapter.updatePersistentVolumeClaimFromVolume(persistentVolumeClaimConfigMap.Labels[k2dtypes.PersistentVolumeClaimLastAppliedConfigLabelKey], persistentVolumeClaimConfigMap) - if err != nil { - return nil, fmt.Errorf("unable to update persistent volume claim from volume: %w", err) + persistentVolumeClaims.Items = append(persistentVolumeClaims.Items, *persistentVolumeClaim) } - - persistentVolumeClaims.Items = append(persistentVolumeClaims.Items, *persistentVolumeClaim) } } - return &persistentVolumeClaims, nil + return persistentVolumeClaims, nil } diff --git a/internal/adapter/storageclass.go b/internal/adapter/storageclass.go index d484da4..c4315b1 100644 --- a/internal/adapter/storageclass.go +++ b/internal/adapter/storageclass.go @@ -19,27 +19,47 @@ func (adapter *KubeDockerAdapter) GetStorageClass(ctx context.Context, storageCl } reclaimPolicy := corev1.PersistentVolumeReclaimPolicy("Retain") + volumeBindingMode := storagev1.VolumeBindingMode("WaitForFirstConsumer") 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, + }, }, TypeMeta: metav1.TypeMeta{ Kind: "StorageClass", APIVersion: "storage.k8s.io/v1", }, - Provisioner: "local", - ReclaimPolicy: &reclaimPolicy, + Provisioner: "k2d.io/local", + ReclaimPolicy: &reclaimPolicy, + VolumeBindingMode: &volumeBindingMode, }, nil } -func (adapter *KubeDockerAdapter) ListStorageClasses(ctx context.Context) (storage.StorageClassList, error) { +func (adapter *KubeDockerAdapter) ListStorageClasses(ctx context.Context) (storagev1.StorageClassList, error) { storageClassList, err := adapter.listStorageClasses(ctx) if err != nil { - return storage.StorageClassList{}, fmt.Errorf("unable to list storageClasses: %w", err) + return storagev1.StorageClassList{}, fmt.Errorf("unable to list storage classes: %w", err) } - return storageClassList, nil + versionedStorageClassList := storagev1.StorageClassList{ + TypeMeta: metav1.TypeMeta{ + Kind: "StorageClassList", + APIVersion: "storage.k8s.io/v1", + }, + } + + err = adapter.ConvertK8SResource(&storageClassList, &versionedStorageClassList) + if err != nil { + return storagev1.StorageClassList{}, fmt.Errorf("unable to convert internal StorageClassList to versioned StorageClassList: %w", err) + } + + return versionedStorageClassList, nil } func (adapter *KubeDockerAdapter) GetStorageClassTable(ctx context.Context) (*metav1.Table, error) { @@ -55,16 +75,25 @@ func (adapter *KubeDockerAdapter) listStorageClasses(ctx context.Context) (stora storageClasses := []storage.StorageClass{} reclaimPolicy := core.PersistentVolumeReclaimPolicy("Retain") + volumeBindingMode := storage.VolumeBindingMode("WaitForFirstConsumer") + 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: "local", - ReclaimPolicy: &reclaimPolicy, + Provisioner: "k2d.io/local", + ReclaimPolicy: &reclaimPolicy, + VolumeBindingMode: &volumeBindingMode, }) return storage.StorageClassList{