Skip to content

Commit

Permalink
fix(kubernetes): add method to list control planes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-github committed Aug 20, 2024
1 parent 3bcc884 commit a4d1e5b
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 2 deletions.
42 changes: 40 additions & 2 deletions xelon/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ type KubernetesClusterHealth struct {
LastCheckingData string `json:"lastCheckingData,omitempty"`
}

type ClusterControlPlane struct {
CPUCoreCount int `json:"control_plane_cpu,omitempty"`
DiskSize int `json:"control_plane_disk,omitempty"`
Memory int `json:"control_plane_ram,omitempty"`
Nodes []ClusterControlPlaneNode `json:"nodes,omitempty"`
}

type ClusterControlPlaneNode struct {
ID string `json:"identifier,omitempty"`
LocalVMID string `json:"localvmid,omitempty"`
Name string `json:"name,omitempty"`
}

type ClusterPool struct {
CPUCoreCount int `json:"cpu,omitempty"`
DiskSize int `json:"disk,omitempty"`
Expand All @@ -46,11 +59,15 @@ func (v KubernetesCluster) String() string {
return Stringify(v)
}

func (v ClusterControlPlane) String() string {
return Stringify(v)
}

func (v ClusterPool) String() string {
return Stringify(v)
}

// List providers information about Kubernetes clusters.
// List provides information about Kubernetes clusters.
func (s *KubernetesService) List(ctx context.Context) ([]KubernetesCluster, *Response, error) {
path := fmt.Sprintf("%v/clusters", kubernetesBasePath)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
Expand All @@ -67,7 +84,28 @@ func (s *KubernetesService) List(ctx context.Context) ([]KubernetesCluster, *Res
return kubernetesClusters, resp, nil
}

// ListClusterPools providers information about cluster pools on Kubernetes cluster.
// ListControlPlanes provides information about control plane on Kubernetes cluster.
func (s *KubernetesService) ListControlPlanes(ctx context.Context, kubernetesClusterID string) (*ClusterControlPlane, *Response, error) {
if kubernetesClusterID == "" {
return nil, nil, ErrEmptyArgument
}

path := fmt.Sprintf("%v/%v/cluster-control-planes", kubernetesBasePath, kubernetesClusterID)
req, err := s.client.NewRequest(http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

clusterControlPlane := new(ClusterControlPlane)
resp, err := s.client.Do(ctx, req, clusterControlPlane)
if err != nil {
return nil, resp, err
}

return clusterControlPlane, resp, nil
}

// ListClusterPools provides information about cluster pools on Kubernetes cluster.
func (s *KubernetesService) ListClusterPools(ctx context.Context, kubernetesClusterID string) ([]ClusterPool, *Response, error) {
if kubernetesClusterID == "" {
return nil, nil, ErrEmptyArgument
Expand Down
40 changes: 40 additions & 0 deletions xelon/kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,46 @@ func TestKubernetes_List(t *testing.T) {
assert.Equal(t, expected, clusters)
}

func TestKubernetes_ListControlPlanes(t *testing.T) {
setup()
defer teardown()

mux.HandleFunc("/kubernetes-talos/abc/cluster-control-planes", func(w http.ResponseWriter, r *http.Request) {
assert.Equal(t, http.MethodGet, r.Method)
_, _ = fmt.Fprint(w, `
{
"control_plane_cpu": 2,
"control_plane_disk": 50,
"control_plane_ram": 4,
"nodes": [
{"identifier":"def","localvmid":"def123","name":"cp-node-1"},
{"identifier":"ghi","localvmid":"ghi456","name":"cp-node-2"}
]
}`)
})
expected := &ClusterControlPlane{
CPUCoreCount: 2,
DiskSize: 50,
Memory: 4,
Nodes: []ClusterControlPlaneNode{
{ID: "def", LocalVMID: "def123", Name: "cp-node-1"},
{ID: "ghi", LocalVMID: "ghi456", Name: "cp-node-2"},
},
}

controlPlanes, _, err := client.Kubernetes.ListControlPlanes(ctx, "abc")

assert.NoError(t, err)
assert.Equal(t, expected, controlPlanes)
}

func TestKubernetes_ListControlPlanes_emptyKubernetesClusterID(t *testing.T) {
_, _, err := client.Kubernetes.ListControlPlanes(ctx, "")

assert.Error(t, err)
assert.Equal(t, ErrEmptyArgument, err)
}

func TestKubernetes_ListClusterPools(t *testing.T) {
setup()
defer teardown()
Expand Down

0 comments on commit a4d1e5b

Please sign in to comment.