Commit 44a7be98 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #41618 from JiangtianLi/k8swin

Automatic merge from submit-queue (batch tested with PRs 42316, 41618, 42201, 42113, 42191) Support unqualified and partially qualified domain name in DNS query in Windows kube-proxy **What this PR does / why we need it**: In Windows container networking, --dns-search is not currently supported on Windows Docker. Besides, even with --dns-suffix, inside Windows container DNS suffix is not appended to DNS query names. That makes unqualified domain name or partially qualified domain name in DNS query not able to resolve. This PR provides a solution to resolve unqualified domain name or partially qualified domain name in DNS query for Windows container in Windows kube-proxy. It uses well-known Kubernetes DNS suffix as well host DNS suffix search list to append to the name in DNS query. DNS packet in kube-proxy UDP stream is modified as appropriate. This PR affects the Windows kube-proxy only. **Special notes for your reviewer**: This PR is based on top of Anthony Howe's commit 48647fb9, 0e37f0a8 and 7e2c71f6 which is already included in the PR 41487. Please only review commit b9dfb69d. **Release note**: ```release-note Add DNS suffix search list support in Windows kube-proxy. ```
parents 2249550b b9dfb69d
...@@ -22,6 +22,8 @@ go_library( ...@@ -22,6 +22,8 @@ go_library(
deps = [ deps = [
"//pkg/api:go_default_library", "//pkg/api:go_default_library",
"//pkg/proxy:go_default_library", "//pkg/proxy:go_default_library",
"//pkg/util/exec:go_default_library",
"//pkg/util/ipconfig:go_default_library",
"//pkg/util/netsh:go_default_library", "//pkg/util/netsh:go_default_library",
"//pkg/util/slice:go_default_library", "//pkg/util/slice:go_default_library",
"//vendor:github.com/golang/glog", "//vendor:github.com/golang/glog",
...@@ -35,6 +37,7 @@ go_test( ...@@ -35,6 +37,7 @@ go_test(
name = "go_default_test", name = "go_default_test",
srcs = [ srcs = [
"proxier_test.go", "proxier_test.go",
"proxysocket_test.go",
"roundrobin_test.go", "roundrobin_test.go",
], ],
library = ":go_default_library", library = ":go_default_library",
......
...@@ -50,6 +50,7 @@ type serviceInfo struct { ...@@ -50,6 +50,7 @@ type serviceInfo struct {
socket proxySocket socket proxySocket
timeout time.Duration timeout time.Duration
activeClients *clientCache activeClients *clientCache
dnsClients *dnsClientCache
sessionAffinityType api.ServiceAffinity sessionAffinityType api.ServiceAffinity
} }
...@@ -260,6 +261,7 @@ func (proxier *Proxier) addServicePortPortal(servicePortPortalName ServicePortPo ...@@ -260,6 +261,7 @@ func (proxier *Proxier) addServicePortPortal(servicePortPortalName ServicePortPo
socket: sock, socket: sock,
timeout: timeout, timeout: timeout,
activeClients: newClientCache(), activeClients: newClientCache(),
dnsClients: newDnsClientCache(),
sessionAffinityType: api.ServiceAffinityNone, // default sessionAffinityType: api.ServiceAffinityNone, // default
} }
proxier.setServiceInfo(servicePortPortalName, si) proxier.setServiceInfo(servicePortPortalName, si)
......
/*
Copyright 2017 The Kubernetes 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 winuserspace
import (
"reflect"
"testing"
)
func TestPackUnpackDnsMsgUnqualifiedName(t *testing.T) {
msg := &dnsMsg{}
var buffer [4096]byte
msg.header.id = 1
msg.header.qdCount = 1
msg.question = make([]dnsQuestion, msg.header.qdCount)
msg.question[0].qClass = 0x01
msg.question[0].qType = 0x01
msg.question[0].qName.name = "kubernetes"
length, ok := msg.packDnsMsg(buffer[:])
if !ok {
t.Errorf("Pack DNS message failed.")
}
unpackedMsg := &dnsMsg{}
if !unpackedMsg.unpackDnsMsg(buffer[:length]) {
t.Errorf("Unpack DNS message failed.")
}
if !reflect.DeepEqual(msg, unpackedMsg) {
t.Errorf("Pack and Unpack DNS message are not consistent.")
}
}
func TestPackUnpackDnsMsgFqdn(t *testing.T) {
msg := &dnsMsg{}
var buffer [4096]byte
msg.header.id = 1
msg.header.qdCount = 1
msg.question = make([]dnsQuestion, msg.header.qdCount)
msg.question[0].qClass = 0x01
msg.question[0].qType = 0x01
msg.question[0].qName.name = "kubernetes.default.svc.cluster.local"
length, ok := msg.packDnsMsg(buffer[:])
if !ok {
t.Errorf("Pack DNS message failed.")
}
unpackedMsg := &dnsMsg{}
if !unpackedMsg.unpackDnsMsg(buffer[:length]) {
t.Errorf("Unpack DNS message failed.")
}
if !reflect.DeepEqual(msg, unpackedMsg) {
t.Errorf("Pack and Unpack DNS message are not consistent.")
}
}
func TestPackUnpackDnsMsgEmptyName(t *testing.T) {
msg := &dnsMsg{}
var buffer [4096]byte
msg.header.id = 1
msg.header.qdCount = 1
msg.question = make([]dnsQuestion, msg.header.qdCount)
msg.question[0].qClass = 0x01
msg.question[0].qType = 0x01
msg.question[0].qName.name = ""
length, ok := msg.packDnsMsg(buffer[:])
if !ok {
t.Errorf("Pack DNS message failed.")
}
unpackedMsg := &dnsMsg{}
if !unpackedMsg.unpackDnsMsg(buffer[:length]) {
t.Errorf("Unpack DNS message failed.")
}
if !reflect.DeepEqual(msg, unpackedMsg) {
t.Errorf("Pack and Unpack DNS message are not consistent.")
}
}
func TestPackUnpackDnsMsgMultipleQuestions(t *testing.T) {
msg := &dnsMsg{}
var buffer [4096]byte
msg.header.id = 1
msg.header.qdCount = 2
msg.question = make([]dnsQuestion, msg.header.qdCount)
msg.question[0].qClass = 0x01
msg.question[0].qType = 0x01
msg.question[0].qName.name = "kubernetes"
msg.question[1].qClass = 0x01
msg.question[1].qType = 0x1c
msg.question[1].qName.name = "kubernetes.default"
length, ok := msg.packDnsMsg(buffer[:])
if !ok {
t.Errorf("Pack DNS message failed.")
}
unpackedMsg := &dnsMsg{}
if !unpackedMsg.unpackDnsMsg(buffer[:length]) {
t.Errorf("Unpack DNS message failed.")
}
if !reflect.DeepEqual(msg, unpackedMsg) {
t.Errorf("Pack and Unpack DNS message are not consistent.")
}
}
...@@ -65,6 +65,7 @@ filegroup( ...@@ -65,6 +65,7 @@ filegroup(
"//pkg/util/interrupt:all-srcs", "//pkg/util/interrupt:all-srcs",
"//pkg/util/intstr:all-srcs", "//pkg/util/intstr:all-srcs",
"//pkg/util/io:all-srcs", "//pkg/util/io:all-srcs",
"//pkg/util/ipconfig:all-srcs",
"//pkg/util/iptables:all-srcs", "//pkg/util/iptables:all-srcs",
"//pkg/util/json:all-srcs", "//pkg/util/json:all-srcs",
"//pkg/util/keymutex:all-srcs", "//pkg/util/keymutex:all-srcs",
......
package(default_visibility = ["//visibility:public"])
licenses(["notice"])
load(
"@io_bazel_rules_go//go:def.bzl",
"go_library",
"go_test",
)
go_library(
name = "go_default_library",
srcs = [
"doc.go",
"ipconfig.go",
],
tags = ["automanaged"],
deps = [
"//pkg/util/exec:go_default_library",
"//vendor:github.com/golang/glog",
],
)
go_test(
name = "go_default_test",
srcs = ["ipconfig_test.go"],
library = ":go_default_library",
tags = ["automanaged"],
deps = ["//pkg/util/exec:go_default_library"],
)
filegroup(
name = "package-srcs",
srcs = glob(["**"]),
tags = ["automanaged"],
visibility = ["//visibility:private"],
)
filegroup(
name = "all-srcs",
srcs = [":package-srcs"],
tags = ["automanaged"],
)
/*
Copyright 2016 The Kubernetes 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 ipconfig provides an interface and implementations for running Windows ipconfig commands.
package ipconfig // import "k8s.io/kubernetes/pkg/util/ipconfig"
/*
Copyright 2016 The Kubernetes 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 ipconfig
import (
"runtime"
"strings"
"sync"
"github.com/golang/glog"
utilexec "k8s.io/kubernetes/pkg/util/exec"
)
// Interface is an injectable interface for running ipconfig commands. Implementations must be goroutine-safe.
type Interface interface {
// GetDnsSuffixSearchList returns the list of DNS suffix to search
GetDnsSuffixSearchList() ([]string, error)
}
const (
cmdIpconfig string = "ipconfig"
cmdDefaultArgs string = "/all"
dnsSuffixSearchLisLabel string = "DNS Suffix Search List"
)
// runner implements Interface in terms of exec("ipconfig").
type runner struct {
mu sync.Mutex
exec utilexec.Interface
}
// New returns a new Interface which will exec ipconfig.
func New(exec utilexec.Interface) Interface {
runner := &runner{
exec: exec,
}
return runner
}
// GetDnsSuffixSearchList returns the list of DNS suffix to search
func (runner *runner) GetDnsSuffixSearchList() ([]string, error) {
// Parse the DNS suffix search list from ipconfig output
// ipconfig /all on Windows displays the entry of DNS suffix search list
// An example output contains:
//
// DNS Suffix Search List. . . . . . : example1.com
// example2.com
//
// TODO: this does not work when the label is localized
suffixList := []string{}
if runtime.GOOS != "windows" {
glog.V(1).Infof("ipconfig not supported on GOOS=%s", runtime.GOOS)
return suffixList, nil
}
out, err := runner.exec.Command(cmdIpconfig, cmdDefaultArgs).Output()
if err == nil {
lines := strings.Split(string(out), "\n")
for i, line := range lines {
if trimmed := strings.TrimSpace(line); strings.HasPrefix(trimmed, dnsSuffixSearchLisLabel) {
if parts := strings.Split(trimmed, ":"); len(parts) > 1 {
if trimmed := strings.TrimSpace(parts[1]); trimmed != "" {
suffixList = append(suffixList, strings.TrimSpace(parts[1]))
}
for j := i + 1; j < len(lines); j++ {
if trimmed := strings.TrimSpace(lines[j]); trimmed != "" && !strings.Contains(trimmed, ":") {
suffixList = append(suffixList, trimmed)
} else {
break
}
}
}
break
}
}
} else {
glog.V(1).Infof("Running %s %s failed: %v", cmdIpconfig, cmdDefaultArgs, err)
}
return suffixList, err
}
/*
Copyright 2016 The Kubernetes 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 ipconfig
import (
"testing"
"k8s.io/kubernetes/pkg/util/exec"
)
func TestGetDnsSuffixSearchList(t *testing.T) {
// Simple test
ipconfigInterface := New(exec.New())
_, err := ipconfigInterface.GetDnsSuffixSearchList()
if err != nil {
t.Errorf("expected success, got %v", err)
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment