From 99e26460674b1ad881cb5b96c56a9b3219003305 Mon Sep 17 00:00:00 2001 From: Claudiu Belu Date: Mon, 20 May 2019 08:24:35 -0700 Subject: [PATCH] test images: Refactors agnhost image Refactors the functions used in agnhost into different modules, based on their functionality, leaving only the main in the base folder. Future commits will add several functionalities to agnhost, so this change will be necessary to keep it clean. --- test/images/agnhost/BUILD | 11 ++-- test/images/agnhost/agnhost.go | 43 ++------------ test/images/agnhost/dns/BUILD | 32 ++++++++++ test/images/agnhost/{ => dns}/common.go | 56 +++++++++--------- test/images/agnhost/{utils.go => dns/dns.go} | 2 +- .../{utils_windows.go => dns/dns_windows.go} | 2 +- test/images/agnhost/pause/BUILD | 28 +++++++++ test/images/agnhost/pause/pause.go | 59 +++++++++++++++++++ 8 files changed, 160 insertions(+), 73 deletions(-) create mode 100644 test/images/agnhost/dns/BUILD rename test/images/agnhost/{ => dns}/common.go (64%) rename test/images/agnhost/{utils.go => dns/dns.go} (99%) rename test/images/agnhost/{utils_windows.go => dns/dns_windows.go} (98%) create mode 100644 test/images/agnhost/pause/BUILD create mode 100644 test/images/agnhost/pause/pause.go diff --git a/test/images/agnhost/BUILD b/test/images/agnhost/BUILD index afe2df5d582..c11a32865e7 100644 --- a/test/images/agnhost/BUILD +++ b/test/images/agnhost/BUILD @@ -13,17 +13,14 @@ go_binary( go_library( name = "go_default_library", - srcs = [ - "agnhost.go", - "common.go", - "utils.go", - "utils_windows.go", - ], + srcs = ["agnhost.go"], importpath = "k8s.io/kubernetes/test/images/agnhost", deps = [ + "//test/images/agnhost/dns:go_default_library", "//test/images/agnhost/net:go_default_library", "//test/images/agnhost/netexec:go_default_library", "//test/images/agnhost/nettest:go_default_library", + "//test/images/agnhost/pause:go_default_library", "//test/images/agnhost/webhook:go_default_library", "//vendor/github.com/spf13/cobra:go_default_library", "//vendor/k8s.io/klog:go_default_library", @@ -41,9 +38,11 @@ filegroup( name = "all-srcs", srcs = [ ":package-srcs", + "//test/images/agnhost/dns:all-srcs", "//test/images/agnhost/net:all-srcs", "//test/images/agnhost/netexec:all-srcs", "//test/images/agnhost/nettest:all-srcs", + "//test/images/agnhost/pause:all-srcs", "//test/images/agnhost/webhook:all-srcs", ], tags = ["automanaged"], diff --git a/test/images/agnhost/agnhost.go b/test/images/agnhost/agnhost.go index 5e9a4c15f1c..0f1a7809767 100644 --- a/test/images/agnhost/agnhost.go +++ b/test/images/agnhost/agnhost.go @@ -22,54 +22,23 @@ import ( "github.com/spf13/cobra" "k8s.io/klog" + "k8s.io/kubernetes/test/images/agnhost/dns" "k8s.io/kubernetes/test/images/agnhost/net" "k8s.io/kubernetes/test/images/agnhost/netexec" "k8s.io/kubernetes/test/images/agnhost/nettest" + "k8s.io/kubernetes/test/images/agnhost/pause" "k8s.io/kubernetes/test/images/agnhost/webhook" ) func main() { - cmdDNSSuffix := &cobra.Command{ - Use: "dns-suffix", - Short: "Prints the host's DNS suffix list", - Long: `Prints the DNS suffixes of this host.`, - Args: cobra.MaximumNArgs(0), - Run: printDNSSuffixList, - } - - cmdDNSServerList := &cobra.Command{ - Use: "dns-server-list", - Short: "Prints the host's DNS Server list", - Long: `Prints the DNS Server list of this host.`, - Args: cobra.MaximumNArgs(0), - Run: printDNSServerList, - } - - cmdEtcHosts := &cobra.Command{ - Use: "etc-hosts", - Short: "Prints the host's /etc/hosts file", - Long: `Prints the "hosts" file of this host."`, - Args: cobra.MaximumNArgs(0), - Run: printHostsFile, - } - - cmdPause := &cobra.Command{ - Use: "pause", - Short: "Pauses", - Long: `Pauses the execution. Useful for keeping the containers running, so other commands can be executed.`, - Args: cobra.MaximumNArgs(0), - Run: pause, - } - rootCmd := &cobra.Command{Use: "app"} - rootCmd.AddCommand(cmdDNSSuffix) - rootCmd.AddCommand(cmdDNSServerList) - rootCmd.AddCommand(cmdEtcHosts) - rootCmd.AddCommand(cmdPause) - + rootCmd.AddCommand(dns.CmdDNSSuffix) + rootCmd.AddCommand(dns.CmdDNSServerList) + rootCmd.AddCommand(dns.CmdEtcHosts) rootCmd.AddCommand(net.CmdNet) rootCmd.AddCommand(netexec.CmdNetexec) rootCmd.AddCommand(nettest.CmdNettest) + rootCmd.AddCommand(pause.CmdPause) rootCmd.AddCommand(webhook.CmdWebhook) // NOTE(claudiub): Some tests are passing logging related flags, so we need to be able to diff --git a/test/images/agnhost/dns/BUILD b/test/images/agnhost/dns/BUILD new file mode 100644 index 00000000000..193173737f1 --- /dev/null +++ b/test/images/agnhost/dns/BUILD @@ -0,0 +1,32 @@ +package(default_visibility = ["//visibility:public"]) + +load( + "@io_bazel_rules_go//go:def.bzl", + "go_library", +) + +go_library( + name = "go_default_library", + srcs = [ + "common.go", + "dns.go", + "dns_windows.go", + ], + importpath = "k8s.io/kubernetes/test/images/agnhost/dns", + deps = [ + "//vendor/github.com/spf13/cobra:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], +) diff --git a/test/images/agnhost/common.go b/test/images/agnhost/dns/common.go similarity index 64% rename from test/images/agnhost/common.go rename to test/images/agnhost/dns/common.go index 9d168025434..06166e9c229 100644 --- a/test/images/agnhost/common.go +++ b/test/images/agnhost/dns/common.go @@ -14,21 +14,45 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package dns import ( "bytes" "fmt" "io/ioutil" - "os" "os/exec" - "os/signal" "strings" - "syscall" "github.com/spf13/cobra" ) +// CmdDNSSuffix is used by agnhost Cobra. +var CmdDNSSuffix = &cobra.Command{ + Use: "dns-suffix", + Short: "Prints the host's DNS suffix list", + Long: `Prints the DNS suffixes of this host.`, + Args: cobra.MaximumNArgs(0), + Run: printDNSSuffixList, +} + +// CmdDNSServerList is used by agnhost Cobra. +var CmdDNSServerList = &cobra.Command{ + Use: "dns-server-list", + Short: "Prints the host's DNS Server list", + Long: `Prints the DNS Server list of this host.`, + Args: cobra.MaximumNArgs(0), + Run: printDNSServerList, +} + +// CmdEtcHosts is used by agnhost Cobra. +var CmdEtcHosts = &cobra.Command{ + Use: "etc-hosts", + Short: "Prints the host's /etc/hosts file", + Long: `Prints the "hosts" file of this host."`, + Args: cobra.MaximumNArgs(0), + Run: printHostsFile, +} + func printDNSSuffixList(cmd *cobra.Command, args []string) { dnsSuffixList := getDNSSuffixList() fmt.Println(strings.Join(dnsSuffixList, ",")) @@ -43,30 +67,6 @@ func printHostsFile(cmd *cobra.Command, args []string) { fmt.Println(readFile(etcHostsFile)) } -func pause(cmd *cobra.Command, args []string) { - sigCh := make(chan os.Signal) - done := make(chan int, 1) - signal.Notify(sigCh, syscall.SIGINT) - signal.Notify(sigCh, syscall.SIGTERM) - signal.Notify(sigCh, syscall.SIGKILL) - go func() { - sig := <-sigCh - switch sig { - case syscall.SIGINT: - done <- 1 - os.Exit(1) - case syscall.SIGTERM: - done <- 2 - os.Exit(2) - case syscall.SIGKILL: - done <- 0 - os.Exit(0) - } - }() - result := <-done - fmt.Printf("exiting %d\n", result) -} - func readFile(fileName string) string { fileData, err := ioutil.ReadFile(fileName) if err != nil { diff --git a/test/images/agnhost/utils.go b/test/images/agnhost/dns/dns.go similarity index 99% rename from test/images/agnhost/utils.go rename to test/images/agnhost/dns/dns.go index 26cab3ce62c..6908dbfa0c9 100644 --- a/test/images/agnhost/utils.go +++ b/test/images/agnhost/dns/dns.go @@ -16,7 +16,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package dns import ( "strings" diff --git a/test/images/agnhost/utils_windows.go b/test/images/agnhost/dns/dns_windows.go similarity index 98% rename from test/images/agnhost/utils_windows.go rename to test/images/agnhost/dns/dns_windows.go index 0012fe7e6cf..68fbf51926c 100644 --- a/test/images/agnhost/utils_windows.go +++ b/test/images/agnhost/dns/dns_windows.go @@ -14,7 +14,7 @@ See the License for the specific language governing permissions and limitations under the License. */ -package main +package dns import ( "strings" diff --git a/test/images/agnhost/pause/BUILD b/test/images/agnhost/pause/BUILD new file mode 100644 index 00000000000..c03ff568f8f --- /dev/null +++ b/test/images/agnhost/pause/BUILD @@ -0,0 +1,28 @@ +package(default_visibility = ["//visibility:public"]) + +load( + "@io_bazel_rules_go//go:def.bzl", + "go_library", +) + +go_library( + name = "go_default_library", + srcs = ["pause.go"], + importpath = "k8s.io/kubernetes/test/images/agnhost/pause", + deps = [ + "//vendor/github.com/spf13/cobra:go_default_library", + ], +) + +filegroup( + name = "package-srcs", + srcs = glob(["**"]), + tags = ["automanaged"], + visibility = ["//visibility:private"], +) + +filegroup( + name = "all-srcs", + srcs = [":package-srcs"], + tags = ["automanaged"], +) diff --git a/test/images/agnhost/pause/pause.go b/test/images/agnhost/pause/pause.go new file mode 100644 index 00000000000..1f93037a66b --- /dev/null +++ b/test/images/agnhost/pause/pause.go @@ -0,0 +1,59 @@ +/* +Copyright 2019 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 pause + +import ( + "fmt" + "os" + "os/signal" + "syscall" + + "github.com/spf13/cobra" +) + +// CmdPause is used by agnhost Cobra. +var CmdPause = &cobra.Command{ + Use: "pause", + Short: "Pauses the execution", + Long: `Pauses the execution. Useful for keeping the containers running, so other commands can be executed.`, + Args: cobra.MaximumNArgs(0), + Run: pause, +} + +func pause(cmd *cobra.Command, args []string) { + sigCh := make(chan os.Signal) + done := make(chan int, 1) + signal.Notify(sigCh, syscall.SIGINT) + signal.Notify(sigCh, syscall.SIGTERM) + signal.Notify(sigCh, syscall.SIGKILL) + go func() { + sig := <-sigCh + switch sig { + case syscall.SIGINT: + done <- 1 + os.Exit(1) + case syscall.SIGTERM: + done <- 2 + os.Exit(2) + case syscall.SIGKILL: + done <- 0 + os.Exit(0) + } + }() + result := <-done + fmt.Printf("exiting %d\n", result) +}