k8s.io/kubernetes@v1.31.0-alpha.0.0.20240520171757-56147500dadc/test/images/agnhost/dns/dns.go (about)

     1  //go:build !windows
     2  // +build !windows
     3  
     4  /*
     5  Copyright 2019 The Kubernetes Authors.
     6  
     7  Licensed under the Apache License, Version 2.0 (the "License");
     8  you may not use this file except in compliance with the License.
     9  You may obtain a copy of the License at
    10  
    11      http://www.apache.org/licenses/LICENSE-2.0
    12  
    13  Unless required by applicable law or agreed to in writing, software
    14  distributed under the License is distributed on an "AS IS" BASIS,
    15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    16  See the License for the specific language governing permissions and
    17  limitations under the License.
    18  */
    19  
    20  package dns
    21  
    22  import (
    23  	"strings"
    24  )
    25  
    26  const etcHostsFile = "/etc/hosts"
    27  
    28  // A /etc/resolv.conf file managed by kubelet looks like this:
    29  // nameserver DNS_CLUSTER_IP
    30  // search test-dns.svc.cluster.local svc.cluster.local cluster.local q53aahaikqaehcai3ylfqdtc5b.bx.internal.cloudapp.net
    31  // options ndots:5
    32  
    33  // GetDNSSuffixList reads DNS config file and returns the list of configured DNS suffixes
    34  func GetDNSSuffixList() []string {
    35  	fileData := readFile("/etc/resolv.conf")
    36  	lines := strings.Split(fileData, "\n")
    37  	for _, line := range lines {
    38  		if strings.HasPrefix(line, "search") {
    39  			// omit the starting "search".
    40  			return strings.Split(line, " ")[1:]
    41  		}
    42  	}
    43  
    44  	panic("Could not find DNS search list!")
    45  }
    46  
    47  func getDNSServerList() []string {
    48  	fileData := readFile("/etc/resolv.conf")
    49  	lines := strings.Split(fileData, "\n")
    50  	for _, line := range lines {
    51  		if strings.HasPrefix(line, "nameserver") {
    52  			// omit the starting "nameserver".
    53  			return strings.Split(line, " ")[1:]
    54  		}
    55  	}
    56  
    57  	panic("Could not find DNS search list!")
    58  }