Skip to content

Commit

Permalink
Updates
Browse files Browse the repository at this point in the history
- Update README
- Update to go1.18
- Update to k8s v1.24.3
- Reworked vfstats collector
- Implemented endpoint unit tests
- Add netlink support detection
- Add image building to Makefile
- Remove deprecated references
- Add Mellanox driver to drivers DB
- Refactor code to enable testing
- Support for NFD SR-IOV feature label
- Changes to ensure more uniform Makefile
- Implemented initial unit tests
- Implemented vfstats package unit tests

Co-Authored-By: Eoghan1232 <[email protected]>
Co-Authored-By: eoghanlawless <[email protected]>
Co-Authored-By: Ipawlikx <[email protected]>
Co-Authored-By: nhennigan <[email protected]>
  • Loading branch information
5 people committed Jan 31, 2023
1 parent 18d24f7 commit 031d7f9
Show file tree
Hide file tree
Showing 17 changed files with 1,383 additions and 174 deletions.
41 changes: 41 additions & 0 deletions .github/workflows/codeql.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: "CodeQL"

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]
schedule:
- cron: "1 7 * * 5"

jobs:
analyze:
name: Analyze
runs-on: ubuntu-latest
permissions:
actions: read
contents: read
security-events: write

strategy:
fail-fast: false
matrix:
language: [ go ]

steps:
- name: Checkout
uses: actions/checkout@v3

- name: Initialize CodeQL
uses: github/codeql-action/init@v2
with:
languages: ${{ matrix.language }}
queries: +security-and-quality

- name: Autobuild
uses: github/codeql-action/autobuild@v2

- name: Perform CodeQL Analysis
uses: github/codeql-action/analyze@v2
with:
category: "/language:${{ matrix.language }}"
26 changes: 19 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ ifdef HTTPS_PROXY
DOCKERARGS += --build-arg https_proxy=$(HTTPS_PROXY)
endif

all: build
all: build docker-build test

clean:
rm -rf bin
go clean --modcache

go clean -modcache -testcache
build:
go install github.com/golangci/golangci-lint/cmd/[email protected]
go mod tidy
go fmt ./...
golangci-lint run
GO111MODULE=on go build -ldflags "-s -w" -buildmode=pie -o bin/sriov-exporter cmd/sriov-network-metrics-exporter.go

docker-build:
Expand All @@ -30,3 +26,19 @@ docker-build:

docker-push:
docker push $(IMAGE_NAME)

test:
go test ./... -coverprofile cover.out

test-coverage:
ginkgo -v -r -cover -coverprofile=cover.out --output-dir=.
go tool cover -html=cover.out

go-lint:
go install github.com/golangci/golangci-lint/cmd/[email protected]
go mod tidy
go fmt ./...
golangci-lint run --color always -v ./...

go-lint-report:
golangci-lint run --color always -v ./... &> golangci-lint.txt
2 changes: 1 addition & 1 deletion cmd/sriov-network-metrics-exporter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
"golang.org/x/time/rate"
)

func TestLogging(t *testing.T) {
func TestMain(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "main test suite")
}
Expand Down
19 changes: 11 additions & 8 deletions collectors/collectors.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,19 @@ var (
collectorNamespace = "sriov"
enabled = true
disabled = false
enabledCollectors = make(map[string]func() prometheus.Collector)
collectorState = make(map[string]*bool)
collectorFunctions = make(map[string]func() prometheus.Collector)
)

// SriovCollector registers the collectors used for specific data and exposes a Collect method to gather the data
type SriovCollector []prometheus.Collector

// Register defines a flag for a collector and adds it to the registry of enabled collectors if the flag is set to true - either through the default option or the flag passed on start
// Run by each individual collector in its init function.
func register(name string, isDefault bool, collector func() prometheus.Collector) {
if enabled := flag.Bool("collector."+name, isDefault, fmt.Sprintf("Enables the %v collector", name)); *enabled {
enabledCollectors[name] = collector
}
func register(name string, enabled bool, collector func() prometheus.Collector) {
collectorState[name] = &enabled
collectorFunctions[name] = collector
flag.BoolVar(collectorState[name], "collector."+name, enabled, fmt.Sprintf("Enables the %v collector", name))
}

// Collect metrics from all enabled collectors in unordered sequence.
Expand All @@ -46,9 +47,11 @@ func (s SriovCollector) Describe(ch chan<- *prometheus.Desc) {
// Enabled adds collectors enabled by default or command line flag to an SriovCollector object
func Enabled() SriovCollector {
collectors := make([]prometheus.Collector, 0)
for collectorName, collectorFunc := range enabledCollectors {
log.Printf("The %v collector is enabled", collectorName)
collectors = append(collectors, collectorFunc())
for collector, enabled := range collectorState {
if enabled != nil && *enabled {
log.Printf("The %v collector is enabled", collector)
collectors = append(collectors, collectorFunctions[collector]())
}
}
return collectors
}
Expand Down
101 changes: 101 additions & 0 deletions collectors/collectors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package collectors

import (
"fmt"
"io/fs"
"log"
"path/filepath"
"testing"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gbytes"
"github.com/prometheus/client_golang/prometheus"

"sriov-network-metrics-exporter/pkg/utils"
)

var buffer gbytes.Buffer

func TestCollectors(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "collectors test suite")
}

var _ = BeforeSuite(func() {
utils.EvalSymlinks = evalSymlinks

logFatal = func(msg string, args ...any) {
log.Printf(msg, args...)
}

log.SetFlags(0)
})

var _ = BeforeEach(func() {
buffer = *gbytes.NewBuffer()
log.SetOutput(&buffer)
})

type metric struct {
labels map[string]string
counter float64
}

type testCollector struct {
name string
}

func createTestCollector() prometheus.Collector {
return testCollector{
name: "collector.test",
}
}

func (c testCollector) Collect(ch chan<- prometheus.Metric) {}
func (c testCollector) Describe(chan<- *prometheus.Desc) {}

var _ = DescribeTable("test registering collector", // register
func(name string, enabled bool, collector func() prometheus.Collector) {
register(name, enabled, collector)

Expect(collectorState).To(HaveKey(name))
Expect(collectorState[name]).To(Equal(&enabled))

Expect(collectorFunctions).To(HaveKey(name))
// Expect(allCollectors[name]).To(Equal(collector)) // TODO: verify expected collector is returned

},
Entry("the correct collector is enabled when default is true",
"test_true",
true,
createTestCollector),
Entry("the correct collector is not enabled when default is false",
"test_false",
false,
createTestCollector),
)

// TODO: create Enabled unit test

func assertLogs(logs []string) {
for _, log := range logs {
Eventually(&buffer).WithTimeout(time.Duration(2 * time.Second)).Should(gbytes.Say(log))
}
}

// Replaces filepath.EvalSymlinks with an emulated evaluation to work with the in-memory fs.
var evalSymlinks = func(path string) (string, error) {
path = filepath.Join(filepath.Base(filepath.Dir(path)), filepath.Base(path))

if stat, err := fs.Stat(devfs, path); err == nil && stat.Mode() == fs.ModeSymlink {
if target, err := fs.ReadFile(devfs, path); err == nil {
return string(target), nil
} else {
return "", fmt.Errorf("error")
}
} else {
return "", fmt.Errorf("error")
}
}
Loading

0 comments on commit 031d7f9

Please sign in to comment.