ipconfig.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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. "sync"
  18. "k8s.io/klog"
  19. utilexec "k8s.io/utils/exec"
  20. )
  21. // Interface is an injectable interface for running ipconfig commands. Implementations must be goroutine-safe.
  22. type Interface interface {
  23. // GetDNSSuffixSearchList returns the list of DNS suffix to search
  24. GetDNSSuffixSearchList() ([]string, error)
  25. }
  26. const (
  27. cmdIpconfig string = "ipconfig"
  28. cmdDefaultArgs string = "/all"
  29. dnsSuffixSearchLisLabel string = "DNS Suffix Search List"
  30. )
  31. // runner implements Interface in terms of exec("ipconfig").
  32. type runner struct {
  33. mu sync.Mutex
  34. exec utilexec.Interface
  35. }
  36. // New returns a new Interface which will exec ipconfig.
  37. func New(exec utilexec.Interface) Interface {
  38. runner := &runner{
  39. exec: exec,
  40. }
  41. return runner
  42. }
  43. // GetDNSSuffixSearchList returns the list of DNS suffix to search
  44. func (runner *runner) GetDNSSuffixSearchList() ([]string, error) {
  45. // Parse the DNS suffix search list from ipconfig output
  46. // ipconfig /all on Windows displays the entry of DNS suffix search list
  47. // An example output contains:
  48. //
  49. // DNS Suffix Search List. . . . . . : example1.com
  50. // example2.com
  51. //
  52. // TODO: this does not work when the label is localized
  53. suffixList := []string{}
  54. if runtime.GOOS != "windows" {
  55. klog.V(1).Infof("ipconfig not supported on GOOS=%s", runtime.GOOS)
  56. return suffixList, nil
  57. }
  58. out, err := runner.exec.Command(cmdIpconfig, cmdDefaultArgs).Output()
  59. if err == nil {
  60. lines := strings.Split(string(out), "\n")
  61. for i, line := range lines {
  62. if trimmed := strings.TrimSpace(line); strings.HasPrefix(trimmed, dnsSuffixSearchLisLabel) {
  63. if parts := strings.Split(trimmed, ":"); len(parts) > 1 {
  64. if trimmed := strings.TrimSpace(parts[1]); trimmed != "" {
  65. suffixList = append(suffixList, strings.TrimSpace(parts[1]))
  66. }
  67. for j := i + 1; j < len(lines); j++ {
  68. if trimmed := strings.TrimSpace(lines[j]); trimmed != "" && !strings.Contains(trimmed, ":") {
  69. suffixList = append(suffixList, trimmed)
  70. } else {
  71. break
  72. }
  73. }
  74. }
  75. break
  76. }
  77. }
  78. } else {
  79. klog.V(1).Infof("Running %s %s failed: %v", cmdIpconfig, cmdDefaultArgs, err)
  80. }
  81. return suffixList, err
  82. }