From 7a0563e7b2b87e3661a0db8b8e897ff2f9bf37ec Mon Sep 17 00:00:00 2001 From: Miguel Angel Ajo Date: Wed, 19 Jan 2022 18:17:11 +0100 Subject: [PATCH 1/2] Add the NodeName and NodeIP to the NO_PROXY environment Fixes-Issue: #558 Signed-off-by: Miguel Angel Ajo --- pkg/cmd/run.go | 18 +++++++++++++++++- pkg/util/net.go | 39 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/pkg/cmd/run.go b/pkg/cmd/run.go index 8603e59e07..836b50f17d 100644 --- a/pkg/cmd/run.go +++ b/pkg/cmd/run.go @@ -2,7 +2,6 @@ package cmd import ( "context" - "errors" "os" "os/signal" "path/filepath" @@ -17,9 +16,11 @@ import ( "github.com/openshift/microshift/pkg/node" "github.com/openshift/microshift/pkg/servicemanager" "github.com/openshift/microshift/pkg/util" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "github.com/spf13/cobra" "github.com/spf13/pflag" + "k8s.io/klog/v2" ) @@ -51,6 +52,7 @@ func NewRunMicroshiftCommand() *cobra.Command { } func RunMicroshift(cfg *config.MicroshiftConfig, flags *pflag.FlagSet) error { + if err := cfg.ReadAndValidate(flags); err != nil { logrus.Fatal(err) } @@ -60,6 +62,20 @@ func RunMicroshift(cfg *config.MicroshiftConfig, flags *pflag.FlagSet) error { logrus.Fatalf("MicroShift must be run privileged for role 'node'") } + // TO-DO: When multi-node is ready, we need to add the controller host-name/mDNS hostname + // or VIP to this list on start + // see https://github.com/redhat-et/microshift/pull/471 + + if err := util.AddToNoProxyEnv( + cfg.NodeIP, + cfg.NodeName, + cfg.Cluster.ClusterCIDR, + cfg.Cluster.ServiceCIDR, + ".svc", + "."+cfg.Cluster.Domain); err != nil { + klog.Fatal(err) + } + os.MkdirAll(cfg.DataDir, 0700) os.MkdirAll(cfg.AuditLogDir, 0700) diff --git a/pkg/util/net.go b/pkg/util/net.go index a0600b4ae8..aa77d15154 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -19,9 +19,12 @@ import ( "crypto/tls" tcpnet "net" "net/http" + "os" "strconv" + "strings" "time" + "github.com/pkg/errors" "github.com/sirupsen/logrus" "k8s.io/apimachinery/pkg/util/net" "k8s.io/apimachinery/pkg/util/wait" @@ -81,3 +84,39 @@ func CreateLocalhostListenerOnPort(port int) (tcpnet.Listener, error) { return ln, nil } + +func AddToNoProxyEnv(additionalEntries ...string) error { + entries := map[string]struct{}{} + + // put both the NO_PROXY and no_proxy elements in a map to avoid duplicates + addNoProxyEnvVarEntries(entries, "NO_PROXY") + addNoProxyEnvVarEntries(entries, "no_proxy") + + for _, entry := range additionalEntries { + entries[entry] = struct{}{} + } + + noProxyEnv := strings.Join(mapKeys(entries), ",") + + // unset the lower-case one, and keep only upper-case + os.Unsetenv("no_proxy") + return errors.Wrap(os.Setenv("NO_PROXY", noProxyEnv), "error updating NO_PROXY") +} + +func mapKeys(entries map[string]struct{}) []string { + keys := make([]string, 0, len(entries)) + for k := range entries { + keys = append(keys, k) + } + return keys +} + +func addNoProxyEnvVarEntries(entries map[string]struct{}, envVar string) { + noProxy := os.Getenv(envVar) + + if noProxy != "" { + for _, entry := range strings.Split(noProxy, ",") { + entries[strings.Trim(entry, " ")] = struct{}{} + } + } +} From e65c3c86f71dbf9189c092d42eb3c45e2822b036 Mon Sep 17 00:00:00 2001 From: Miguel Angel Ajo Date: Thu, 20 Jan 2022 14:08:53 +0100 Subject: [PATCH 2/2] Add testing for AddToNoProxyEnv function Signed-off-by: Miguel Angel Ajo --- pkg/util/net.go | 4 ++++ pkg/util/net_test.go | 52 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 pkg/util/net_test.go diff --git a/pkg/util/net.go b/pkg/util/net.go index aa77d15154..b4c402fe0a 100644 --- a/pkg/util/net.go +++ b/pkg/util/net.go @@ -20,6 +20,7 @@ import ( tcpnet "net" "net/http" "os" + "sort" "strconv" "strings" "time" @@ -108,6 +109,9 @@ func mapKeys(entries map[string]struct{}) []string { for k := range entries { keys = append(keys, k) } + + // sort keys to avoid issues with map key ordering in go future versions on the unit-test side + sort.Strings(keys) return keys } diff --git a/pkg/util/net_test.go b/pkg/util/net_test.go new file mode 100644 index 0000000000..1ead81b1a6 --- /dev/null +++ b/pkg/util/net_test.go @@ -0,0 +1,52 @@ +package util + +import ( + "os" + "testing" + + "github.com/stretchr/testify/assert" +) + +func TestAddToNoProxyEnv(t *testing.T) { + clearNoProxy() + AddToNoProxyEnv(".svc", "10.40.0.0/16") + + assert.Equal(t, ".svc,10.40.0.0/16", os.Getenv("NO_PROXY"), "NO_PROXY has unexpected value") + assert.Equal(t, "", os.Getenv("no_proxy"), "no_proxy expected to be empty") + clearNoProxy() +} + +func clearNoProxy() { + os.Setenv("NO_PROXY", "") + os.Setenv("no_proxy", "") +} + +func TestAddToNoProxyEnv_with_contents(t *testing.T) { + os.Setenv("NO_PROXY", "my.host.local") + os.Setenv("no_proxy", "") + AddToNoProxyEnv(".svc", "10.40.0.0/16") + + assert.Equal(t, ".svc,10.40.0.0/16,my.host.local", os.Getenv("NO_PROXY"), "NO_PROXY has unexpected value") + assert.Equal(t, "", os.Getenv("no_proxy"), "no_proxy expected to be empty") + clearNoProxy() +} + +func TestAddToNoProxyEnv_with_dups(t *testing.T) { + os.Setenv("NO_PROXY", "my.host.local") + os.Setenv("no_proxy", "my.host.local") + AddToNoProxyEnv(".svc", "10.40.0.0/16") + + assert.Equal(t, ".svc,10.40.0.0/16,my.host.local", os.Getenv("NO_PROXY"), "NO_PROXY has unexpected value") + assert.Equal(t, "", os.Getenv("no_proxy"), "no_proxy expected to be empty") + clearNoProxy() +} + +func TestAddToNoProxyEnv_with_both(t *testing.T) { + os.Setenv("NO_PROXY", "my.host.local") + os.Setenv("no_proxy", "another.host.local") + AddToNoProxyEnv(".svc", "10.40.0.0/16") + + assert.Equal(t, ".svc,10.40.0.0/16,another.host.local,my.host.local", os.Getenv("NO_PROXY"), "NO_PROXY has unexpected value") + assert.Equal(t, "", os.Getenv("no_proxy"), "no_proxy expected to be empty") + clearNoProxy() +}