Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
…ature-discovery into nx-gzip-support
  • Loading branch information
Chandan-Abhyankar committed Nov 28, 2022
2 parents c669d2f + 3139897 commit 875dce2
Show file tree
Hide file tree
Showing 43 changed files with 917 additions and 436 deletions.
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,20 @@ templates:
@# Need to prepend each line in the sample config with spaces in order to
@# fit correctly in the configmap spec.
@sed s'/^/ /' deployment/components/worker-config/nfd-worker.conf.example > nfd-worker.conf.tmp
@sed s'/^/ /' deployment/components/topology-updater-config/nfd-topology-updater.conf.example > nfd-topology-updater.conf.tmp
@# The sed magic below replaces the block of text between the lines with start and end markers
@start=NFD-WORKER-CONF-START-DO-NOT-REMOVE; \
end=NFD-WORKER-CONF-END-DO-NOT-REMOVE; \
sed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-worker.conf.tmp" \
-e "}; /$$end/p; d }" -i deployment/helm/node-feature-discovery/values.yaml
@start=NFD-TOPOLOGY-UPDATER-CONF-START-DO-NOT-REMOVE; \
end=NFD-TOPOLOGY-UPDATER-CONF-END-DO-NOT-REMOVE; \
sed -e "/$$start/,/$$end/{ /$$start/{ p; r nfd-topology-updater.conf.tmp" \
-e "}; /$$end/p; d }" -i deployment/helm/node-feature-discovery/values.yaml
@rm nfd-worker.conf.tmp
@rm nfd-topology-updater.conf.tmp



.generator.image.stamp: Dockerfile_generator
$(IMAGE_BUILD_CMD) \
Expand Down
52 changes: 46 additions & 6 deletions cmd/nfd-topology-updater/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@ package main
import (
"flag"
"fmt"
"net/url"
"os"
"time"

"k8s.io/klog/v2"
kubeletconfigv1beta1 "k8s.io/kubelet/config/v1beta1"

"sigs.k8s.io/node-feature-discovery/pkg/kubeconf"
nfdclient "sigs.k8s.io/node-feature-discovery/pkg/nfd-client"
topology "sigs.k8s.io/node-feature-discovery/pkg/nfd-client/topology-updater"
"sigs.k8s.io/node-feature-discovery/pkg/resourcemonitor"
"sigs.k8s.io/node-feature-discovery/pkg/topologypolicy"
"sigs.k8s.io/node-feature-discovery/pkg/utils"
"sigs.k8s.io/node-feature-discovery/pkg/utils/hostpath"
"sigs.k8s.io/node-feature-discovery/pkg/utils/kubeconf"
"sigs.k8s.io/node-feature-discovery/pkg/version"
)

const (
// ProgramName is the canonical name of this program
ProgramName = "nfd-topology-updater"
ProgramName = "nfd-topology-updater"
kubeletSecurePort = 10250
)

