github.com/verrazzano/verrazzano@v1.7.0/pkg/k8sutil/platform_operator_utils.go (about)

     1  // Copyright (c) 2022, 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  package k8sutil
     4  
     5  import (
     6  	"fmt"
     7  	vzos "github.com/verrazzano/verrazzano/pkg/os"
     8  	"os/exec"
     9  	"strings"
    10  )
    11  
    12  // cmdRunner needed for unit tests
    13  var runner vzos.CmdRunner = vzos.DefaultRunner{}
    14  
    15  // GetInstalledBOMData Exec's into the Platform Operator pod and returns the installed BOM file data as JSON
    16  func GetInstalledBOMData(kubeconfigPath string) ([]byte, error) {
    17  	const platformOperatorPodNameSearchString = "verrazzano-platform-operator" // Pod Substring for finding the platform operator pod
    18  
    19  	kubeconfigArgs := []string{}
    20  	if len(kubeconfigPath) > 0 {
    21  		kubeconfigArgs = append(kubeconfigArgs, "--kubeconfig", kubeconfigPath)
    22  	}
    23  
    24  	listPodsArgs := []string{"get", "pod", "-o", "name", "--no-headers=true", "-n", "verrazzano-install"}
    25  	if len(kubeconfigArgs) > 0 {
    26  		listPodsArgs = append(listPodsArgs, kubeconfigArgs...)
    27  	}
    28  	cmd := exec.Command("kubectl", listPodsArgs...)
    29  	podListOutput, _, err := runner.Run(cmd)
    30  	if err != nil {
    31  		return []byte{}, err
    32  	}
    33  	var platformOperatorPodName = ""
    34  	vzInstallPods := string(podListOutput)
    35  	vzInstallPodArray := strings.Split(vzInstallPods, "\n")
    36  	for _, podName := range vzInstallPodArray {
    37  		if strings.Contains(podName, platformOperatorPodNameSearchString) {
    38  			platformOperatorPodName = podName
    39  			break
    40  		}
    41  	}
    42  	if platformOperatorPodName == "" {
    43  		return []byte{}, fmt.Errorf("pod not found in verrazzano-install namespace")
    44  	}
    45  
    46  	platformOperatorPodName = strings.TrimSuffix(platformOperatorPodName, "\n")
    47  
    48  	//  Get the BOM from platform-operator
    49  	getBOMArgs := []string{"exec", "-it", platformOperatorPodName, "-n", "verrazzano-install", "--", "cat", "/verrazzano/platform-operator/verrazzano-bom.json"}
    50  	if len(kubeconfigPath) > 0 {
    51  		getBOMArgs = append(getBOMArgs, "--kubeconfig", kubeconfigPath)
    52  	}
    53  	cmd = exec.Command("kubectl", getBOMArgs...)
    54  	bomBytes, _, err := runner.Run(cmd)
    55  	if err != nil {
    56  		return []byte{}, err
    57  	}
    58  	if len(bomBytes) == 0 {
    59  		return bomBytes, fmt.Errorf("Error retrieving BOM from platform operator, no data found")
    60  	}
    61  	return bomBytes, nil
    62  }
    63  
    64  // SetCmdRunner sets the command runner as needed by unit tests
    65  func SetCmdRunner(r vzos.CmdRunner) {
    66  	runner = r
    67  }
    68  
    69  // SetDefaultRunner sets the command runner to default
    70  func SetDefaultRunner() {
    71  	runner = vzos.DefaultRunner{}
    72  }