Skip to content

Commit

Permalink
Update unstructured conversion of runtime.Object (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
micnncim committed Sep 19, 2020
1 parent 033f116 commit cd097da
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 34 deletions.
26 changes: 13 additions & 13 deletions pkg/determiner/determiner.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,18 @@ func (d *Determiner) DeterminePrune(ctx context.Context, info *cliresource.Info)
return true, nil
}

case kindPod:
pod, err := resource.ObjectToPod(info.Object)
if err != nil {
return false, err
}

if pod.Status.Phase != corev1.PodRunning {
return true, nil
}

case kindPersistentVolume:
volume, err := resource.InfoToPersistentVolume(info)
volume, err := resource.ObjectToPersistentVolume(info.Object)
if err != nil {
return false, err
}
Expand All @@ -134,18 +144,8 @@ func (d *Determiner) DeterminePrune(ctx context.Context, info *cliresource.Info)
return true, nil
}

case kindPod:
pod, err := resource.InfoToPod(info)
if err != nil {
return false, err
}

if pod.Status.Phase != corev1.PodRunning {
return true, nil
}

case kindPodDisruptionBudget:
pdb, err := resource.InfoToPodDisruptionBudget(info)
pdb, err := resource.ObjectToPodDisruptionBudget(info.Object)
if err != nil {
return false, err
}
Expand All @@ -157,7 +157,7 @@ func (d *Determiner) DeterminePrune(ctx context.Context, info *cliresource.Info)
return !used, nil

case kindHorizontalPodAutoscaler:
hpa, err := resource.InfoToHorizontalPodAutoscaler(info)
hpa, err := resource.ObjectToHorizontalPodAutoscaler(info.Object)
if err != nil {
return false, err
}
Expand Down
53 changes: 32 additions & 21 deletions pkg/resource/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
cliresource "k8s.io/cli-runtime/pkg/resource"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
)

var (
unstructuredConverter = runtime.DefaultUnstructuredConverter
)

type Client interface {
ListPods(ctx context.Context, namespace string) ([]*corev1.Pod, error)
ListServiceAccounts(ctx context.Context, namespace string) ([]*corev1.ServiceAccount, error)
Expand Down Expand Up @@ -95,48 +98,56 @@ func (c *client) FindScaleTargetRefObject(ctx context.Context, objectRef *autosc
}
}

func InfoToPod(info *cliresource.Info) (*corev1.Pod, error) {
func ObjectToPod(obj runtime.Object) (*corev1.Pod, error) {
u, err := unstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

var pod corev1.Pod
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(
info.Object.(runtime.Unstructured).UnstructuredContent(),
&pod,
); err != nil {
if err := unstructuredConverter.FromUnstructured(u, &pod); err != nil {
return nil, err
}

return &pod, nil
}

func InfoToPersistentVolume(info *cliresource.Info) (*corev1.PersistentVolume, error) {
func ObjectToPersistentVolume(obj runtime.Object) (*corev1.PersistentVolume, error) {
u, err := unstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

var volume corev1.PersistentVolume
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(
info.Object.(runtime.Unstructured).UnstructuredContent(),
&volume,
); err != nil {
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, &volume); err != nil {
return nil, err
}

return &volume, nil
}

func InfoToPodDisruptionBudget(info *cliresource.Info) (*policyv1beta1.PodDisruptionBudget, error) {
func ObjectToPodDisruptionBudget(obj runtime.Object) (*policyv1beta1.PodDisruptionBudget, error) {
u, err := unstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

var pdb policyv1beta1.PodDisruptionBudget
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(
info.Object.(runtime.Unstructured).UnstructuredContent(),
&pdb,
); err != nil {
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, &pdb); err != nil {
return nil, err
}

return &pdb, nil
}

func InfoToHorizontalPodAutoscaler(info *cliresource.Info) (*autoscalingv1.HorizontalPodAutoscaler, error) {
func ObjectToHorizontalPodAutoscaler(obj runtime.Object) (*autoscalingv1.HorizontalPodAutoscaler, error) {
u, err := unstructuredConverter.ToUnstructured(obj)
if err != nil {
return nil, err
}

var hpa autoscalingv1.HorizontalPodAutoscaler
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(
info.Object.(runtime.Unstructured).UnstructuredContent(),
&hpa,
); err != nil {
if err := runtime.DefaultUnstructuredConverter.FromUnstructured(u, &hpa); err != nil {
return nil, err
}

Expand Down

0 comments on commit cd097da

Please sign in to comment.