Skip to content

Commit

Permalink
Add e2e test
Browse files Browse the repository at this point in the history
Add a test that deploys the example manifest to a kind cluster and
checks if any of the configured labels are attachted to the k8s node.
  • Loading branch information
leonnicolas committed Dec 7, 2024
1 parent 7da4131 commit 7f16429
Show file tree
Hide file tree
Showing 11 changed files with 222 additions and 27 deletions.
2 changes: 0 additions & 2 deletions .dockerignore

This file was deleted.

12 changes: 0 additions & 12 deletions .github/workflows/build.yaml

This file was deleted.

14 changes: 7 additions & 7 deletions .github/workflows/push.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ jobs:
main:
runs-on: ubuntu-latest
steps:
-
-
name: Checkout
uses: actions/checkout@v2
uses: actions/checkout@v4
- id: sha
run: echo "::set-output name=sha::$(git describe --always --tags --dirty)"
-
name: Set up QEMU
uses: docker/setup-qemu-action@v1
uses: docker/setup-qemu-action@v3
-
name: Set up Docker Buildx
uses: docker/setup-buildx-action@v1
uses: docker/setup-buildx-action@v3
-
name: Login to DockerHub
uses: docker/login-action@v1
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PW }}
-
name: Login to GitHub Container Registry
uses: docker/login-action@v1
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
-
name: Build and push
id: docker_build
uses: docker/build-push-action@v2
uses: docker/build-push-action@v6
with:
push: true
platforms: linux/arm64, linux/arm, linux/amd64
Expand Down
16 changes: 16 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
name: test

on:
pull_request:

jobs:
e2e:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4
- run: |
sudo apt update
sudo apt install libusb-1.0-0-dev -y
- run: docker build -t "nudl:e2e" .
- run: go test .
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM golang:1.23-bookworm as build
FROM golang:1.23-bookworm AS build

RUN apt-get update && apt-get install libusb-1.0-0-dev -y

Expand All @@ -7,7 +7,7 @@ WORKDIR /nudl
COPY go.mod go.sum /nudl/
RUN go mod download

COPY . /nudl
COPY main.go /nudl
RUN ls -la
WORKDIR /nudl
RUN go build -o nudl
Expand Down
65 changes: 65 additions & 0 deletions e2e.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
apiVersion: v1
kind: ServiceAccount
metadata:
name: nudl
namespace: default
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRole
metadata:
name: nudl
rules:
- apiGroups:
- ""
resources:
- nodes
verbs:
- patch
- get
---
apiVersion: rbac.authorization.k8s.io/v1
kind: ClusterRoleBinding
metadata:
name: nudl
roleRef:
apiGroup: rbac.authorization.k8s.io
kind: ClusterRole
name: nudl
subjects:
- kind: ServiceAccount
name: nudl
namespace: default
---
kind: DaemonSet
apiVersion: apps/v1
metadata:
name: nudl
labels:
app.kubernetes.io/name: nudl
spec:
selector:
matchLabels:
app.kubernetes.io/name: nudl
template:
metadata:
labels:
app.kubernetes.io/name: nudl
spec:
serviceAccountName: nudl
containers:
- name: nudl
image: "nudl:e2e"
imagePullPolicy: IfNotPresent
args:
- --hostname=$(NODE_NAME)
- --no-contain=usb,hub
- --human-readable=false
- --only=test_test
env:
- name: NODE_NAME
valueFrom:
fieldRef:
fieldPath: spec.nodeName
ports:
- name: http
containerPort: 8080
5 changes: 3 additions & 2 deletions example.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ subjects:
---
kind: DaemonSet
apiVersion: apps/v1
metadata:
metadata:
name: nudl
labels:
app.kubernetes.io/name: nudl
Expand All @@ -49,7 +49,8 @@ spec:
containers:
- name: nudl
image: ghcr.io/leonnicolas/nudl
args:
imagePullPolicy: IfNotPresent
args:
- --hostname=$(NODE_NAME)
- --no-contain=usb,hub
env:
Expand Down
6 changes: 6 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,20 @@ module github.com/leonnicolas/nudl
go 1.23.2

