Skip to content

Commit

Permalink
support ncloud
Browse files Browse the repository at this point in the history
  • Loading branch information
backguynn committed May 2, 2024
1 parent 5123cc7 commit 7f54c12
Show file tree
Hide file tree
Showing 6 changed files with 101 additions and 8 deletions.
15 changes: 15 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ go 1.21
toolchain go1.21.5

require (
github.com/aws/aws-sdk-go-v2/config v1.27.11
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1
github.com/aws/aws-sdk-go-v2/service/ec2 v1.159.1
github.com/go-openapi/errors v0.20.3
github.com/go-openapi/loads v0.21.2
github.com/go-openapi/runtime v0.24.1
Expand All @@ -29,6 +32,17 @@ require (
)

require (
github.com/aws/aws-sdk-go-v2 v1.26.1 // indirect
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 // indirect
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 // indirect
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 // indirect
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 // indirect
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 // indirect
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 // indirect
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 // indirect
github.com/aws/smithy-go v1.20.2 // indirect
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
Expand All @@ -39,6 +53,7 @@ require (
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.2.0 // indirect
github.com/imdario/mergo v0.3.6 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/loxilb-io/sctp v0.0.0-20230519081703-6d1baec82fd4 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
Expand Down
32 changes: 32 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,34 @@ github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk5
github.com/asaskevich/govalidator v0.0.0-20200907205600-7a23bdc65eef/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d h1:Byv0BzEl3/e6D5CLfI0j/7hiIEtvGVFPCZ7Ei2oq8iQ=
github.com/asaskevich/govalidator v0.0.0-20210307081110-f21760c49a8d/go.mod h1:WaHUgvxTVq04UNunO+XhnAqY/wQc+bxr74GqbsZ/Jqw=
github.com/aws/aws-sdk-go-v2 v1.26.1 h1:5554eUqIYVWpU0YmeeYZ0wU64H2VLBs8TlhRB2L+EkA=
github.com/aws/aws-sdk-go-v2 v1.26.1/go.mod h1:ffIFB97e2yNsv4aTSGkqtHnppsIJzw7G7BReUZ3jCXM=
github.com/aws/aws-sdk-go-v2/config v1.27.11 h1:f47rANd2LQEYHda2ddSCKYId18/8BhSRM4BULGmfgNA=
github.com/aws/aws-sdk-go-v2/config v1.27.11/go.mod h1:SMsV78RIOYdve1vf36z8LmnszlRWkwMQtomCAI0/mIE=
github.com/aws/aws-sdk-go-v2/credentials v1.17.11 h1:YuIB1dJNf1Re822rriUOTxopaHHvIq0l/pX3fwO+Tzs=
github.com/aws/aws-sdk-go-v2/credentials v1.17.11/go.mod h1:AQtFPsDH9bI2O+71anW6EKL+NcD7LG3dpKGMV4SShgo=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1 h1:FVJ0r5XTHSmIHJV6KuDmdYhEpvlHpiSd38RQWhut5J4=
github.com/aws/aws-sdk-go-v2/feature/ec2/imds v1.16.1/go.mod h1:zusuAeqezXzAB24LGuzuekqMAEgWkVYukBec3kr3jUg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5 h1:aw39xVGeRWlWx9EzGVnhOR4yOjQDHPQ6o6NmBlscyQg=
github.com/aws/aws-sdk-go-v2/internal/configsources v1.3.5/go.mod h1:FSaRudD0dXiMPK2UjknVwwTYyZMRsHv3TtkabsZih5I=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5 h1:PG1F3OD1szkuQPzDw3CIQsRIrtTlUC3lP84taWzHlq0=
github.com/aws/aws-sdk-go-v2/internal/endpoints/v2 v2.6.5/go.mod h1:jU1li6RFryMz+so64PpKtudI+QzbKoIEivqdf6LNpOc=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0 h1:hT8rVHwugYE2lEfdFE0QWVo81lF7jMrYJVDWI+f+VxU=
github.com/aws/aws-sdk-go-v2/internal/ini v1.8.0/go.mod h1:8tu/lYfQfFe6IGnaOdrpVgEL2IrrDOf6/m9RQum4NkY=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.159.1 h1:ZwDFKZ40MQyVmHAAEmSe63FPKkMhNO//Yom+WvdszfA=
github.com/aws/aws-sdk-go-v2/service/ec2 v1.159.1/go.mod h1:xejKuuRDjz6z5OqyeLsz01MlOqqW7CqpAB4PabNvpu8=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2 h1:Ji0DY1xUsUr3I8cHps0G+XM3WWU16lP6yG8qu1GAZAs=
github.com/aws/aws-sdk-go-v2/service/internal/accept-encoding v1.11.2/go.mod h1:5CsjAbs3NlGQyZNFACh+zztPDI7fU6eW9QsxjfnuBKg=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7 h1:ogRAwT1/gxJBcSWDMZlgyFUM962F51A5CRhDLbxLdmo=
github.com/aws/aws-sdk-go-v2/service/internal/presigned-url v1.11.7/go.mod h1:YCsIZhXfRPLFFCl5xxY+1T9RKzOKjCut+28JSX2DnAk=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5 h1:vN8hEbpRnL7+Hopy9dzmRle1xmDc7o8tmY0klsr175w=
github.com/aws/aws-sdk-go-v2/service/sso v1.20.5/go.mod h1:qGzynb/msuZIE8I75DVRCUXw3o3ZyBmUvMwQ2t/BrGM=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4 h1:Jux+gDDyi1Lruk+KHF91tK2KCuY61kzoCpvtvJJBtOE=
github.com/aws/aws-sdk-go-v2/service/ssooidc v1.23.4/go.mod h1:mUYPBhaF2lGiukDEjJX2BLRRKTmoUSitGDUgM4tRxak=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6 h1:cwIxeBttqPN3qkaAjcEcsh8NYr8n2HZPkcKgPAi1phU=
github.com/aws/aws-sdk-go-v2/service/sts v1.28.6/go.mod h1:FZf1/nKNEkHdGGJP/cI2MoIMquumuRK6ol3QQJNDxmw=
github.com/aws/smithy-go v1.20.2 h1:tbp628ireGtzcHDDmLT/6ADHidqnwgF57XOXZe6tp4Q=
github.com/aws/smithy-go v1.20.2/go.mod h1:krry+ya/rV9RDcV/Q16kpu6ypI4K2czasz0NC3qS14E=
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
Expand Down Expand Up @@ -228,6 +256,10 @@ github.com/imdario/mergo v0.3.6/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJ
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/jessevdk/go-flags v1.5.0 h1:1jKYvbxEjfUl0fmqTCOfonvskHHXMjBySTLW4y9LFvc=
github.com/jessevdk/go-flags v1.5.0/go.mod h1:Fw0T6WPc1dYxT4mKEZRfG5kJhaTDP9pj1c2EWnYs/m4=
github.com/jmespath/go-jmespath v0.4.0 h1:BEgLn5cpjn8UN1mAw4NjwDrS35OdebyEtFe+9YPoQUg=
github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo=
github.com/jmespath/go-jmespath/internal/testify v1.5.1 h1:shLQSRRSCCPj3f2gpwzGwWFoC7ycTf1rcQZHOlsJ6N8=
github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/josharian/intern v1.0.0 h1:vlS4z54oSdjm0bgjRigI+G1HpF+tI+9rE5LLzOg8HmY=
github.com/josharian/intern v1.0.0/go.mod h1:5DoeVV0s6jJacbCEi61lwdGj/aVlrQvzHFFd8Hwg//Y=
Expand Down
2 changes: 1 addition & 1 deletion options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,5 @@ var Opts struct {
IPVSCompat bool `long:"ipvs-compat" description:"Enable ipvs-compat(experimental)"`
FallBack bool `long:"fallback" description:"Fallback to system default networking(experimental)"`
LocalVIP bool `long:"localvip" description:"support vip availability from lb node(experimental)"`
Cloud string `long:"cloud" description:"cloud type if any e.g aws" default:"on-prem"`
Cloud string `long:"cloud" description:"cloud type if any e.g aws,ncloud" default:"on-prem"`
}
2 changes: 2 additions & 0 deletions pkg/loxinet/loxinet.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,8 @@ func loxiNetInit() {

if mh.cloudLabel == "aws" {
AWSApiInit()
} else if mh.cloudLabel == "ncloud" {
NcloudApiInit()
}

// Check if profiling is enabled
Expand Down
11 changes: 11 additions & 0 deletions pkg/loxinet/rules.go
Original file line number Diff line number Diff line change
Expand Up @@ -1535,6 +1535,11 @@ func (R *RuleH) DeleteNatLbRule(serv cmn.LbServiceArg) (int, error) {
if err != nil {
tk.LogIt(tk.LogError, "aws lb-rule vip %s delete failed. err: %v\n", sNetAddr.IP.String(), err)
}
} else if mh.cloudLabel == "ncloud" {
err := nClient.NcloudUpdatePrivateIp(sNetAddr.IP, false)
if err != nil {
tk.LogIt(tk.LogError, "ncloud lb-rule vip %s delete failed. err: %v\n", sNetAddr.IP.String(), err)
}
}
}
dev := fmt.Sprintf("llb-rule-%s", sNetAddr.IP.String())
Expand Down Expand Up @@ -2637,6 +2642,12 @@ func (R *RuleH) AdvRuleVIPIfL2(IP net.IP) error {
tk.LogIt(tk.LogError, "aws lb-rule vip %s add failed. err: %v\n", IP.String(), err)
return err
}
} else if mh.cloudLabel == "ncloud" {
err := nClient.NcloudUpdatePrivateIp(IP, true)
if err != nil {
tk.LogIt(tk.LogError, "ncloud lb-rule vip %s add failed. err: %v\n", IP.String(), err)
return err
}
}
if loxinlp.AddAddrNoHook(IP.String()+"/32", "lo") != 0 {
tk.LogIt(tk.LogError, "nat lb-rule vip %s:%s add failed\n", IP.String(), "lo")
Expand Down
47 changes: 40 additions & 7 deletions pkg/loxinet/utils_naver.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"crypto/hmac"
"crypto/sha256"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"net"
Expand All @@ -44,7 +45,7 @@ type NcloudConfig struct {

type NcloudClient struct {
config *NcloudConfig
client http.Client
client *http.Client
serverURL string
}

Expand Down Expand Up @@ -96,7 +97,7 @@ func (n *NcloudClient) NcloudGetMetadataInterfaceID() (string, error) {
}

func (n *NcloudClient) NcloudCreatePrivateIp(ni string, vIP net.IP) error {
urls := fmt.Sprintf("%s?networkInterfaceNo=%s&secondaryIpList.1=%s&allowReassign=yes", "/vserver/v2/assignSecondaryIps", ni, vIP.String())
urls := fmt.Sprintf("%s?networkInterfaceNo=%s&secondaryIpList.1=%s&allowReassign=yes&responseFormatType=json", "/vserver/v2/assignSecondaryIps", ni, vIP.String())
req, err := http.NewRequest(http.MethodGet, n.serverURL+urls, nil)
if err != nil {
return err
Expand All @@ -109,16 +110,32 @@ func (n *NcloudClient) NcloudCreatePrivateIp(ni string, vIP net.IP) error {
}

defer res.Body.Close()
_, err = io.ReadAll(res.Body)
respBody, err := io.ReadAll(res.Body)
if err != nil {
return err
}

type AssignSecondaryIpsResponse struct {
ReturnMessage string `json:"returnMessage"`
}
type ncloudResponse struct {
AssignSecondaryIpsResponse AssignSecondaryIpsResponse `json:"assignSecondaryIpsResponse"`
}

checkReturn := ncloudResponse{}
if err := json.Unmarshal(respBody, &checkReturn); err != nil {
return err
}

if checkReturn.AssignSecondaryIpsResponse.ReturnMessage != "success" {
return fmt.Errorf(string(respBody))
}

return nil
}

func (n *NcloudClient) NcloudDeletePrivateIp(ni string, vIP net.IP) error {
urls := fmt.Sprintf("%s?networkInterfaceNo=%s&secondaryIpList.1=%s", "/vserver/v2/unassignSecondaryIps", ni, vIP.String())
urls := fmt.Sprintf("%s?networkInterfaceNo=%s&secondaryIpList.1=%s&responseFormatType=json", "/vserver/v2/unassignSecondaryIps", ni, vIP.String())
req, err := http.NewRequest(http.MethodGet, n.serverURL+urls, nil)
if err != nil {
return err
Expand All @@ -131,18 +148,34 @@ func (n *NcloudClient) NcloudDeletePrivateIp(ni string, vIP net.IP) error {
}

defer res.Body.Close()
_, err = io.ReadAll(res.Body)
respBody, err := io.ReadAll(res.Body)
if err != nil {
return err
}

type UnassignSecondaryIpsResponse struct {
ReturnMessage string `json:"returnMessage"`
}
type ncloudResponse struct {
UnassignSecondaryIpsResponse UnassignSecondaryIpsResponse `json:"unassignSecondaryIpsResponse"`
}

checkReturn := ncloudResponse{}
if err := json.Unmarshal(respBody, &checkReturn); err != nil {
return err
}

if checkReturn.UnassignSecondaryIpsResponse.ReturnMessage != "success" {
return fmt.Errorf(string(respBody))
}

return nil
}

func (n *NcloudClient) NcloudUpdatePrivateIp(vIP net.IP, add bool) error {
niID, err := n.NcloudGetMetadataInterfaceID()
if err != nil {
tk.LogIt(tk.LogError, "AWS get instance failed: %v\n", err)
tk.LogIt(tk.LogError, "NCloud get instance failed: %v\n", err)
return err
}

Expand Down Expand Up @@ -192,7 +225,7 @@ func NcloudApiInit() error {
func newFromConfig(cfg *NcloudConfig) *NcloudClient {
return &NcloudClient{
config: cfg,
client: http.Client{},
client: &http.Client{},
serverURL: "https://ncloud.apigw.ntruss.com",
}
}
Expand Down

0 comments on commit 7f54c12

Please sign in to comment.