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

     1  // Copyright (c) 2021, 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  	"context"
     8  	"fmt"
     9  
    10  	globalconst "github.com/verrazzano/verrazzano/pkg/constants"
    11  	"github.com/verrazzano/verrazzano/pkg/k8sutil"
    12  	appv1 "k8s.io/api/apps/v1"
    13  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    14  )
    15  
    16  const verrazzanoNamespace string = "verrazzano-system"
    17  const VmiESURL = "http://verrazzano-authproxy-opensearch:8775"
    18  const VmiESLegacySecret = "verrazzano"               //nolint:gosec //#gosec G101
    19  const VmiESInternalSecret = "verrazzano-es-internal" //nolint:gosec //#gosec G101
    20  
    21  func getFluentdDaemonset() (*appv1.DaemonSet, error) {
    22  	clientset, err := k8sutil.GetKubernetesClientset()
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  	ds, err := clientset.AppsV1().DaemonSets(verrazzanoNamespace).Get(context.TODO(), globalconst.FluentdDaemonSetName, metav1.GetOptions{})
    27  	if err != nil {
    28  		return nil, err
    29  	}
    30  	return ds, nil
    31  }
    32  
    33  // AssertFluentdURLAndSecret makes sure that expectedURL and expectedSecret are the same as
    34  // what is configured in the fluentd daemonset.
    35  // empty string expectedURL means skip comparing URL. Verrazzano prior to 1.1 could configure URL differently.
    36  func AssertFluentdURLAndSecret(expectedURL, expectedSecret string) bool {
    37  	urlFound := ""
    38  	usernameSecretFound := ""
    39  	passwordSecretFound := ""
    40  	volumeSecretFound := ""
    41  	fluentdDaemonset, err := getFluentdDaemonset()
    42  	if err != nil {
    43  		Log(Info, "Can not find fluentdDaemonset")
    44  		return false
    45  	}
    46  	if fluentdDaemonset == nil {
    47  		Log(Info, "Can not find fluentdDaemonset")
    48  		return false
    49  	}
    50  	containers := fluentdDaemonset.Spec.Template.Spec.Containers
    51  	if len(containers) > 0 {
    52  		for _, env := range containers[0].Env {
    53  			if env.Name == "ELASTICSEARCH_URL" {
    54  				urlFound = env.Value
    55  			}
    56  			if env.Name == "ELASTICSEARCH_USER" {
    57  				usernameSecretFound = env.ValueFrom.SecretKeyRef.Name
    58  			}
    59  			if env.Name == "ELASTICSEARCH_PASSWORD" {
    60  				passwordSecretFound = env.ValueFrom.SecretKeyRef.Name
    61  			}
    62  		}
    63  	} else {
    64  		Log(Info, "Can not find containers in fluentdDaemonset")
    65  		return false
    66  	}
    67  	if len(fluentdDaemonset.Spec.Template.Spec.Volumes) > 0 {
    68  		for _, vol := range fluentdDaemonset.Spec.Template.Spec.Volumes {
    69  			if vol.Name == "secret-volume" {
    70  				volumeSecretFound = vol.Secret.SecretName
    71  			}
    72  		}
    73  	} else {
    74  		Log(Info, "Can not find volumes in fluentdDaemonset")
    75  		return false
    76  	}
    77  	if urlFound != expectedURL {
    78  		Log(Info, fmt.Sprintf("ES URL in fluentdDaemonset %s doesn't match expected %s", urlFound, expectedURL))
    79  	}
    80  	if usernameSecretFound != expectedSecret {
    81  		Log(Info, fmt.Sprintf("ES user secret in fluentdDaemonset %s doesn't match expected %s", usernameSecretFound, expectedSecret))
    82  	}
    83  	if passwordSecretFound != expectedSecret {
    84  		Log(Info, fmt.Sprintf("ES password secret in fluentdDaemonset %s doesn't match expected %s", passwordSecretFound, expectedSecret))
    85  	}
    86  	if volumeSecretFound != expectedSecret {
    87  		Log(Info, fmt.Sprintf("ES volume secret in fluentdDaemonset %s doesn't match expected %s", volumeSecretFound, expectedSecret))
    88  	}
    89  	return (expectedURL == "" || urlFound == expectedURL) && usernameSecretFound == expectedSecret && passwordSecretFound == expectedSecret && volumeSecretFound == expectedSecret
    90  }