require (
github.com/efficientgo/core v1.0.0-rc.0
github.com/efficientgo/e2e v0.14.1-0.20240418111536-97db25a0c6c0
github.com/go-kit/log v0.2.1
github.com/google/gousb v1.1.3
github.com/prometheus/client_golang v1.19.0
github.com/spf13/pflag v1.0.5
github.com/stretchr/testify v1.8.4
k8s.io/api v0.30.0
k8s.io/apimachinery v0.30.0
k8s.io/client-go v0.30.0
)

replace github.com/efficientgo/e2e v0.14.1-0.20240418111536-97db25a0c6c0 => github.com/leonnicolas/e2e v0.14.1-0.20241206212748-bd1e26e8cb50

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
Expand All @@ -34,6 +39,7 @@ require (
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/prometheus/client_model v0.5.0 // indirect
github.com/prometheus/common v0.48.0 // indirect
github.com/prometheus/procfs v0.12.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ3
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/efficientgo/core v1.0.0-rc.0 h1:jJoA0N+C4/knWYVZ6GrdHOtDyrg8Y/TR4vFpTaqTsqs=
github.com/efficientgo/core v1.0.0-rc.0/go.mod h1:kQa0V74HNYMfuJH6jiPiwNdpWXl4xd/K4tzlrcvYDQI=
github.com/emicklei/go-restful/v3 v3.11.0 h1:rAQeMHw1c7zTmncogyy8VvRZwtkmkZ4FxERmMY4rD+g=
github.com/emicklei/go-restful/v3 v3.11.0/go.mod h1:6n3XBCmQQb25CM2LCACGz8ukIrRry+4bhvbpWn3mrbc=
github.com/go-kit/log v0.2.1 h1:MRVx0/zhvdseW+Gza6N9rVzU/IVzaeE1SFI4raAhmBU=
Expand Down Expand Up @@ -45,6 +47,8 @@ github.com/imdario/mergo v0.3.13 h1:lFzP57bqS/wsqKssCGmtLAb8A0wKjLGrve2q3PPVcBk=
github.com/imdario/mergo v0.3.13/go.mod h1:4lJ1jqUDcsbIECGy0RUJAXNIhg+6ocWgb1ALK2O4oXg=
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=
github.com/jpillora/backoff v1.0.0 h1:uvFg412JmmHBHw7iwprIxkPMI+sGQ4kzOWsMeHnm2EA=
github.com/jpillora/backoff v1.0.0/go.mod h1:J/6gKK9jxlEcS3zixgDgUAsiuZ7yrSoa/FX5e0EB2j4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand All @@ -56,6 +60,8 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
github.com/leonnicolas/e2e v0.14.1-0.20241206212748-bd1e26e8cb50 h1:F9prGfkP2ObQLelUQMpTNPO0ChjBrxZedjhBVv5vWJI=
github.com/leonnicolas/e2e v0.14.1-0.20241206212748-bd1e26e8cb50/go.mod h1:plsKU0YHE9uX+7utvr7SiDtVBSHJyEfHRO4UnUgDmts=
github.com/mailru/easyjson v0.7.7 h1:UGYAvKxe3sBsEDzO8ZeWOSlIQfWFlxbzLZe7hwFURr0=
github.com/mailru/easyjson v0.7.7/go.mod h1:xzfreul335JAWq5oZzymOObrkdz5UnU4kGfJJLY9Nlc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
Expand All @@ -65,6 +71,8 @@ github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9G
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 h1:C3w9PqII01/Oq1c1nUAm88MOHcQC9l5mIlSMApZMrHA=
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822/go.mod h1:+n7T8mK8HuQTcFwEeznm/DIxMOiR9yIdICNftLE1DvQ=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f h1:KUppIJq7/+SVif2QVs3tOP0zanoHgBEVAwHxUSIzRqU=
github.com/mwitkow/go-conntrack v0.0.0-20190716064945-2f068394615f/go.mod h1:qRWi+5nqEBWmkhHvq77mSJWrCKwh8bxhgT7d/eI7P4U=
github.com/onsi/ginkgo/v2 v2.15.0 h1:79HwNRBAZHOEwrczrgSOPy+eFTTlIGELKy5as+ClttY=
github.com/onsi/ginkgo/v2 v2.15.0/go.mod h1:HlxMHtYF57y6Dpf+mc5529KKmSq9h2FpCF+/ZkwUxKM=
github.com/onsi/gomega v1.31.0 h1:54UJxxj6cPInHS3a35wm6BK/F9nHYueZ1NVujHDrnXE=
Expand Down
24 changes: 22 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ var (
kubeconfig = flag.String("kubeconfig", "", "path to kubeconfig")
hostname = flag.String("hostname", "", "Hostname of the node on which this process is running")
noContain = flag.StringSlice("no-contain", []string{}, "list of strings, usb devices containing these case-insensitive strings will not be considered for labeling")
only = flag.StringSlice("only", []string{}, "list of strings in the format of <vendor id>_<product id>. These usb devices are considered for labeling only. If a provided device is not found, the label value will be set to false.")
logLevel = flag.String("log-level", logLevelInfo, fmt.Sprintf("Log level to use. Possible values: %s", availableLogLevels))
updateTime = flag.Duration("update-time", 10*time.Second, "renewal time for labels in seconds")
labelPrefix = flag.String("label-prefix", "nudl.squat.ai", "prefix for labels")
Expand Down Expand Up @@ -84,6 +85,10 @@ var (
regTrim *regexp.Regexp = regexp.MustCompile(`[^\w._-]`)
)

func sprintLabelKey(k string) string {
return fmt.Sprintf("%s/%s", *labelPrefix, k)
}

// genKey generates a key with prefix labelPrefix out of a device description.
func genKey(desc *gousb.DeviceDesc) string {
var key string
Expand All @@ -99,7 +104,7 @@ func genKey(desc *gousb.DeviceDesc) string {
} else {
key = fmt.Sprintf("%s_%s", desc.Vendor.String(), desc.Product.String())
}
return fmt.Sprintf("%s/%s", *labelPrefix, key)
return sprintLabelKey(key)
}

// createLables is a wrapper function to pass it to gousb.Context.OpenDevices().
Expand All @@ -112,7 +117,8 @@ func createLabels(nl *labels) func(*gousb.DeviceDesc) bool {
return false
}
}
(*nl)[genKey(desc)] = "true"
(*nl)[genKey(desc)] = fmt.Sprintf("%t", true)

return false
}
}
Expand All @@ -128,6 +134,15 @@ func scanUSB() (labels, error) {
if _, err := ctx.OpenDevices(createLabels(&l)); err != nil {
return nil, err
}

if len(*only) > 0 {
onlyLabels := make(labels)
for _, str := range *only {
_, ok := l[sprintLabelKey(str)]
onlyLabels[sprintLabelKey(str)] = fmt.Sprintf("%t", ok)
}
return onlyLabels, nil
}
return l, nil
}

Expand Down Expand Up @@ -263,8 +278,13 @@ func Main() error {
logger = log.With(logger, "ts", log.DefaultTimestampUTC)
logger = log.With(logger, "caller", log.DefaultCaller)

if len(*only) > 0 && *humanReadable {
return fmt.Errorf("only and human-readable flags are mutually exclusive")
}

// Create context to be able to cancel calls to the Kubernetes API in clean up.
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Create prometheus registry instead of using default one.
r := prometheus.NewRegistry()
Expand Down
Loading

0 comments on commit 7f16429

Please sign in to comment.