Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] CCE: Refactor CCE Nodepools #771

Merged
merged 7 commits into from
Dec 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions acceptance/openstack/cce/v3/nodepool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,13 +100,17 @@ func TestNodePoolLifecycle(t *testing.T) {
},
}

nodePool, err := nodepools.Create(client, clusterId, createOpts).Extract()
existingNodepools, err := nodepools.List(client, clusterId, nodepools.ListOpts{})
th.AssertNoErr(t, err)
numExistingNodepools := len(existingNodepools)

nodePool, err := nodepools.Create(client, clusterId, createOpts)
th.AssertNoErr(t, err)

nodeId := nodePool.Metadata.Id

th.AssertNoErr(t, golangsdk.WaitFor(1800, func() (bool, error) {
n, err := nodepools.Get(client, clusterId, nodeId).Extract()
n, err := nodepools.Get(client, clusterId, nodeId)
if err != nil {
return false, err
}
Expand All @@ -117,7 +121,11 @@ func TestNodePoolLifecycle(t *testing.T) {
return false, nil
}))

pool, err := nodepools.Get(client, clusterId, nodeId).Extract()
nodepoolList, err := nodepools.List(client, clusterId, nodepools.ListOpts{})
th.AssertNoErr(t, err)
th.AssertEquals(t, numExistingNodepools+1, len(nodepoolList))

pool, err := nodepools.Get(client, clusterId, nodeId)
th.AssertNoErr(t, err)
th.AssertEquals(t, 55, pool.Spec.NodeTemplate.ExtendParam.MaxPods)
// Not supported params by now
Expand All @@ -133,11 +141,11 @@ func TestNodePoolLifecycle(t *testing.T) {
NodeTemplate: nodepools.UpdateNodeTemplate{},
},
}
updatedPool, err := nodepools.Update(client, clusterId, nodeId, updateOpts).Extract()
updatedPool, err := nodepools.Update(client, clusterId, nodeId, updateOpts)
th.AssertNoErr(t, err)
th.AssertEquals(t, "nodepool-test-updated", updatedPool.Metadata.Name)
th.AssertNoErr(t, golangsdk.WaitFor(1800, func() (bool, error) {
n, err := nodepools.Get(client, clusterId, nodeId).Extract()
n, err := nodepools.Get(client, clusterId, nodeId)
if err != nil {
return false, err
}
Expand All @@ -148,10 +156,10 @@ func TestNodePoolLifecycle(t *testing.T) {
return false, nil
}))

th.AssertNoErr(t, nodepools.Delete(client, clusterId, nodeId).ExtractErr())
th.AssertNoErr(t, nodepools.Delete(client, clusterId, nodeId))

err = golangsdk.WaitFor(1800, func() (bool, error) {
_, err := nodepools.Get(client, clusterId, nodeId).Extract()
_, err := nodepools.Get(client, clusterId, nodeId)
if err != nil {
if _, ok := err.(golangsdk.ErrDefault404); ok {
return true, nil
Expand Down
63 changes: 63 additions & 0 deletions openstack/cce/v3/nodepools/Create.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package nodepools

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/nodes"
)

// CreateOpts allows extensions to add additional parameters to the
// Create request.
type CreateOpts struct {
// API type, fixed value Node
Kind string `json:"kind" required:"true"`
// API version, fixed value v3
ApiVersion string `json:"apiversion" required:"true"`
// Metadata required to create a Node Pool
Metadata CreateMetaData `json:"metadata" required:"true"`
// specifications to create a Node Pool
Spec CreateSpec `json:"spec" required:"true"`
}

// CreateMetaData required to create a Node Pool
type CreateMetaData struct {
// Name of the node pool.
Name string `json:"name" required:"true"`
}

// CreateSpec describes Node pools specification
type CreateSpec struct {
// Node pool type. Currently, only `vm`(ECSs) are supported.
Type string `json:"type" required:"true"`
// Node template
NodeTemplate nodes.Spec `json:"nodeTemplate" required:"true"`
// Initial number of expected nodes
InitialNodeCount int `json:"initialNodeCount"`
// Auto scaling parameters
Autoscaling AutoscalingSpec `json:"autoscaling,omitempty"`
// Node management parameters
NodeManagement NodeManagementSpec `json:"nodeManagement,omitempty"`
// Custom security group settings for a node pool
CustomSecurityGroupIds []string `json:"customSecurityGroups,omitempty"`
}

// Create accepts a CreateOpts struct and uses the values to create a new
// logical node pool.
func Create(client *golangsdk.ServiceClient, clusterId string, opts CreateOpts) (*NodePool, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

// POST /api/v3/projects/{project_id}/clusters/{cluster_id}/nodepools
raw, err := client.Post(client.ServiceURL("clusters", clusterId, "nodepools"), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{201},
})
if err != nil {
return nil, err
}

var res NodePool
return &res, extract.Into(raw.Body, &res)
}
19 changes: 19 additions & 0 deletions openstack/cce/v3/nodepools/Delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package nodepools

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
)

// Delete will permanently delete a particular node pool based on its unique ID and cluster ID.
func Delete(client *golangsdk.ServiceClient, clusterId, nodepoolId string) error {
// DELETE /api/v3/projects/{project_id}/clusters/{cluster_id}/nodepools/{nodepool_id}
_, err := client.Delete(client.ServiceURL("clusters", clusterId, "nodepools", nodepoolId), &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
JSONBody: nil,
})
if err != nil {
return err
}
return nil
}
22 changes: 22 additions & 0 deletions openstack/cce/v3/nodepools/Get.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package nodepools

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// Get retrieves a particular node pool based on its unique ID and cluster ID.
func Get(client *golangsdk.ServiceClient, clusterId, nodepoolId string) (*NodePool, error) {
// GET /api/v3/projects/{project_id}/clusters/{cluster_id}/nodepools/{nodepool_id}
raw, err := client.Get(client.ServiceURL("clusters", clusterId, "nodepools", nodepoolId), nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
JSONBody: nil,
})
if err != nil {
return nil, err
}

var res NodePool
return &res, extract.Into(raw.Body, &res)
}
72 changes: 72 additions & 0 deletions openstack/cce/v3/nodepools/List.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package nodepools

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
)

