github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/pkg/bom.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  
     4  package pkg
     5  
     6  import (
     7  	"encoding/json"
     8  	"fmt"
     9  	"os/exec"
    10  	"strings"
    11  
    12  	"github.com/verrazzano/verrazzano/pkg/bom"
    13  )
    14  
    15  const (
    16  	// Pod Substring for finding the platform operator pod
    17  	platformOperatorPodNameSearchString = "verrazzano-platform-operator"
    18  )
    19  
    20  // Get the BOM from the platform operator in the cluster and build the BOM structure from it
    21  func GetBOMDoc() (*bom.BomDoc, error) {
    22  	var platformOperatorPodName = ""
    23  
    24  	out, err := exec.Command("kubectl", "get", "pod", "-o", "name", "--no-headers=true", "-n", "verrazzano-install").Output()
    25  	if err != nil {
    26  		return nil, fmt.Errorf("error in gettting %s pod name: %v", platformOperatorPodNameSearchString, err)
    27  	}
    28  	vzInstallPods := string(out)
    29  	vzInstallPodArray := strings.Split(vzInstallPods, "\n")
    30  	for _, podName := range vzInstallPodArray {
    31  		if strings.Contains(podName, platformOperatorPodNameSearchString) {
    32  			platformOperatorPodName = podName
    33  			break
    34  		}
    35  	}
    36  	if platformOperatorPodName == "" {
    37  		return nil, fmt.Errorf("platform operator pod name not found in verrazzano-install namespace")
    38  	}
    39  
    40  	platformOperatorPodName = strings.TrimSuffix(platformOperatorPodName, "\n")
    41  	fmt.Printf("Getting the registry details in BOM from the platform operator %s\n", platformOperatorPodName)
    42  
    43  	// Get the BOM from platform-operator
    44  	out, err = exec.Command("kubectl", "exec", "-it", platformOperatorPodName, "-n", "verrazzano-install", "--",
    45  		"cat", "/verrazzano/platform-operator/verrazzano-bom.json").Output()
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	if len(string(out)) == 0 {
    50  		return nil, fmt.Errorf("error retrieving BOM from platform operator, zero length")
    51  	}
    52  	var bomDoc bom.BomDoc
    53  	err = json.Unmarshal(out, &bomDoc)
    54  	return &bomDoc, err
    55  }