Skip to content

Commit

Permalink
orchestrator/clickhouse: remove previous networks.csv temporary files
Browse files Browse the repository at this point in the history
Fix #1221
  • Loading branch information
vincentbernat committed May 15, 2024
1 parent 3b669ff commit 600f22f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 5 deletions.
1 change: 1 addition & 0 deletions console/data/docs/99-changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ identified with a specific icon:

- 💥 *console*: persist metadata cache on the default `docker compose` setup
- 🩹 *clickhouse*: disable experimental analyzer on recent versions of ClickHouse
- 🩹 *orchestrator*: remove previous networks.csv temporary files on start
- 🌱 *console*: add support for PostgreSQL and MySQL to store filters
- 🌱 *console*: add `console``homepage-graph-timerange` to define the time range for the homepage graph
- 🌱 *docker*: update to Traefik 3.0 (not mandatory)
Expand Down
13 changes: 12 additions & 1 deletion orchestrator/clickhouse/networks.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"encoding/csv"
"net"
"os"
"path/filepath"
"strconv"
"time"

Expand All @@ -19,6 +20,8 @@ import (
"akvorado/orchestrator/geoip"
)

const networksCSVPattern = "networks*.csv.gz"

func (c *Component) refreshNetworksCSV() {
select {
case c.networksCSVUpdateChan <- true:
Expand Down Expand Up @@ -120,8 +123,16 @@ func (c *Component) networksCSVRefresher() {
}
}

// Clean up old files
oldFiles, err := filepath.Glob(filepath.Join(os.TempDir(), networksCSVPattern))
if err == nil {
for _, oldFile := range oldFiles {
os.Remove(oldFile)
}
}

// Create a temporary file to hold results
tmpfile, err := os.CreateTemp("", "networks*.csv.gz")
tmpfile, err := os.CreateTemp("", networksCSVPattern)
if err != nil {
c.r.Err(err).Msg("cannot create temporary file for networks.csv")
return
Expand Down
53 changes: 49 additions & 4 deletions orchestrator/clickhouse/networks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
package clickhouse

import (
"os"
"path/filepath"
"testing"

"akvorado/common/clickhousedb"
Expand All @@ -21,7 +23,7 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
r := reporter.NewMock(t)
clickHouseComponent := clickhousedb.SetupClickHouse(t, r, false)

{
t.Run("only GeoIP", func(t *testing.T) {
// First use only GeoIP
c, err := New(r, config, Dependencies{
Daemon: daemon.NewMock(t),
Expand Down Expand Up @@ -61,9 +63,9 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
},
},
})
}
})

{
t.Run("custom networks", func(t *testing.T) {
// Second use: add custom networks
config.Networks = helpers.MustNewSubnetMap(map[string]NetworkAttributes{
"::ffff:12.80.0.0/112": {Name: "infra"}, // not covered by GeoIP
Expand Down Expand Up @@ -112,6 +114,49 @@ func TestNetworksCSVWithGeoip(t *testing.T) {
},
},
})
}
})

t.Run("cleanup old files", func(t *testing.T) {
_, err := os.CreateTemp("", networksCSVPattern)
if err != nil {
t.Fatalf("os.CreateTemp() error:\n%+v", err)
}
c, err := New(r, config, Dependencies{
Daemon: daemon.NewMock(t),
HTTP: httpserver.NewMock(t, r),
Schema: schema.NewMock(t),
GeoIP: geoip.NewMock(t, r, true),
ClickHouse: clickHouseComponent,
})
if err != nil {
t.Fatalf("New() error:\n%+v", err)
}
helpers.StartStop(t, c)

// HTTP request to ensure we are ready
helpers.TestHTTPEndpoints(t, c.d.HTTP.LocalAddr(), helpers.HTTPEndpointCases{
{
Description: "networks.csv",
URL: "/api/v0/orchestrator/clickhouse/networks.csv",
ContentType: "text/csv; charset=utf-8",
FirstLines: []string{
"network,name,role,site,region,country,state,city,tenant,asn",
},
},
})

// Clean up old files
got, err := filepath.Glob(filepath.Join(os.TempDir(), networksCSVPattern))
if err != nil {
t.Fatalf("filepath.Glob() error:\n%+v", err)
}
c.networksCSVLock.Lock()
expected := []string{c.networksCSVFile.Name()}
c.networksCSVLock.Unlock()

if diff := helpers.Diff(got, expected); diff != "" {
t.Fatalf("Temporary files (-got, +want):\n%s", diff)
}
})

}

0 comments on commit 600f22f

Please sign in to comment.