github.com/verrazzano/verrazzano@v1.7.1/tests/e2e/update/overrides/overrides_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 overrides
     5  
     6  import (
     7  	"fmt"
     8  	"strings"
     9  	"time"
    10  
    11  	dump "github.com/verrazzano/verrazzano/tests/e2e/pkg/test/clusterdump"
    12  
    13  	. "github.com/onsi/ginkgo/v2"
    14  	"github.com/onsi/gomega"
    15  	vzapi "github.com/verrazzano/verrazzano/platform-operator/apis/verrazzano/v1alpha1"
    16  	"github.com/verrazzano/verrazzano/platform-operator/constants"
    17  	"github.com/verrazzano/verrazzano/tests/e2e/pkg"
    18  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/test/framework"
    19  	"github.com/verrazzano/verrazzano/tests/e2e/pkg/update"
    20  	corev1 "k8s.io/api/core/v1"
    21  	apiextensionsv1 "k8s.io/apiextensions-apiserver/pkg/apis/apiextensions/v1"
    22  	k8serrors "k8s.io/apimachinery/pkg/api/errors"
    23  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    24  )
    25  
    26  const (
    27  	waitTimeout                        = 5 * time.Minute
    28  	pollingInterval                    = 5 * time.Second
    29  	overrideConfigMapSecretName string = "test-overrides-1"
    30  	dataKey                     string = "values.yaml"
    31  	overrideKey                 string = "override"
    32  	inlineOverrideKey           string = "inlineOverride"
    33  	overrideOldValue            string = "true"
    34  	overrideNewValue            string = "false"
    35  	deploymentName              string = "prometheus-operator-kube-p-operator"
    36  )
    37  
    38  var (
    39  	t = framework.NewTestFramework("overrides")
    40  )
    41  
    42  var inlineData string
    43  var monitorChanges bool
    44  
    45  var failed = false
    46  var _ = t.AfterEach(func() {
    47  	failed = failed || CurrentSpecReport().Failed()
    48  })
    49  
    50  type PrometheusOperatorOverridesModifier struct {
    51  }
    52  
    53  type PrometheusOperatorValuesModifier struct {
    54  }
    55  
    56  type PrometheusOperatorDefaultModifier struct {
    57  }
    58  
    59  func (d PrometheusOperatorDefaultModifier) ModifyCR(cr *vzapi.Verrazzano) {
    60  	if cr.Spec.Components.PrometheusOperator != nil {
    61  		if cr.Spec.Components.PrometheusOperator.ValueOverrides != nil {
    62  			cr.Spec.Components.PrometheusOperator.ValueOverrides = nil
    63  		}
    64  	}
    65  }
    66  
    67  func (o PrometheusOperatorOverridesModifier) ModifyCR(cr *vzapi.Verrazzano) {
    68  	if cr.Spec.Components.PrometheusOperator == nil {
    69  		cr.Spec.Components.PrometheusOperator = &vzapi.PrometheusOperatorComponent{}
    70  	}
    71  	var trueVal = true
    72  	overrides := []vzapi.Overrides{
    73  		{
    74  			ConfigMapRef: &corev1.ConfigMapKeySelector{
    75  				LocalObjectReference: corev1.LocalObjectReference{
    76  					Name: overrideConfigMapSecretName,
    77  				},
    78  				Key:      dataKey,
    79  				Optional: &trueVal,
    80  			},
    81  		},
    82  		{
    83  			SecretRef: &corev1.SecretKeySelector{
    84  				LocalObjectReference: corev1.LocalObjectReference{
    85  					Name: overrideConfigMapSecretName,
    86  				},
    87  				Key:      dataKey,
    88  				Optional: &trueVal,
    89  			},
    90  		},
    91  		{
    92  			Values: &apiextensionsv1.JSON{
    93  				Raw: []byte(inlineData),
    94  			},
    95  		},
    96  	}
    97  	cr.Spec.Components.PrometheusOperator.Enabled = &trueVal
    98  	cr.Spec.Components.PrometheusOperator.MonitorChanges = &monitorChanges
    99  	cr.Spec.Components.PrometheusOperator.ValueOverrides = overrides
   100  }
   101  
   102  func (o PrometheusOperatorValuesModifier) ModifyCR(cr *vzapi.Verrazzano) {
   103  	var trueVal = true
   104  	overrides := []vzapi.Overrides{
   105  		{
   106  			ConfigMapRef: &corev1.ConfigMapKeySelector{
   107  				LocalObjectReference: corev1.LocalObjectReference{
   108  					Name: overrideConfigMapSecretName,
   109  				},
   110  				Key:      dataKey,
   111  				Optional: &trueVal,
   112  			},
   113  		},
   114  		{
   115  			SecretRef: &corev1.SecretKeySelector{
   116  				LocalObjectReference: corev1.LocalObjectReference{
   117  					Name: overrideConfigMapSecretName,
   118  				},
   119  				Key:      dataKey,
   120  				Optional: &trueVal,
   121  			},
   122  		},
   123  	}
   124  	cr.Spec.Components.PrometheusOperator.Enabled = &trueVal
   125  	cr.Spec.Components.PrometheusOperator.MonitorChanges = &monitorChanges
   126  	cr.Spec.Components.PrometheusOperator.ValueOverrides = overrides
   127  }
   128  
   129  var beforeSuite = t.BeforeSuiteFunc(func() {
   130  	m := PrometheusOperatorOverridesModifier{}
   131  	inlineData = oldInlineData
   132  	monitorChanges = true
   133  	update.UpdateCRWithRetries(m, pollingInterval, waitTimeout)
   134  	_ = update.GetCR()
   135  })
   136  
   137  var _ = BeforeSuite(beforeSuite)
   138  
   139  var afterSuite = t.AfterSuiteFunc(func() {
   140  	m := PrometheusOperatorDefaultModifier{}
   141  	update.UpdateCRWithRetries(m, pollingInterval, waitTimeout)
   142  	_ = update.GetCR()
   143  	if failed {
   144  		dump.ExecuteBugReport()
   145  	}
   146  })
   147  
   148  var _ = AfterSuite(afterSuite)
   149  
   150  var _ = t.Describe("Post Install Overrides", func() {
   151  
   152  	t.Context("Test overrides creation", func() {
   153  		// Create the overrides resources listed in Verrazzano and verify
   154  		// that the values 	have been applied to promtheus-operator
   155  		t.Context("Create Overrides", func() {
   156  			t.It("Create ConfigMap", func() {
   157  				testConfigMap.Data[dataKey] = oldCMData
   158  				gomega.Eventually(func() error {
   159  					return pkg.CreateConfigMap(&testConfigMap)
   160  				}, waitTimeout, pollingInterval).Should(gomega.BeNil())
   161  			})
   162  
   163  			t.It("Create Secret", func() {
   164  				testSecret.StringData[dataKey] = oldSecretData
   165  				gomega.Eventually(func() error {
   166  					return pkg.CreateSecret(&testSecret)
   167  				}, waitTimeout, pollingInterval).Should(gomega.BeNil())
   168  			})
   169  		})
   170  
   171  		t.It("Verify override values are applied", func() {
   172  			gomega.Eventually(func() bool {
   173  				return checkValues(overrideOldValue)
   174  			}, waitTimeout, pollingInterval).Should(gomega.BeTrue())
   175  		})
   176  
   177  		// Verify that re-install succeeds
   178  		t.It("Verify Verrazzano re-install is successful", func() {
   179  			gomega.Eventually(func() error {
   180  				return vzReady()
   181  			}, waitTimeout, pollingInterval).Should(gomega.BeNil(), "Expected to get Verrazzano CR with Ready state")
   182  		})
   183  	})
   184  
   185  	/* Disable these tests for now as part of the modules work, we may want to remove/deprecate this as it is
   186  	   undocumented and may not make sense.  We will review this as part of the Modules cleanup effort.
   187  
   188  	t.Context("Test no update with monitorChanges false", func() {
   189  		// Update the overrides resources listed in Verrazzano and set monitorChanges to false and verify
   190  		// that the new values have not been applied to Prometheus Operator
   191  		t.Context("Update Overrides", func() {
   192  			t.It("Update Inline Data", func() {
   193  				inlineData = newInlineData
   194  				monitorChanges = false
   195  				m := PrometheusOperatorOverridesModifier{}
   196  				update.UpdateCRWithRetries(m, pollingInterval, waitTimeout)
   197  				_ = update.GetCR()
   198  			})
   199  
   200  			t.It("Update ConfigMap", func() {
   201  				testConfigMap.Data[dataKey] = newCMData
   202  				gomega.Eventually(func() error {
   203  					return pkg.UpdateConfigMap(&testConfigMap)
   204  				}, waitTimeout, pollingInterval).Should(gomega.BeNil())
   205  			})
   206  
   207  			t.It("Update Secret", func() {
   208  				testSecret.StringData[dataKey] = newSecretData
   209  				gomega.Eventually(func() error {
   210  					return pkg.UpdateSecret(&testSecret)
   211  				}, waitTimeout, pollingInterval).Should(gomega.BeNil())
   212  			})
   213  		})
   214  
   215  		t.It("Verify override values are applied", func() {
   216  			gomega.Eventually(func() bool {
   217  				return checkValues(overrideOldValue)
   218  			}, waitTimeout, pollingInterval).Should(gomega.BeTrue())
   219  		})
   220  
   221  		// Verify that re-install succeeds
   222  		t.It("Verify Verrazzano re-install is successful", func() {
   223  			gomega.Eventually(func() error {
   224  				return vzReady()
   225  			}, waitTimeout, pollingInterval).Should(gomega.BeNil(), "Expected to get Verrazzano CR with Ready state")
   226  		})
   227  	})
   228  
   229  	t.Context("Test overrides update", func() {
   230  		// Change monitorChanges to true in Verrazzano and verify
   231  		// that the new values have been applied to promtheus-operator
   232  		t.Context("Update Overrides", func() {
   233  			t.It("Update Inline Data", func() {
   234  				inlineData = newInlineData
   235  				monitorChanges = true
   236  				m := PrometheusOperatorOverridesModifier{}
   237  				update.UpdateCRWithRetries(m, pollingInterval, waitTimeout)
   238  				_ = update.GetCR()
   239  			})
   240  		})
   241  
   242  		t.It("Verify override values are applied", func() {
   243  			gomega.Eventually(func() bool {
   244  				return checkValues(overrideNewValue)
   245  			}, waitTimeout, pollingInterval).Should(gomega.BeTrue())
   246  		})
   247  
   248  		// Verify that re-install succeeds
   249  		t.It("Verify Verrazzano re-install is successful", func() {
   250  			gomega.Eventually(func() error {
   251  				return vzReady()
   252  			}, waitTimeout, pollingInterval).Should(gomega.BeNil(), "Expected to get Verrazzano CR with Ready state")
   253  		})
   254  	})
   255  	*/
   256  
   257  	t.Context("Test overrides deletion", func() {
   258  		// Delete the resources and verify that the deleted
   259  		// values are now unapplied
   260  		t.It("Delete Resources", func() {
   261  			deleteOverrides()
   262  		})
   263  
   264  		t.It("Verify deleted values are removed", func() {
   265  			gomega.Eventually(func() bool {
   266  				pods, err := pkg.GetPodsFromSelector(nil, constants.VerrazzanoMonitoringNamespace)
   267  				if err != nil {
   268  					return false
   269  				}
   270  				for _, pod := range pods {
   271  					if strings.Contains(pod.Name, deploymentName) {
   272  						_, foundLabel := pod.Labels[overrideKey]
   273  						_, foundAnnotation := pod.Annotations[overrideKey]
   274  						_, foundInlineAnnotation := pod.Annotations[inlineOverrideKey]
   275  						if !foundLabel && !foundAnnotation && !foundInlineAnnotation {
   276  							return true
   277  						}
   278  					}
   279  				}
   280  				return false
   281  			}, waitTimeout, pollingInterval).Should(gomega.BeTrue())
   282  		})
   283  
   284  		// Verify that re-install succeeds
   285  		t.It("Verify Verrazzano re-install is successful", func() {
   286  			gomega.Eventually(func() error {
   287  				return vzReady()
   288  			}, waitTimeout, pollingInterval).Should(gomega.BeNil(), "Expected to get Verrazzano CR with Ready state")
   289  		})
   290  	})
   291  })
   292  
   293  func deleteOverrides() {
   294  	err0 := pkg.DeleteConfigMap(constants.DefaultNamespace, overrideConfigMapSecretName)
   295  	if err0 != nil && !k8serrors.IsNotFound(err0) {
   296  		AbortSuite("Failed to delete ConfigMap")
   297  	}
   298  
   299  	err1 := pkg.DeleteSecret(constants.DefaultNamespace, overrideConfigMapSecretName)
   300  	if err1 != nil && !k8serrors.IsNotFound(err1) {
   301  		AbortSuite("Failed to delete Secret")
   302  	}
   303  	m := PrometheusOperatorValuesModifier{}
   304  	update.UpdateCRWithRetries(m, pollingInterval, waitTimeout)
   305  	_ = update.GetCR()
   306  }
   307  
   308  func vzReady() error {
   309  	cr, err := pkg.GetVerrazzano()
   310  	if err != nil {
   311  		return err
   312  	}
   313  	if cr.Status.State != vzapi.VzStateReady {
   314  		return fmt.Errorf("CR in state %s, not Ready yet", cr.Status.State)
   315  	}
   316  	return nil
   317  }
   318  
   319  func checkValues(overrideValue string) bool {
   320  	labelMatch := map[string]string{overrideKey: overrideValue}
   321  	pods, err := pkg.GetPodsFromSelector(&metav1.LabelSelector{
   322  		MatchLabels: labelMatch,
   323  	}, constants.VerrazzanoMonitoringNamespace)
   324  	if err != nil {
   325  		AbortSuite(fmt.Sprintf("Label override not found for the Prometheus Operator pod in namespace %s: %v", constants.VerrazzanoMonitoringNamespace, err))
   326  	}
   327  	foundAnnotation := false
   328  	for _, pod := range pods {
   329  		if val, ok := pod.Annotations[overrideKey]; ok && val == overrideValue {
   330  			foundAnnotation = true
   331  		}
   332  	}
   333  	foundInlineAnnotation := false
   334  	for _, pod := range pods {
   335  		if val, ok := pod.Annotations[inlineOverrideKey]; ok && val == overrideValue {
   336  			foundInlineAnnotation = true
   337  		}
   338  	}
   339  	return len(pods) == 1 && foundAnnotation && foundInlineAnnotation
   340  }