k8s.io/kubernetes@v1.29.3/test/images/agnhost/dns/common.go (about)

     1  /*
     2  Copyright 2019 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package dns
    18  
    19  import (
    20  	"fmt"
    21  	"os"
    22  	"strings"
    23  
    24  	"github.com/spf13/cobra"
    25  )
    26  
    27  // CmdDNSSuffix is used by agnhost Cobra.
    28  var CmdDNSSuffix = &cobra.Command{
    29  	Use:   "dns-suffix",
    30  	Short: "Prints the host's DNS suffix list",
    31  	Long:  `Prints the DNS suffixes of this host.`,
    32  	Args:  cobra.MaximumNArgs(0),
    33  	Run:   printDNSSuffixList,
    34  }
    35  
    36  // CmdDNSServerList is used by agnhost Cobra.
    37  var CmdDNSServerList = &cobra.Command{
    38  	Use:   "dns-server-list",
    39  	Short: "Prints the host's DNS Server list",
    40  	Long:  `Prints the DNS Server list of this host.`,
    41  	Args:  cobra.MaximumNArgs(0),
    42  	Run:   printDNSServerList,
    43  }
    44  
    45  // CmdEtcHosts is used by agnhost Cobra.
    46  var CmdEtcHosts = &cobra.Command{
    47  	Use:   "etc-hosts",
    48  	Short: "Prints the host's /etc/hosts file",
    49  	Long:  `Prints the "hosts" file of this host."`,
    50  	Args:  cobra.MaximumNArgs(0),
    51  	Run:   printHostsFile,
    52  }
    53  
    54  func printDNSSuffixList(cmd *cobra.Command, args []string) {
    55  	dnsSuffixList := GetDNSSuffixList()
    56  	fmt.Println(strings.Join(dnsSuffixList, ","))
    57  }
    58  
    59  func printDNSServerList(cmd *cobra.Command, args []string) {
    60  	dnsServerList := getDNSServerList()
    61  	fmt.Println(strings.Join(dnsServerList, ","))
    62  }
    63  
    64  func printHostsFile(cmd *cobra.Command, args []string) {
    65  	fmt.Println(readFile(etcHostsFile))
    66  }
    67  
    68  func readFile(fileName string) string {
    69  	fileData, err := os.ReadFile(fileName)
    70  	if err != nil {
    71  		panic(err)
    72  	}
    73  
    74  	return string(fileData)
    75  }