Skip to content

Commit

Permalink
💽 implements persistentVolume backend (cyclops-ui#480)
Browse files Browse the repository at this point in the history
  • Loading branch information
jplourenco1 authored Jul 31, 2024
1 parent 13ff1ee commit 18c4ce8
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
37 changes: 37 additions & 0 deletions cyclops-ctrl/internal/cluster/k8sclient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,8 @@ func (k *KubernetesClient) GetResource(group, version, kind, name, namespace str
return k.mapPod(group, version, kind, name, namespace)
case isConfigMap(group, version, kind):
return k.mapConfigMap(group, version, kind, name, namespace)
case isPersistentVolume(group, version, kind):
return k.mapPersistentVolumes(group, version, kind, name, namespace)
case isPersistentVolumeClaims(group, version, kind):
return k.mapPersistentVolumeClaims(group, version, kind, name, namespace)
case isSecret(group, version, kind):
Expand Down Expand Up @@ -772,6 +774,37 @@ func (k *KubernetesClient) mapPersistentVolumeClaims(group, version, kind, name,
}, nil
}

func (k *KubernetesClient) mapPersistentVolumes(group, version, kind, name, namespace string) (*dto.PersistentVolume, error) {
persistentVolume, err := k.clientset.CoreV1().PersistentVolumes().Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
return nil, err
}

capacity := ""
if persistentVolume.Spec.Capacity != nil && persistentVolume.Spec.Capacity.Storage() != nil {
capacity = persistentVolume.Spec.Capacity.Storage().String()
}

claimRef := ""
if persistentVolume.Spec.ClaimRef != nil && persistentVolume.Spec.ClaimRef.Name != "" {
claimRef = persistentVolume.Spec.ClaimRef.Name
}

return &dto.PersistentVolume{
Group: group,
Version: version,
Kind: kind,
Name: name,
Namespace: namespace,
AccessModes: persistentVolume.Spec.AccessModes,
PersistentVolumeClaim: claimRef,
Capacity: capacity,
ReclaimPolicy: persistentVolume.Spec.PersistentVolumeReclaimPolicy,
StorageClass: persistentVolume.Spec.StorageClassName,
Status: persistentVolume.Status,
}, nil
}

func (k *KubernetesClient) mapSecret(group, version, kind, name, namespace string) (*dto.Secret, error) {
secret, err := k.clientset.CoreV1().Secrets(namespace).Get(context.Background(), name, metav1.GetOptions{})
if err != nil {
Expand Down Expand Up @@ -909,6 +942,10 @@ func isPersistentVolumeClaims(group, version, kind string) bool {
return group == "" && version == "v1" && kind == "PersistentVolumeClaim"
}

func isPersistentVolume(group, version, kind string) bool {
return group == "" && version == "v1" && kind == "PersistentVolume"
}

func isSecret(group, version, kind string) bool {
return group == "" && version == "v1" && kind == "Secret"
}
Expand Down
63 changes: 63 additions & 0 deletions cyclops-ctrl/internal/models/dto/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,69 @@ func (p *PersistentVolumeClaim) SetDeleted(deleted bool) {
p.Deleted = deleted
}

type PersistentVolume struct {
Group string `json:"group"`
Version string `json:"version"`
Kind string `json:"kind"`
Name string `json:"name"`
Namespace string `json:"namespace"`
AccessModes []v1.PersistentVolumeAccessMode `json:"accessmodes"`
Capacity string `json:"capacity"`
PersistentVolumeClaim string `json:"persistentvolumeclaim"`
StorageClass string `json:"storageclass"`
Status v1.PersistentVolumeStatus `json:"status"`
ReclaimPolicy v1.PersistentVolumeReclaimPolicy `json:"reclaimpolicy"`
Deleted bool `json:"deleted"`
}

func (p *PersistentVolume) GetGroupVersionKind() string {
return p.Group + "/" + p.Version + ", Kind=" + p.Kind
}

func (p *PersistentVolume) GetGroup() string {
return p.Group
}

func (p *PersistentVolume) GetVersion() string {
return p.Version
}

func (p *PersistentVolume) GetKind() string {
return p.Kind
}

func (p *PersistentVolume) GetName() string {
return p.Name
}

func (p *PersistentVolume) GetNamespace() string {
return p.Namespace
}

func (p *PersistentVolume) GetDeleted() bool {
return p.Deleted
}

func (p *PersistentVolume) GetCapacity() string {
return p.Capacity
}

func (p *PersistentVolume) GetPersistentVolumeClaim() string {
return p.PersistentVolumeClaim
}

func (p *PersistentVolume) GetStorageClass() string {
return p.StorageClass
}

func (p *PersistentVolume) GetStatus() v1.PersistentVolumeStatus {
return p.Status
}

func (p *PersistentVolume) SetDeleted(deleted bool) {
p.Deleted = deleted
}

type Secret struct {
Group string `json:"group"`
Version string `json:"version"`
Expand Down

0 comments on commit 18c4ce8

Please sign in to comment.