Skip to content

Commit

Permalink
DAOS-12746 control: Support of of NetSVC Azure VM NIC
Browse files Browse the repository at this point in the history
Integrate reviewers comments:
* Rename FindEnvValue and associated functions
  #12465 (comment)
* Remove useless slice initialization with make
  #12465 (comment)
* Remove useless slice initialization with make
  #12465 (comment)

Features: control
Required-githooks: true
Signed-off-by: Cedric Koch-Hofer <cedric.koch-hofer@intel.com>
  • Loading branch information
kanard38 authored and knard-intel committed Jun 29, 2023
1 parent 1158199 commit 48e1308
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 30 deletions.
21 changes: 10 additions & 11 deletions src/control/common/env_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ func BoolAsInt(b bool) int {
return 0
}

// FindEnvValue will return value from supplied name key if found in input slice of key-pairs
// (environment). ErrNotExist error returned if key cannot be found.
func FindEnvValue(keyPairs []string, name string) (string, error) {
// FindKeyValue will return value from supplied name key if found in input slice of key-pairs
// (e.g. environment). ErrNotExist error returned if key cannot be found.
func FindKeyValue(keyPairs []string, name string) (string, error) {
for _, pair := range keyPairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 && kv[0] == name {
Expand All @@ -91,8 +91,8 @@ func FindEnvValue(keyPairs []string, name string) (string, error) {
return "", errors.Wrapf(os.ErrNotExist, "Undefined environment variable %q", name)
}

// UpdateEnvValue updates a value for existing key and returns new slice of key-value pairs.
func UpdateEnvValue(keyPairs []string, name, newValue string) ([]string, error) {
// UpdateKeyValue updates a value for existing key and returns new slice of key-value pairs.
func UpdateKeyValue(keyPairs []string, name, newValue string) ([]string, error) {
for i, pair := range keyPairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 && kv[0] == name {
Expand All @@ -104,8 +104,8 @@ func UpdateEnvValue(keyPairs []string, name, newValue string) ([]string, error)
return nil, errors.Wrapf(os.ErrNotExist, "Undefined environment variable %q", name)
}

// DeleteEnvValue removes an existing key and returns new slice of key-value pairs.
func DeleteEnvValue(keyPairs []string, name string) ([]string, error) {
// DeleteKeyValue removes an existing key and returns new slice of key-value pairs.
func DeleteKeyValue(keyPairs []string, name string) ([]string, error) {
for i, pair := range keyPairs {
kv := strings.SplitN(pair, "=", 2)
if len(kv) == 2 && kv[0] == name {
Expand All @@ -116,10 +116,9 @@ func DeleteEnvValue(keyPairs []string, name string) ([]string, error) {
return nil, errors.Wrapf(os.ErrNotExist, "Undefined environment variable %q", name)
}

// MergeEnvVars merges and deduplicates two slices of environment
// variables. Conflicts are resolved by taking the value from the
// second list.
func MergeEnvVars(curVars []string, newVars []string) (merged []string) {
// MergeKeyValues merges and deduplicates two slices of key-value pairs. Conflicts are resolved by
// taking the value from the second list.
func MergeKeyValues(curVars []string, newVars []string) (merged []string) {
mergeMap := make(map[string]string)
for _, pair := range curVars {
kv := strings.SplitN(pair, "=", 2)
Expand Down
4 changes: 2 additions & 2 deletions src/control/common/env_utils_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ func TestScrubProxyVariables(t *testing.T) {
}
}

func TestCommon_MergeEnvVars(t *testing.T) {
func TestCommon_MergeKeyValues(t *testing.T) {
for name, tc := range map[string]struct {
baseVars []string
mergeVars []string
Expand Down Expand Up @@ -206,7 +206,7 @@ func TestCommon_MergeEnvVars(t *testing.T) {
},
} {
t.Run(name, func(t *testing.T) {
gotVars := MergeEnvVars(tc.baseVars, tc.mergeVars)
gotVars := MergeKeyValues(tc.baseVars, tc.mergeVars)
if diff := cmp.Diff(tc.wantVars, gotVars, defCmpOpts...); diff != "" {
t.Fatalf("(-want, +got):\n%s", diff)
}
Expand Down
7 changes: 2 additions & 5 deletions src/control/lib/hardware/sysfs/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func isNetvscDevice(path string, subsystem string) bool {
return false
}

val, err := common.FindEnvValue(strings.Split(string(content), "\n"), "DRIVER")
val, err := common.FindKeyValue(strings.Split(string(content), "\n"), "DRIVER")
return err == nil && val == netvscDriver
}

Expand Down Expand Up @@ -123,7 +123,7 @@ func (s *Provider) topologySubsystems() []string {
}

func (s *Provider) addSubsystemDevices(topo *hardware.Topology, subsystem string) error {
netvscPaths := make([]string, 0)
var netvscPaths []string
subsysRoot := s.sysPath("class", subsystem)
err := filepath.Walk(subsysRoot, func(path string, fi os.FileInfo, err error) error {
if fi == nil {
Expand Down Expand Up @@ -267,9 +267,6 @@ func (s *Provider) addNetvscDevice(topo *hardware.Topology, path string) error {
virt.BackingDevice = backingDev

s.log.Tracef("Adding NetVSC network adapter at %q", path)
if len(topo.VirtualDevices) == 0 {
topo.VirtualDevices = make([]*hardware.VirtualDevice, 0)
}
topo.VirtualDevices = append(topo.VirtualDevices, virt)

return nil
Expand Down
4 changes: 2 additions & 2 deletions src/control/server/config/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ func (cfg *Server) WithFabricProvider(provider string) *Server {
// WithFabricAuthKey sets the top-level fabric authorization key.
func (cfg *Server) WithFabricAuthKey(key string) *Server {
cfg.Fabric.AuthKey = key
cfg.ClientEnvVars = common.MergeEnvVars(cfg.ClientEnvVars, []string{cfg.Fabric.GetAuthKeyEnv()})
cfg.ClientEnvVars = common.MergeKeyValues(cfg.ClientEnvVars, []string{cfg.Fabric.GetAuthKeyEnv()})
for _, engine := range cfg.Engines {
engine.Fabric.AuthKey = cfg.Fabric.AuthKey
}
Expand Down Expand Up @@ -370,7 +370,7 @@ func (cfg *Server) Load() error {
}

if cfg.Fabric.AuthKey != "" {
cfg.ClientEnvVars = common.MergeEnvVars(cfg.ClientEnvVars, []string{cfg.Fabric.GetAuthKeyEnv()})
cfg.ClientEnvVars = common.MergeKeyValues(cfg.ClientEnvVars, []string{cfg.Fabric.GetAuthKeyEnv()})
}

return nil
Expand Down
10 changes: 5 additions & 5 deletions src/control/server/engine/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,10 +274,10 @@ func (c *Config) CmdLineEnv() ([]string, error) {
if err != nil {
return nil, err
}
env = common.MergeEnvVars(env, sEnv)
env = common.MergeKeyValues(env, sEnv)
}

return common.MergeEnvVars(c.EnvVars, env), nil
return common.MergeKeyValues(c.EnvVars, env), nil
}

// HasEnvVar returns true if the configuration contains
Expand All @@ -299,16 +299,16 @@ func (c *Config) GetEnvVar(name string) (string, error) {
return "", err
}

env = common.MergeEnvVars(cleanEnvVars(os.Environ(), c.EnvPassThrough), env)
env = common.MergeKeyValues(cleanEnvVars(os.Environ(), c.EnvPassThrough), env)

return common.FindEnvValue(env, name)
return common.FindKeyValue(env, name)
}

// WithEnvVars applies the supplied list of environment
// variables to any existing variables, with new values
// overwriting existing values.
func (c *Config) WithEnvVars(newVars ...string) *Config {
c.EnvVars = common.MergeEnvVars(c.EnvVars, newVars)
c.EnvVars = common.MergeKeyValues(c.EnvVars, newVars)

return c
}
Expand Down
10 changes: 5 additions & 5 deletions src/control/server/engine/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ func (r *Runner) run(parent context.Context, args, env []string, exitCh RunnerEx

// Try to integrate DD_SUBSYS into D_LOG_MASK then unset DD_SUBSYS in environment.
func processLogEnvs(env []string) ([]string, error) {
subsys, err := common.FindEnvValue(env, envLogSubsystems)
subsys, err := common.FindKeyValue(env, envLogSubsystems)
if err != nil || subsys == "" {
return env, nil // No DD_SUBSYS to process.
}

logMasks, err := common.FindEnvValue(env, envLogMasks)
logMasks, err := common.FindKeyValue(env, envLogMasks)
if err != nil || logMasks == "" {
return env, nil // No D_LOG_MASK to process.
}
Expand All @@ -144,12 +144,12 @@ func processLogEnvs(env []string) ([]string, error) {
return nil, errors.New("empty log masks string is invalid")
}

env, err = common.UpdateEnvValue(env, envLogMasks, newLogMasks)
env, err = common.UpdateKeyValue(env, envLogMasks, newLogMasks)
if err != nil {
return nil, err
}

env, err = common.DeleteEnvValue(env, envLogSubsystems)
env, err = common.DeleteKeyValue(env, envLogSubsystems)
if err != nil && !errors.Is(err, os.ErrNotExist) {
return nil, err
}
Expand All @@ -167,7 +167,7 @@ func (r *Runner) Start(ctx context.Context) (RunnerExitChan, error) {
if err != nil {
return nil, err
}
env = common.MergeEnvVars(cleanEnvVars(os.Environ(), r.Config.EnvPassThrough), env)
env = common.MergeKeyValues(cleanEnvVars(os.Environ(), r.Config.EnvPassThrough), env)

env, err = processLogEnvs(env)
if err != nil {
Expand Down

0 comments on commit 48e1308

Please sign in to comment.