Skip to content

Commit 4959a13

Browse files
committed
nfd-worker: replace --metrics with --port
Use a single port for serving http. In addition to metrics we will have the healthz endpoint.
1 parent 25914ec commit 4959a13

File tree

6 files changed

+27
-18
lines changed

6 files changed

+27
-18
lines changed

cmd/nfd-worker/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,8 @@ func initFlags(flagset *flag.FlagSet) (*worker.Args, *worker.ConfigOverrideArgs)
108108
"Kubeconfig to use")
109109
flagset.BoolVar(&args.Oneshot, "oneshot", false,
110110
"Do not publish feature labels")
111-
flagset.IntVar(&args.MetricsPort, "metrics", 8081,
112-
"Port on which to expose metrics.")
111+
flagset.IntVar(&args.Port, "port", 8080,
112+
"Port on which to metrics and healthz endpoints are served")
113113
flagset.StringVar(&args.Options, "options", "",
114114
"Specify config options from command line. Config options are specified "+
115115
"in the same format as in the config file (i.e. json or yaml). These options")

deployment/base/worker-daemonset/worker-daemonset.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,5 +36,5 @@ spec:
3636
cpu: 5m
3737
memory: 64Mi
3838
ports:
39-
- name: metrics
40-
containerPort: 8081
39+
- name: http
40+
containerPort: 8080

deployment/helm/node-feature-discovery/templates/worker.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,13 +100,13 @@ spec:
100100
{{- range $key, $value := .Values.featureGates }}
101101
- "-feature-gates={{ $key }}={{ $value }}"
102102
{{- end }}
103-
- "-metrics={{ .Values.worker.metricsPort | default "8081"}}"
103+
- "-port={{ .Values.worker.port | default "8080"}}"
104104
{{- with .Values.worker.extraArgs }}
105105
{{- toYaml . | nindent 8 }}
106106
{{- end }}
107107
ports:
108-
- containerPort: {{ .Values.worker.metricsPort | default "8081"}}
109-
name: metrics
108+
- containerPort: {{ .Values.worker.port | default "8080"}}
109+
name: http
110110
volumeMounts:
111111
- name: host-boot
112112
mountPath: "/host-boot"

deployment/helm/node-feature-discovery/values.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -418,7 +418,7 @@ worker:
418418
# matchName: {op: In, value: ["SWAP", "X86", "ARM"]}
419419
### <NFD-WORKER-CONF-END-DO-NOT-REMOVE>
420420

421-
metricsPort: 8081
421+
port: 8080
422422
daemonsetAnnotations: {}
423423
podSecurityContext: {}
424424
# fsGroup: 2000

docs/deployment/helm.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ API's you need to install the prometheus operator in your cluster.
233233
| `worker.*` | dict | | NFD worker daemonset configuration |
234234
| `worker.enable` | bool | true | Specifies whether nfd-worker should be deployed |
235235
| `worker.hostNetwork` | bool | false | Specifies whether to enable or disable running the container in the host's network namespace |
236-
| `worker.metricsPort` | int | 8081 | Port on which to expose metrics from components to prometheus operator. **DEPRECATED**: will be replaced by `worker.port` in NFD v0.18. |
236+
| `worker.port` | int | 8080 | Port on which to serve http for metrics and healthz endpoints. |
237237
| `worker.config` | dict | | NFD worker [configuration](../reference/worker-configuration-reference) |
238238
| `worker.podSecurityContext` | dict | {} | [PodSecurityContext](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-pod) holds pod-level security attributes and common container settins |
239239
| `worker.securityContext` | dict | {} | Container [security settings](https://kubernetes.io/docs/tasks/configure-pod-container/security-context/#set-the-security-context-for-a-container) |

pkg/nfd-worker/nfd-worker.go

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,16 @@ package nfdworker
1919
import (
2020
"encoding/json"
2121
"fmt"
22+
"net/http"
2223
"os"
2324
"path/filepath"
2425
"regexp"
2526
"sort"
2627
"strings"
2728
"time"
2829

30+
"github.com/prometheus/client_golang/prometheus"
31+
"github.com/prometheus/client_golang/prometheus/promhttp"
2932
"golang.org/x/exp/maps"
3033
"golang.org/x/net/context"
3134
"k8s.io/apimachinery/pkg/api/errors"
@@ -94,7 +97,7 @@ type Args struct {
9497
Kubeconfig string
9598
Oneshot bool
9699
Options string
97-
MetricsPort int
100+
Port int
98101
NoOwnerRefs bool
99102

100103
Overrides ConfigOverrideArgs
@@ -295,15 +298,13 @@ func (w *nfdWorker) Run() error {
295298
labelTrigger.Reset(w.config.Core.SleepInterval.Duration)
296299
defer labelTrigger.Stop()
297300

301+
httpMux := http.NewServeMux()
302+
298303
// Register to metrics server
299-
if w.args.MetricsPort > 0 {
300-
m := utils.CreateMetricsServer(w.args.MetricsPort,
301-
buildInfo,
302-
featureDiscoveryDuration)
303-
go m.Run()
304-
registerVersion(version.Get())
305-
defer m.Stop()
306-
}
304+
promRegistry := prometheus.NewRegistry()
305+
promRegistry.MustRegister(buildInfo, featureDiscoveryDuration)
306+
httpMux.Handle("/metrics", promhttp.HandlerFor(promRegistry, promhttp.HandlerOpts{}))
307+
registerVersion(version.Get())
307308

308309
err = w.runFeatureDiscovery()
309310
if err != nil {
@@ -317,6 +318,14 @@ func (w *nfdWorker) Run() error {
317318

318319
// Start readiness probe (at this point we're "ready and live")
319320

321+
// Start HTTP server
322+
httpServer := http.Server{Addr: fmt.Sprintf(":%d", w.args.Port), Handler: httpMux}
323+
go func() {
324+
klog.InfoS("http server starting", "port", httpServer.Addr)
325+
klog.InfoS("http server stopped", "exitCode", httpServer.ListenAndServe())
326+
}()
327+
defer httpServer.Close()
328+
320329
for {
321330
select {
322331
case <-labelTrigger.C:

0 commit comments

Comments
 (0)