Skip to content

Commit 76962a0

Browse files
authored
feat(k8s): add support for ACL routes (scaleway#2290)
1 parent c1e03a8 commit 76962a0

File tree

1 file changed

+261
-0
lines changed

1 file changed

+261
-0
lines changed

api/k8s/v1/k8s_sdk.go

Lines changed: 261 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -872,6 +872,37 @@ type Pool struct {
872872
Region scw.Region `json:"region"`
873873
}
874874

875+
// ACLRuleRequest: acl rule request.
876+
type ACLRuleRequest struct {
877+
// IP: IP subnet to allow.
878+
// Precisely one of IP, ScalewayRanges must be set.
879+
IP *scw.IPNet `json:"ip,omitempty"`
880+
881+
// ScalewayRanges: only one rule with this field set to true can be added.
882+
// Precisely one of IP, ScalewayRanges must be set.
883+
ScalewayRanges *bool `json:"scaleway_ranges,omitempty"`
884+
885+
// Description: description of the ACL.
886+
Description string `json:"description"`
887+
}
888+
889+
// ACLRule: acl rule.
890+
type ACLRule struct {
891+
// ID: ID of the ACL rule.
892+
ID string `json:"id"`
893+
894+
// IP: IP subnet to allow.
895+
// Precisely one of IP, ScalewayRanges must be set.
896+
IP *scw.IPNet `json:"ip,omitempty"`
897+
898+
// ScalewayRanges: only one rule with this field set to true can be added.
899+
// Precisely one of IP, ScalewayRanges must be set.
900+
ScalewayRanges *bool `json:"scaleway_ranges,omitempty"`
901+
902+
// Description: description of the ACL.
903+
Description string `json:"description"`
904+
}
905+
875906
// CreateClusterRequestAutoUpgrade: create cluster request auto upgrade.
876907
type CreateClusterRequestAutoUpgrade struct {
877908
// Enable: defines whether auto upgrade is enabled for the cluster.
@@ -1284,6 +1315,24 @@ type UpdatePoolRequestUpgradePolicy struct {
12841315
MaxSurge *uint32 `json:"max_surge"`
12851316
}
12861317

1318+
// AddClusterACLRulesRequest: add cluster acl rules request.
1319+
type AddClusterACLRulesRequest struct {
1320+
// Region: region to target. If none is passed will use default region from the config.
1321+
Region scw.Region `json:"-"`
1322+
1323+
// ClusterID: ID of the cluster whose ACLs will be added.
1324+
ClusterID string `json:"-"`
1325+
1326+
// ACLs: aCLs to add.
1327+
ACLs []*ACLRuleRequest `json:"acls"`
1328+
}
1329+
1330+
// AddClusterACLRulesResponse: add cluster acl rules response.
1331+
type AddClusterACLRulesResponse struct {
1332+
// Rules: aCLs that were added.
1333+
Rules []*ACLRule `json:"rules"`
1334+
}
1335+
12871336
// AuthExternalNodeRequest: auth external node request.
12881337
type AuthExternalNodeRequest struct {
12891338
// Region: region to target. If none is passed will use default region from the config.
@@ -1417,6 +1466,15 @@ type CreatePoolRequest struct {
14171466
PublicIPDisabled bool `json:"public_ip_disabled"`
14181467
}
14191468

1469+
// DeleteACLRuleRequest: delete acl rule request.
1470+
type DeleteACLRuleRequest struct {
1471+
// Region: region to target. If none is passed will use default region from the config.
1472+
Region scw.Region `json:"-"`
1473+
1474+
// ACLID: ID of the ACL rule to delete.
1475+
ACLID string `json:"-"`
1476+
}
1477+
14201478
// DeleteClusterRequest: delete cluster request.
14211479
type DeleteClusterRequest struct {
14221480
// Region: region to target. If none is passed will use default region from the config.
@@ -1543,6 +1601,49 @@ type GetVersionRequest struct {
15431601
VersionName string `json:"-"`
15441602
}
15451603

1604+
// ListClusterACLRulesRequest: list cluster acl rules request.
1605+
type ListClusterACLRulesRequest struct {
1606+
// Region: region to target. If none is passed will use default region from the config.
1607+
Region scw.Region `json:"-"`
1608+
1609+
// ClusterID: ID of the cluster whose ACLs will be listed.
1610+
ClusterID string `json:"-"`
1611+
1612+
// Page: page number for the returned ACLs.
1613+
Page *int32 `json:"-"`
1614+
1615+
// PageSize: maximum number of ACLs per page.
1616+
PageSize *uint32 `json:"-"`
1617+
}
1618+
1619+
// ListClusterACLRulesResponse: list cluster acl rules response.
1620+
type ListClusterACLRulesResponse struct {
1621+
// TotalCount: total number of ACLs that exist for the cluster.
1622+
TotalCount uint64 `json:"total_count"`
1623+
1624+
// Rules: paginated returned ACLs.
1625+
Rules []*ACLRule `json:"rules"`
1626+
}
1627+
1628+
// UnsafeGetTotalCount should not be used
1629+
// Internal usage only
1630+
func (r *ListClusterACLRulesResponse) UnsafeGetTotalCount() uint64 {
1631+
return r.TotalCount
1632+
}
1633+
1634+
// UnsafeAppend should not be used
1635+
// Internal usage only
1636+
func (r *ListClusterACLRulesResponse) UnsafeAppend(res interface{}) (uint64, error) {
1637+
results, ok := res.(*ListClusterACLRulesResponse)
1638+
if !ok {
1639+
return 0, errors.New("%T type cannot be appended to type %T", res, r)
1640+
}
1641+
1642+
r.Rules = append(r.Rules, results.Rules...)
1643+
r.TotalCount += uint64(len(results.Rules))
1644+
return uint64(len(results.Rules)), nil
1645+
}
1646+
15461647
// ListClusterAvailableTypesRequest: list cluster available types request.
15471648
type ListClusterAvailableTypesRequest struct {
15481649
// Region: region to target. If none is passed will use default region from the config.
@@ -1888,6 +1989,24 @@ type ResetClusterAdminTokenRequest struct {
18881989
ClusterID string `json:"-"`
18891990
}
18901991

1992+
// SetClusterACLRulesRequest: set cluster acl rules request.
1993+
type SetClusterACLRulesRequest struct {
1994+
// Region: region to target. If none is passed will use default region from the config.
1995+
Region scw.Region `json:"-"`
1996+
1997+
// ClusterID: ID of the cluster whose ACLs will be set.
1998+
ClusterID string `json:"-"`
1999+
2000+
// ACLs: aCLs to set.
2001+
ACLs []*ACLRuleRequest `json:"acls"`
2002+
}
2003+
2004+
// SetClusterACLRulesResponse: set cluster acl rules response.
2005+
type SetClusterACLRulesResponse struct {
2006+
// Rules: aCLs that were set.
2007+
Rules []*ACLRule `json:"rules"`
2008+
}
2009+
18912010
// SetClusterTypeRequest: set cluster type request.
18922011
type SetClusterTypeRequest struct {
18932012
// Region: region to target. If none is passed will use default region from the config.
@@ -2443,6 +2562,148 @@ func (s *API) MigrateClusterToSBSCSI(req *MigrateClusterToSBSCSIRequest, opts ..
24432562
return &resp, nil
24442563
}
24452564

2565+
// ListClusterACLRules: List ACLs for a specific cluster.
2566+
func (s *API) ListClusterACLRules(req *ListClusterACLRulesRequest, opts ...scw.RequestOption) (*ListClusterACLRulesResponse, error) {
2567+
var err error
2568+
2569+
if req.Region == "" {
2570+
defaultRegion, _ := s.client.GetDefaultRegion()
2571+
req.Region = defaultRegion
2572+
}
2573+
2574+
defaultPageSize, exist := s.client.GetDefaultPageSize()
2575+
if (req.PageSize == nil || *req.PageSize == 0) && exist {
2576+
req.PageSize = &defaultPageSize
2577+
}
2578+
2579+
query := url.Values{}
2580+
parameter.AddToQuery(query, "page", req.Page)
2581+
parameter.AddToQuery(query, "page_size", req.PageSize)
2582+
2583+
if fmt.Sprint(req.Region) == "" {
2584+
return nil, errors.New("field Region cannot be empty in request")
2585+
}
2586+
2587+
if fmt.Sprint(req.ClusterID) == "" {
2588+
return nil, errors.New("field ClusterID cannot be empty in request")
2589+
}
2590+
2591+
scwReq := &scw.ScalewayRequest{
2592+
Method: "GET",
2593+
Path: "/k8s/v1/regions/" + fmt.Sprint(req.Region) + "/clusters/" + fmt.Sprint(req.ClusterID) + "/acls",
2594+
Query: query,
2595+
}
2596+
2597+
var resp ListClusterACLRulesResponse
2598+
2599+
err = s.client.Do(scwReq, &resp, opts...)
2600+
if err != nil {
2601+
return nil, err
2602+
}
2603+
return &resp, nil
2604+
}
2605+
2606+
// AddClusterACLRules: Add new ACL rules for a specific cluster.
2607+
func (s *API) AddClusterACLRules(req *AddClusterACLRulesRequest, opts ...scw.RequestOption) (*AddClusterACLRulesResponse, error) {
2608+
var err error
2609+
2610+
if req.Region == "" {
2611+
defaultRegion, _ := s.client.GetDefaultRegion()
2612+
req.Region = defaultRegion
2613+
}
2614+
2615+
if fmt.Sprint(req.Region) == "" {
2616+
return nil, errors.New("field Region cannot be empty in request")
2617+
}
2618+
2619+
if fmt.Sprint(req.ClusterID) == "" {
2620+
return nil, errors.New("field ClusterID cannot be empty in request")
2621+
}
2622+
2623+
scwReq := &scw.ScalewayRequest{
2624+
Method: "POST",
2625+
Path: "/k8s/v1/regions/" + fmt.Sprint(req.Region) + "/clusters/" + fmt.Sprint(req.ClusterID) + "/acls",
2626+
}
2627+
2628+
err = scwReq.SetBody(req)
2629+
if err != nil {
2630+
return nil, err
2631+
}
2632+
2633+
var resp AddClusterACLRulesResponse
2634+
2635+
err = s.client.Do(scwReq, &resp, opts...)
2636+
if err != nil {
2637+
return nil, err
2638+
}
2639+
return &resp, nil
2640+
}
2641+
2642+
// SetClusterACLRules: Set new ACL rules for a specific cluster.
2643+
func (s *API) SetClusterACLRules(req *SetClusterACLRulesRequest, opts ...scw.RequestOption) (*SetClusterACLRulesResponse, error) {
2644+
var err error
2645+
2646+
if req.Region == "" {
2647+
defaultRegion, _ := s.client.GetDefaultRegion()
2648+
req.Region = defaultRegion
2649+
}
2650+
2651+
if fmt.Sprint(req.Region) == "" {
2652+
return nil, errors.New("field Region cannot be empty in request")
2653+
}
2654+
2655+
if fmt.Sprint(req.ClusterID) == "" {
2656+
return nil, errors.New("field ClusterID cannot be empty in request")
2657+
}
2658+
2659+
scwReq := &scw.ScalewayRequest{
2660+
Method: "PUT",
2661+
Path: "/k8s/v1/regions/" + fmt.Sprint(req.Region) + "/clusters/" + fmt.Sprint(req.ClusterID) + "/acls",
2662+
}
2663+
2664+
err = scwReq.SetBody(req)
2665+
if err != nil {
2666+
return nil, err
2667+
}
2668+
2669+
var resp SetClusterACLRulesResponse
2670+
2671+
err = s.client.Do(scwReq, &resp, opts...)
2672+
if err != nil {
2673+
return nil, err
2674+
}
2675+
return &resp, nil
2676+
}
2677+
2678+
// DeleteACLRule: Delete an existing ACL.
2679+
func (s *API) DeleteACLRule(req *DeleteACLRuleRequest, opts ...scw.RequestOption) error {
2680+
var err error
2681+
2682+
if req.Region == "" {
2683+
defaultRegion, _ := s.client.GetDefaultRegion()
2684+
req.Region = defaultRegion
2685+
}
2686+
2687+
if fmt.Sprint(req.Region) == "" {
2688+
return errors.New("field Region cannot be empty in request")
2689+
}
2690+
2691+
if fmt.Sprint(req.ACLID) == "" {
2692+
return errors.New("field ACLID cannot be empty in request")
2693+
}
2694+
2695+
scwReq := &scw.ScalewayRequest{
2696+
Method: "DELETE",
2697+
Path: "/k8s/v1/regions/" + fmt.Sprint(req.Region) + "/acls/" + fmt.Sprint(req.ACLID) + "",
2698+
}
2699+
2700+
err = s.client.Do(scwReq, nil, opts...)
2701+
if err != nil {
2702+
return err
2703+
}
2704+
return nil
2705+
}
2706+
24462707
// ListPools: List all the existing pools for a specific Kubernetes cluster.
24472708
func (s *API) ListPools(req *ListPoolsRequest, opts ...scw.RequestOption) (*ListPoolsResponse, error) {
24482709
var err error

0 commit comments

Comments
 (0)