Skip to content

Commit

Permalink
fix: deployment and pod status for Lens (#54)
Browse files Browse the repository at this point in the history
  • Loading branch information
stevensbkang authored Sep 18, 2023
1 parent bed9ce9 commit dbfc019
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 30 deletions.
4 changes: 4 additions & 0 deletions internal/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -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,
Expand Down
18 changes: 15 additions & 3 deletions internal/adapter/persistentvolume.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
48 changes: 28 additions & 20 deletions internal/adapter/persistentvolumeclaim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand All @@ -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
}
43 changes: 36 additions & 7 deletions internal/adapter/storageclass.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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{
Expand Down

0 comments on commit dbfc019

Please sign in to comment.