func main() {
Expand All @@ -58,10 +62,33 @@ func main() {
// Plug klog into grpc logging infrastructure
utils.ConfigureGrpcKlog()

klConfig, err := kubeconf.GetKubeletConfigFromLocalFile(resourcemonitorArgs.KubeletConfigFile)
u, err := url.ParseRequestURI(resourcemonitorArgs.KubeletConfigURI)
if err != nil {
klog.Exitf("error reading kubelet config: %v", err)
klog.Exitf("failed to parse args for kubelet-config-uri: %v", err)
}

// init kubelet API client
var klConfig *kubeletconfigv1beta1.KubeletConfiguration
switch u.Scheme {
case "file":
klConfig, err = kubeconf.GetKubeletConfigFromLocalFile(u.Path)
if err != nil {
klog.Exitf("failed to read kubelet config: %v", err)
}
case "https":
restConfig, err := kubeconf.InsecureConfig(u.String(), resourcemonitorArgs.APIAuthTokenFile)
if err != nil {
klog.Exitf("failed to initialize rest config for kubelet config uri: %v", err)
}

klConfig, err = kubeconf.GetKubeletConfiguration(restConfig)
if err != nil {
klog.Exitf("failed to get kubelet config from configz endpoint: %v", err)
}
default:
klog.Exitf("unsupported URI scheme: %v", u.Scheme)
}

tmPolicy := string(topologypolicy.DetectTopologyPolicy(klConfig.TopologyManagerPolicy, klConfig.TopologyManagerScope))
klog.Infof("detected kubelet Topology Manager policy %q", tmPolicy)

Expand All @@ -86,6 +113,15 @@ func parseArgs(flags *flag.FlagSet, osArgs ...string) (*topology.Args, *resource
os.Exit(2)
}

if len(resourcemonitorArgs.KubeletConfigURI) == 0 {
if len(nfdclient.NodeName()) == 0 {
fmt.Fprintf(flags.Output(), "unable to determine the default kubelet config endpoint 'https://${NODE_NAME}:%d/configz' due to empty NODE_NAME environment, "+
"please either define the NODE_NAME environment variable or specify endpoint with the -kubelet-config-uri flag\n", kubeletSecurePort)
os.Exit(1)
}
resourcemonitorArgs.KubeletConfigURI = fmt.Sprintf("https://%s:%d/configz", nfdclient.NodeName(), kubeletSecurePort)
}

return args, resourcemonitorArgs
}

Expand All @@ -109,14 +145,18 @@ func initFlags(flagset *flag.FlagSet) (*topology.Args, *resourcemonitor.Args) {
"Time to sleep between CR updates. Non-positive value implies no CR updatation (i.e. infinite sleep). [Default: 60s]")
flagset.StringVar(&resourcemonitorArgs.Namespace, "watch-namespace", "*",
"Namespace to watch pods (for testing/debugging purpose). Use * for all namespaces.")
flagset.StringVar(&resourcemonitorArgs.KubeletConfigFile, "kubelet-config-file", hostpath.VarDir.Path("lib/kubelet/config.yaml"),
"Kubelet config file path.")
flagset.StringVar(&resourcemonitorArgs.KubeletConfigURI, "kubelet-config-uri", "",
"Kubelet config URI path. Default to kubelet configz endpoint.")
flagset.StringVar(&resourcemonitorArgs.APIAuthTokenFile, "api-auth-token-file", "/var/run/secrets/kubernetes.io/serviceaccount/token",
"API auth token file path. It is used to request kubelet configz endpoint, only takes effect when kubelet-config-uri is https. Default to /var/run/secrets/kubernetes.io/serviceaccount/token.")
flagset.StringVar(&resourcemonitorArgs.PodResourceSocketPath, "podresources-socket", hostpath.VarDir.Path("lib/kubelet/pod-resources/kubelet.sock"),
"Pod Resource Socket path to use.")
flagset.StringVar(&args.Server, "server", "localhost:8080",
"NFD server address to connecto to.")
flagset.StringVar(&args.ServerNameOverride, "server-name-override", "",
"Hostname expected from server certificate, useful in testing")
flagset.StringVar(&args.ConfigFile, "config", "/etc/kubernetes/node-feature-discovery/nfd-topology-updater.conf",
"Config file to use.")

klog.InitFlags(flagset)

Expand Down
22 changes: 12 additions & 10 deletions cmd/nfd-topology-updater/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,53 +29,55 @@ func TestArgsParse(t *testing.T) {
flags := flag.NewFlagSet(ProgramName, flag.ExitOnError)

Convey("When -no-publish and -oneshot flags are passed", func() {
args, finderArgs := parseArgs(flags, "-oneshot", "-no-publish")
args, finderArgs := parseArgs(flags, "-oneshot", "-no-publish", "-kubelet-config-uri=https://%s:%d/configz")

Convey("noPublish is set and args.sources is set to the default value", func() {
So(args.NoPublish, ShouldBeTrue)
So(args.Oneshot, ShouldBeTrue)
So(args.ConfigFile, ShouldEqual, "/etc/kubernetes/node-feature-discovery/nfd-topology-updater.conf")
So(finderArgs.SleepInterval, ShouldEqual, 60*time.Second)
So(finderArgs.KubeletConfigFile, ShouldEqual, "/var/lib/kubelet/config.yaml")
So(finderArgs.PodResourceSocketPath, ShouldEqual, "/var/lib/kubelet/pod-resources/kubelet.sock")
})
})

Convey("When valid args are specified for -kubelet-config-file and -sleep-interval,", func() {
Convey("When valid args are specified for -kubelet-config-url, -sleep-interval and -config,", func() {
args, finderArgs := parseArgs(flags,
"-kubelet-config-file=/path/testconfig.yaml",
"-sleep-interval=30s")
"-kubelet-config-uri=file:///path/testconfig.yaml",
"-sleep-interval=30s",
"-config=/path/nfd-topology-updater.conf")

Convey("args.sources is set to appropriate values", func() {
So(args.NoPublish, ShouldBeFalse)
So(args.Oneshot, ShouldBeFalse)
So(args.ConfigFile, ShouldEqual, "/path/nfd-topology-updater.conf")
So(finderArgs.SleepInterval, ShouldEqual, 30*time.Second)
So(finderArgs.KubeletConfigFile, ShouldEqual, "/path/testconfig.yaml")
So(finderArgs.KubeletConfigURI, ShouldEqual, "file:///path/testconfig.yaml")
So(finderArgs.PodResourceSocketPath, ShouldEqual, "/var/lib/kubelet/pod-resources/kubelet.sock")
})
})

Convey("When valid args are specified for -podresources-socket flag and -sleep-interval is specified", func() {
args, finderArgs := parseArgs(flags,
"-kubelet-config-uri=https://%s:%d/configz",
"-podresources-socket=/path/testkubelet.sock",
"-sleep-interval=30s")

Convey("args.sources is set to appropriate values", func() {
So(args.NoPublish, ShouldBeFalse)
So(args.Oneshot, ShouldBeFalse)
So(finderArgs.SleepInterval, ShouldEqual, 30*time.Second)
So(finderArgs.KubeletConfigFile, ShouldEqual, "/var/lib/kubelet/config.yaml")
So(finderArgs.PodResourceSocketPath, ShouldEqual, "/path/testkubelet.sock")
})
})
Convey("When valid -sleep-inteval is specified", func() {
args, finderArgs := parseArgs(flags,
"-kubelet-config-uri=https://%s:%d/configz",
"-sleep-interval=30s")

Convey("args.sources is set to appropriate values", func() {
So(args.NoPublish, ShouldBeFalse)
So(args.Oneshot, ShouldBeFalse)
So(finderArgs.SleepInterval, ShouldEqual, 30*time.Second)
So(finderArgs.KubeletConfigFile, ShouldEqual, "/var/lib/kubelet/config.yaml")
So(finderArgs.PodResourceSocketPath, ShouldEqual, "/var/lib/kubelet/pod-resources/kubelet.sock")
})
})
Expand All @@ -84,7 +86,7 @@ func TestArgsParse(t *testing.T) {
args, finderArgs := parseArgs(flags,
"-no-publish",
"-sleep-interval=30s",
"-kubelet-config-file=/path/testconfig.yaml",
"-kubelet-config-uri=file:///path/testconfig.yaml",
"-podresources-socket=/path/testkubelet.sock",
"-ca-file=ca",
"-cert-file=crt",
Expand All @@ -96,7 +98,7 @@ func TestArgsParse(t *testing.T) {
So(args.CertFile, ShouldEqual, "crt")
So(args.KeyFile, ShouldEqual, "key")
So(finderArgs.SleepInterval, ShouldEqual, 30*time.Second)
So(finderArgs.KubeletConfigFile, ShouldEqual, "/path/testconfig.yaml")
So(finderArgs.KubeletConfigURI, ShouldEqual, "file:///path/testconfig.yaml")
So(finderArgs.PodResourceSocketPath, ShouldEqual, "/path/testkubelet.sock")
})
})
Expand Down
21 changes: 0 additions & 21 deletions cmd/nfd-worker/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,6 @@ func parseArgs(flags *flag.FlagSet, osArgs ...string) *worker.Args {
args.Overrides.FeatureSources = overrides.FeatureSources
case "label-sources":
args.Overrides.LabelSources = overrides.LabelSources
case "label-whitelist":
klog.Warningf("-label-whitelist is deprecated, use 'core.labelWhiteList' option in the config file, instead")
args.Overrides.LabelWhiteList = overrides.LabelWhiteList
case "sleep-interval":
klog.Warningf("-sleep-interval is deprecated, use 'core.sleepInterval' option in the config file, instead")
args.Overrides.SleepInterval = overrides.SleepInterval
case "sources":
klog.Warningf("-sources is deprecated, use '-label-sources' flag, instead")
args.Overrides.LabelSources = overrides.LabelSources
}
})