// ListOpts allows the filtering of list data using given parameters.
type ListOpts struct {
Name string `json:"name"`
Uid string `json:"uid"`
Phase string `json:"phase"`
}

// List returns collection of node pools.
func List(client *golangsdk.ServiceClient, clusterID string, opts ListOpts) ([]NodePool, error) {
// GET /api/v3/projects/{project_id}/clusters/{cluster_id}/nodepools
raw, err := client.Get(client.ServiceURL("clusters", clusterID, "nodepools"), nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
MoreHeaders: map[string]string{"Content-Type": "application/json"},
JSONBody: nil,
})
if err != nil {
return nil, err
}

var res ListNodePool
err = extract.Into(raw.Body, &res)
if err != nil {
return nil, err
}

return FilterNodePools(res.NodePools, opts), nil
}

// ListNodePool - Describes the Node Pool Structure of cluster
type ListNodePool struct {
// API type, fixed value "List"
Kind string `json:"kind"`
// API version, fixed value "v3"
Apiversion string `json:"apiVersion"`
// all Node Pools
NodePools []NodePool `json:"items"`
}

// FilterNodes filters a list of Nodes based on the given ListOpts.
func FilterNodePools(nodepools []NodePool, opts ListOpts) []NodePool {
var filteredNodepools []NodePool

for _, nodepool := range nodepools {
if matchesFilters(nodepool, opts) {
filteredNodepools = append(filteredNodepools, nodepool)
}
}
return filteredNodepools
}

// matchesFilters checks if a node satisfies the filtering criteria in ListOpts.
func matchesFilters(nodepool NodePool, opts ListOpts) bool {
// Check each filter explicitly
if opts.Name != "" && nodepool.Metadata.Name != opts.Name {
return false
}
if opts.Uid != "" && nodepool.Metadata.Id != opts.Uid {
return false
}
if opts.Phase != "" && nodepool.Status.Phase != opts.Phase {
return false
}

return true
}
71 changes: 71 additions & 0 deletions openstack/cce/v3/nodepools/Update.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
package nodepools

import (
golangsdk "github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/internal/build"
"github.com/opentelekomcloud/gophertelekomcloud/internal/extract"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/nodes"
)

// UpdateOpts contains all the values needed to update a new node pool
type UpdateOpts struct {
// Metadata required to update a Node Pool
Metadata UpdateMetaData `json:"metadata" required:"true"`
// specifications to update a Node Pool
Spec UpdateSpec `json:"spec,omitempty" required:"true"`
}

