Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion pkg/cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package cmd

import (
"context"
"errors"
"os"
"os/signal"
"path/filepath"
Expand All @@ -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"
)

Expand Down Expand Up @@ -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)
}
Expand All @@ -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)

Expand Down
43 changes: 43 additions & 0 deletions pkg/util/net.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,13 @@ import (
"crypto/tls"
tcpnet "net"
"net/http"
"os"
"sort"
"strconv"
"strings"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"k8s.io/apimachinery/pkg/util/net"
"k8s.io/apimachinery/pkg/util/wait"
Expand Down Expand Up @@ -81,3 +85,42 @@ 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)
}

// sort keys to avoid issues with map key ordering in go future versions on the unit-test side
sort.Strings(keys)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know the sort is unnecessary on production, but guarantees key ordering during unit testing, which is not a problem in current go, but future go versions will change this.

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{}{}
}
}
}
52 changes: 52 additions & 0 deletions pkg/util/net_test.go
Original file line number Diff line number Diff line change
@@ -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()
}