|
| 1 | +/* |
| 2 | +Copyright 2025 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 nimbus |
| 18 | + |
| 19 | +import ( |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "os" |
| 23 | + "os/exec" |
| 24 | + |
| 25 | + "github.com/davecgh/go-spew/spew" |
| 26 | + "github.com/onsi/gomega" |
| 27 | + "k8s.io/kubernetes/test/e2e/framework" |
| 28 | + "sigs.k8s.io/vsphere-csi-driver/v3/tests/e2e/config" |
| 29 | +) |
| 30 | + |
| 31 | +// This function provides power on/off functionality to nimbus VMs (space separated list) |
| 32 | +func VMPowerMgmt(user string, location string, podname string, hostList string, shouldBePoweredOn bool) error { |
| 33 | + var err error |
| 34 | + op := "off" |
| 35 | + if shouldBePoweredOn { |
| 36 | + op = "on" |
| 37 | + } |
| 38 | + nimbusCmd := fmt.Sprintf("USER=%s /mts/git/bin/nimbus-ctl --nimbusLocation %s --nimbus %s %s %s", user, |
| 39 | + location, podname, op, hostList) |
| 40 | + framework.Logf("Running command: %s", nimbusCmd) |
| 41 | + cmd := exec.Command("/bin/bash", "-c", nimbusCmd) |
| 42 | + err = cmd.Start() |
| 43 | + if err != nil { |
| 44 | + return err |
| 45 | + } |
| 46 | + err = cmd.Wait() |
| 47 | + |
| 48 | + framework.Logf("stdout:\n%v\nstderr:\n%v\n", cmd.Stdout, cmd.Stderr) |
| 49 | + return err |
| 50 | +} |
| 51 | + |
| 52 | +// This function is used to perform datatsore nimbus operations |
| 53 | +func DatatoreOperations(user string, location string, podname string, vmName string, op string) error { |
| 54 | + var err error |
| 55 | + nimbusCmd := fmt.Sprintf("USER=%s /mts/git/bin/nimbus-ctl --nimbusLocation %s --nimbus %s %s %s", user, |
| 56 | + location, podname, op, vmName) |
| 57 | + framework.Logf("Running command: %s", nimbusCmd) |
| 58 | + cmd := exec.Command("/bin/bash", "-c", nimbusCmd) |
| 59 | + err = cmd.Start() |
| 60 | + if err != nil { |
| 61 | + return err |
| 62 | + } |
| 63 | + err = cmd.Wait() |
| 64 | + |
| 65 | + framework.Logf("stdout:\n%v\nstderr:\n%v\n", cmd.Stdout, cmd.Stderr) |
| 66 | + return err |
| 67 | +} |
| 68 | + |
| 69 | +// This function read basic testbed info from the provided json file |
| 70 | +func ReadVcEsxIpsViaTestbedInfoJson(filePath string) { |
| 71 | + tbinfo := config.TestBedConfig{} |
| 72 | + |
| 73 | + file, err := os.ReadFile(filePath) |
| 74 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 75 | + |
| 76 | + framework.Logf("Fetching basic testbed info from json file") |
| 77 | + |
| 78 | + var tb map[string]interface{} |
| 79 | + err = json.Unmarshal(file, &tb) |
| 80 | + gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 81 | + |
| 82 | + vcs := tb["vc"].([]interface{}) |
| 83 | + vc1 := vcs[0].(map[string]interface{}) |
| 84 | + tbinfo.VcIp = vc1["ip"].(string) |
| 85 | + tbinfo.VcVmName = vc1["name"].(string) |
| 86 | + |
| 87 | + esxs := tb["esx"].([]interface{}) |
| 88 | + nfsDS := tb["nfs"].([]interface{}) |
| 89 | + iscsiDS := tb["iscsi"].([]interface{}) |
| 90 | + |
| 91 | + esxHosts := []map[string]string{} |
| 92 | + nfsDatastores := []map[string]string{} |
| 93 | + iscsiDatastores := []map[string]string{} |
| 94 | + |
| 95 | + for _, esx := range esxs { |
| 96 | + host := make(map[string]string) |
| 97 | + host["ip"] = esx.(map[string]interface{})["ip"].(string) |
| 98 | + host["vmName"] = esx.(map[string]interface{})["name"].(string) |
| 99 | + esxHosts = append(esxHosts, host) |
| 100 | + } |
| 101 | + |
| 102 | + for _, nfs := range nfsDS { |
| 103 | + ds := make(map[string]string) |
| 104 | + ds["ip"] = nfs.(map[string]interface{})["ip"].(string) |
| 105 | + ds["vmName"] = nfs.(map[string]interface{})["name"].(string) |
| 106 | + nfsDatastores = append(nfsDatastores, ds) |
| 107 | + } |
| 108 | + |
| 109 | + for _, iscsi := range iscsiDS { |
| 110 | + ds := make(map[string]string) |
| 111 | + ds["ip"] = iscsi.(map[string]interface{})["ip"].(string) |
| 112 | + ds["vmName"] = iscsi.(map[string]interface{})["name"].(string) |
| 113 | + iscsiDatastores = append(iscsiDatastores, ds) |
| 114 | + } |
| 115 | + iscsiDatastores = append(iscsiDatastores, nfsDatastores...) |
| 116 | + |
| 117 | + tbinfo.EsxHosts = esxHosts |
| 118 | + tbinfo.Datastores = iscsiDatastores |
| 119 | + |
| 120 | + tbinfo.Name = tb["name"].(string) |
| 121 | + tbinfo.User = tb["user_name"].(string) |
| 122 | + tbinfo.Location = tb["nimbusLocation"].(string) |
| 123 | + tbinfo.Podname = tb["podname"].(string) |
| 124 | + |
| 125 | + framework.Logf("Basic testbed info:\n%s\n", spew.Sdump(tbinfo)) |
| 126 | +} |
0 commit comments