From 0399ef66255dce5c0eafe111443c7cd2f0a22569 Mon Sep 17 00:00:00 2001 From: vivekr-splunk <94569031+vivekr-splunk@users.noreply.github.com> Date: Wed, 9 Aug 2023 11:30:46 -0700 Subject: [PATCH] added upgradepath to clustermanager Signed-off-by: vivekr-splunk <94569031+vivekr-splunk@users.noreply.github.com> --- pkg/splunk/enterprise/clustermanager.go | 5 +- pkg/splunk/enterprise/upgrade.go | 486 ++++++++++++------------ pkg/splunk/enterprise/upgrade_test.go | 300 +++++++++++++++ 3 files changed, 546 insertions(+), 245 deletions(-) create mode 100644 pkg/splunk/enterprise/upgrade_test.go diff --git a/pkg/splunk/enterprise/clustermanager.go b/pkg/splunk/enterprise/clustermanager.go index 68590e59c..05866fd0e 100644 --- a/pkg/splunk/enterprise/clustermanager.go +++ b/pkg/splunk/enterprise/clustermanager.go @@ -180,9 +180,10 @@ func ApplyClusterManager(ctx context.Context, client splcommon.ControllerClient, return result, err } + cr.Kind = "ClusterManager" // check if the ClusterManager is ready for version upgrade, if required - continueReconcile, err := isClusterManagerReadyForUpgrade(ctx, client, cr) - if err != nil || !continueReconcile { + continueReconcile, err := UpgradePathValidation(ctx, client, cr, cr.Spec.CommonSplunkSpec, nil) + if err != nil || !continueReconcile { return result, err } diff --git a/pkg/splunk/enterprise/upgrade.go b/pkg/splunk/enterprise/upgrade.go index f4fed0f3a..da4ec3ea2 100644 --- a/pkg/splunk/enterprise/upgrade.go +++ b/pkg/splunk/enterprise/upgrade.go @@ -13,304 +13,304 @@ import ( "sigs.k8s.io/controller-runtime/pkg/log" ) - -func upgradePathValidation(ctx context.Context, c splcommon.ControllerClient, cr splcommon.MetaObject, spec enterpriseApi.CommonSplunkSpec, mgr *indexerClusterPodManager) (bool, error) { +func UpgradePathValidation(ctx context.Context, c splcommon.ControllerClient, cr splcommon.MetaObject, spec enterpriseApi.CommonSplunkSpec, mgr *indexerClusterPodManager) (bool, error) { reqLogger := log.FromContext(ctx) scopedLog := reqLogger.WithName("isClusterManagerReadyForUpgrade").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace()) eventPublisher, _ := newK8EventPublisher(c, cr) + kind := cr.GroupVersionKind().Kind + scopedLog.Info("kind is set to ", "kind", kind) goto Standalone - Standalone: - if cr.GroupVersionKind().Kind == "Standalone" { - return true, nil - } else { - goto LicenseManager - } - LicenseManager: - if cr.GroupVersionKind().Kind == "LicenseManager" { - return true , nil - } else { - licenseManagerRef := spec.LicenseManagerRef - if licenseManagerRef.Name == "" { - goto ClusterManager - } - - namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: licenseManagerRef.Name} - licenseManager := &enterpriseApi.LicenseManager{} - // get the license manager referred in CR - err := c.Get(ctx, namespacedName, licenseManager) - if err != nil { - if k8serrors.IsNotFound(err) { - goto ClusterManager - } - return false, err - } - - lmImage, err := getCurrentImage(ctx, c, licenseManager, SplunkLicenseManager) - if err != nil { - eventPublisher.Warning(ctx, "isClusterManagerReadyForUpgrade", fmt.Sprintf("Could not get the License Manager Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get licenseManager current image") - return false, err - } - if licenseManager.Status.Phase != enterpriseApi.PhaseReady || lmImage != spec.Image { - return false, err - } +Standalone: + if cr.GroupVersionKind().Kind == "Standalone" { + return true, nil + } else { + goto LicenseManager + } +LicenseManager: + if cr.GroupVersionKind().Kind == "LicenseManager" { + return true, nil + } else { + licenseManagerRef := spec.LicenseManagerRef + if licenseManagerRef.Name == "" { goto ClusterManager } - ClusterManager: - if cr.GroupVersionKind().Kind == "ClusterManager" { - licenseManagerRef := spec.LicenseManagerRef - if licenseManagerRef.Name == "" { - return true, nil - } - namespacedName := types.NamespacedName{ - Namespace: cr.GetNamespace(), - Name: GetSplunkStatefulsetName(SplunkClusterManager, cr.GetName()), - } - - // check if the stateful set is created at this instance - statefulSet := &appsv1.StatefulSet{} - err := c.Get(ctx, namespacedName, statefulSet) - if err != nil && k8serrors.IsNotFound(err) { - return true, nil - } - return false, nil - } else { - // check if a LicenseManager is attached to the instance - clusterManagerRef := spec.ClusterManagerRef - if clusterManagerRef.Name == "" { - goto MonitoringConsole + namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: licenseManagerRef.Name} + licenseManager := &enterpriseApi.LicenseManager{} + // get the license manager referred in CR + err := c.Get(ctx, namespacedName, licenseManager) + if err != nil { + if k8serrors.IsNotFound(err) { + goto ClusterManager } + return false, err + } - namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: clusterManagerRef.Name} - clusterManager := &enterpriseApi.ClusterManager{} - - // get the cluster manager referred in monitoring console - err := c.Get(ctx, namespacedName, clusterManager) - if err != nil { - eventPublisher.Warning(ctx, "isMonitoringConsoleReadyForUpgrade", fmt.Sprintf("Could not find the Cluster Manager. Reason %v", err)) - scopedLog.Error(err, "Unable to get clusterManager") - goto MonitoringConsole - } + lmImage, err := getCurrentImage(ctx, c, licenseManager, SplunkLicenseManager) + if err != nil { + eventPublisher.Warning(ctx, "isClusterManagerReadyForUpgrade", fmt.Sprintf("Could not get the License Manager Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get licenseManager current image") + return false, err + } + if licenseManager.Status.Phase != enterpriseApi.PhaseReady || lmImage != spec.Image { + return false, err + } + goto ClusterManager + } +ClusterManager: + if cr.GroupVersionKind().Kind == "ClusterManager" { - cmImage, err := getCurrentImage(ctx, c, cr, SplunkClusterManager) - if err != nil { - eventPublisher.Warning(ctx, "isMonitoringConsoleReadyForUpgrade", fmt.Sprintf("Could not get the Cluster Manager Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get clusterManager current image") - return false, err - } + licenseManagerRef := spec.LicenseManagerRef + if licenseManagerRef.Name == "" { + return true, nil + } + namespacedName := types.NamespacedName{ + Namespace: cr.GetNamespace(), + Name: GetSplunkStatefulsetName(SplunkClusterManager, cr.GetName()), + } - // check if an image upgrade is happening and whether CM has finished updating yet, return false to stop - // further reconcile operations on MC until CM is ready - if clusterManager.Status.Phase != enterpriseApi.PhaseReady || cmImage != spec.Image { - return false, nil - } + // check if the stateful set is created at this instance + statefulSet := &appsv1.StatefulSet{} + err := c.Get(ctx, namespacedName, statefulSet) + if err != nil && k8serrors.IsNotFound(err) { + return true, nil + } + return false, nil + } else { + // check if a LicenseManager is attached to the instance + clusterManagerRef := spec.ClusterManagerRef + if clusterManagerRef.Name == "" { goto MonitoringConsole } - MonitoringConsole: - if cr.GroupVersionKind().Kind == "MonitoringConsole" { + namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: clusterManagerRef.Name} + clusterManager := &enterpriseApi.ClusterManager{} - namespacedName := types.NamespacedName{ - Namespace: cr.GetNamespace(), - Name: GetSplunkStatefulsetName(SplunkMonitoringConsole, cr.GetName()), - } - - // check if the stateful set is created at this instance - statefulSet := &appsv1.StatefulSet{} - err := c.Get(ctx, namespacedName, statefulSet) - if err != nil && k8serrors.IsNotFound(err) { - return true, nil - } + // get the cluster manager referred in monitoring console + err := c.Get(ctx, namespacedName, clusterManager) + if err != nil { + eventPublisher.Warning(ctx, "isMonitoringConsoleReadyForUpgrade", fmt.Sprintf("Could not find the Cluster Manager. Reason %v", err)) + scopedLog.Error(err, "Unable to get clusterManager") + goto MonitoringConsole + } - mcImage, err := getCurrentImage(ctx, c, cr, SplunkMonitoringConsole) - if err != nil { - eventPublisher.Warning(ctx, "isMonitoringConsolerReadyForUpgrade", fmt.Sprintf("Could not get the Monitoring Console Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get monitoring console current image") - return false, err - } + cmImage, err := getCurrentImage(ctx, c, cr, SplunkClusterManager) + if err != nil { + eventPublisher.Warning(ctx, "isMonitoringConsoleReadyForUpgrade", fmt.Sprintf("Could not get the Cluster Manager Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get clusterManager current image") + return false, err + } - // check if an image upgrade is happening and whether CM has finished updating yet, return false to stop - // further reconcile operations on MC until CM is ready - if spec.Image != mcImage{ - return true, nil - } + // check if an image upgrade is happening and whether CM has finished updating yet, return false to stop + // further reconcile operations on MC until CM is ready + if clusterManager.Status.Phase != enterpriseApi.PhaseReady || cmImage != spec.Image { + return false, nil + } + goto MonitoringConsole + } +MonitoringConsole: + if cr.GroupVersionKind().Kind == "MonitoringConsole" { + + namespacedName := types.NamespacedName{ + Namespace: cr.GetNamespace(), + Name: GetSplunkStatefulsetName(SplunkMonitoringConsole, cr.GetName()), + } + // check if the stateful set is created at this instance + statefulSet := &appsv1.StatefulSet{} + err := c.Get(ctx, namespacedName, statefulSet) + if err != nil && k8serrors.IsNotFound(err) { return true, nil - } else { + } - // check if a MonitoringConsole is attached to the instance - monitoringConsoleRef := spec.MonitoringConsoleRef - if monitoringConsoleRef.Name == "" { - goto SearchHeadCluster - } + mcImage, err := getCurrentImage(ctx, c, cr, SplunkMonitoringConsole) + if err != nil { + eventPublisher.Warning(ctx, "isMonitoringConsolerReadyForUpgrade", fmt.Sprintf("Could not get the Monitoring Console Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get monitoring console current image") + return false, err + } - namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: monitoringConsoleRef.Name} - monitoringConsole := &enterpriseApi.MonitoringConsole{} + // check if an image upgrade is happening and whether CM has finished updating yet, return false to stop + // further reconcile operations on MC until CM is ready + if spec.Image != mcImage { + return false, nil + } - // get the monitoring console referred in search head cluster - err := c.Get(ctx, namespacedName, monitoringConsole) - if err != nil { - if k8serrors.IsNotFound(err) { - goto SearchHeadCluster - } - eventPublisher.Warning(ctx, "isSearchHeadReadyForUpgrade", fmt.Sprintf("Could not find the Monitoring Console. Reason %v", err)) - scopedLog.Error(err, "Unable to get Monitoring Console") - return false, err - } + return true, nil + } else { - mcImage, err := getCurrentImage(ctx, c, monitoringConsole, SplunkMonitoringConsole) - if err != nil { - eventPublisher.Warning(ctx, "isSearchHeadReadyForUpgrade", fmt.Sprintf("Could not get the Monitoring Console Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get Monitoring Console current image") - return false, err - } + // check if a MonitoringConsole is attached to the instance + monitoringConsoleRef := spec.MonitoringConsoleRef + if monitoringConsoleRef.Name == "" { + goto SearchHeadCluster + } - // check if an image upgrade is happening and whether the SearchHeadCluster is ready for the upgrade - if monitoringConsole.Status.Phase != enterpriseApi.PhaseReady || mcImage != spec.Image { - return false, nil + namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: monitoringConsoleRef.Name} + monitoringConsole := &enterpriseApi.MonitoringConsole{} + + // get the monitoring console referred in search head cluster + err := c.Get(ctx, namespacedName, monitoringConsole) + if err != nil { + if k8serrors.IsNotFound(err) { + goto SearchHeadCluster } + eventPublisher.Warning(ctx, "isSearchHeadReadyForUpgrade", fmt.Sprintf("Could not find the Monitoring Console. Reason %v", err)) + scopedLog.Error(err, "Unable to get Monitoring Console") + return false, err + } - goto SearchHeadCluster + mcImage, err := getCurrentImage(ctx, c, monitoringConsole, SplunkMonitoringConsole) + if err != nil { + eventPublisher.Warning(ctx, "isSearchHeadReadyForUpgrade", fmt.Sprintf("Could not get the Monitoring Console Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get Monitoring Console current image") + return false, err } - SearchHeadCluster: - if cr.GroupVersionKind().Kind == "SearchHeadCluster" { - namespacedName := types.NamespacedName{ - Namespace: cr.GetNamespace(), - Name: GetSplunkStatefulsetName(SplunkSearchHead, cr.GetName()), - } + // check if an image upgrade is happening and whether the SearchHeadCluster is ready for the upgrade + if monitoringConsole.Status.Phase != enterpriseApi.PhaseReady || mcImage != spec.Image { + return false, nil + } - // check if the stateful set is created at this instance - statefulSet := &appsv1.StatefulSet{} - err := c.Get(ctx, namespacedName, statefulSet) - if err != nil && k8serrors.IsNotFound(err) { - return true, nil - } + goto SearchHeadCluster + } +SearchHeadCluster: + if cr.GroupVersionKind().Kind == "SearchHeadCluster" { - shcImage, err := getCurrentImage(ctx, c, cr, SplunkSearchHead) - if err != nil { - eventPublisher.Warning(ctx, "isSearchHeadReadyForUpgrade", fmt.Sprintf("Could not get the Search Head Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get Search Head current image") - return false, err - } + namespacedName := types.NamespacedName{ + Namespace: cr.GetNamespace(), + Name: GetSplunkStatefulsetName(SplunkSearchHead, cr.GetName()), + } - // check if an image upgrade is happening and whether the SearchHeadCluster is ready for the upgrade - if spec.Image != shcImage { - return true, nil - } + // check if the stateful set is created at this instance + statefulSet := &appsv1.StatefulSet{} + err := c.Get(ctx, namespacedName, statefulSet) + if err != nil && k8serrors.IsNotFound(err) { return true, nil - } else { + } - // get the clusterManagerRef attached to the instance - clusterManagerRef := spec.ClusterManagerRef + shcImage, err := getCurrentImage(ctx, c, cr, SplunkSearchHead) + if err != nil { + eventPublisher.Warning(ctx, "isSearchHeadReadyForUpgrade", fmt.Sprintf("Could not get the Search Head Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get Search Head current image") + return false, err + } - // check if a search head cluster exists with the same ClusterManager instance attached - searchHeadClusterInstance := enterpriseApi.SearchHeadCluster{} - opts := []rclient.ListOption{ - rclient.InNamespace(cr.GetNamespace()), - } - searchHeadList, err := getSearchHeadClusterList(ctx, c, cr, opts) - if err != nil { - if err.Error() == "NotFound" { - goto IndexerCluster - } - return false, err - } - if len(searchHeadList.Items) == 0 { - goto IndexerCluster - } + // check if an image upgrade is happening and whether the SearchHeadCluster is ready for the upgrade + if spec.Image != shcImage { + return false, nil + } + return true, nil + } else { - // check if instance has the required ClusterManagerRef - for _, shc := range searchHeadList.Items { - if shc.Spec.ClusterManagerRef.Name == clusterManagerRef.Name { - searchHeadClusterInstance = shc - break - } - } - if len(searchHeadClusterInstance.GetName()) == 0 { + // get the clusterManagerRef attached to the instance + clusterManagerRef := spec.ClusterManagerRef + + // check if a search head cluster exists with the same ClusterManager instance attached + searchHeadClusterInstance := enterpriseApi.SearchHeadCluster{} + opts := []rclient.ListOption{ + rclient.InNamespace(cr.GetNamespace()), + } + searchHeadList, err := getSearchHeadClusterList(ctx, c, cr, opts) + if err != nil { + if err.Error() == "NotFound" { goto IndexerCluster } + return false, err + } + if len(searchHeadList.Items) == 0 { + goto IndexerCluster + } - shcImage, err := getCurrentImage(ctx, c, &searchHeadClusterInstance, SplunkSearchHead) - if err != nil { - eventPublisher.Warning(ctx, "isIndexerClusterReadyForUpgrade", fmt.Sprintf("Could not get the Search Head Cluster Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get SearchHeadCluster current image") - return false, err + // check if instance has the required ClusterManagerRef + for _, shc := range searchHeadList.Items { + if shc.Spec.ClusterManagerRef.Name == clusterManagerRef.Name { + searchHeadClusterInstance = shc + break } + } + if len(searchHeadClusterInstance.GetName()) == 0 { + goto IndexerCluster + } - idxImage, err := getCurrentImage(ctx, c, cr, SplunkIndexer) - if err != nil { - eventPublisher.Warning(ctx, "isIndexerClusterReadyForUpgrade", fmt.Sprintf("Could not get the Indexer Cluster Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get IndexerCluster current image") - return false, err - } + shcImage, err := getCurrentImage(ctx, c, &searchHeadClusterInstance, SplunkSearchHead) + if err != nil { + eventPublisher.Warning(ctx, "isIndexerClusterReadyForUpgrade", fmt.Sprintf("Could not get the Search Head Cluster Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get SearchHeadCluster current image") + return false, err + } - // check if an image upgrade is happening and whether SHC has finished updating yet, return false to stop - // further reconcile operations on IDX until SHC is ready - if (spec.Image != idxImage) && (searchHeadClusterInstance.Status.Phase != enterpriseApi.PhaseReady || shcImage != spec.Image) { - return false, nil - } - goto IndexerCluster + idxImage, err := getCurrentImage(ctx, c, cr, SplunkIndexer) + if err != nil { + eventPublisher.Warning(ctx, "isIndexerClusterReadyForUpgrade", fmt.Sprintf("Could not get the Indexer Cluster Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get IndexerCluster current image") + return false, err } - IndexerCluster: - if cr.GroupVersionKind().Kind == "IndexerCluster" { - if mgr.c == nil { - mgr.c = c - } + // check if an image upgrade is happening and whether SHC has finished updating yet, return false to stop + // further reconcile operations on IDX until SHC is ready + if (spec.Image != idxImage) && (searchHeadClusterInstance.Status.Phase != enterpriseApi.PhaseReady || shcImage != spec.Image) { + return false, nil + } + goto IndexerCluster + } +IndexerCluster: + if cr.GroupVersionKind().Kind == "IndexerCluster" { + + if mgr.c == nil { + mgr.c = c + } - cm := mgr.getClusterManagerClient(ctx) - clusterInfo, err := cm.GetClusterInfo(false) + cm := mgr.getClusterManagerClient(ctx) + clusterInfo, err := cm.GetClusterInfo(false) + if err != nil { + return false, fmt.Errorf("could not get cluster info from cluster manager") + } + if clusterInfo.MultiSite == "true" { + opts := []rclient.ListOption{ + rclient.InNamespace(cr.GetNamespace()), + } + indexerList, err := getIndexerClusterList(ctx, c, cr, opts) if err != nil { - return false, fmt.Errorf("could not get cluster info from cluster manager") + return false, err } - if clusterInfo.MultiSite == "true" { - opts := []rclient.ListOption{ - rclient.InNamespace(cr.GetNamespace()), - } - indexerList, err := getIndexerClusterList(ctx, c, cr, opts) - if err != nil { - return false, err - } - sortedList, err := getIndexerClusterSortedSiteList(ctx, c, spec.ClusterManagerRef, indexerList) - - preIdx := enterpriseApi.IndexerCluster{} + sortedList, err := getIndexerClusterSortedSiteList(ctx, c, spec.ClusterManagerRef, indexerList) - for i, v := range sortedList.Items { - if &v == cr { - if i > 0 { - preIdx = sortedList.Items[i-1] - } - break + preIdx := enterpriseApi.IndexerCluster{} + for i, v := range sortedList.Items { + if &v == cr { + if i > 0 { + preIdx = sortedList.Items[i-1] } + break + } - if len(preIdx.Name) != 0 { - image, _ := getCurrentImage(ctx, c, &preIdx, SplunkIndexer) - if preIdx.Status.Phase != enterpriseApi.PhaseReady || image != spec.Image { - return false, nil - } + } + if len(preIdx.Name) != 0 { + image, _ := getCurrentImage(ctx, c, &preIdx, SplunkIndexer) + if preIdx.Status.Phase != enterpriseApi.PhaseReady || image != spec.Image { + return false, nil } - } - idxImage, err := getCurrentImage(ctx, c, cr, SplunkIndexer) - if err != nil { - eventPublisher.Warning(ctx, "isIndexerClusterReadyForUpgrade", fmt.Sprintf("Could not get the Indexer Cluster Image. Reason %v", err)) - scopedLog.Error(err, "Unable to get IndexerCluster current image") - return false, err - } + } - if (spec.Image != idxImage) { - return false, nil - } - return true, nil - } else { - goto EndLabel + idxImage, err := getCurrentImage(ctx, c, cr, SplunkIndexer) + if err != nil { + eventPublisher.Warning(ctx, "isIndexerClusterReadyForUpgrade", fmt.Sprintf("Could not get the Indexer Cluster Image. Reason %v", err)) + scopedLog.Error(err, "Unable to get IndexerCluster current image") + return false, err + } + + if spec.Image != idxImage { + return false, nil } - EndLabel: return true, nil + } else { + goto EndLabel + } +EndLabel: + return true, nil -} \ No newline at end of file +} diff --git a/pkg/splunk/enterprise/upgrade_test.go b/pkg/splunk/enterprise/upgrade_test.go new file mode 100644 index 000000000..55928d6bb --- /dev/null +++ b/pkg/splunk/enterprise/upgrade_test.go @@ -0,0 +1,300 @@ +package enterprise + +import ( + "context" + "testing" + k8serrors "k8s.io/apimachinery/pkg/api/errors" + enterpriseApi "github.com/splunk/splunk-operator/api/v4" + corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" + utilruntime "k8s.io/apimachinery/pkg/util/runtime" + clientgoscheme "k8s.io/client-go/kubernetes/scheme" + "sigs.k8s.io/controller-runtime/pkg/client/fake" +) + +func TestUpgradePathValidation(t *testing.T) { + + builder := fake.NewClientBuilder() + client := builder.Build() + utilruntime.Must(enterpriseApi.AddToScheme(clientgoscheme.Scheme)) + + ctx := context.TODO() + stdln := enterpriseApi.Standalone{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + Spec: enterpriseApi.StandaloneSpec{ + CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{ + Spec: enterpriseApi.Spec{ + ImagePullPolicy: "Always", + Image: "splunk/splunk:old", + }, + Volumes: []corev1.Volume{}, + }, + }, + } + + err := client.Create(ctx, &stdln) + if err != nil { + t.Errorf("create should not have returned error; err=%v", err) + } + _, err = ApplyStandalone(ctx, client, &stdln) + if err != nil { + t.Errorf("ApplyStandalone should not have returned error; err=%v", err) + } + + // cluster manager + + lm := enterpriseApi.LicenseManager{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + Spec: enterpriseApi.LicenseManagerSpec{ + CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{ + Spec: enterpriseApi.Spec{ + ImagePullPolicy: "Always", + Image: "splunk/splunk:old", + }, + Volumes: []corev1.Volume{}, + }, + }, + } + + cm := enterpriseApi.ClusterManager{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + Spec: enterpriseApi.ClusterManagerSpec{ + CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{ + Spec: enterpriseApi.Spec{ + ImagePullPolicy: "Always", + Image: "splunk/splunk:old", + }, + Volumes: []corev1.Volume{}, + LicenseManagerRef: corev1.ObjectReference{ + Name: "test", + }, + }, + }, + } + + mc := enterpriseApi.MonitoringConsole{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + Spec: enterpriseApi.MonitoringConsoleSpec{ + CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{ + Spec: enterpriseApi.Spec{ + ImagePullPolicy: "Always", + Image: "splunk/splunk:old", + }, + Volumes: []corev1.Volume{}, + LicenseManagerRef: corev1.ObjectReference{ + Name: "test", + }, + ClusterManagerRef: corev1.ObjectReference{ + Name: "test", + }, + }, + }, + } + + idx := enterpriseApi.IndexerCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + Spec: enterpriseApi.IndexerClusterSpec{ + CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{ + Spec: enterpriseApi.Spec{ + ImagePullPolicy: "Always", + Image: "splunk/splunk:old", + }, + Volumes: []corev1.Volume{}, + LicenseManagerRef: corev1.ObjectReference{ + Name: "test", + }, + ClusterManagerRef: corev1.ObjectReference{ + Name: "test", + }, + }, + }, + } + + shc := enterpriseApi.SearchHeadCluster{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test", + Namespace: "test", + }, + Spec: enterpriseApi.SearchHeadClusterSpec{ + CommonSplunkSpec: enterpriseApi.CommonSplunkSpec{ + Spec: enterpriseApi.Spec{ + ImagePullPolicy: "Always", + Image: "splunk/splunk:old", + }, + Volumes: []corev1.Volume{}, + LicenseManagerRef: corev1.ObjectReference{ + Name: "test", + }, + ClusterManagerRef: corev1.ObjectReference{ + Name: "test", + }, + }, + }, + } + + err = client.Create(ctx, &lm) + if err != nil { + t.Errorf("create should not have returned error; err=%v", err) + } + err = client.Create(ctx, &cm) + if err != nil { + t.Errorf("create should not have returned error; err=%v", err) + } + err = client.Create(ctx, &mc) + if err != nil { + t.Errorf("create should not have returned error; err=%v", err) + } + err = client.Create(ctx, &idx) + if err != nil { + t.Errorf("create should not have returned error; err=%v", err) + } + err = client.Create(ctx, &shc) + if err != nil { + t.Errorf("create should not have returned error; err=%v", err) + } + + _, err = ApplyClusterManager(ctx, client, &cm) + // license manager statefulset is not created + if err != nil && !k8serrors.IsNotFound(err) { + t.Errorf("applyClusterManager should not have returned error; err=%v", err) + } + + // create license manager statefulset + _, err = ApplyLicenseManager(ctx, client, &lm) + if err != nil { + t.Errorf("ApplyLicenseManager should not have returned error; err=%v", err) + } + + _, err = ApplyClusterManager(ctx, client, &cm) + // lm statefulset should have been created by now, this should pass + if err != nil { + t.Errorf("applyClusterManager should not have returned error; err=%v", err) + } + + _, err = ApplyMonitoringConsole(ctx, client, &mc) + if err != nil { + t.Errorf("applyMonitoringConsole should not have returned error; err=%v", err) + } + _, err = ApplyIndexerCluster(ctx, client, &idx) + if err != nil { + t.Errorf("applyIndexerCluster should not have returned error; err=%v", err) + } + _, err = ApplySearchHeadCluster(ctx, client, &shc) + if err != nil { + t.Errorf("applySearchHeadCluster should not have returned error; err=%v", err) + } + + // Update + // standalone + namespacedName := types.NamespacedName{ + Name: "test", + Namespace: "test", + } + err = client.Get(ctx, namespacedName, &lm) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + + stdln.Spec.Image = "splunk/splunk:latest" + err = client.Update(ctx, &stdln) + if err != nil { + t.Errorf("update should not have returned error; err=%v", err) + } + _, err = ApplyStandalone(ctx, client, &stdln) + if err != nil { + t.Errorf("ApplyStandalone should not have returned error; err=%v", err) + } + + // cluster manager + err = client.Get(ctx, namespacedName, &cm) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + + cm.Spec.Image = "splunk/splunk:latest" + err = client.Update(ctx, &cm) + if err != nil { + t.Errorf("update should not have returned error; err=%v", err) + } + + // license manager + err = client.Get(ctx, namespacedName, &lm) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + lm.Spec.Image = "splunk/splunk:latest" + err = client.Update(ctx, &lm) + if err != nil { + t.Errorf("update should not have returned error; err=%v", err) + } + + // monitoring console + err = client.Get(ctx, namespacedName, &mc) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + mc.Spec.Image = "splunk/splunk:latest" + err = client.Update(ctx, &mc) + if err != nil { + t.Errorf("update should not have returned error; err=%v", err) + } + + // indexer cluster console + err = client.Get(ctx, namespacedName, &idx) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + idx.Spec.Image = "splunk/splunk:latest" + err = client.Update(ctx, &idx) + if err != nil { + t.Errorf("update should not have returned error; err=%v", err) + } + + // searchhead cluster console + err = client.Get(ctx, namespacedName, &shc) + if err != nil { + t.Errorf("get should not have returned error; err=%v", err) + } + shc.Spec.Image = "splunk/splunk:latest" + err = client.Update(ctx, &shc) + if err != nil { + t.Errorf("update should not have returned error; err=%v", err) + } + + _, err = ApplyClusterManager(ctx, client, &cm) + if err != nil { + t.Errorf("applyClusterManager after update should not have returned error; err=%v", err) + } + _, err = ApplyLicenseManager(ctx, client, &lm) + if err != nil { + t.Errorf("ApplyLicenseManager after update should not have returned error; err=%v", err) + } + _, err = ApplyMonitoringConsole(ctx, client, &mc) + if err != nil { + t.Errorf("applyMonitoringConsole after update should not have returned error; err=%v", err) + } + _, err = ApplyIndexerCluster(ctx, client, &idx) + if err != nil { + t.Errorf("applyIndexerCluster after update should not have returned error; err=%v", err) + } + _, err = ApplySearchHeadCluster(ctx, client, &shc) + if err != nil { + t.Errorf("applySearchHeadCluster after update should not have returned error; err=%v", err) + } +}