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

Attach firewall when creating nodebalancer #143

Merged
merged 11 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
44 changes: 26 additions & 18 deletions cloud/linode/fake_linode_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/linode/linodego"
)

const apiVersion = "v4"
rammanoj marked this conversation as resolved.
Show resolved Hide resolved

type fakeAPI struct {
t *testing.T
nb map[string]*linodego.NodeBalancer
Expand Down Expand Up @@ -46,12 +48,12 @@ func (f *fakeAPI) ResetRequests() {
f.requests = make(map[fakeRequest]struct{})
}

func (f *fakeAPI) recordRequest(r *http.Request) {
func (f *fakeAPI) recordRequest(r *http.Request, urlPath string) {
bodyBytes, _ := ioutil.ReadAll(r.Body)
r.Body.Close()
r.Body = ioutil.NopCloser(bytes.NewBuffer(bodyBytes))
f.requests[fakeRequest{
Path: r.URL.Path,
Path: urlPath,
Method: r.Method,
Body: string(bodyBytes),
}] = struct{}{}
Expand All @@ -67,10 +69,16 @@ func (f *fakeAPI) didRequestOccur(method, path, body string) bool {
}

func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
f.recordRequest(r)

w.Header().Set("Content-Type", "application/json")
urlPath := r.URL.Path

if !strings.HasPrefix(urlPath, "/"+apiVersion) {
http.Error(w, "not found", http.StatusNotFound)
return
}
urlPath = strings.TrimPrefix(urlPath, "/"+apiVersion)
f.recordRequest(r, urlPath)

switch r.Method {
case "GET":
whichAPI := strings.Split(urlPath[1:], "/")
Expand Down Expand Up @@ -99,7 +107,7 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
rx, _ = regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+/nodes")
if rx.MatchString(urlPath) {
res := 0
parts := strings.Split(r.URL.Path[1:], "/")
parts := strings.Split(urlPath[1:], "/")
nbcID, err := strconv.Atoi(parts[3])
if err != nil {
f.t.Fatal(err)
Expand Down Expand Up @@ -236,7 +244,7 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

case "POST":
tp := filepath.Base(r.URL.Path)
tp := filepath.Base(urlPath)
if tp == "nodebalancers" {
nbco := linodego.NodeBalancerCreateOptions{}
if err := json.NewDecoder(r.Body).Decode(&nbco); err != nil {
Expand Down Expand Up @@ -313,7 +321,7 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return

} else if tp == "rebuild" {
parts := strings.Split(r.URL.Path[1:], "/")
parts := strings.Split(urlPath[1:], "/")
nbcco := new(linodego.NodeBalancerConfigRebuildOptions)
if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil {
f.t.Fatal(err)
Expand Down Expand Up @@ -382,7 +390,7 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(resp)
return
} else if tp == "configs" {
parts := strings.Split(r.URL.Path[1:], "/")
parts := strings.Split(urlPath[1:], "/")
nbcco := new(linodego.NodeBalancerConfigCreateOptions)
if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil {
f.t.Fatal(err)
Expand Down Expand Up @@ -422,7 +430,7 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
_, _ = w.Write(resp)
return
} else if tp == "nodes" {
parts := strings.Split(r.URL.Path[1:], "/")
parts := strings.Split(urlPath[1:], "/")
nbnco := new(linodego.NodeBalancerNodeCreateOptions)
if err := json.NewDecoder(r.Body).Decode(nbnco); err != nil {
f.t.Fatal(err)
Expand Down Expand Up @@ -454,22 +462,22 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
return
}
case "DELETE":
idRaw := filepath.Base(r.URL.Path)
idRaw := filepath.Base(urlPath)
id, err := strconv.Atoi(idRaw)
if err != nil {
f.t.Fatal(err)
}
if strings.Contains(r.URL.Path, "nodes") {
if strings.Contains(urlPath, "nodes") {
delete(f.nbn, idRaw)
} else if strings.Contains(r.URL.Path, "configs") {
} else if strings.Contains(urlPath, "configs") {
delete(f.nbc, idRaw)

for k, n := range f.nbn {
if n.ConfigID == id {
delete(f.nbn, k)
}
}
} else if strings.Contains(r.URL.Path, "nodebalancers") {
} else if strings.Contains(urlPath, "nodebalancers") {
delete(f.nb, idRaw)

for k, c := range f.nbc {
Expand All @@ -485,10 +493,10 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
}
case "PUT":
if strings.Contains(r.URL.Path, "nodes") {
if strings.Contains(urlPath, "nodes") {
f.t.Fatal("PUT ...nodes is not supported by the mock API")
} else if strings.Contains(r.URL.Path, "configs") {
parts := strings.Split(r.URL.Path[1:], "/")
} else if strings.Contains(urlPath, "configs") {
parts := strings.Split(urlPath[1:], "/")
nbcco := new(linodego.NodeBalancerConfigUpdateOptions)
if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil {
f.t.Fatal(err)
Expand Down Expand Up @@ -545,8 +553,8 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}
_, _ = w.Write(resp)
return
} else if strings.Contains(r.URL.Path, "nodebalancer") {
parts := strings.Split(r.URL.Path[1:], "/")
} else if strings.Contains(urlPath, "nodebalancer") {
parts := strings.Split(urlPath[1:], "/")
nbuo := new(linodego.NodeBalancerUpdateOptions)
if err := json.NewDecoder(r.Body).Decode(nbuo); err != nil {
f.t.Fatal(err)
Expand Down
14 changes: 12 additions & 2 deletions cloud/linode/loadbalancers.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ const (

annLinodeHostnameOnlyIngress = "service.beta.kubernetes.io/linode-loadbalancer-hostname-only-ingress"
annLinodeLoadBalancerTags = "service.beta.kubernetes.io/linode-loadbalancer-tags"
annLinodeCloudFirewallId = "service.beta.kubernetes.io/linode-loadbalancer-firewall-id"
)

var (
Expand Down Expand Up @@ -510,7 +511,7 @@ func (l *loadbalancers) getLoadbalancerTags(ctx context.Context, service *v1.Ser
return []string{}
}

func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions) (lb *linodego.NodeBalancer, err error) {
func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName string, service *v1.Service, configs []*linodego.NodeBalancerConfigCreateOptions, firewallId int) (lb *linodego.NodeBalancer, err error) {
rammanoj marked this conversation as resolved.
Show resolved Hide resolved
connThrottle := getConnectionThrottle(service)

label := l.GetLoadBalancerName(ctx, clusterName, service)
Expand All @@ -521,6 +522,7 @@ func (l *loadbalancers) createNodeBalancer(ctx context.Context, clusterName stri
ClientConnThrottle: &connThrottle,
Configs: configs,
Tags: tags,
FirewallID: firewallId,
}
return l.client.CreateNodeBalancer(ctx, createOpts)
okokes-akamai marked this conversation as resolved.
Show resolved Hide resolved
}
Expand Down Expand Up @@ -639,7 +641,15 @@ func (l *loadbalancers) buildLoadBalancerRequest(ctx context.Context, clusterNam

configs = append(configs, &createOpt)
}
return l.createNodeBalancer(ctx, clusterName, service, configs)

var firewallId int
var err error
if fwid, ok := service.Annotations[annLinodeCloudFirewallId]; ok {
if firewallId, err = strconv.Atoi(fwid); err != nil {
return nil, err
}
}
return l.createNodeBalancer(ctx, clusterName, service, configs, firewallId)
}

func (l *loadbalancers) buildNodeBalancerNodeCreateOptions(node *v1.Node, nodePort int32) linodego.NodeBalancerNodeCreateOptions {
Expand Down
8 changes: 4 additions & 4 deletions cloud/linode/loadbalancers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1281,7 +1281,7 @@ func testEnsureLoadBalancerPreserveAnnotation(t *testing.T, client *linodego.Cli
Spec: testServiceSpec,
}

nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, []*linodego.NodeBalancerConfigCreateOptions{})
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, []*linodego.NodeBalancerConfigCreateOptions{}, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1363,7 +1363,7 @@ func testEnsureLoadBalancerDeleted(t *testing.T, client *linodego.Client, fake *

lb := &loadbalancers{client, "us-west", nil}
configs := []*linodego.NodeBalancerConfigCreateOptions{}
_, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs)
_, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1414,7 +1414,7 @@ func testEnsureExistingLoadBalancer(t *testing.T, client *linodego.Client, _ *fa
addTLSSecret(t, lb.kubeClient)

configs := []*linodego.NodeBalancerConfigCreateOptions{}
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs)
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs, 0)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -1854,7 +1854,7 @@ func testGetLoadBalancer(t *testing.T, client *linodego.Client, _ *fakeAPI) {
}

configs := []*linodego.NodeBalancerConfigCreateOptions{}
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs)
nb, err := lb.createNodeBalancer(context.TODO(), "linodelb", svc, configs, 0)
if err != nil {
t.Fatal(err)
}
Expand Down
32 changes: 16 additions & 16 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ require (
github.com/appscode/go v0.0.0-20200323182826-54e98e09185a
github.com/getsentry/sentry-go v0.4.0
github.com/golang/mock v1.6.0
github.com/linode/linodego v0.32.2
github.com/pkg/errors v0.9.1
github.com/linode/linodego v1.25.0
luthermonson marked this conversation as resolved.
Show resolved Hide resolved
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.4
k8s.io/api v0.21.0
k8s.io/apimachinery v0.21.0
k8s.io/client-go v0.21.0
Expand Down Expand Up @@ -39,11 +38,11 @@ require (
github.com/go-openapi/jsonreference v0.19.3 // indirect
github.com/go-openapi/spec v0.19.5 // indirect
github.com/go-openapi/swag v0.19.5 // indirect
github.com/go-resty/resty/v2 v2.1.1-0.20191201195748-d7b97669fe48 // indirect
github.com/go-resty/resty/v2 v2.9.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/go-cmp v0.5.2 // indirect
github.com/google/go-cmp v0.6.0 // indirect
github.com/google/gofuzz v1.1.0 // indirect
github.com/google/uuid v1.1.2 // indirect
github.com/googleapis/gnostic v0.4.1 // indirect
Expand All @@ -59,6 +58,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_golang v1.7.1 // indirect
github.com/prometheus/client_model v0.2.0 // indirect
Expand All @@ -70,26 +70,26 @@ require (
go.uber.org/multierr v1.3.0 // indirect
go.uber.org/tools v0.0.0-20190618225709-2cfd321de3ee // indirect
go.uber.org/zap v1.13.0 // indirect
golang.org/x/crypto v0.0.0-20210220033148-5ea612d1eb83 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/lint v0.0.0-20200302205851-738671d3881b // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20210405180319-a5a99cb37ef4 // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/oauth2 v0.0.0-20200107190931-bf48bf16ab8d // indirect
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
golang.org/x/sys v0.0.0-20210510120138-977fb7262007 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.4 // indirect
golang.org/x/time v0.0.0-20210220033141-f8bda1e9f3ba // indirect
golang.org/x/tools v0.1.1 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
golang.org/x/sync v0.1.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/term v0.13.0 // indirect
golang.org/x/text v0.14.0 // indirect
golang.org/x/time v0.0.0-20211116232009-f0f3c7e86c11 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/appengine v1.6.5 // indirect
google.golang.org/genproto v0.0.0-20201110150050-8816d57aaa9a // indirect
google.golang.org/grpc v1.27.1 // indirect
google.golang.org/protobuf v1.25.0 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/ini.v1 v1.66.6 // indirect
gopkg.in/natefinch/lumberjack.v2 v2.0.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
honnef.co/go/tools v0.0.1-2020.1.3 // indirect
k8s.io/apiserver v0.21.0 // indirect
k8s.io/controller-manager v0.21.0 // indirect
Expand Down
Loading
Loading