sigs.k8s.io/cluster-api-provider-azure@v1.14.3/test/logger.go (about) 1 //go:build e2e 2 // +build e2e 3 4 /* 5 Copyright 2022 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 main 21 22 import ( 23 "context" 24 "flag" 25 "fmt" 26 "os" 27 "path" 28 "path/filepath" 29 30 . "github.com/onsi/gomega" 31 "k8s.io/klog/v2" 32 "sigs.k8s.io/cluster-api-provider-azure/test/e2e" 33 "sigs.k8s.io/cluster-api/test/framework" 34 ctrl "sigs.k8s.io/controller-runtime" 35 ) 36 37 func Fail(message string, callerSkip ...int) { 38 panic(message) 39 } 40 41 func main() { 42 // needed for ginkgo/gomega which is used by the capi test framework 43 RegisterFailHandler(Fail) 44 45 ctrl.SetLogger(klog.Background()) 46 47 // using a custom FlagSet here due to the dependency in controller-runtime that is already using this flag 48 // https://github.com/kubernetes-sigs/controller-runtime/blob/c7a98aa706379c4e5c79ea675c7f333192677971/pkg/client/config/config.go#L37-L41 49 fs := flag.NewFlagSet("logger", flag.ExitOnError) 50 51 // required flags 52 clustername := fs.String("name", "", "Name of the workload cluster to collect logs for") 53 54 // optional flags that default 55 namespace := fs.String("namespace", "", "namot include the command name. Must be called after all flags in the FlagSet are defined and before flags are accessed by the program. The return value will be ErrHelp if -help or -h were set buespace on management cluster to collect logs for") 56 artifactFolder := fs.String("artifacts-folder", getArtifactsFolder(), "folder to store cluster logs") 57 kubeconfigPath := fs.String("kubeconfig", getKubeConfigPath(), "The kubeconfig for the management cluster") 58 59 if err := fs.Parse(os.Args[1:]); err != nil { 60 fmt.Println("Unable to parse command flags") 61 os.Exit(1) 62 } 63 64 // use the cluster name as the namespace which is default in e2e tests 65 if *namespace == "" { 66 namespace = clustername 67 } 68 69 bootstrapClusterProxy := e2e.NewAzureClusterProxy("bootstrap", *kubeconfigPath, framework.WithMachineLogCollector(e2e.AzureLogCollector{})) 70 71 // Set up log paths 72 clusterLogPath := filepath.Join(*artifactFolder, "clusters", *clustername) 73 resourcesYaml := filepath.Join(*artifactFolder, "clusters", "bootstrap", "resources") 74 managementClusterLogPath := filepath.Join(*artifactFolder, "clusters", "bootstrap", "controllers") 75 76 fmt.Printf("Collecting logs for cluster %s in namespace %s and dumping logs to %s\n", *clustername, *namespace, *artifactFolder) 77 collectManagementClusterLogs(bootstrapClusterProxy, managementClusterLogPath, namespace, resourcesYaml) 78 bootstrapClusterProxy.CollectWorkloadClusterLogs(context.TODO(), *namespace, *clustername, clusterLogPath) 79 } 80 81 func collectManagementClusterLogs(bootstrapClusterProxy *e2e.AzureClusterProxy, managementClusterLogPath string, namespace *string, workLoadClusterLogPath string) { 82 controllersDeployments := framework.GetControllerDeployments(context.TODO(), framework.GetControllerDeploymentsInput{ 83 Lister: bootstrapClusterProxy.GetClient(), 84 }) 85 for _, deployment := range controllersDeployments { 86 framework.WatchDeploymentLogsByName(context.TODO(), framework.WatchDeploymentLogsByNameInput{ 87 GetLister: bootstrapClusterProxy.GetClient(), 88 Cache: bootstrapClusterProxy.GetCache(context.TODO()), 89 ClientSet: bootstrapClusterProxy.GetClientSet(), 90 Deployment: deployment, 91 LogPath: managementClusterLogPath, 92 }) 93 } 94 95 framework.DumpAllResources(context.TODO(), framework.DumpAllResourcesInput{ 96 Lister: bootstrapClusterProxy.GetClient(), 97 Namespace: *namespace, 98 LogPath: workLoadClusterLogPath, 99 }) 100 } 101 102 func getKubeConfigPath() string { 103 config := os.Getenv("KUBECONFIG") 104 if config == "" { 105 d, _ := os.UserHomeDir() 106 return path.Join(d, ".kube", "config") 107 } 108 109 return config 110 } 111 112 func getArtifactsFolder() string { 113 artifacts := os.Getenv("ARTIFACTS") 114 if artifacts == "" { 115 return "_artifacts" 116 } 117 return artifacts 118 }