Expand Down Expand Up @@ -124,7 +115,6 @@ func initFlags(flagset *flag.FlagSet) (*worker.Args, *worker.ConfigOverrideArgs)

// Flags overlapping with config file options
overrides := &worker.ConfigOverrideArgs{
LabelWhiteList: &utils.RegexpVal{},
FeatureSources: &utils.StringSliceVal{},
LabelSources: &utils.StringSliceVal{},
}
Expand All @@ -136,17 +126,6 @@ func initFlags(flagset *flag.FlagSet) (*worker.Args, *worker.ConfigOverrideArgs)
flagset.Var(overrides.LabelSources, "label-sources",
"Comma separated list of label sources. Special value 'all' enables all sources. "+
"Prefix the source name with '-' to disable it.")
flagset.Var(overrides.LabelWhiteList, "label-whitelist",
"Regular expression to filter label names to publish to the Kubernetes API server. "+
"NB: the label namespace is omitted i.e. the filter is only applied to the name part after '/'. "+
"DEPRECATED: This parameter should be set via the config file.")
overrides.SleepInterval = flagset.Duration("sleep-interval", 0,
"Time to sleep between re-labeling. Non-positive value implies no re-labeling (i.e. infinite sleep). "+
"DEPRECATED: This parameter should be set via the config file")
flagset.Var(overrides.LabelSources, "sources",
"Comma separated list of label sources. Special value 'all' enables all feature sources. "+
"Prefix the source name with '-' to disable it. "+
"DEPRECATED: use -label-sources instead")

return args, overrides
}
Expand Down
9 changes: 1 addition & 8 deletions cmd/nfd-worker/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ package main
import (
"flag"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"

Expand All @@ -36,8 +35,6 @@ func TestParseArgs(t *testing.T) {
Convey("overrides should be nil", func() {
So(args.Oneshot, ShouldBeTrue)
So(args.Overrides.NoPublish, ShouldBeNil)
So(args.Overrides.LabelWhiteList, ShouldBeNil)
So(args.Overrides.SleepInterval, ShouldBeNil)
So(args.Overrides.FeatureSources, ShouldBeNil)
So(args.Overrides.LabelSources, ShouldBeNil)
})
Expand All @@ -46,18 +43,14 @@ func TestParseArgs(t *testing.T) {
Convey("When all override args are specified", func() {
args := parseArgs(flags,
"-no-publish",
"-label-whitelist=.*rdt.*",
"-feature-sources=cpu",
"-label-sources=fake1,fake2,fake3",
"-sleep-interval=30s")
"-label-sources=fake1,fake2,fake3")

Convey("args.sources is set to appropriate values", func() {
So(args.Oneshot, ShouldBeFalse)
So(*args.Overrides.NoPublish, ShouldBeTrue)
So(*args.Overrides.SleepInterval, ShouldEqual, 30*time.Second)
So(*args.Overrides.FeatureSources, ShouldResemble, utils.StringSliceVal{"cpu"})
So(*args.Overrides.LabelSources, ShouldResemble, utils.StringSliceVal{"fake1", "fake2", "fake3"})
So(args.Overrides.LabelWhiteList.Regexp.String(), ShouldResemble, ".*rdt.*")
})
})
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ rules:
verbs:
- get
- list
- apiGroups:
- ""
resources:
- nodes/proxy
verbs:
- get
- apiGroups:
- ""
resources:
Expand Down
10 changes: 10 additions & 0 deletions deployment/components/topology-updater-config/kustomization.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: kustomize.config.k8s.io/v1alpha1
kind: Component

generatorOptions:
disableNameSuffixHash: true

configMapGenerator:
- files:
- nfd-topology-updater.conf=nfd-topology-updater.conf.example
name: nfd-topology-updater-conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## key = node name, value = list of resources to be excluded.
## use * to exclude from all nodes.
## an example for how the exclude list should looks like
#excludeList:
# node1: [cpu]
# node2: [memory, example/deviceA]
# *: [hugepages-2Mi]
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
- name: kubelet-podresources-sock
hostPath:
path: /var/lib/kubelet/pod-resources/kubelet.sock
- name: nfd-topology-updater-conf
configMap:
name: nfd-topology-updater-conf

- op: add
path: /spec/template/spec/containers/0/volumeMounts
Expand All @@ -20,10 +23,9 @@
mountPath: /host-var/lib/kubelet/pod-resources/kubelet.sock
- name: host-sys
mountPath: /host-sys

- op: add
path: /spec/template/spec/containers/0/args/-
value: "-kubelet-config-file=/host-var/lib/kubelet/config.yaml"
- name: nfd-topology-updater-conf
mountPath: "/etc/kubernetes/node-feature-discovery"
readOnly: true

- op: add
path: /spec/template/spec/containers/0/args/-
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@ rules:
- patch
- update
- list
- apiGroups:
- ""
resources:
- nodes/proxy
verbs:
- get
- apiGroups:
- nfd.k8s-sigs.io
resources:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ include "node-feature-discovery.fullname" . }}-topology-updater-conf
namespace: {{ include "node-feature-discovery.namespace" . }}
labels:
{{- include "node-feature-discovery.labels" . | nindent 4 }}
data:
nfd-topology-updater.conf: |-
{{- .Values.topologyUpdater.config | toYaml | nindent 4 }}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ spec:
mountPath: "/etc/kubernetes/node-feature-discovery/certs"
readOnly: true
{{- end }}
- name: topology-updater-conf
mountPath: "/etc/kubernetes/node-feature-discovery"
readOnly: true

resources:
{{- toYaml .Values.topologyUpdater.resources | nindent 12 }}
Expand All @@ -91,12 +94,19 @@ spec:
{{- else }}
path: /var/lib/kubelet/pod-resources/kubelet.sock
{{- end }}
- name: nfd-topology-updater-conf
configMap:
name: {{ include "node-feature-discovery.fullname" . }}-topology-updater-conf
items:
- key: nfd-topology-updater.conf
path: nfd-topology-updater.conf
{{- if .Values.tls.enable }}
- name: nfd-topology-updater-cert
secret:
secretName: nfd-topology-updater-cert
{{- end }}


{{- with .Values.topologyUpdater.nodeSelector }}
nodeSelector:
{{- toYaml . | nindent 8 }}
Expand Down
Loading

0 comments on commit 875dce2

Please sign in to comment.