Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
Signed-off-by: Francesco Romani <[email protected]>
  • Loading branch information
ffromani committed Nov 29, 2024
1 parent 57c5786 commit 890e483
Show file tree
Hide file tree
Showing 17 changed files with 732 additions and 83 deletions.
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
module github.com/openshift-kni/numaresources-operator

go 1.22.0

toolchain go1.22.8
go 1.23.0

require (
github.com/aquasecurity/go-version v0.0.0-20210121072130-637058cfe492
github.com/drone/envsubst v1.0.3
github.com/go-logr/logr v1.4.2
github.com/google/go-cmp v0.6.0
github.com/jaypipes/ghw v0.13.0
github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4
github.com/k8stopologyawareschedwg/deployer v0.21.0
github.com/k8stopologyawareschedwg/noderesourcetopology-api v0.1.2
github.com/k8stopologyawareschedwg/podfingerprint v0.2.2
github.com/k8stopologyawareschedwg/resource-topology-exporter v0.20.1-0.20241128172145-f151403bf1af
github.com/k8stopologyawareschedwg/resource-topology-exporter v0.20.1-0.20241129101434-65be1bf78412
github.com/kubevirt/device-plugin-manager v1.19.4
github.com/mdomke/git-semver v1.0.0
github.com/onsi/ginkgo/v2 v2.19.1
Expand Down Expand Up @@ -71,7 +70,6 @@ require (
github.com/imdario/mergo v0.3.16 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jaypipes/pcidb v1.0.1 // indirect
github.com/jeremywohl/flatten/v2 v2.0.0-20211013061545-07e4a09fb8e4 // indirect
github.com/josharian/intern v1.0.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/mailru/easyjson v0.7.7 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -1806,8 +1806,8 @@ github.com/k8stopologyawareschedwg/noderesourcetopology-api v0.1.2 h1:uAwqOtyrFY
github.com/k8stopologyawareschedwg/noderesourcetopology-api v0.1.2/go.mod h1:LBzS4n6GX1C69tzSd5EibZ9cGOXFuHP7GxEMDYVe1sM=
github.com/k8stopologyawareschedwg/podfingerprint v0.2.2 h1:iFHPfZInM9pz2neye5RdmORMp1hPmte1EGJYpOOzZVg=
github.com/k8stopologyawareschedwg/podfingerprint v0.2.2/go.mod h1:C23pM15t06dXg/OihGlqBvnYzLr+MXDXJ7zMfbNAyXI=
github.com/k8stopologyawareschedwg/resource-topology-exporter v0.20.1-0.20241128172145-f151403bf1af h1:Vvu56PbSr+fh8Mz6UdmAr99SDejBBVzlNzv4CpHwlhw=
github.com/k8stopologyawareschedwg/resource-topology-exporter v0.20.1-0.20241128172145-f151403bf1af/go.mod h1:BqR22PCCCU7qb1qZgvrEEvtr9lxjB8oVRj2yb/IZpzA=
github.com/k8stopologyawareschedwg/resource-topology-exporter v0.20.1-0.20241129101434-65be1bf78412 h1:HFDqQVPKfklbLyN5pCvTJQPhd4XE+y96bszLU79usOE=
github.com/k8stopologyawareschedwg/resource-topology-exporter v0.20.1-0.20241129101434-65be1bf78412/go.mod h1:BqR22PCCCU7qb1qZgvrEEvtr9lxjB8oVRj2yb/IZpzA=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/kisielk/errcheck v1.1.0/go.mod h1:EZBBE59ingxPouuu3KfxchcWSUPOHkagtvWXihfKN4Q=
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
Expand Down
4 changes: 2 additions & 2 deletions rte/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (

"k8s.io/klog/v2"

"github.com/k8stopologyawareschedwg/resource-topology-exporter/pkg/config"
"github.com/k8stopologyawareschedwg/resource-topology-exporter/pkg/k8shelpers"
"github.com/k8stopologyawareschedwg/resource-topology-exporter/pkg/metrics"
metricssrv "github.com/k8stopologyawareschedwg/resource-topology-exporter/pkg/metrics/server"
Expand All @@ -35,12 +34,13 @@ import (
"github.com/k8stopologyawareschedwg/resource-topology-exporter/pkg/resourcetopologyexporter"

"github.com/openshift-kni/numaresources-operator/pkg/version"
rteconfig "github.com/openshift-kni/numaresources-operator/rte/pkg/rte/config"
)

func main() {
klog.Infof("starting %s %s %s %s\n", version.ProgramName(), version.Get(), version.GetGitCommit(), runtime.Version())

parsedArgs, err := config.LoadArgs(os.Args[1:]...)
parsedArgs, err := rteconfig.LoadArgs(os.Args[1:]...)
if err != nil {
klog.Fatalf("failed to parse args: %v", err)
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -32,31 +32,22 @@ import (
)

const (
DefaultconfigRoot = "/etc/rte"
LegacyExtraConfigPath = "/etc/resource-topology-exporter/config.yaml"
DefaultconfigRoot = "/etc/rte"

configDirDaemon = "daemon"
configDirExtra = "extra"
)

func FixExtraConfigPath(configRoot string) string {
return filepath.Join(configRoot, configDirExtra, "config.yaml")
}

func FromFiles(pArgs *ProgArgs, configRoot, extraConfigPath string) error {
func FromFiles(pArgs *ProgArgs, configRoot string) error {
cfgRoot, err := validateConfigRootPath(configRoot)
if err != nil {
return err
}
extraCfgPath, err := validateConfigRootPath(extraConfigPath)
if err != nil {
return err
}
err = fromDaemonFiles(pArgs, cfgRoot)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return err
}
return fromExtraFile(pArgs, extraCfgPath)
return fromExtraFile(pArgs, getExtraConfigPath(cfgRoot))
}

func fromExtraFile(pArgs *ProgArgs, extraConfigPath string) error {
Expand Down Expand Up @@ -108,10 +99,13 @@ func fromDaemonFiles(pArgs *ProgArgs, configPathRoot string) error {
klog.Infof("configlet %q not regular file: ignored", configlet.Name())
continue
}
configletPath, err := validateConfigPath(filepath.Join(configletDir, configlet.Name()))
configletPath, err := validateConfigletPath(configletDir, filepath.Join(configletDir, configlet.Name()))
if err != nil {
klog.Infof("could not load %q: %v", configlet.Name(), err)
continue
if errors.Is(err, SkipConfiglet) {
klog.Infof("skipped: %q: %v", configlet.Name(), err)
continue
}
return err
}
if pArgs.Global.Debug {
klog.Infof("loading configlet: %q", configletPath)
Expand Down Expand Up @@ -232,3 +226,7 @@ func mergeConfObj(obj, upd map[string]interface{}) {
obj[key] = val
}
}

func getExtraConfigPath(configRoot string) string {
return filepath.Join(configRoot, configDirExtra, "config.yaml")
}
139 changes: 139 additions & 0 deletions rte/pkg/rte/config/cfgfile_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
/*
Copyright 2022 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"errors"
"io/fs"
"log"
"os"
"path/filepath"
"reflect"
"strings"
"testing"

"github.com/k8stopologyawareschedwg/resource-topology-exporter/pkg/resourcemonitor"
)

func TestReadResourceExclude(t *testing.T) {
testDir, closer := setupTest(t)
t.Cleanup(closer)

err := os.MkdirAll(filepath.Join(testDir, "extra"), 0755)
if err != nil {
t.Fatalf("unexpected error creating temp file: %v", err)
}

cfgContent := `resourceExclude:
masternode: [memory, device/exampleA]
workernode1: [memory, device/exampleB]
workernode2: [cpu]
"*": [device/exampleC]`

if err := os.WriteFile(filepath.Join(testDir, "extra", "config.yaml"), []byte(cfgContent), 0644); err != nil {
t.Fatalf("unexpected error writing data: %v", err)
}

pArgs, err := LoadArgs(testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

expectedResourceExclude := resourcemonitor.ResourceExclude{
"masternode": {"memory", "device/exampleA"},
"workernode1": {"memory", "device/exampleB"},
"workernode2": {"cpu"},
"*": {"device/exampleC"},
}

if !reflect.DeepEqual(pArgs.Resourcemonitor.ResourceExclude, expectedResourceExclude) {
t.Errorf("ResourceExclude is different!\ngot=%+#v\nexpected=%+#v", pArgs.Resourcemonitor.ResourceExclude, expectedResourceExclude)
}
}

func TestFromFiles(t *testing.T) {
testDir, closer := setupTest(t)
t.Cleanup(closer)

type testCase struct {
name string
}

for _, tcase := range []testCase{
{
name: "00-full",
},
} {
t.Run(tcase.name, func(t *testing.T) {
confRoot := filepath.Join(testDir, "conftree", tcase.name)
cleanCase := setupCase(t, testDir, tcase.name)
t.Cleanup(cleanCase)

var pArgs ProgArgs
SetDefaults(&pArgs)
err := FromFiles(&pArgs, testDir)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

golden := filepath.Join(confRoot, "_output", "output.yaml")

expectedRaw, err := os.ReadFile(golden)
if err != nil {
t.Fatalf("unexpected error: %v", err)
}

got := strings.TrimSpace(string(pArgs.ToYAMLString()))

Check failure on line 100 in rte/pkg/rte/config/cfgfile_test.go

View workflow job for this annotation

GitHub Actions / build

unnecessary conversion (unconvert)

Check failure on line 100 in rte/pkg/rte/config/cfgfile_test.go

View workflow job for this annotation

GitHub Actions / golangci-lint

unnecessary conversion (unconvert)
expected := strings.TrimSpace(string(expectedRaw))
if got != expected {
t.Errorf("invalid defaults.\n>>> got:\n{%s}\n>>> expected:\n{%s}", got, expected)
}
})
}
}

func setupCase(t *testing.T, testDir, name string) func() {
t.Helper()

log.Printf("setupCase: %s", name)

confRoot := filepath.Join(testDir, "conftree", name)

if err := os.CopyFS(filepath.Join(testDir, "daemon"), os.DirFS(filepath.Join(confRoot, "daemon"))); err != nil {
t.Fatalf("cannot setup daemon: %v", err)
}
log.Printf("copied: %s -> %s", filepath.Join(confRoot, "daemon"), filepath.Join(testDir, "daemon"))

if err := os.CopyFS(filepath.Join(testDir, "extra"), os.DirFS(filepath.Join(confRoot, "extra"))); err != nil {
t.Fatalf("cannot setup extra: %v", err)
}
log.Printf("copied: %s -> %s", filepath.Join(confRoot, "extra"), filepath.Join(testDir, "extra"))

return func() {
var err error
err = os.RemoveAll(filepath.Join(testDir, "daemon"))
if err != nil && !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("cannot cleanup daemon: %v", err)
}
log.Printf("cleanup: %s", filepath.Join(testDir, "daemon"))
err = os.RemoveAll(filepath.Join(testDir, "extra"))
if err != nil && !errors.Is(err, fs.ErrNotExist) {
t.Fatalf("cannot cleanup extra: %v", err)
}
log.Printf("cleanup: %s", filepath.Join(testDir, "extra"))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func LoadArgs(args ...string) (ProgArgs, error) {
// to learn about the config root, and the most robust way (bar only)
// to learn about the parameters is using flags.Parse(), so be it.
// the action will waste few cycles but is expected to be idempotent.
configRoot, extraConfigPath, err = FromFlags(&tmp, args...)
configRoot, err = FromFlags(&tmp, args...)
if err != nil {
return pArgs, err
}
Expand All @@ -119,7 +119,7 @@ func LoadArgs(args ...string) (ProgArgs, error) {
klog.Infof("config defaults:{{\n%s}}", pArgs.ToYAMLString())
}

err = FromFiles(&pArgs, configRoot, extraConfigPath)
err = FromFiles(&pArgs, configRoot)
if err != nil {
return pArgs, err
}
Expand All @@ -132,7 +132,7 @@ func LoadArgs(args ...string) (ProgArgs, error) {
klog.Infof("config from environment variables:{{\n%s}}", pArgs.ToYAMLString())
}

_, _, err = FromFlags(&pArgs, args...)
_, err = FromFlags(&pArgs, args...)
if err != nil {
return pArgs, err
}
Expand Down
Loading

0 comments on commit 890e483

Please sign in to comment.