github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/pkg/vmi.go (about)

     1  // Copyright (c) 2020, 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 pkg
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  
    11  	"github.com/hashicorp/go-retryablehttp"
    12  	"github.com/onsi/gomega"
    13  	"go.uber.org/zap"
    14  
    15  	"gopkg.in/yaml.v3"
    16  
    17  	vzconst "github.com/verrazzano/verrazzano/pkg/constants"
    18  	v1 "k8s.io/api/core/v1"
    19  	netv1 "k8s.io/api/networking/v1"
    20  )
    21  
    22  func VerifySystemVMIComponent(log *zap.SugaredLogger, api *APIEndpoint, sysVmiHTTPClient *retryablehttp.Client, vmiCredentials *UsernamePassword, ingressName, expectedURLPrefix string) bool {
    23  	var ingress *netv1.Ingress
    24  	var err error
    25  
    26  	// retry in case of transient network errors
    27  	for i := 1; i <= 5; i++ {
    28  		if api != nil {
    29  			ingress, err = api.GetIngress(vzconst.VerrazzanoSystemNamespace, ingressName)
    30  		} else {
    31  			ingress, err = GetIngress(vzconst.VerrazzanoSystemNamespace, ingressName)
    32  		}
    33  		if err == nil {
    34  			break
    35  		}
    36  		time.Sleep(time.Duration(i) * time.Second)
    37  	}
    38  	if err != nil {
    39  		log.Errorf("Error getting ingress: %v", err)
    40  		return false
    41  	}
    42  	vmiComponentURL := fmt.Sprintf("https://%s", ingress.Spec.Rules[0].Host)
    43  	if !strings.HasPrefix(vmiComponentURL, expectedURLPrefix) {
    44  		log.Errorf("URL '%s' does not have expected prefix: %s", vmiComponentURL, expectedURLPrefix)
    45  		return false
    46  	}
    47  	return AssertURLAccessibleAndAuthorized(sysVmiHTTPClient, vmiComponentURL, vmiCredentials)
    48  }
    49  
    50  func VerifyOpenSearchComponent(log *zap.SugaredLogger, api *APIEndpoint, sysVmiHTTPClient *retryablehttp.Client, vmiCredentials *UsernamePassword) bool {
    51  	return VerifySystemVMIComponent(log, api, sysVmiHTTPClient, vmiCredentials, "opensearch", "https://opensearch.vmi.system")
    52  }
    53  
    54  func VerifyOpenSearchDashboardsComponent(log *zap.SugaredLogger, api *APIEndpoint, sysVmiHTTPClient *retryablehttp.Client, vmiCredentials *UsernamePassword) bool {
    55  	return VerifySystemVMIComponent(log, api, sysVmiHTTPClient, vmiCredentials, "opensearch-dashboards", "https://osd.vmi.system")
    56  }
    57  
    58  func VerifyPrometheusComponent(log *zap.SugaredLogger, api *APIEndpoint, sysVmiHTTPClient *retryablehttp.Client, vmiCredentials *UsernamePassword) bool {
    59  	return VerifySystemVMIComponent(log, api, sysVmiHTTPClient, vmiCredentials, "vmi-system-prometheus", "https://prometheus.vmi.system")
    60  }
    61  
    62  func VerifyGrafanaComponent(log *zap.SugaredLogger, api *APIEndpoint, sysVmiHTTPClient *retryablehttp.Client, vmiCredentials *UsernamePassword) bool {
    63  	return VerifySystemVMIComponent(log, api, sysVmiHTTPClient, vmiCredentials, "vmi-system-grafana", "https://grafana.vmi.system")
    64  }
    65  
    66  func EventuallyGetSystemVMICredentials() *UsernamePassword {
    67  	var vmiCredentials *UsernamePassword
    68  	gomega.Eventually(func() (*UsernamePassword, error) {
    69  		var err error
    70  		vmiCredentials, err = GetSystemVMICredentials()
    71  		return vmiCredentials, err
    72  	}, waitTimeout, pollingInterval).ShouldNot(gomega.BeNil())
    73  	return vmiCredentials
    74  }
    75  
    76  // GetSystemVMICredentials - Obtain VMI system credentials
    77  func GetSystemVMICredentials() (*UsernamePassword, error) {
    78  	secret, err := GetSecret("verrazzano-system", "verrazzano")
    79  	if err != nil {
    80  		return nil, err
    81  	}
    82  
    83  	username := secret.Data["username"]
    84  	password := secret.Data["password"]
    85  	if username == nil || password == nil {
    86  		return nil, fmt.Errorf("username and password fields required in secret %v", secret)
    87  	}
    88  
    89  	return &UsernamePassword{
    90  		Username: string(username),
    91  		Password: string(password),
    92  	}, nil
    93  }
    94  
    95  // GetPrometheusConfig - Returns the Prometehus Configmap, Marshalled prometehus.yml and the scrape config list
    96  func GetPrometheusConfig() (*v1.ConfigMap, []interface{}, map[interface{}]interface{}, error) {
    97  	configMap, err := GetConfigMap(vzconst.VmiPromConfigName, vzconst.VerrazzanoSystemNamespace)
    98  	if err != nil {
    99  		Log(Error, fmt.Sprintf("Failed getting configmap: %v", err))
   100  		return nil, nil, nil, err
   101  	}
   102  
   103  	prometheusConfig := configMap.Data["prometheus.yml"]
   104  	var configYaml map[interface{}]interface{}
   105  	err = yaml.Unmarshal([]byte(prometheusConfig), &configYaml)
   106  	if err != nil {
   107  		Log(Error, fmt.Sprintf("Failed getting configmap yaml: %v", err))
   108  		return nil, nil, nil, err
   109  	}
   110  
   111  	scrapeConfigsData := configYaml["scrape_configs"]
   112  	scrapeConfigs := scrapeConfigsData.([]interface{})
   113  	return configMap, scrapeConfigs, configYaml, nil
   114  }