ipconfig.go 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Copyright 2016 The Kubernetes Authors.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package ipconfig
  14. import (
  15. "runtime"
  16. "strings"
  17. "k8s.io/klog"
  18. utilexec "k8s.io/utils/exec"
  19. )
  20. // Interface is an injectable interface for running ipconfig commands. Implementations must be goroutine-safe.
  21. type Interface interface {
  22. // GetDNSSuffixSearchList returns the list of DNS suffix to search
  23. GetDNSSuffixSearchList() ([]string, error)
  24. }
  25. const (
  26. cmdIpconfig string = "ipconfig"
  27. cmdDefaultArgs string = "/all"
  28. dnsSuffixSearchLisLabel string = "DNS Suffix Search List"
  29. )
  30. // runner implements Interface in terms of exec("ipconfig").
  31. type runner struct {
  32. exec utilexec.Interface
  33. }
  34. // New returns a new Interface which will exec ipconfig.
  35. func New(exec utilexec.Interface) Interface {
  36. runner := &runner{
  37. exec: exec,
  38. }
  39. return runner
  40. }
  41. // GetDNSSuffixSearchList returns the list of DNS suffix to search
  42. func (runner *runner) GetDNSSuffixSearchList() ([]string, error) {
  43. // Parse the DNS suffix search list from ipconfig output
  44. // ipconfig /all on Windows displays the entry of DNS suffix search list
  45. // An example output contains:
  46. //
  47. // DNS Suffix Search List. . . . . . : example1.com
  48. // example2.com
  49. //
  50. // TODO: this does not work when the label is localized
  51. suffixList := []string{}
  52. if runtime.GOOS != "windows" {
  53. klog.V(1).Infof("ipconfig not supported on GOOS=%s", runtime.GOOS)
  54. return suffixList, nil
  55. }
  56. out, err := runner.exec.Command(cmdIpconfig, cmdDefaultArgs).Output()
  57. if err == nil {
  58. lines := strings.Split(string(out), "\n")
  59. for i, line := range lines {
  60. if trimmed := strings.TrimSpace(line); strings.HasPrefix(trimmed, dnsSuffixSearchLisLabel) {
  61. if parts := strings.Split(trimmed, ":"); len(parts) > 1 {
  62. if trimmed := strings.TrimSpace(parts[1]); trimmed != "" {
  63. suffixList = append(suffixList, strings.TrimSpace(parts[1]))
  64. }
  65. for j := i + 1; j < len(lines); j++ {
  66. if trimmed := strings.TrimSpace(lines[j]); trimmed != "" && !strings.Contains(trimmed, ":") {
  67. suffixList = append(suffixList, trimmed)
  68. } else {
  69. break
  70. }
  71. }
  72. }
  73. break
  74. }
  75. }
  76. } else {
  77. klog.V(1).Infof("Running %s %s failed: %v", cmdIpconfig, cmdDefaultArgs, err)
  78. }
  79. return suffixList, err
  80. }