From 59bdca2f2b6238564af2bd961746abfcec353a9a Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 14:27:51 +0200 Subject: [PATCH 1/7] squash --- Makefile | 5 +- cloud/linode/fake_linode_test.go | 1203 +++++++++++++++--------------- go.mod | 2 +- 3 files changed, 611 insertions(+), 599 deletions(-) diff --git a/Makefile b/Makefile index 2bc6d3c5..2c029a6e 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,5 @@ IMG ?= linode/linode-cloud-controller-manager:canary RELEASE_DIR ?= release -GOLANGCI_LINT_IMG := golangci/golangci-lint:v1.55-alpine PLATFORM ?= linux/amd64 export GO111MODULE=on @@ -26,9 +25,9 @@ vet: fmt .PHONY: lint lint: docker run --rm -v "$(shell pwd):/var/work:ro" -w /var/work \ - golangci/golangci-lint:v1.55.2 golangci-lint run -v --timeout=5m + golangci/golangci-lint:v1.57.2 golangci-lint run -v --timeout=5m docker run --rm -v "$(shell pwd):/var/work:ro" -w /var/work/e2e \ - golangci/golangci-lint:v1.55.2 golangci-lint run -v --timeout=5m + golangci/golangci-lint:v1.57.2 golangci-lint run -v --timeout=5m .PHONY: fmt fmt: diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index d3a37e61..c494db13 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -5,11 +5,10 @@ import ( "encoding/json" "fmt" "io" + "log" "math/rand" "net" "net/http" - "path/filepath" - "regexp" "strconv" "strings" "testing" @@ -28,6 +27,7 @@ type fakeAPI struct { fwd map[int]map[int]*linodego.FirewallDevice // map of firewallID -> firewallDeviceID:FirewallDevice requests map[fakeRequest]struct{} + mux *http.ServeMux } type fakeRequest struct { @@ -37,7 +37,7 @@ type fakeRequest struct { } func newFake(t *testing.T) *fakeAPI { - return &fakeAPI{ + fake := &fakeAPI{ t: t, nb: make(map[string]*linodego.NodeBalancer), nbc: make(map[string]*linodego.NodeBalancerConfig), @@ -45,7 +45,10 @@ func newFake(t *testing.T) *fakeAPI { fw: make(map[int]*linodego.Firewall), fwd: make(map[int]map[int]*linodego.FirewallDevice), requests: make(map[fakeRequest]struct{}), + mux: http.NewServeMux(), } + fake.setupRoutes() + return fake } func (f *fakeAPI) ResetRequests() { @@ -72,346 +75,205 @@ func (f *fakeAPI) didRequestOccur(method, path, body string) bool { return ok } -func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { - 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:], "/") - switch whichAPI[0] { - case "nodebalancers": - rx, _ := regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+/nodes/[0-9]+") - if rx.MatchString(urlPath) { - id := filepath.Base(urlPath) - nbn, found := f.nbn[id] - if found { - rr, _ := json.Marshal(nbn) - _, _ = w.Write(rr) - - } else { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - } - return - } - rx, _ = regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+/nodes") - if rx.MatchString(urlPath) { - res := 0 - parts := strings.Split(urlPath[1:], "/") - nbcID, err := strconv.Atoi(parts[3]) - if err != nil { - f.t.Fatal(err) - } - - data := []linodego.NodeBalancerNode{} - - for _, nbn := range f.nbn { - if nbcID == nbn.ConfigID { - data = append(data, *nbn) - } - } - - resp := linodego.NodeBalancerNodesPagedResponse{ - PageOptions: &linodego.PageOptions{ - Page: 1, - Pages: 1, - Results: res, - }, - Data: data, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - return +func (f *fakeAPI) setupRoutes() { + f.mux.HandleFunc("GET /v4/nodebalancers", func(w http.ResponseWriter, r *http.Request) { + res := 0 + data := []linodego.NodeBalancer{} + filter := r.Header.Get("X-Filter") + if filter == "" { + for _, n := range f.nb { + data = append(data, *n) + } + } else { + var fs map[string]string + err := json.Unmarshal([]byte(filter), &fs) + if err != nil { + f.t.Fatal(err) } - rx, _ = regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+") - if rx.MatchString(urlPath) { - id := filepath.Base(urlPath) - nbc, found := f.nbc[id] - if found { - rr, _ := json.Marshal(nbc) - _, _ = w.Write(rr) - - } else { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) + for _, n := range f.nb { + if (n.Label != nil && fs["label"] != "" && *n.Label == fs["label"]) || + (fs["ipv4"] != "" && n.IPv4 != nil && *n.IPv4 == fs["ipv4"]) { + data = append(data, *n) } - return } - rx, _ = regexp.Compile("/nodebalancers/[0-9]+/configs") - if rx.MatchString(urlPath) { - res := 0 - data := []linodego.NodeBalancerConfig{} - filter := r.Header.Get("X-Filter") - if filter == "" { - for _, n := range f.nbc { - data = append(data, *n) - } - } else { - var fs map[string]string - err := json.Unmarshal([]byte(filter), &fs) - if err != nil { - f.t.Fatal(err) - } - for _, n := range f.nbc { - if strconv.Itoa(n.NodeBalancerID) == fs["nodebalancer_id"] { - data = append(data, *n) - } - } - } - resp := linodego.NodeBalancerConfigsPagedResponse{ - PageOptions: &linodego.PageOptions{ - Page: 1, - Pages: 1, - Results: res, - }, - Data: data, - } - rr, err := json.Marshal(resp) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(rr) - return + } + resp := linodego.NodeBalancersPagedResponse{ + PageOptions: &linodego.PageOptions{ + Page: 1, + Pages: 1, + Results: res, + }, + Data: data, + } + rr, _ := json.Marshal(resp) + _, _ = w.Write(rr) + }) + + f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}", func(w http.ResponseWriter, r *http.Request) { + nb, found := f.nb[r.PathValue("nodeBalancerId")] + if found { + rr, _ := json.Marshal(nb) + _, _ = w.Write(rr) + + } else { + w.WriteHeader(404) + resp := linodego.APIError{ + Errors: []linodego.APIErrorReason{ + {Reason: "Not Found"}, + }, } + rr, _ := json.Marshal(resp) + _, _ = w.Write(rr) + } + }) - rx = regexp.MustCompile("/nodebalancers/[0-9]+/firewalls") - if rx.MatchString(urlPath) { - id := strings.Split(urlPath, "/")[2] - devID, err := strconv.Atoi(id) - if err != nil { - f.t.Fatal(err) - } + f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}/firewalls", func(w http.ResponseWriter, r *http.Request) { + devID, err := strconv.Atoi(r.PathValue("nodeBalancerId")) + if err != nil { + f.t.Fatal(err) + } - data := linodego.NodeBalancerFirewallsPagedResponse{ - Data: []linodego.Firewall{}, - PageOptions: &linodego.PageOptions{ - Page: 1, - Pages: 1, - Results: 0, - }, - } + data := linodego.NodeBalancerFirewallsPagedResponse{ + Data: []linodego.Firewall{}, + PageOptions: &linodego.PageOptions{ + Page: 1, + Pages: 1, + Results: 0, + }, + } - out: - for fwid, devices := range f.fwd { - for _, device := range devices { - if device.Entity.ID == devID { - data.Data = append(data.Data, *f.fw[fwid]) - data.PageOptions.Results = 1 - break out - } - } + out: + for fwid, devices := range f.fwd { + for _, device := range devices { + if device.Entity.ID == devID { + data.Data = append(data.Data, *f.fw[fwid]) + data.PageOptions.Results = 1 + break out } - - resp, _ := json.Marshal(data) - _, _ = w.Write(resp) - return } + } - rx, _ = regexp.Compile("/nodebalancers/[0-9]+") - if rx.MatchString(urlPath) { - id := filepath.Base(urlPath) - nb, found := f.nb[id] - if found { - rr, _ := json.Marshal(nb) - _, _ = w.Write(rr) - - } else { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - } - return + resp, _ := json.Marshal(data) + _, _ = w.Write(resp) + }) + + // TODO: note that we discard `nodeBalancerId` + f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}/configs", func(w http.ResponseWriter, r *http.Request) { + res := 0 + data := []linodego.NodeBalancerConfig{} + filter := r.Header.Get("X-Filter") + if filter == "" { + for _, n := range f.nbc { + data = append(data, *n) + } + } else { + var fs map[string]string + err := json.Unmarshal([]byte(filter), &fs) + if err != nil { + f.t.Fatal(err) } - rx, _ = regexp.Compile("/nodebalancers") - if rx.MatchString(urlPath) { - res := 0 - data := []linodego.NodeBalancer{} - filter := r.Header.Get("X-Filter") - if filter == "" { - for _, n := range f.nb { - data = append(data, *n) - } - } else { - var fs map[string]string - err := json.Unmarshal([]byte(filter), &fs) - if err != nil { - f.t.Fatal(err) - } - for _, n := range f.nb { - if (n.Label != nil && fs["label"] != "" && *n.Label == fs["label"]) || - (fs["ipv4"] != "" && n.IPv4 != nil && *n.IPv4 == fs["ipv4"]) { - data = append(data, *n) - } - } + for _, n := range f.nbc { + if strconv.Itoa(n.NodeBalancerID) == fs["nodebalancer_id"] { + data = append(data, *n) } - resp := linodego.NodeBalancersPagedResponse{ - PageOptions: &linodego.PageOptions{ - Page: 1, - Pages: 1, - Results: res, - }, - Data: data, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - return } - case "networking": - rx, _ := regexp.Compile("/networking/firewalls/[0-9]+/devices") - if rx.MatchString(urlPath) { - fwdId, err := strconv.Atoi(strings.Split(urlPath, "/")[3]) - if err != nil { - f.t.Fatal(err) - } + } + resp := linodego.NodeBalancerConfigsPagedResponse{ + PageOptions: &linodego.PageOptions{ + Page: 1, + Pages: 1, + Results: res, + }, + Data: data, + } + rr, err := json.Marshal(resp) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(rr) + return + }) - firewallDevices, found := f.fwd[fwdId] - if found { - firewallDeviceList := []linodego.FirewallDevice{} - for i := range firewallDevices { - firewallDeviceList = append(firewallDeviceList, *firewallDevices[i]) - } - rr, _ := json.Marshal(linodego.FirewallDevicesPagedResponse{ - PageOptions: &linodego.PageOptions{Page: 1, Pages: 1, Results: len(firewallDeviceList)}, - Data: firewallDeviceList, - }) - _, _ = w.Write(rr) - } else { - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - } - return - } + f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}/configs/{configId}/nodes", func(w http.ResponseWriter, r *http.Request) { + res := 0 + nbcID, err := strconv.Atoi(r.PathValue("configId")) + if err != nil { + f.t.Fatal(err) } - case "POST": - tp := filepath.Base(urlPath) - if tp == "nodebalancers" { - nbco := linodego.NodeBalancerCreateOptions{} - if err := json.NewDecoder(r.Body).Decode(&nbco); err != nil { - f.t.Fatal(err) - } + data := []linodego.NodeBalancerNode{} - ip := net.IPv4(byte(rand.Intn(100)), byte(rand.Intn(100)), byte(rand.Intn(100)), byte(rand.Intn(100))).String() - hostname := fmt.Sprintf("nb-%s.%s.linode.com", strings.Replace(ip, ".", "-", 4), strings.ToLower(nbco.Region)) - nb := linodego.NodeBalancer{ - ID: rand.Intn(9999), - Label: nbco.Label, - Region: nbco.Region, - IPv4: &ip, - Hostname: &hostname, - Tags: nbco.Tags, + for _, nbn := range f.nbn { + if nbcID == nbn.ConfigID { + data = append(data, *nbn) } + } - if nbco.ClientConnThrottle != nil { - nb.ClientConnThrottle = *nbco.ClientConnThrottle - } - f.nb[strconv.Itoa(nb.ID)] = &nb - - for _, nbcco := range nbco.Configs { - if nbcco.Protocol == "https" { - if !strings.Contains(nbcco.SSLCert, "BEGIN CERTIFICATE") { - f.t.Fatal("HTTPS port declared without calid ssl cert", nbcco.SSLCert) - } - if !strings.Contains(nbcco.SSLKey, "BEGIN RSA PRIVATE KEY") { - f.t.Fatal("HTTPS port declared without calid ssl key", nbcco.SSLKey) - } - } - nbc := linodego.NodeBalancerConfig{ - ID: rand.Intn(9999), - Port: nbcco.Port, - Protocol: nbcco.Protocol, - ProxyProtocol: nbcco.ProxyProtocol, - Algorithm: nbcco.Algorithm, - Stickiness: nbcco.Stickiness, - Check: nbcco.Check, - CheckInterval: nbcco.CheckInterval, - CheckAttempts: nbcco.CheckAttempts, - CheckPath: nbcco.CheckPath, - CheckBody: nbcco.CheckBody, - CheckPassive: *nbcco.CheckPassive, - CheckTimeout: nbcco.CheckTimeout, - CipherSuite: nbcco.CipherSuite, - NodeBalancerID: nb.ID, - SSLCommonName: "sslcommonname", - SSLFingerprint: "sslfingerprint", - SSLCert: "", - SSLKey: "", - } - f.nbc[strconv.Itoa(nbc.ID)] = &nbc - - for _, nbnco := range nbcco.Nodes { - nbn := linodego.NodeBalancerNode{ - ID: rand.Intn(99999), - Address: nbnco.Address, - Label: nbnco.Label, - Weight: nbnco.Weight, - Mode: nbnco.Mode, - NodeBalancerID: nb.ID, - ConfigID: nbc.ID, - } - f.nbn[strconv.Itoa(nbn.ID)] = &nbn - } - } + resp := linodego.NodeBalancerNodesPagedResponse{ + PageOptions: &linodego.PageOptions{ + Page: 1, + Pages: 1, + Results: res, + }, + Data: data, + } + rr, _ := json.Marshal(resp) + _, _ = w.Write(rr) - if nbco.FirewallID != 0 { - createFirewallDevice(nbco.FirewallID, f, linodego.FirewallDeviceCreateOptions{ - ID: nb.ID, - Type: "nodebalancer", - }) - } + }) - resp, err := json.Marshal(nb) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return + f.mux.HandleFunc("GET /v4/networking/firewalls/{firewallId}/devices", func(w http.ResponseWriter, r *http.Request) { + fwdId, err := strconv.Atoi(r.PathValue("firewallId")) + if err != nil { + f.t.Fatal(err) + } - } else if tp == "rebuild" { - parts := strings.Split(urlPath[1:], "/") - nbcco := new(linodego.NodeBalancerConfigRebuildOptions) - if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil { - f.t.Fatal(err) - } - nbid, err := strconv.Atoi(parts[1]) - if err != nil { - f.t.Fatal(err) + firewallDevices, found := f.fwd[fwdId] + if found { + firewallDeviceList := []linodego.FirewallDevice{} + for i := range firewallDevices { + firewallDeviceList = append(firewallDeviceList, *firewallDevices[i]) } - nbcid, err := strconv.Atoi(parts[3]) - if err != nil { - f.t.Fatal(err) + rr, _ := json.Marshal(linodego.FirewallDevicesPagedResponse{ + PageOptions: &linodego.PageOptions{Page: 1, Pages: 1, Results: len(firewallDeviceList)}, + Data: firewallDeviceList, + }) + _, _ = w.Write(rr) + } else { + w.WriteHeader(404) + resp := linodego.APIError{ + Errors: []linodego.APIErrorReason{ + {Reason: "Not Found"}, + }, } + rr, _ := json.Marshal(resp) + _, _ = w.Write(rr) + } + }) + + f.mux.HandleFunc("POST /v4/nodebalancers", func(w http.ResponseWriter, r *http.Request) { + nbco := linodego.NodeBalancerCreateOptions{} + if err := json.NewDecoder(r.Body).Decode(&nbco); err != nil { + f.t.Fatal(err) + } + + ip := net.IPv4(byte(rand.Intn(100)), byte(rand.Intn(100)), byte(rand.Intn(100)), byte(rand.Intn(100))).String() + hostname := fmt.Sprintf("nb-%s.%s.linode.com", strings.Replace(ip, ".", "-", 4), strings.ToLower(nbco.Region)) + nb := linodego.NodeBalancer{ + ID: rand.Intn(9999), + Label: nbco.Label, + Region: nbco.Region, + IPv4: &ip, + Hostname: &hostname, + Tags: nbco.Tags, + } + + if nbco.ClientConnThrottle != nil { + nb.ClientConnThrottle = *nbco.ClientConnThrottle + } + f.nb[strconv.Itoa(nb.ID)] = &nb + + for _, nbcco := range nbco.Configs { if nbcco.Protocol == "https" { if !strings.Contains(nbcco.SSLCert, "BEGIN CERTIFICATE") { f.t.Fatal("HTTPS port declared without calid ssl cert", nbcco.SSLCert) @@ -420,8 +282,8 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { f.t.Fatal("HTTPS port declared without calid ssl key", nbcco.SSLKey) } } - nbcc := linodego.NodeBalancerConfig{ - ID: nbcid, + nbc := linodego.NodeBalancerConfig{ + ID: rand.Intn(9999), Port: nbcco.Port, Protocol: nbcco.Protocol, ProxyProtocol: nbcco.ProxyProtocol, @@ -435,336 +297,397 @@ func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { CheckPassive: *nbcco.CheckPassive, CheckTimeout: nbcco.CheckTimeout, CipherSuite: nbcco.CipherSuite, - NodeBalancerID: nbid, + NodeBalancerID: nb.ID, SSLCommonName: "sslcommonname", SSLFingerprint: "sslfingerprint", SSLCert: "", SSLKey: "", } + f.nbc[strconv.Itoa(nbc.ID)] = &nbc - f.nbc[strconv.Itoa(nbcc.ID)] = &nbcc - for k, n := range f.nbn { - if n.ConfigID == nbcc.ID { - delete(f.nbn, k) - } - } - - for _, n := range nbcco.Nodes { - node := linodego.NodeBalancerNode{ + for _, nbnco := range nbcco.Nodes { + nbn := linodego.NodeBalancerNode{ ID: rand.Intn(99999), - Address: n.Address, - Label: n.Label, - Weight: n.Weight, - Mode: n.Mode, - NodeBalancerID: nbid, - ConfigID: nbcc.ID, + Address: nbnco.Address, + Label: nbnco.Label, + Weight: nbnco.Weight, + Mode: nbnco.Mode, + NodeBalancerID: nb.ID, + ConfigID: nbc.ID, } - f.nbn[strconv.Itoa(node.ID)] = &node - } - resp, err := json.Marshal(nbcc) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return - } else if tp == "configs" { - parts := strings.Split(urlPath[1:], "/") - nbcco := new(linodego.NodeBalancerConfigCreateOptions) - if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil { - f.t.Fatal(err) - } - nbid, err := strconv.Atoi(parts[1]) - if err != nil { - f.t.Fatal(err) + f.nbn[strconv.Itoa(nbn.ID)] = &nbn } + } - nbcc := linodego.NodeBalancerConfig{ - ID: rand.Intn(9999), - Port: nbcco.Port, - Protocol: nbcco.Protocol, - ProxyProtocol: nbcco.ProxyProtocol, - Algorithm: nbcco.Algorithm, - Stickiness: nbcco.Stickiness, - Check: nbcco.Check, - CheckInterval: nbcco.CheckInterval, - CheckAttempts: nbcco.CheckAttempts, - CheckPath: nbcco.CheckPath, - CheckBody: nbcco.CheckBody, - CheckPassive: *nbcco.CheckPassive, - CheckTimeout: nbcco.CheckTimeout, - CipherSuite: nbcco.CipherSuite, - NodeBalancerID: nbid, - SSLCommonName: "sslcomonname", - SSLFingerprint: "sslfingerprint", - SSLCert: "", - SSLKey: "", - } - f.nbc[strconv.Itoa(nbcc.ID)] = &nbcc + if nbco.FirewallID != 0 { + createFirewallDevice(nbco.FirewallID, f, linodego.FirewallDeviceCreateOptions{ + ID: nb.ID, + Type: "nodebalancer", + }) + } - resp, err := json.Marshal(nbcc) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return - } else if tp == "nodes" { - parts := strings.Split(urlPath[1:], "/") - nbnco := new(linodego.NodeBalancerNodeCreateOptions) - if err := json.NewDecoder(r.Body).Decode(nbnco); err != nil { - f.t.Fatal(err) + resp, err := json.Marshal(nb) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(resp) + return + }) + + f.mux.HandleFunc("POST /v4/nodebalancers/{nodeBalancerId}/configs", func(w http.ResponseWriter, r *http.Request) { + nbcco := new(linodego.NodeBalancerConfigCreateOptions) + if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil { + f.t.Fatal(err) + } + nbid, err := strconv.Atoi(r.PathValue("nodeBalancerId")) + if err != nil { + f.t.Fatal(err) + } + + nbcc := linodego.NodeBalancerConfig{ + ID: rand.Intn(9999), + Port: nbcco.Port, + Protocol: nbcco.Protocol, + ProxyProtocol: nbcco.ProxyProtocol, + Algorithm: nbcco.Algorithm, + Stickiness: nbcco.Stickiness, + Check: nbcco.Check, + CheckInterval: nbcco.CheckInterval, + CheckAttempts: nbcco.CheckAttempts, + CheckPath: nbcco.CheckPath, + CheckBody: nbcco.CheckBody, + CheckPassive: *nbcco.CheckPassive, + CheckTimeout: nbcco.CheckTimeout, + CipherSuite: nbcco.CipherSuite, + NodeBalancerID: nbid, + SSLCommonName: "sslcomonname", + SSLFingerprint: "sslfingerprint", + SSLCert: "", + SSLKey: "", + } + f.nbc[strconv.Itoa(nbcc.ID)] = &nbcc + + resp, err := json.Marshal(nbcc) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(resp) + }) + + f.mux.HandleFunc("POST /v4/nodebalancers/{nodeBalancerId}/configs/{configId}/rebuild", func(w http.ResponseWriter, r *http.Request) { + nbcco := new(linodego.NodeBalancerConfigRebuildOptions) + if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil { + f.t.Fatal(err) + } + nbid, err := strconv.Atoi(r.PathValue("nodeBalancerId")) + if err != nil { + f.t.Fatal(err) + } + nbcid, err := strconv.Atoi(r.PathValue("configId")) + if err != nil { + f.t.Fatal(err) + } + if nbcco.Protocol == "https" { + if !strings.Contains(nbcco.SSLCert, "BEGIN CERTIFICATE") { + f.t.Fatal("HTTPS port declared without calid ssl cert", nbcco.SSLCert) } - nbid, err := strconv.Atoi(parts[1]) - if err != nil { - f.t.Fatal(err) + if !strings.Contains(nbcco.SSLKey, "BEGIN RSA PRIVATE KEY") { + f.t.Fatal("HTTPS port declared without calid ssl key", nbcco.SSLKey) } - nbcid, err := strconv.Atoi(parts[3]) - if err != nil { - f.t.Fatal(err) + } + nbcc := linodego.NodeBalancerConfig{ + ID: nbcid, + Port: nbcco.Port, + Protocol: nbcco.Protocol, + ProxyProtocol: nbcco.ProxyProtocol, + Algorithm: nbcco.Algorithm, + Stickiness: nbcco.Stickiness, + Check: nbcco.Check, + CheckInterval: nbcco.CheckInterval, + CheckAttempts: nbcco.CheckAttempts, + CheckPath: nbcco.CheckPath, + CheckBody: nbcco.CheckBody, + CheckPassive: *nbcco.CheckPassive, + CheckTimeout: nbcco.CheckTimeout, + CipherSuite: nbcco.CipherSuite, + NodeBalancerID: nbid, + SSLCommonName: "sslcommonname", + SSLFingerprint: "sslfingerprint", + SSLCert: "", + SSLKey: "", + } + + f.nbc[strconv.Itoa(nbcc.ID)] = &nbcc + for k, n := range f.nbn { + if n.ConfigID == nbcc.ID { + delete(f.nbn, k) } - nbn := linodego.NodeBalancerNode{ + } + + for _, n := range nbcco.Nodes { + node := linodego.NodeBalancerNode{ ID: rand.Intn(99999), - Address: nbnco.Address, - Label: nbnco.Label, - Status: "UP", - Weight: nbnco.Weight, - Mode: nbnco.Mode, - ConfigID: nbcid, + Address: n.Address, + Label: n.Label, + Weight: n.Weight, + Mode: n.Mode, NodeBalancerID: nbid, + ConfigID: nbcc.ID, } - f.nbn[strconv.Itoa(nbn.ID)] = &nbn - resp, err := json.Marshal(nbn) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return - } else if tp == "firewalls" { - fco := linodego.FirewallCreateOptions{} - if err := json.NewDecoder(r.Body).Decode(&fco); err != nil { - f.t.Fatal(err) - } + f.nbn[strconv.Itoa(node.ID)] = &node + } + resp, err := json.Marshal(nbcc) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(resp) - firewall := linodego.Firewall{ - ID: rand.Intn(9999), - Label: fco.Label, - Rules: fco.Rules, - Tags: fco.Tags, - Status: "enabled", - } + }) - f.fw[firewall.ID] = &firewall - resp, err := json.Marshal(firewall) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return - } else if tp == "devices" { - fwId := strings.Split(urlPath, "/")[3] - fdco := linodego.FirewallDeviceCreateOptions{} - if err := json.NewDecoder(r.Body).Decode(&fdco); err != nil { - f.t.Fatal(err) - } + f.mux.HandleFunc("POST /v4/networking/firewalls", func(w http.ResponseWriter, r *http.Request) { + fco := linodego.FirewallCreateOptions{} + if err := json.NewDecoder(r.Body).Decode(&fco); err != nil { + f.t.Fatal(err) + } - firewallID, err := strconv.Atoi(fwId) - if err != nil { - f.t.Fatal(err) - } + firewall := linodego.Firewall{ + ID: rand.Intn(9999), + Label: fco.Label, + Rules: fco.Rules, + Tags: fco.Tags, + Status: "enabled", + } - fwd := createFirewallDevice(firewallID, f, fdco) - resp, err := json.Marshal(fwd) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return + f.fw[firewall.ID] = &firewall + resp, err := json.Marshal(firewall) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(resp) + }) + + f.mux.HandleFunc("POST /v4/networking/firewalls/{firewallId}/devices", func(w http.ResponseWriter, r *http.Request) { + fdco := linodego.FirewallDeviceCreateOptions{} + if err := json.NewDecoder(r.Body).Decode(&fdco); err != nil { + f.t.Fatal(err) } - case "DELETE": - idRaw := filepath.Base(urlPath) - id, err := strconv.Atoi(idRaw) + + firewallID, err := strconv.Atoi(r.PathValue("firewallId")) if err != nil { f.t.Fatal(err) } - if strings.Contains(urlPath, "nodes") { - delete(f.nbn, idRaw) - } 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(urlPath, "nodebalancers") { - delete(f.nb, idRaw) + fwd := createFirewallDevice(firewallID, f, fdco) + resp, err := json.Marshal(fwd) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(resp) + }) - for k, c := range f.nbc { - if c.NodeBalancerID == id { - delete(f.nbc, k) - } - } + f.mux.HandleFunc("DELETE /v4/nodebalancers/{nodeBalancerId}", func(w http.ResponseWriter, r *http.Request) { + delete(f.nb, r.PathValue("nodeBalancerId")) + nid, err := strconv.Atoi(r.PathValue("nodeBalancerId")) + if err != nil { + f.t.Fatal(err) + } - for k, n := range f.nbn { - if n.NodeBalancerID == id { - delete(f.nbn, k) - } - } - } else if strings.Contains(urlPath, "devices") { - firewallId, err := strconv.Atoi(strings.Split(urlPath, "/")[3]) - if err != nil { - f.t.Fatal(err) + for k, c := range f.nbc { + if c.NodeBalancerID == nid { + delete(f.nbc, k) } + } - deviceId, err := strconv.Atoi(strings.Split(urlPath, "/")[5]) - if err != nil { - f.t.Fatal(err) - } - delete(f.fwd[firewallId], deviceId) - } else if strings.Contains(urlPath, "firewalls") { - firewallId, err := strconv.Atoi(strings.Split(urlPath, "/")[3]) - if err != nil { - f.t.Fatal(err) + for k, n := range f.nbn { + if n.NodeBalancerID == nid { + delete(f.nbn, k) } + } + }) + + f.mux.HandleFunc("DELETE /v4/nodebalancers/{nodeBalancerId}/configs/{configId}/nodes/{nodeId}", func(w http.ResponseWriter, r *http.Request) { + delete(f.nbn, r.PathValue("nodeId")) + }) - delete(f.fwd, firewallId) - delete(f.fw, firewallId) + f.mux.HandleFunc("DELETE /v4/nodebalancers/{nodeBalancerId}/configs/{configId}", func(w http.ResponseWriter, r *http.Request) { + delete(f.nbc, r.PathValue("configId")) + + cid, err := strconv.Atoi(r.PathValue("configId")) + if err != nil { + f.t.Fatal(err) } - case "PUT": - if strings.Contains(urlPath, "nodes") { - f.t.Fatal("PUT ...nodes is not supported by the mock API") - } 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) - } - nbcid, err := strconv.Atoi(parts[3]) - if err != nil { - f.t.Fatal(err) - } - nbid, err := strconv.Atoi(parts[1]) - if err != nil { - f.t.Fatal(err) + + for k, n := range f.nbn { + if n.ConfigID == cid { + delete(f.nbn, k) } + } + }) - nbcc := linodego.NodeBalancerConfig{ - ID: nbcid, - Port: nbcco.Port, - Protocol: nbcco.Protocol, - ProxyProtocol: nbcco.ProxyProtocol, - Algorithm: nbcco.Algorithm, - Stickiness: nbcco.Stickiness, - Check: nbcco.Check, - CheckInterval: nbcco.CheckInterval, - CheckAttempts: nbcco.CheckAttempts, - CheckPath: nbcco.CheckPath, - CheckBody: nbcco.CheckBody, - CheckPassive: *nbcco.CheckPassive, - CheckTimeout: nbcco.CheckTimeout, - CipherSuite: nbcco.CipherSuite, + f.mux.HandleFunc("DELETE /v4/networking/firewalls/{firewallId}", func(w http.ResponseWriter, r *http.Request) { + firewallId, err := strconv.Atoi(r.PathValue("firewallId")) + if err != nil { + f.t.Fatal(err) + } + + delete(f.fwd, firewallId) + delete(f.fw, firewallId) + }) + + f.mux.HandleFunc("DELETE /v4/networking/firewalls/{firewallId}/devices/{deviceId}", func(w http.ResponseWriter, r *http.Request) { + firewallId, err := strconv.Atoi(r.PathValue("firewallId")) + if err != nil { + f.t.Fatal(err) + } + + deviceId, err := strconv.Atoi(r.PathValue("deviceId")) + if err != nil { + f.t.Fatal(err) + } + delete(f.fwd[firewallId], deviceId) + }) + + // TODO: reimplement all of this + f.mux.HandleFunc("PUT /v4/nodebalancers/{nodeBalancerId}/configs/{configId}", func(w http.ResponseWriter, r *http.Request) { + nbcco := new(linodego.NodeBalancerConfigUpdateOptions) + if err := json.NewDecoder(r.Body).Decode(nbcco); err != nil { + f.t.Fatal(err) + } + nbcid, err := strconv.Atoi(r.PathValue("configId")) + if err != nil { + f.t.Fatal(err) + } + nbid, err := strconv.Atoi(r.PathValue("nodeBalancerId")) + if err != nil { + f.t.Fatal(err) + } + + nbcc := linodego.NodeBalancerConfig{ + ID: nbcid, + Port: nbcco.Port, + Protocol: nbcco.Protocol, + ProxyProtocol: nbcco.ProxyProtocol, + Algorithm: nbcco.Algorithm, + Stickiness: nbcco.Stickiness, + Check: nbcco.Check, + CheckInterval: nbcco.CheckInterval, + CheckAttempts: nbcco.CheckAttempts, + CheckPath: nbcco.CheckPath, + CheckBody: nbcco.CheckBody, + CheckPassive: *nbcco.CheckPassive, + CheckTimeout: nbcco.CheckTimeout, + CipherSuite: nbcco.CipherSuite, + NodeBalancerID: nbid, + SSLCommonName: "sslcommonname", + SSLFingerprint: "sslfingerprint", + SSLCert: "", + SSLKey: "", + } + f.nbc[strconv.Itoa(nbcc.ID)] = &nbcc + + for _, n := range nbcco.Nodes { + node := linodego.NodeBalancerNode{ + ID: rand.Intn(99999), + Address: n.Address, + Label: n.Label, + Weight: n.Weight, + Mode: n.Mode, NodeBalancerID: nbid, - SSLCommonName: "sslcommonname", - SSLFingerprint: "sslfingerprint", - SSLCert: "", - SSLKey: "", + ConfigID: nbcc.ID, } - f.nbc[strconv.Itoa(nbcc.ID)] = &nbcc - for _, n := range nbcco.Nodes { - node := linodego.NodeBalancerNode{ - ID: rand.Intn(99999), - Address: n.Address, - Label: n.Label, - Weight: n.Weight, - Mode: n.Mode, - NodeBalancerID: nbid, - ConfigID: nbcc.ID, - } + f.nbn[strconv.Itoa(node.ID)] = &node + } - f.nbn[strconv.Itoa(node.ID)] = &node - } + resp, err := json.Marshal(nbcc) + if err != nil { + f.t.Fatal(err) + } + _, _ = w.Write(resp) + }) + + f.mux.HandleFunc("PUT /v4/networking/firewalls/{firewallID}/rules", func(w http.ResponseWriter, r *http.Request) { + fwrs := new(linodego.FirewallRuleSet) + if err := json.NewDecoder(r.Body).Decode(fwrs); err != nil { + f.t.Fatal(err) + } + + fwID, err := strconv.Atoi(r.PathValue("firewallID")) + if err != nil { + f.t.Fatal(err) + } - resp, err := json.Marshal(nbcc) + if firewall, found := f.fw[fwID]; found { + firewall.Rules.Inbound = fwrs.Inbound + firewall.Rules.InboundPolicy = fwrs.InboundPolicy + // outbound rules do not apply, ignoring. + f.fw[fwID] = firewall + resp, err := json.Marshal(firewall) if err != nil { f.t.Fatal(err) } _, _ = w.Write(resp) return - } 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) - } - if _, err := strconv.Atoi(parts[1]); err != nil { - f.t.Fatal(err) - } + } - if nb, found := f.nb[parts[1]]; found { - if nbuo.ClientConnThrottle != nil { - nb.ClientConnThrottle = *nbuo.ClientConnThrottle - } - if nbuo.Label != nil { - nb.Label = nbuo.Label - } - if nbuo.Tags != nil { - nb.Tags = *nbuo.Tags - } + w.WriteHeader(404) + resp := linodego.APIError{ + Errors: []linodego.APIErrorReason{ + {Reason: "Not Found"}, + }, + } + rr, _ := json.Marshal(resp) + _, _ = w.Write(rr) + }) - f.nb[strconv.Itoa(nb.ID)] = nb - resp, err := json.Marshal(nb) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return - } + f.mux.HandleFunc("PUT /v4/nodebalancers/{nodeBalancerId}", func(w http.ResponseWriter, r *http.Request) { + nbuo := new(linodego.NodeBalancerUpdateOptions) + if err := json.NewDecoder(r.Body).Decode(nbuo); err != nil { + f.t.Fatal(err) + } + if _, err := strconv.Atoi(r.PathValue("nodeBalancerId")); err != nil { + f.t.Fatal(err) + } - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, + if nb, found := f.nb[r.PathValue("nodeBalancerId")]; found { + if nbuo.ClientConnThrottle != nil { + nb.ClientConnThrottle = *nbuo.ClientConnThrottle } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) - - } else if strings.Contains(urlPath, "firewalls") { - // path is networking/firewalls/%d/rules - parts := strings.Split(urlPath[1:], "/") - fwrs := new(linodego.FirewallRuleSet) - if err := json.NewDecoder(r.Body).Decode(fwrs); err != nil { - f.t.Fatal(err) + if nbuo.Label != nil { + nb.Label = nbuo.Label + } + if nbuo.Tags != nil { + nb.Tags = *nbuo.Tags } - fwID, err := strconv.Atoi(parts[2]) + f.nb[strconv.Itoa(nb.ID)] = nb + resp, err := json.Marshal(nb) if err != nil { f.t.Fatal(err) } + _, _ = w.Write(resp) + return + } - if firewall, found := f.fw[fwID]; found { - firewall.Rules.Inbound = fwrs.Inbound - firewall.Rules.InboundPolicy = fwrs.InboundPolicy - // outbound rules do not apply, ignoring. - f.fw[fwID] = firewall - resp, err := json.Marshal(firewall) - if err != nil { - f.t.Fatal(err) - } - _, _ = w.Write(resp) - return - } - - w.WriteHeader(404) - resp := linodego.APIError{ - Errors: []linodego.APIErrorReason{ - {Reason: "Not Found"}, - }, - } - rr, _ := json.Marshal(resp) - _, _ = w.Write(rr) + w.WriteHeader(404) + resp := linodego.APIError{ + Errors: []linodego.APIErrorReason{ + {Reason: "Not Found"}, + }, } - } + rr, _ := json.Marshal(resp) + _, _ = w.Write(rr) + }) +} + +func (f *fakeAPI) ServeHTTP(w http.ResponseWriter, r *http.Request) { + log.Printf("fakeAPI: %s %s", r.Method, r.URL.Path) + + urlPath := strings.TrimPrefix(r.URL.Path, "/"+apiVersion) + f.recordRequest(r, urlPath) + + w.Header().Add("Content-Type", "application/json") + f.mux.ServeHTTP(w, r) } func createFirewallDevice(fwId int, f *fakeAPI, fdco linodego.FirewallDeviceCreateOptions) linodego.FirewallDevice { @@ -791,3 +714,93 @@ func randString() string { } return string(b) } + +// TODO(PR): these are outstanding, but I didn't find them in the call graph + +// switch r.Method { +// case "GET": +// whichAPI := strings.Split(urlPath[1:], "/") +// switch whichAPI[0] { +// case "nodebalancers": +// rx, _ := regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+/nodes/[0-9]+") +// if rx.MatchString(urlPath) { +// id := filepath.Base(urlPath) +// nbn, found := f.nbn[id] +// if found { +// rr, _ := json.Marshal(nbn) +// _, _ = w.Write(rr) + +// } else { +// w.WriteHeader(404) +// resp := linodego.APIError{ +// Errors: []linodego.APIErrorReason{ +// {Reason: "Not Found"}, +// }, +// } +// rr, _ := json.Marshal(resp) +// _, _ = w.Write(rr) +// } +// return +// } + +// rx, _ = regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+") +// if rx.MatchString(urlPath) { +// id := filepath.Base(urlPath) +// nbc, found := f.nbc[id] +// if found { +// rr, _ := json.Marshal(nbc) +// _, _ = w.Write(rr) + +// } else { +// w.WriteHeader(404) +// resp := linodego.APIError{ +// Errors: []linodego.APIErrorReason{ +// {Reason: "Not Found"}, +// }, +// } +// rr, _ := json.Marshal(resp) +// _, _ = w.Write(rr) +// } +// return +// } +// case "networking": + +// } + +// case "POST": +// tp := filepath.Base(urlPath) +// if tp == "nodebalancers" { + +// } else if tp == "rebuild" { + +// } else if tp == "nodes" { +// parts := strings.Split(urlPath[1:], "/") +// nbnco := new(linodego.NodeBalancerNodeCreateOptions) +// if err := json.NewDecoder(r.Body).Decode(nbnco); err != nil { +// f.t.Fatal(err) +// } +// nbid, err := strconv.Atoi(parts[1]) +// if err != nil { +// f.t.Fatal(err) +// } +// nbcid, err := strconv.Atoi(parts[3]) +// if err != nil { +// f.t.Fatal(err) +// } +// nbn := linodego.NodeBalancerNode{ +// ID: rand.Intn(99999), +// Address: nbnco.Address, +// Label: nbnco.Label, +// Status: "UP", +// Weight: nbnco.Weight, +// Mode: nbnco.Mode, +// ConfigID: nbcid, +// NodeBalancerID: nbid, +// } +// f.nbn[strconv.Itoa(nbn.ID)] = &nbn +// resp, err := json.Marshal(nbn) +// if err != nil { +// f.t.Fatal(err) +// } +// _, _ = w.Write(resp) +// return diff --git a/go.mod b/go.mod index d6d7fc6a..90842d34 100644 --- a/go.mod +++ b/go.mod @@ -1,6 +1,6 @@ module github.com/linode/linode-cloud-controller-manager -go 1.20 +go 1.22 require ( github.com/appscode/go v0.0.0-20200323182826-54e98e09185a From b25661f3cebce5d45206c4cfc50767b01ba6bd9d Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 14:29:00 +0200 Subject: [PATCH 2/7] remove redundant endpoints --- cloud/linode/fake_linode_test.go | 90 -------------------------------- 1 file changed, 90 deletions(-) diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index c494db13..34891342 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -714,93 +714,3 @@ func randString() string { } return string(b) } - -// TODO(PR): these are outstanding, but I didn't find them in the call graph - -// switch r.Method { -// case "GET": -// whichAPI := strings.Split(urlPath[1:], "/") -// switch whichAPI[0] { -// case "nodebalancers": -// rx, _ := regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+/nodes/[0-9]+") -// if rx.MatchString(urlPath) { -// id := filepath.Base(urlPath) -// nbn, found := f.nbn[id] -// if found { -// rr, _ := json.Marshal(nbn) -// _, _ = w.Write(rr) - -// } else { -// w.WriteHeader(404) -// resp := linodego.APIError{ -// Errors: []linodego.APIErrorReason{ -// {Reason: "Not Found"}, -// }, -// } -// rr, _ := json.Marshal(resp) -// _, _ = w.Write(rr) -// } -// return -// } - -// rx, _ = regexp.Compile("/nodebalancers/[0-9]+/configs/[0-9]+") -// if rx.MatchString(urlPath) { -// id := filepath.Base(urlPath) -// nbc, found := f.nbc[id] -// if found { -// rr, _ := json.Marshal(nbc) -// _, _ = w.Write(rr) - -// } else { -// w.WriteHeader(404) -// resp := linodego.APIError{ -// Errors: []linodego.APIErrorReason{ -// {Reason: "Not Found"}, -// }, -// } -// rr, _ := json.Marshal(resp) -// _, _ = w.Write(rr) -// } -// return -// } -// case "networking": - -// } - -// case "POST": -// tp := filepath.Base(urlPath) -// if tp == "nodebalancers" { - -// } else if tp == "rebuild" { - -// } else if tp == "nodes" { -// parts := strings.Split(urlPath[1:], "/") -// nbnco := new(linodego.NodeBalancerNodeCreateOptions) -// if err := json.NewDecoder(r.Body).Decode(nbnco); err != nil { -// f.t.Fatal(err) -// } -// nbid, err := strconv.Atoi(parts[1]) -// if err != nil { -// f.t.Fatal(err) -// } -// nbcid, err := strconv.Atoi(parts[3]) -// if err != nil { -// f.t.Fatal(err) -// } -// nbn := linodego.NodeBalancerNode{ -// ID: rand.Intn(99999), -// Address: nbnco.Address, -// Label: nbnco.Label, -// Status: "UP", -// Weight: nbnco.Weight, -// Mode: nbnco.Mode, -// ConfigID: nbcid, -// NodeBalancerID: nbid, -// } -// f.nbn[strconv.Itoa(nbn.ID)] = &nbn -// resp, err := json.Marshal(nbn) -// if err != nil { -// f.t.Fatal(err) -// } -// _, _ = w.Write(resp) -// return From 1d69e79e3bf9fac98fcfb116f3f89f221c8e0dc0 Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 14:31:12 +0200 Subject: [PATCH 3/7] do not run against 1.20 or oldstable --- .github/workflows/ci.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 45cf6169..9537a2ca 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -11,7 +11,7 @@ jobs: runs-on: ubuntu-latest strategy: matrix: - go-version: [ 'stable', 'oldstable', '1.20' ] + go-version: [ 'stable', '1.22' ] steps: - uses: actions/checkout@v4 with: From dd8b45d229bbdc1ce3541e3d7b217ea8b1b8bc8f Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 14:32:28 +0200 Subject: [PATCH 4/7] bump golang and alpine --- Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Dockerfile b/Dockerfile index 20942766..e2dff202 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.21-alpine as builder +FROM golang:1.22-alpine as builder RUN mkdir -p /linode WORKDIR /linode @@ -11,7 +11,7 @@ COPY sentry ./sentry RUN go mod download RUN go build -a -ldflags '-extldflags "-static"' -o /bin/linode-cloud-controller-manager-linux /linode -FROM alpine:3.18.4 +FROM alpine:3.19.1 RUN apk add --update --no-cache ca-certificates LABEL maintainers="Linode" LABEL description="Linode Cloud Controller Manager" From 9390dc87cf40a9e64ca9c6f70b07da4d6e9a7220 Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 14:37:09 +0200 Subject: [PATCH 5/7] lint --- cloud/linode/fake_linode_test.go | 4 ---- 1 file changed, 4 deletions(-) diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index 34891342..f4891988 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -191,7 +191,6 @@ func (f *fakeAPI) setupRoutes() { f.t.Fatal(err) } _, _ = w.Write(rr) - return }) f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}/configs/{configId}/nodes", func(w http.ResponseWriter, r *http.Request) { @@ -219,7 +218,6 @@ func (f *fakeAPI) setupRoutes() { } rr, _ := json.Marshal(resp) _, _ = w.Write(rr) - }) f.mux.HandleFunc("GET /v4/networking/firewalls/{firewallId}/devices", func(w http.ResponseWriter, r *http.Request) { @@ -331,7 +329,6 @@ func (f *fakeAPI) setupRoutes() { f.t.Fatal(err) } _, _ = w.Write(resp) - return }) f.mux.HandleFunc("POST /v4/nodebalancers/{nodeBalancerId}/configs", func(w http.ResponseWriter, r *http.Request) { @@ -441,7 +438,6 @@ func (f *fakeAPI) setupRoutes() { f.t.Fatal(err) } _, _ = w.Write(resp) - }) f.mux.HandleFunc("POST /v4/networking/firewalls", func(w http.ResponseWriter, r *http.Request) { From 5b650113bbfe2d7faaa51e09368d30e08ea0e92d Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 16:00:30 +0200 Subject: [PATCH 6/7] symbol rename + happy paths to the left --- cloud/linode/fake_linode_test.go | 38 +++++++++++++++++--------------- 1 file changed, 20 insertions(+), 18 deletions(-) diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index f4891988..9897b149 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -111,11 +111,7 @@ func (f *fakeAPI) setupRoutes() { f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}", func(w http.ResponseWriter, r *http.Request) { nb, found := f.nb[r.PathValue("nodeBalancerId")] - if found { - rr, _ := json.Marshal(nb) - _, _ = w.Write(rr) - - } else { + if !found { w.WriteHeader(404) resp := linodego.APIError{ Errors: []linodego.APIErrorReason{ @@ -124,11 +120,16 @@ func (f *fakeAPI) setupRoutes() { } rr, _ := json.Marshal(resp) _, _ = w.Write(rr) + return } + + rr, _ := json.Marshal(nb) + _, _ = w.Write(rr) + }) f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}/firewalls", func(w http.ResponseWriter, r *http.Request) { - devID, err := strconv.Atoi(r.PathValue("nodeBalancerId")) + nodebalancerID, err := strconv.Atoi(r.PathValue("nodeBalancerId")) if err != nil { f.t.Fatal(err) } @@ -145,7 +146,7 @@ func (f *fakeAPI) setupRoutes() { out: for fwid, devices := range f.fwd { for _, device := range devices { - if device.Entity.ID == devID { + if device.Entity.ID == nodebalancerID { data.Data = append(data.Data, *f.fw[fwid]) data.PageOptions.Results = 1 break out @@ -227,17 +228,7 @@ func (f *fakeAPI) setupRoutes() { } firewallDevices, found := f.fwd[fwdId] - if found { - firewallDeviceList := []linodego.FirewallDevice{} - for i := range firewallDevices { - firewallDeviceList = append(firewallDeviceList, *firewallDevices[i]) - } - rr, _ := json.Marshal(linodego.FirewallDevicesPagedResponse{ - PageOptions: &linodego.PageOptions{Page: 1, Pages: 1, Results: len(firewallDeviceList)}, - Data: firewallDeviceList, - }) - _, _ = w.Write(rr) - } else { + if !found { w.WriteHeader(404) resp := linodego.APIError{ Errors: []linodego.APIErrorReason{ @@ -246,7 +237,18 @@ func (f *fakeAPI) setupRoutes() { } rr, _ := json.Marshal(resp) _, _ = w.Write(rr) + return } + + firewallDeviceList := []linodego.FirewallDevice{} + for i := range firewallDevices { + firewallDeviceList = append(firewallDeviceList, *firewallDevices[i]) + } + rr, _ := json.Marshal(linodego.FirewallDevicesPagedResponse{ + PageOptions: &linodego.PageOptions{Page: 1, Pages: 1, Results: len(firewallDeviceList)}, + Data: firewallDeviceList, + }) + _, _ = w.Write(rr) }) f.mux.HandleFunc("POST /v4/nodebalancers", func(w http.ResponseWriter, r *http.Request) { From 3372ba12fc12a488f1e5e4baf6096df247ad99b2 Mon Sep 17 00:00:00 2001 From: Ondrej Kokes Date: Mon, 8 Apr 2024 16:07:31 +0200 Subject: [PATCH 7/7] gofumpt --- cloud/linode/fake_linode_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/cloud/linode/fake_linode_test.go b/cloud/linode/fake_linode_test.go index 9897b149..c65baafd 100644 --- a/cloud/linode/fake_linode_test.go +++ b/cloud/linode/fake_linode_test.go @@ -125,7 +125,6 @@ func (f *fakeAPI) setupRoutes() { rr, _ := json.Marshal(nb) _, _ = w.Write(rr) - }) f.mux.HandleFunc("GET /v4/nodebalancers/{nodeBalancerId}/firewalls", func(w http.ResponseWriter, r *http.Request) {