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.
This commit is contained in:
Claudiu Belu 2019-05-20 08:24:35 -07:00
parent 70b46ca5fb
commit 99e2646067
8 changed files with 160 additions and 73 deletions

View File

@ -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"],

View File

@ -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

View File

@ -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"],
)

View File

@ -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 {

View File

@ -16,7 +16,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package main
package dns
import (
"strings"

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License.
*/
package main
package dns
import (
"strings"

View File

@ -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"],
)

View File

@ -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)
}