-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cnitool: deduplicate environment variable definitions
create a reusable package for environment variable to be used by `cnitool` and the other library code. Signed-off-by: Gordon Bleux <33967640+UiP9AV6Y@users.noreply.github.com>
- Loading branch information
Showing
7 changed files
with
255 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// Copyright 2015 CNI 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 env | ||
|
||
import ( | ||
"os" | ||
"path/filepath" | ||
) | ||
|
||
// Protocol parameters are passed to the plugins via OS environment variables. | ||
const ( | ||
VarCapabilityArgs = "CAP_ARGS" | ||
VarCNIArgs = "CNI_ARGS" | ||
VarCNICommand = "CNI_COMMAND" | ||
VarCNIContainerId = "CNI_CONTAINERID" | ||
VarCNIIfname = "CNI_IFNAME" | ||
VarCNINetNs = "CNI_NETNS" | ||
VarCNINetNsOverride = "CNI_NETNS_OVERRIDE" | ||
VarCNIPath = "CNI_PATH" | ||
VarNetDir = "NETCONFPATH" | ||
|
||
DefaultCapabilityArgs = "" | ||
DefaultCNIArgs = "" | ||
DefaultCNIIfname = "eth0" | ||
DefaultNetDir = "/etc/cni/net.d" | ||
DefaultCNIPath = "" | ||
|
||
// supported CNI_COMMAND values | ||
|
||
CmdAdd = "ADD" | ||
CmdCheck = "CHECK" | ||
CmdDel = "DEL" | ||
CmdVersion = "VERSION" | ||
) | ||
|
||
// GetValue is a wrapper around os.GetEnv, which | ||
// returns the given fallback value in case the | ||
// environment variable is not set or an empty | ||
// string. | ||
func GetValue(key, fallbackValue string) string { | ||
val := os.Getenv(key) | ||
if val == "" { | ||
return fallbackValue | ||
} | ||
|
||
return val | ||
} | ||
|
||
// GetNetDir gets the network configuration | ||
// directory setting from the environment | ||
func GetNetDir() string { | ||
return GetValue(VarNetDir, DefaultNetDir) | ||
} | ||
|
||
// GetCapabilityArgs gets the capability | ||
// settings from the environment | ||
func GetCapabilityArgs() string { | ||
return GetValue(VarCapabilityArgs, DefaultCapabilityArgs) | ||
} | ||
|
||
// GetCNIArgs gets the plugin arguments | ||
// from the environment | ||
func GetCNIArgs() string { | ||
return GetValue(VarCNIArgs, DefaultCNIArgs) | ||
} | ||
|
||
// GetCNIIfname gets the network interface | ||
// setting from the environment | ||
func GetCNIIfname() string { | ||
return GetValue(VarCNIIfname, DefaultCNIIfname) | ||
} | ||
|
||
// GetCNIPath gets the plugin lookup path | ||
// from the environment | ||
func GetCNIPath() string { | ||
return GetValue(VarCNIPath, DefaultCNIPath) | ||
} | ||
|
||
// ParseCNIPath returns a list of directories to | ||
// search for executables | ||
func ParseCNIPath() []string { | ||
return filepath.SplitList(GetCNIPath()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
// Copyright 2016 CNI 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 env_test | ||
|
||
import ( | ||
"os" | ||
"strings" | ||
|
||
"github.com/containernetworking/cni/pkg/env" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
) | ||
|
||
var _ = Describe("GetValue", func() { | ||
BeforeEach(func() { | ||
os.Setenv("FOO", "bar") | ||
os.Unsetenv("BAC") | ||
}) | ||
|
||
Context("when environment has variable", func() { | ||
It("returns the variable value", func() { | ||
val := env.GetValue("FOO", "gaff") | ||
Expect(val).To(Equal("bar")) | ||
}) | ||
}) | ||
|
||
Context("when environment does not have variable", func() { | ||
It("returns the fallback value", func() { | ||
val := env.GetValue("BAC", "gaff") | ||
Expect(val).To(Equal("gaff")) | ||
}) | ||
}) | ||
}) | ||
|
||
var _ = Describe("ParseCNIPath", func() { | ||
BeforeEach(func() { | ||
os.Unsetenv("CNI_PATH") | ||
}) | ||
|
||
AfterEach(func() { | ||
os.Unsetenv("CNI_PATH") | ||
}) | ||
|
||
Context("when no directories are specified", func() { | ||
It("returns an empty list", func() { | ||
val := env.ParseCNIPath() | ||
Expect(val).To(BeEmpty()) | ||
}) | ||
}) | ||
|
||
Context("when multiple directories are specified", func() { | ||
It("returns the directories as a list", func() { | ||
mockCNIPath("/test/bin", "/test/libexec") | ||
|
||
val := env.ParseCNIPath() | ||
Expect(val).To(Equal([]string{"/test/bin", "/test/libexec"})) | ||
}) | ||
}) | ||
}) | ||
|
||
func mockCNIPath(dir ...string) { | ||
mock := strings.Join(dir, string(os.PathListSeparator)) | ||
|
||
os.Setenv(env.VarCNIPath, mock) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.