Skip to content

Commit

Permalink
Add pagination for Kubernetes clusters (#34)
Browse files Browse the repository at this point in the history
* (hotfix) changed the kubernetes cluster config

- Modify the kubernetes cluster config to add pagination
- Changed the old kubernetesCluster in favor of kubernetesItem

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>

* (hotfix) fixed comment

- fix comment inside kubernetes.go

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>

* (hotfix) fixed code style

- fix code style in kubernetes.go

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>

* refactor(kubernetes): modify the struct for kubernets object

- modify the struct for kubernets object
- fixed error in code

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm authored Apr 7, 2020
1 parent 77d2996 commit 6ce671a
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 52 deletions.
30 changes: 21 additions & 9 deletions kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type KubernetesInstalledApplication struct {
Configuration map[string]string `json:"configuration,omitempty"`
}

// KubernetesCluster is a Kubernetes k3s cluster
// KubernetesCluster is a Kubernetes item inside the cluster
type KubernetesCluster struct {
ID string `json:"id"`
Name string `json:"name"`
Expand All @@ -59,6 +59,14 @@ type KubernetesCluster struct {
InstalledApplications []KubernetesInstalledApplication `json:"installed_applications"`
}

// PaginatedKubernetesClusters is a Kubernetes k3s cluster
type PaginatedKubernetesClusters struct {
Page int `json:"page"`
PerPage int `json:"per_page"`
Pages int `json:"pages"`
Items []KubernetesCluster `json:"items"`
}

// KubernetesClusterConfig is used to create a new cluster
type KubernetesClusterConfig struct {
Name string `json:"name"`
Expand Down Expand Up @@ -105,13 +113,13 @@ type KubernetesVersion struct {
}

// ListKubernetesClusters returns all cluster of kubernetes in the account
func (c *Client) ListKubernetesClusters() ([]KubernetesCluster, error) {
func (c *Client) ListKubernetesClusters() (*PaginatedKubernetesClusters, error) {
resp, err := c.SendGetRequest("/v2/kubernetes/clusters")
if err != nil {
return nil, err
}

kubernetes := make([]KubernetesCluster, 0)
kubernetes := &PaginatedKubernetesClusters{}
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&kubernetes); err != nil {
return nil, err
}
Expand All @@ -128,7 +136,7 @@ func (c *Client) FindKubernetesCluster(search string) (*KubernetesCluster, error

found := -1

for i, cluster := range clusters {
for i, cluster := range clusters.Items {
if strings.Contains(cluster.ID, search) || strings.Contains(cluster.Name, search) {
if found != -1 {
return nil, fmt.Errorf("unable to find %s because there were multiple matches", search)
Expand All @@ -141,7 +149,7 @@ func (c *Client) FindKubernetesCluster(search string) (*KubernetesCluster, error
return nil, fmt.Errorf("unable to find %s, zero matches", search)
}

return &clusters[found], nil
return &clusters.Items[found], nil
}

// NewKubernetesClusters create a new cluster of kubernetes
Expand All @@ -167,7 +175,9 @@ func (c *Client) GetKubernetesClusters(id string) (*KubernetesCluster, error) {
}

kubernetes := &KubernetesCluster{}
err = json.NewDecoder(bytes.NewReader(resp)).Decode(kubernetes)
if err = json.NewDecoder(bytes.NewReader(resp)).Decode(kubernetes); err != nil {
return nil, err
}
return kubernetes, nil
}

Expand All @@ -186,7 +196,9 @@ func (c *Client) UpdateKubernetesCluster(id string, i *KubernetesClusterConfig)
}

kubernetes := &KubernetesCluster{}
err = json.NewDecoder(bytes.NewReader(resp)).Decode(kubernetes)
if err = json.NewDecoder(bytes.NewReader(resp)).Decode(kubernetes); err != nil {
return nil, err
}
return kubernetes, nil
}

Expand All @@ -198,7 +210,7 @@ func (c *Client) ListKubernetesMarketplaceApplications() ([]KubernetesMarketplac
}

kubernetes := make([]KubernetesMarketplaceApplication, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&kubernetes); err != nil {
if err = json.NewDecoder(bytes.NewReader(resp)).Decode(&kubernetes); err != nil {
return nil, err
}

Expand Down Expand Up @@ -235,7 +247,7 @@ func (c *Client) ListAvailableKubernetesVersions() ([]KubernetesVersion, error)
}

kubernetes := make([]KubernetesVersion, 0)
if err := json.NewDecoder(bytes.NewReader(resp)).Decode(&kubernetes); err != nil {
if err = json.NewDecoder(bytes.NewReader(resp)).Decode(&kubernetes); err != nil {
return nil, err
}

Expand Down
93 changes: 50 additions & 43 deletions kubernetes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (

func TestListKubernetesClusters(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/kubernetes/clusters": `[{
"/v2/kubernetes/clusters": `{"page":1,"per_page":20,"pages":1,"items":[{
"id": "69a23478-a89e-41d2-97b1-6f4c341cee70",
"name": "your-cluster-name",
"version": "2",
Expand Down Expand Up @@ -49,7 +49,7 @@ func TestListKubernetesClusters(t *testing.T) {
"plan": null,
"configuration": {}
}]
}]`,
}]}`,
})
defer server.Close()
got, err := client.ListKubernetesClusters()
Expand All @@ -63,56 +63,63 @@ func TestListKubernetesClusters(t *testing.T) {
createAtInstance, _ := time.Parse(time.RFC3339, "2019-09-23T13:03:00.000+01:00")
updateAt, _ := time.Parse(time.RFC3339, "2019-09-23T13:02:59.000+01:00")

expected := []KubernetesCluster{{
ID: "69a23478-a89e-41d2-97b1-6f4c341cee70",
Name: "your-cluster-name",
Version: "2",
Status: "ACTIVE",
BuiltAt: buildAt,
Ready: true,
NumTargetNode: 1,
TargetNodeSize: "g2.xsmall",
KubeConfig: "YAML_VERSION_OF_KUBECONFIG_HERE\n",
KubernetesVersion: "0.8.1",
APIEndPoint: "https://your.cluster.ip.address:6443",
DNSEntry: "69a23478-a89e-41d2-97b1-6f4c341cee70.k8s.civo.com",
CreatedAt: createAt,
Tags: []string{},
Instances: []KubernetesInstance{{
Hostname: "kube-master-HEXDIGITS",
Size: "g2.xsmall",
Region: "lon1",
CreatedAt: createAtInstance,
Status: "ACTIVE",
FirewallID: "5f0ba9ed-5ca7-4e14-9a09-449a84196d64",
PublicIP: "your.cluster.ip.address",
Tags: []string{"civo-kubernetes:installed", "civo-kubernetes:master"},
}},
InstalledApplications: []KubernetesInstalledApplication{{
Application: "Traefik",
Version: "(default)",
Maintainer: "@Rancher_Labs",
Description: "A reverse proxy/load-balancer that's easy, dynamic, automatic, fast, full-featured, open source, production proven and provides metrics.",
PostInstall: "Some documentation here\n",
URL: "https://traefik.io",
UpdatedAt: updateAt,
Installed: true,
Category: "architecture",
ImageURL: "https://api.civo.com/k3s-marketplace/traefik.png",
Configuration: map[string]string{},
}},
}}
expected := &PaginatedKubernetesClusters{
Page: 1,
PerPage: 20,
Pages: 1,
Items: []KubernetesCluster{
{
ID: "69a23478-a89e-41d2-97b1-6f4c341cee70",
Name: "your-cluster-name",
Version: "2",
Status: "ACTIVE",
BuiltAt: buildAt,
Ready: true,
NumTargetNode: 1,
TargetNodeSize: "g2.xsmall",
KubeConfig: "YAML_VERSION_OF_KUBECONFIG_HERE\n",
KubernetesVersion: "0.8.1",
APIEndPoint: "https://your.cluster.ip.address:6443",
DNSEntry: "69a23478-a89e-41d2-97b1-6f4c341cee70.k8s.civo.com",
CreatedAt: createAt,
Tags: []string{},
Instances: []KubernetesInstance{{
Hostname: "kube-master-HEXDIGITS",
Size: "g2.xsmall",
Region: "lon1",
CreatedAt: createAtInstance,
Status: "ACTIVE",
FirewallID: "5f0ba9ed-5ca7-4e14-9a09-449a84196d64",
PublicIP: "your.cluster.ip.address",
Tags: []string{"civo-kubernetes:installed", "civo-kubernetes:master"},
}},
InstalledApplications: []KubernetesInstalledApplication{{
Application: "Traefik",
Version: "(default)",
Maintainer: "@Rancher_Labs",
Description: "A reverse proxy/load-balancer that's easy, dynamic, automatic, fast, full-featured, open source, production proven and provides metrics.",
PostInstall: "Some documentation here\n",
URL: "https://traefik.io",
UpdatedAt: updateAt,
Installed: true,
Category: "architecture",
ImageURL: "https://api.civo.com/k3s-marketplace/traefik.png",
Configuration: map[string]string{},
}},
},
},
}
if !reflect.DeepEqual(got, expected) {
t.Errorf("Expected %+v, got %+v", expected, got)
}
}

func TestFindKubernetesCluster(t *testing.T) {
client, server, _ := NewClientForTesting(map[string]string{
"/v2/kubernetes/clusters": `[
"/v2/kubernetes/clusters": `{"page":1,"per_page":20,"pages":1,"items":[
{ "id": "69a23478-a89e-41d2-97b1-6f4c341cee70", "name": "your-first-cluster-name", "version": "2", "status": "ACTIVE", "ready": true, "num_target_nodes": 1, "target_nodes_size": "g2.xsmall", "built_at": "2019-09-23T13:04:23.000+01:00", "kubeconfig": "YAML_VERSION_OF_KUBECONFIG_HERE\n", "kubernetes_version": "0.8.1", "api_endpoint": "https://your.cluster.ip.address:6443", "dns_entry": "69a23478-a89e-41d2-97b1-6f4c341cee70.k8s.civo.com", "tags": [], "created_at": "2019-09-23T13:02:59.000+01:00", "instances": [{ "hostname": "kube-master-HEXDIGITS", "size": "g2.xsmall", "region": "lon1", "created_at": "2019-09-23T13:03:00.000+01:00", "status": "ACTIVE", "firewall_id": "5f0ba9ed-5ca7-4e14-9a09-449a84196d64", "public_ip": "your.cluster.ip.address", "tags": ["civo-kubernetes:installed", "civo-kubernetes:master"] }], "installed_applications": [{ "application": "Traefik", "title": null, "version": "(default)", "dependencies": null, "maintainer": "@Rancher_Labs", "description": "A reverse proxy/load-balancer that's easy, dynamic, automatic, fast, full-featured, open source, production proven and provides metrics.", "post_install": "Some documentation here\n", "installed": true, "url": "https://traefik.io", "category": "architecture", "updated_at": "2019-09-23T13:02:59.000+01:00", "image_url": "https://api.civo.com/k3s-marketplace/traefik.png", "plan": null, "configuration": {} }] },
{ "id": "d1cd0b71-5da1-492e-9d0d-a46ccdaae2fa", "name": "your-second-cluster-name", "version": "2", "status": "ACTIVE", "ready": true, "num_target_nodes": 1, "target_nodes_size": "g2.xsmall", "built_at": "2019-09-23T13:04:23.000+01:00", "kubeconfig": "YAML_VERSION_OF_KUBECONFIG_HERE\n", "kubernetes_version": "0.8.1", "api_endpoint": "https://your.cluster.ip.address:6443", "dns_entry": "69a23478-a89e-41d2-97b1-6f4c341cee70.k8s.civo.com", "tags": [], "created_at": "2019-09-23T13:02:59.000+01:00", "instances": [{ "hostname": "kube-master-HEXDIGITS", "size": "g2.xsmall", "region": "lon1", "created_at": "2019-09-23T13:03:00.000+01:00", "status": "ACTIVE", "firewall_id": "5f0ba9ed-5ca7-4e14-9a09-449a84196d64", "public_ip": "your.cluster.ip.address", "tags": ["civo-kubernetes:installed", "civo-kubernetes:master"] }], "installed_applications": [{ "application": "Traefik", "title": null, "version": "(default)", "dependencies": null, "maintainer": "@Rancher_Labs", "description": "A reverse proxy/load-balancer that's easy, dynamic, automatic, fast, full-featured, open source, production proven and provides metrics.", "post_install": "Some documentation here\n", "installed": true, "url": "https://traefik.io", "category": "architecture", "updated_at": "2019-09-23T13:02:59.000+01:00", "image_url": "https://api.civo.com/k3s-marketplace/traefik.png", "plan": null, "configuration": {} }] }
]`,
]}`,
})
defer server.Close()

Expand Down

0 comments on commit 6ce671a

Please sign in to comment.