Skip to content

Commit

Permalink
move types to subpackage
Browse files Browse the repository at this point in the history
  • Loading branch information
laverya committed Aug 27, 2020
1 parent 252b5fb commit a579b2e
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
46 changes: 9 additions & 37 deletions kotsadm/pkg/kurl/kurl_nodes.go
Expand Up @@ -6,44 +6,16 @@ import (

"github.com/pkg/errors"
"github.com/replicatedhq/kots/kotsadm/pkg/k8s"
"github.com/replicatedhq/kots/kotsadm/pkg/kurl/types"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
v1 "k8s.io/api/core/v1"
kuberneteserrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

type KurlNodes struct {
Nodes []Node `json:"nodes"`
HA bool `json:"ha"`
IsKurlEnabled bool `json:"isKurlEnabled"`
}

type Node struct {
Name string `json:"name"`
IsConnected bool `json:"isConnected"`
CanDelete bool `json:"canDelete"`
KubeletVersion string `json:"kubeletVersion"`
CPU CapacityAvailable `json:"cpu"`
Memory CapacityAvailable `json:"memory"`
Pods CapacityAvailable `json:"pods"`
Conditions NodeConditions `json:"conditions"`
}

type CapacityAvailable struct {
Capacity float64 `json:"capacity"`
Available float64 `json:"available"`
}

type NodeConditions struct {
MemoryPressure bool `json:"memoryPressure"`
DiskPressure bool `json:"diskPressure"`
PidPressure bool `json:"pidPressure"`
Ready bool `json:"ready"`
}

// GetNodes will get a list of nodes with stats
func GetNodes(client kubernetes.Interface) (*KurlNodes, error) {
func GetNodes(client kubernetes.Interface) (*types.KurlNodes, error) {
nodes, err := client.CoreV1().Nodes().List(context.TODO(), metav1.ListOptions{})
if err != nil {
return nil, errors.Wrap(err, "list nodes")
Expand All @@ -54,12 +26,12 @@ func GetNodes(client kubernetes.Interface) (*KurlNodes, error) {
return nil, errors.Wrap(err, "get metrics client")
}

toReturn := KurlNodes{}
toReturn := types.KurlNodes{}

for _, node := range nodes.Items {
cpuCapacity := CapacityAvailable{}
memoryCapacity := CapacityAvailable{}
podCapacity := CapacityAvailable{}
cpuCapacity := types.CapacityAvailable{}
memoryCapacity := types.CapacityAvailable{}
podCapacity := types.CapacityAvailable{}

memoryCapacity.Capacity = float64(node.Status.Capacity.Memory().Value()) / 1000000000 // capacity in GB

Expand Down Expand Up @@ -94,7 +66,7 @@ func GetNodes(client kubernetes.Interface) (*KurlNodes, error) {
podCapacity.Available = podCapacity.Capacity - podCapacity.Available
}

toReturn.Nodes = append(toReturn.Nodes, Node{
toReturn.Nodes = append(toReturn.Nodes, types.Node{
Name: node.Name,
IsConnected: true,
CanDelete: node.Spec.Unschedulable,
Expand Down Expand Up @@ -128,8 +100,8 @@ func GetNodes(client kubernetes.Interface) (*KurlNodes, error) {
return &toReturn, nil
}

func findNodeConditions(conditions []v1.NodeCondition) NodeConditions {
discoveredConditions := NodeConditions{}
func findNodeConditions(conditions []v1.NodeCondition) types.NodeConditions {
discoveredConditions := types.NodeConditions{}
for _, condition := range conditions {
if condition.Type == "MemoryPressure" {
discoveredConditions.MemoryPressure = condition.Status == v1.ConditionTrue
Expand Down
30 changes: 30 additions & 0 deletions kotsadm/pkg/kurl/types/types.go
@@ -0,0 +1,30 @@
package types

type KurlNodes struct {
Nodes []Node `json:"nodes"`
HA bool `json:"ha"`
IsKurlEnabled bool `json:"isKurlEnabled"`
}

type Node struct {
Name string `json:"name"`
IsConnected bool `json:"isConnected"`
CanDelete bool `json:"canDelete"`
KubeletVersion string `json:"kubeletVersion"`
CPU CapacityAvailable `json:"cpu"`
Memory CapacityAvailable `json:"memory"`
Pods CapacityAvailable `json:"pods"`
Conditions NodeConditions `json:"conditions"`
}

type CapacityAvailable struct {
Capacity float64 `json:"capacity"`
Available float64 `json:"available"`
}

type NodeConditions struct {
MemoryPressure bool `json:"memoryPressure"`
DiskPressure bool `json:"diskPressure"`
PidPressure bool `json:"pidPressure"`
Ready bool `json:"ready"`
}

0 comments on commit a579b2e

Please sign in to comment.