github.com/verrazzano/verrazzano@v1.7.0/cluster-operator/controllers/vmc/jaeger_test.go (about)

     1  // Copyright (c) 2022, 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 vmc
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	globalconst "github.com/verrazzano/verrazzano/pkg/constants"
    11  	"github.com/verrazzano/verrazzano/pkg/log/vzlog"
    12  	"github.com/verrazzano/verrazzano/pkg/mcconstants"
    13  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1beta1"
    14  	"github.com/verrazzano/verrazzano/platform-operator/constants"
    15  	corev1 "k8s.io/api/core/v1"
    16  	k8net "k8s.io/api/networking/v1"
    17  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    18  	v12 "k8s.io/apimachinery/pkg/apis/meta/v1"
    19  	"k8s.io/apimachinery/pkg/runtime"
    20  	"sigs.k8s.io/controller-runtime/pkg/client/fake"
    21  )
    22  
    23  var (
    24  	falseValue = false
    25  	trueValue  = true
    26  )
    27  
    28  var keycloakDisabledCR = &vzapi.Verrazzano{
    29  	Spec: vzapi.VerrazzanoSpec{
    30  		Components: vzapi.ComponentSpec{
    31  			Keycloak: &vzapi.KeycloakComponent{
    32  				Enabled: &falseValue,
    33  			},
    34  			JaegerOperator: &vzapi.JaegerOperatorComponent{
    35  				Enabled: &trueValue,
    36  			},
    37  		},
    38  	},
    39  }
    40  
    41  var openSearchDisabledCR = &vzapi.Verrazzano{
    42  	Spec: vzapi.VerrazzanoSpec{
    43  		Components: vzapi.ComponentSpec{
    44  			OpenSearch: &vzapi.OpenSearchComponent{
    45  				Enabled: &falseValue,
    46  			},
    47  			JaegerOperator: &vzapi.JaegerOperatorComponent{
    48  				Enabled: &trueValue,
    49  			},
    50  		},
    51  	},
    52  }
    53  
    54  var jaegerEnabledCR = &vzapi.Verrazzano{
    55  	Spec: vzapi.VerrazzanoSpec{
    56  		Components: vzapi.ComponentSpec{
    57  			JaegerOperator: &vzapi.JaegerOperatorComponent{
    58  				Enabled: &trueValue,
    59  			},
    60  		},
    61  	},
    62  }
    63  
    64  var defaultJaegerSecret = &corev1.Secret{
    65  	ObjectMeta: v12.ObjectMeta{Name: globalconst.DefaultJaegerSecretName,
    66  		Namespace: constants.VerrazzanoMonitoringNamespace},
    67  	Data: map[string][]byte{
    68  		mcconstants.JaegerOSUsernameKey: []byte("username"),
    69  		mcconstants.JaegerOSPasswordKey: []byte("password"),
    70  	},
    71  }
    72  
    73  var jaegerExternalOSSecret = &corev1.Secret{
    74  	ObjectMeta: v12.ObjectMeta{Name: customJaegerSecName,
    75  		Namespace: constants.VerrazzanoMonitoringNamespace},
    76  	Data: map[string][]byte{
    77  		mcconstants.JaegerOSUsernameKey: []byte("externalusername"),
    78  		mcconstants.JaegerOSPasswordKey: []byte("externalpassword"),
    79  		"ca-bundle":                     []byte("jaegeropensearchtlscakey"),
    80  		"os.tls.key":                    []byte("jaegeropensearchtlskey"),
    81  		"os.tls.cert":                   []byte("jaegeropensearchtlscert"),
    82  	},
    83  }
    84  
    85  var vzTLSSecret = &corev1.Secret{
    86  	ObjectMeta: v12.ObjectMeta{Name: "verrazzano-tls",
    87  		Namespace: constants.VerrazzanoSystemNamespace},
    88  	Data: map[string][]byte{
    89  		mcconstants.CaCrtKey: []byte("adminCAbundle"),
    90  	},
    91  }
    92  
    93  const (
    94  	externalOSURL        = "https://external-os"
    95  	customJaegerSecName  = "custom-jaeger-secret"
    96  	jaegerDisabledJSON   = "{\"jaeger\": {\"create\": false}}"
    97  	jaegerExternalOSJSON = `
    98  {
    99  	"jaeger": {
   100  		"create": true,
   101  		"spec": {
   102  			"strategy": "production",
   103  			"storage": {
   104  				"type": "opensearch",
   105  				"options": {
   106  					"es": {
   107  						"server-urls": "` + externalOSURL + `",
   108  						"index-prefix": "jaeger",
   109  						"tls": {
   110  							"ca": "/verrazzano/certificates/ca-bundle"
   111  						}
   112  					}
   113  				},
   114  				"secretName": "` + customJaegerSecName + `"
   115  			}
   116  		}
   117  	}
   118  }`
   119  	jaegerExternalOSMutualTLSJSON = `
   120  {
   121  	"jaeger": {
   122  		"create": true,
   123  		"spec": {
   124  			"strategy": "production",
   125  			"storage": {
   126  				"type": "opensearch",
   127  				"options": {
   128  					"es": {
   129  						"server-urls": "` + externalOSURL + `",
   130  						"index-prefix": "jaeger",
   131  						"tls": {
   132  							"ca": "/verrazzano/certificates/ca-bundle",
   133  							"key": "/verrazzano/certificates/os.tls.key",
   134  							"cert": "/verrazzano/certificates/os.tls.cert"
   135  						}
   136  					}
   137  				},
   138  				"secretName": "` + customJaegerSecName + `"
   139  			}
   140  		}
   141  	}
   142  }`
   143  	jaegerStorageTypeOverrideJSON = `
   144  {
   145  	"jaeger": {
   146  		"create": true,
   147  		"spec": {
   148  			"strategy": "production",
   149  			"storage": {
   150  				"type": "inmemory",
   151  				"options": {
   152  					"es": {
   153  						"server-urls": "` + externalOSURL + `",
   154  						"index-prefix": "jaeger",
   155  						"tls": {
   156  							"ca": "/verrazzano/certificates/ca-bundle"
   157  						}
   158  					}
   159  				},
   160  				"secretName": "` + customJaegerSecName + `"
   161  			}
   162  		}
   163  	}
   164  }`
   165  )
   166  
   167  func TestGetJaegerOpenSearchConfig(t *testing.T) {
   168  	type fields struct {
   169  		vz            *vzapi.Verrazzano
   170  		jaegerCreate  bool
   171  		externalOS    bool
   172  		mutualTLS     bool
   173  		OSStorageType bool
   174  	}
   175  	tests := []struct {
   176  		name   string
   177  		fields fields
   178  	}{
   179  		{"Default Jaeger instance", fields{vz: jaegerEnabledCR, jaegerCreate: true, OSStorageType: true}},
   180  		{"Jaeger instance with external OpenSearch", fields{vz: createVZJaegerOverride(jaegerExternalOSJSON),
   181  			jaegerCreate: true, externalOS: true, OSStorageType: true}},
   182  		{"Jaeger instance with external OpenSearch mutual TLS", fields{vz: createVZJaegerOverride(jaegerExternalOSMutualTLSJSON),
   183  			jaegerCreate: true, externalOS: true, mutualTLS: true, OSStorageType: true}},
   184  		{"Jaeger instance creation disabled", fields{vz: createVZJaegerOverride(jaegerDisabledJSON), jaegerCreate: false,
   185  			OSStorageType: true}},
   186  		{"Jaeger instance with different storage type", fields{vz: createVZJaegerOverride(jaegerStorageTypeOverrideJSON),
   187  			jaegerCreate: false, OSStorageType: false}},
   188  		{"Keycloak disabled", fields{vz: keycloakDisabledCR, jaegerCreate: false}},
   189  		{"OpenSearch disabled", fields{vz: openSearchDisabledCR, jaegerCreate: false}},
   190  		{"Jaeger instance with default OpenSearch disabled and external OpenSearch configured",
   191  			fields{vz: createVZWithOSDisabledAndJaegerOverride(jaegerExternalOSJSON),
   192  				jaegerCreate: true, externalOS: true, OSStorageType: true}},
   193  	}
   194  	for _, tt := range tests {
   195  		t.Run(tt.name, func(t *testing.T) {
   196  			scheme := runtime.NewScheme()
   197  			_ = corev1.AddToScheme(scheme)
   198  			_ = k8net.AddToScheme(scheme)
   199  			c := fake.NewClientBuilder().WithScheme(scheme).WithObjects(defaultJaegerSecret, jaegerExternalOSSecret,
   200  				vzTLSSecret, newIngress()).Build()
   201  			rc := &VerrazzanoManagedClusterReconciler{
   202  				Client: c,
   203  				log:    vzlog.DefaultLogger(),
   204  			}
   205  			vzList := &vzapi.VerrazzanoList{}
   206  			vzList.Items = append(vzList.Items, *tt.fields.vz)
   207  			jc, err := rc.getJaegerOpenSearchConfig(vzList)
   208  			assert.NoError(t, err)
   209  			if tt.fields.jaegerCreate {
   210  				if tt.fields.externalOS {
   211  					assert.Equal(t, externalOSURL, jc.URL)
   212  					assert.Equal(t, "jaegeropensearchtlscakey", string(jc.CA))
   213  					if tt.fields.mutualTLS {
   214  						assert.Equal(t, "jaegeropensearchtlskey", string(jc.TLSKey))
   215  						assert.Equal(t, "jaegeropensearchtlscert", string(jc.TLSCert))
   216  					}
   217  					assert.Equal(t, "externalusername", string(jc.username))
   218  					assert.Equal(t, "externalpassword", string(jc.password))
   219  				} else {
   220  					assert.Equal(t, "https://jaeger.unit-test.com:443", jc.URL)
   221  					assert.Equal(t, "username", string(jc.username))
   222  					assert.Equal(t, "password", string(jc.password))
   223  					assert.Equal(t, "adminCAbundle", string(jc.CA))
   224  					assert.Equal(t, "", string(jc.TLSKey))
   225  					assert.Equal(t, "", string(jc.TLSCert))
   226  				}
   227  			} else {
   228  				assert.Equal(t, "", jc.URL)
   229  			}
   230  		})
   231  	}
   232  }
   233  
   234  func createVZJaegerOverride(json string) *vzapi.Verrazzano {
   235  	return &vzapi.Verrazzano{
   236  		Spec: vzapi.VerrazzanoSpec{
   237  			Components: vzapi.ComponentSpec{
   238  				JaegerOperator: &vzapi.JaegerOperatorComponent{
   239  					Enabled: &trueValue,
   240  					InstallOverrides: vzapi.InstallOverrides{
   241  						MonitorChanges: &trueValue,
   242  						ValueOverrides: []vzapi.Overrides{
   243  							{
   244  								Values: &apiextensionsv1.JSON{
   245  									Raw: []byte(json),
   246  								},
   247  							},
   248  						},
   249  					},
   250  				},
   251  			},
   252  		},
   253  	}
   254  }
   255  
   256  func createVZWithOSDisabledAndJaegerOverride(json string) *vzapi.Verrazzano {
   257  	return &vzapi.Verrazzano{
   258  		Spec: vzapi.VerrazzanoSpec{
   259  			Components: vzapi.ComponentSpec{
   260  				OpenSearch: &vzapi.OpenSearchComponent{
   261  					Enabled: &falseValue,
   262  				},
   263  				JaegerOperator: &vzapi.JaegerOperatorComponent{
   264  					Enabled: &trueValue,
   265  					InstallOverrides: vzapi.InstallOverrides{
   266  						MonitorChanges: &trueValue,
   267  						ValueOverrides: []vzapi.Overrides{
   268  							{
   269  								Values: &apiextensionsv1.JSON{
   270  									Raw: []byte(json),
   271  								},
   272  							},
   273  						},
   274  					},
   275  				},
   276  			},
   277  		},
   278  	}
   279  }
   280  
   281  func newIngress() *k8net.Ingress {
   282  	ingress := &k8net.Ingress{}
   283  	ingress.Namespace = constants.VerrazzanoSystemNamespace
   284  	ingress.Name = operatorOSIngress
   285  	rule := k8net.IngressRule{Host: "jaeger.unit-test.com"}
   286  	ingress.Spec.Rules = append(ingress.Spec.Rules, rule)
   287  	return ingress
   288  }