// UpdateMetaData required to update a Node Pool
type UpdateMetaData struct {
// Name of the node pool.
Name string `json:"name" required:"true"`
}

// UpdateSpec describes Node pools update specification
type UpdateSpec struct {
// Node template
NodeTemplate UpdateNodeTemplate `json:"nodeTemplate" required:"true"`
// Initial number of expected nodes
InitialNodeCount int `json:"initialNodeCount" required:"true"`
// Auto scaling parameters
Autoscaling UpdateAutoscalingSpec `json:"autoscaling,omitempty"`
}

type UpdateNodeTemplate struct {
// Tag of a Kubernetes node, key value pair format
K8sTags map[string]string `json:"k8sTags,omitempty"`
// taints to created nodes to configure anti-affinity
Taints []nodes.TaintSpec `json:"taints,omitempty"`
}

type UpdateAutoscalingSpec struct {
// Whether to enable auto scaling
Enable bool `json:"enable,omitempty"`
// Minimum number of nodes allowed if auto scaling is enabled
MinNodeCount int `json:"minNodeCount,omitempty"`
// This value must be greater than or equal to the value of minNodeCount
MaxNodeCount int `json:"maxNodeCount,omitempty"`
// Interval between two scaling operations, in minutes
ScaleDownCooldownTime int `json:"scaleDownCooldownTime,omitempty"`
// Weight of a node pool
Priority int `json:"priority,omitempty"`
}

// Update allows node pools to be updated.
func Update(client *golangsdk.ServiceClient, clusterId, nodepoolId string, opts UpdateOpts) (*NodePool, error) {
b, err := build.RequestBody(opts, "")
if err != nil {
return nil, err
}

// PUT /api/v3/projects/{project_id}/clusters/{cluster_id}/nodepools/{nodepool_id}
raw, err := client.Put(client.ServiceURL("clusters", clusterId, "nodepools", nodepoolId), b, nil, &golangsdk.RequestOpts{
OkCodes: []int{200},
})
if err != nil {
return nil, err
}

var res NodePool
return &res, extract.Into(raw.Body, &res)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,9 @@
package nodepools

import (
"github.com/opentelekomcloud/gophertelekomcloud"
"github.com/opentelekomcloud/gophertelekomcloud/openstack/cce/v3/nodes"
)

// ListNodePool - Describes the Node Pool Structure of cluster
type ListNodePool struct {
// API type, fixed value "List"
Kind string `json:"kind"`
// API version, fixed value "v3"
Apiversion string `json:"apiVersion"`
// all Node Pools
NodePools []NodePool `json:"items"`
}

// NodePool - Individual node pools of the cluster
type NodePool struct {
// Node pool type
Expand Down Expand Up @@ -80,55 +69,3 @@ type NodeManagementSpec struct {
// ECS group ID
ServerGroupReference string `json:"serverGroupReference"`
}

type commonResult struct {
golangsdk.Result
}

// Extract is a function that accepts a result and extracts a node pool.
func (r commonResult) Extract() (*NodePool, error) {
var s NodePool
err := r.ExtractInto(&s)
return &s, err
}

// ExtractNodePool is a function that accepts a ListOpts struct, which allows you to filter and sort
// the returned collection for greater efficiency.
func (r commonResult) ExtractNodePool() ([]NodePool, error) {
var s ListNodePool
err := r.ExtractInto(&s)
if err != nil {
return nil, err
}
return s.NodePools, nil
}

// ListResult represents the result of a list operation. Call its ExtractNode
// method to interpret it as a Node Pool.
type ListResult struct {
commonResult
}

// CreateResult represents the result of a create operation. Call its Extract
// method to interpret it as a Node Pool.
type CreateResult struct {
commonResult
}

// GetResult represents the result of a get operation. Call its Extract
// method to interpret it as a Node Pool.
type GetResult struct {
commonResult
}

// UpdateResult represents the result of an update operation. Call its Extract
// method to interpret it as a Node Pool.
type UpdateResult struct {
commonResult
}

// DeleteResult represents the result of a delete operation. Call its ExtractErr
// method to determine if the request succeeded or failed.
type DeleteResult struct {
golangsdk.ErrResult
}
Loading
Loading