github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/verify-analyze-tool/utility.go (about) 1 // Copyright (c) 2023, Oracle and/or its affiliates. 2 // Licensed under the Universal Permissive License v 1.0 as shown at https://oss.oracle.com/licenses/upl. 3 4 package utility 5 6 import ( 7 "context" 8 "errors" 9 v1 "k8s.io/apimachinery/pkg/apis/meta/v1" 10 "k8s.io/client-go/kubernetes" 11 kv1 "k8s.io/client-go/kubernetes/typed/apps/v1" 12 "os" 13 "os/exec" 14 "strings" 15 "time" 16 ) 17 18 var ( 19 waitTimeout = 10 * time.Second 20 ) 21 22 const ( 23 ImagePullNotFound string = "ImagePullNotFound" 24 ImagePullBackOff string = "ImagePullBackOff" 25 PodProblemsNotReported string = "PodProblemsNotReported" 26 InsufficientMemory string = "InsufficientMemory" 27 InsufficientCPU string = "InsufficientCPU" 28 VzSystemNS string = "verrazzano-system" 29 DeploymentToBePatched string = "verrazzano-console" 30 ) 31 32 var ReportAnalysis = make(map[string][]string) 33 34 // PatchImage patches a deployment's image and feeds cluster analysis report 35 // patching includes both injection of an issue and its revival 36 func PatchImage(client *kubernetes.Clientset, deploymentName, issueType, patchImage string) error { 37 deploymentClient := client.AppsV1().Deployments(VzSystemNS) 38 origImg := "" 39 var err error 40 for i := 0; i < 2; i++ { 41 if origImg, err = update(deploymentClient, deploymentName, issueType, patchImage, origImg); err != nil { 42 return err 43 } 44 r, e := RunVzAnalyze() 45 if e != nil { 46 return e 47 } 48 ReportAnalysis[issueType] = append(ReportAnalysis[issueType], r) 49 patchImage = "" 50 time.Sleep(waitTimeout) 51 } 52 return nil 53 } 54 55 func update(deploymentClient kv1.DeploymentInterface, deploymentName, issueType, patchImage, origImg string) (string, error) { 56 result, err := deploymentClient.Get(context.TODO(), deploymentName, v1.GetOptions{}) 57 if err != nil { 58 return "", err 59 } 60 for i, container := range result.Spec.Template.Spec.Containers { 61 if container.Name == deploymentName { 62 image := result.Spec.Template.Spec.Containers[i].Image 63 if patchImage == "" { 64 patchImage = origImg 65 } else if issueType == ImagePullNotFound { 66 patchImage = image + patchImage 67 } 68 result.Spec.Template.Spec.Containers[i].Image = patchImage 69 _, err = deploymentClient.Update(context.TODO(), result, v1.UpdateOptions{}) 70 time.Sleep(time.Second * 20) 71 return image, err 72 } 73 } 74 return "", errors.New("container not found") 75 } 76 77 // PatchPod patches a deployment's pod and feeds cluster analysis report 78 // patching includes both injection of an issue and its revival 79 func PatchPod(issueType string, resourceReq []string) error { 80 for i := 0; i < len(resourceReq); i++ { 81 if err := SetDepResources(DeploymentToBePatched, VzSystemNS, resourceReq[i]); err != nil { 82 return err 83 } 84 time.Sleep(waitTimeout) 85 out, err := RunVzAnalyze() 86 if err != nil { 87 return err 88 } 89 ReportAnalysis[issueType] = append(ReportAnalysis[issueType], out) 90 if i == 0 { 91 time.Sleep(time.Second * 20) 92 } 93 } 94 return nil 95 } 96 97 // RunVzAnalyze runs and deliver cluster analysis report 98 func RunVzAnalyze() (string, error) { 99 cmd := exec.Command("./vz", "analyze") 100 if goRepoPath := os.Getenv("GO_REPO_PATH"); goRepoPath != "" { 101 cmd.Dir = goRepoPath 102 } 103 out, err := cmd.Output() 104 return string(out), err 105 } 106 107 // SetDepResources sets pod's resources i.e (cpu/ memory) 108 func SetDepResources(dep, ns, req string) error { 109 args := []string{"set", "resources", "deploy/" + dep, "--requests=" + req, "-n", ns} 110 return exec.Command("kubectl", args...).Run() 111 } 112 113 // VerifyIssue verifies issue against cluster analysis report 114 func VerifyIssue(out, issueType string) bool { 115 return strings.Contains(out, issueType) 116 }