-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Injector can define kubernetes clusterDomain (dapr#3164)
* injector support define k8s clusterDomain * use define ContentTypeJSON const * format address all in a single Sprintf statement * auto-detect the k8s cluster domain by read resolv.conf file * make some func private * wait timeout output podStatus info for debug * output pod resources and check string is nil * injector charts add KUBE_CLUSTER_DOMAIN for e2e tests * add injector config test file * adjust test for azure env because pod search domain is not cluster.local * resolve golangci-lint check error Co-authored-by: Artur Souza <[email protected]>
- Loading branch information
1 parent
a16db69
commit 34bbc96
Showing
11 changed files
with
269 additions
and
22 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
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
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,48 @@ | ||
package injector | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestGetInectorConfig(t *testing.T) { | ||
t.Run("with kube cluster domain env", func(t *testing.T) { | ||
os.Setenv("TLS_CERT_FILE", "test-cert-file") | ||
os.Setenv("TLS_KEY_FILE", "test-key-file") | ||
os.Setenv("SIDECAR_IMAGE", "daprd-test-image") | ||
os.Setenv("SIDECAR_IMAGE_PULL_POLICY", "Always") | ||
os.Setenv("NAMESPACE", "test-namespace") | ||
os.Setenv("KUBE_CLUSTER_DOMAIN", "cluster.local") | ||
defer os.Clearenv() | ||
|
||
cfg, err := GetConfig() | ||
assert.Nil(t, err) | ||
assert.Equal(t, "test-cert-file", cfg.TLSCertFile) | ||
assert.Equal(t, "test-key-file", cfg.TLSKeyFile) | ||
assert.Equal(t, "daprd-test-image", cfg.SidecarImage) | ||
assert.Equal(t, "Always", cfg.SidecarImagePullPolicy) | ||
assert.Equal(t, "test-namespace", cfg.Namespace) | ||
assert.Equal(t, "cluster.local", cfg.KubeClusterDomain) | ||
}) | ||
|
||
t.Run("not set kube cluster domain env", func(t *testing.T) { | ||
os.Setenv("TLS_CERT_FILE", "test-cert-file") | ||
os.Setenv("TLS_KEY_FILE", "test-key-file") | ||
os.Setenv("SIDECAR_IMAGE", "daprd-test-image") | ||
os.Setenv("SIDECAR_IMAGE_PULL_POLICY", "IfNotPresent") | ||
os.Setenv("NAMESPACE", "test-namespace") | ||
os.Setenv("KUBE_CLUSTER_DOMAIN", "") | ||
defer os.Clearenv() | ||
|
||
cfg, err := GetConfig() | ||
assert.Nil(t, err) | ||
assert.Equal(t, "test-cert-file", cfg.TLSCertFile) | ||
assert.Equal(t, "test-key-file", cfg.TLSKeyFile) | ||
assert.Equal(t, "daprd-test-image", cfg.SidecarImage) | ||
assert.Equal(t, "IfNotPresent", cfg.SidecarImagePullPolicy) | ||
assert.Equal(t, "test-namespace", cfg.Namespace) | ||
assert.NotEqual(t, "", cfg.KubeClusterDomain) | ||
}) | ||
} |
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
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
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,72 @@ | ||
package utils | ||
|
||
import ( | ||
"bufio" | ||
"bytes" | ||
"io/ioutil" | ||
"regexp" | ||
"sort" | ||
"strings" | ||
) | ||
|
||
const ( | ||
// DefaultKubeClusterDomain is the default value of KubeClusterDomain. | ||
DefaultKubeClusterDomain = "cluster.local" | ||
defaultResolvPath = "/etc/resolv.conf" | ||
commentMarker = "#" | ||
) | ||
|
||
var searchRegexp = regexp.MustCompile(`^\s*search\s*(([^\s]+\s*)*)$`) | ||
|
||
// GetKubeClusterDomain search KubeClusterDomain value from /etc/resolv.conf file. | ||
func GetKubeClusterDomain() (string, error) { | ||
resolvContent, err := getResolvContent(defaultResolvPath) | ||
if err != nil { | ||
return "", err | ||
} | ||
return getClusterDomain(resolvContent) | ||
} | ||
|
||
func getClusterDomain(resolvConf []byte) (string, error) { | ||
var kubeClusterDomian string | ||
searchDomains := getResolvSearchDomains(resolvConf) | ||
sort.Strings(searchDomains) | ||
if len(searchDomains) == 0 || searchDomains[0] == "" { | ||
kubeClusterDomian = DefaultKubeClusterDomain | ||
} else { | ||
kubeClusterDomian = searchDomains[0] | ||
} | ||
return kubeClusterDomian, nil | ||
} | ||
|
||
func getResolvContent(resolvPath string) ([]byte, error) { | ||
return ioutil.ReadFile(resolvPath) | ||
} | ||
|
||
func getResolvSearchDomains(resolvConf []byte) []string { | ||
var ( | ||
domains []string | ||
lines [][]byte | ||
) | ||
|
||
scanner := bufio.NewScanner(bytes.NewReader(resolvConf)) | ||
for scanner.Scan() { | ||
line := scanner.Bytes() | ||
commentIndex := bytes.Index(line, []byte(commentMarker)) | ||
if commentIndex == -1 { | ||
lines = append(lines, line) | ||
} else { | ||
lines = append(lines, line[:commentIndex]) | ||
} | ||
} | ||
|
||
for _, line := range lines { | ||
match := searchRegexp.FindSubmatch(line) | ||
if match == nil { | ||
continue | ||
} | ||
domains = strings.Fields(string(match[1])) | ||
} | ||
|
||
return domains | ||
} |
Oops, something went wrong.