Skip to content

Commit

Permalink
added comments to the new code
Browse files Browse the repository at this point in the history
Signed-off-by: vivekr-splunk <94569031+vivekr-splunk@users.noreply.github.com>
  • Loading branch information
vivekr-splunk committed Aug 23, 2023
1 parent 60bd2a3 commit cba7f12
Showing 1 changed file with 40 additions and 12 deletions.
52 changes: 40 additions & 12 deletions pkg/splunk/enterprise/upgrade.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@ var GetClusterInfoCall = func(ctx context.Context, mgr *indexerClusterPodManager
return cm.GetClusterInfo(false)
}

// UpgradePathValidation is used in validating if upgrade can be done to given custom resource
//
// the method follows the sequence
// 1. Standalone or License Manager
// 2. Cluster Manager - if LM ref is defined, wait for License manager to complete
// 3. Monitoring Console - if CM ref is defined, wait for Cluster Manager to complete
// 4. Search Head Cluster - if MC ref , CM ref , LM ref is defined, wait for them to complete in order,
// if any one of them not defined, ignore them and wait for the one added in ref
// 5. Indexer Cluster - same as above also wait for search head cluster to complete before starting upgrade
// if its multisite then do 1 site at a time
// function returns bool and error , true - go ahead with upgrade
// false - exit the reconciliation loop with 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)
// start from standalone first
goto Standalone

// if custom resource type is standalone or license manager go ahead and upgrade
Standalone:
if cr.GroupVersionKind().Kind == "Standalone" {
return true, nil
Expand All @@ -39,6 +53,9 @@ LicenseManager:
return true, nil
} else {
licenseManagerRef := spec.LicenseManagerRef
// if custom resource type not license manager or standalone then
// check if there is license manager reference
// if no reference go to cluster manager
if licenseManagerRef.Name == "" {
goto ClusterManager
}
Expand All @@ -54,14 +71,17 @@ LicenseManager:
return false, err
}

// get current image of license manager
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 license manager status is ready and CR spec and current license manager image are not same
// then return with error
if licenseManager.Status.Phase != enterpriseApi.PhaseReady || lmImage != spec.Image {
return false, err
return false, fmt.Errorf("license manager is not ready or license manager current image is different than CR image")
}
goto ClusterManager
}
Expand All @@ -88,32 +108,34 @@ ClusterManager:
}
return true, nil
} else {
// check if a LicenseManager is attached to the instance
// check if a cluster manager reference is added to custom resource
clusterManagerRef := spec.ClusterManagerRef
if clusterManagerRef.Name == "" {
// if ref is not defined go to monitoring console step
goto MonitoringConsole
}

namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: clusterManagerRef.Name}
clusterManager := &enterpriseApi.ClusterManager{}

// get the cluster manager referred in monitoring console
// get the cluster manager referred in custom resource
err := c.Get(ctx, namespacedName, clusterManager)
if err != nil {
eventPublisher.Warning(ctx, "isMonitoringConsoleReadyForUpgrade", fmt.Sprintf("Could not find the Cluster Manager. Reason %v", err))
eventPublisher.Warning(ctx, "UpgradePathValidation", fmt.Sprintf("Could not find the Cluster Manager. Reason %v", err))
scopedLog.Error(err, "Unable to get clusterManager")
goto MonitoringConsole
}

/// get the cluster manager image referred in custom resource
cmImage, err := getCurrentImage(ctx, c, clusterManager, SplunkClusterManager)
if err != nil {
eventPublisher.Warning(ctx, "isMonitoringConsoleReadyForUpgrade", fmt.Sprintf("Could not get the Cluster Manager Image. Reason %v", err))
eventPublisher.Warning(ctx, "UpgradePathValidation", 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
// further reconcile operations on custom resource until CM is ready
if clusterManager.Status.Phase != enterpriseApi.PhaseReady || cmImage != spec.Image {
return false, nil
}
Expand Down Expand Up @@ -148,25 +170,26 @@ MonitoringConsole:
namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: monitoringConsoleRef.Name}
monitoringConsole := &enterpriseApi.MonitoringConsole{}

// get the monitoring console referred in search head cluster
// get the monitoring console referred in custom resource
err := c.Get(ctx, namespacedName, monitoringConsole)
if err != nil {
if k8serrors.IsNotFound(err) {
goto SearchHeadCluster
}
eventPublisher.Warning(ctx, "GetMonitoringConsole", fmt.Sprintf("Could not find the Monitoring Console. Reason %v", err))
eventPublisher.Warning(ctx, "UpgradePathValidation", fmt.Sprintf("Could not find the Monitoring Console. Reason %v", err))
scopedLog.Error(err, "Unable to get Monitoring Console")
return false, err
}

mcImage, err := getCurrentImage(ctx, c, monitoringConsole, SplunkMonitoringConsole)
if err != nil {
eventPublisher.Warning(ctx, "getCurrentImage", fmt.Sprintf("Could not get the Monitoring Console Image. Reason %v", err))
eventPublisher.Warning(ctx, "UpgradePathValidation", 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 an image upgrade is happening and whether the SearchHeadCluster is ready for the upgrade
// check if an image upgrade is happening and whether MC has finished updating yet, return false to stop
// further reconcile operations on custom resource until MC is ready
if monitoringConsole.Status.Phase != enterpriseApi.PhaseReady || mcImage != spec.Image {
return false, nil
}
Expand Down Expand Up @@ -212,7 +235,7 @@ SearchHeadCluster:
goto IndexerCluster
}

// check if instance has the required ClusterManagerRef
// check if instance has the ClusterManagerRef defined
for _, shc := range searchHeadList.Items {
if shc.Spec.ClusterManagerRef.Name == clusterManagerRef.Name {
searchHeadClusterInstance = shc
Expand All @@ -225,7 +248,7 @@ SearchHeadCluster:

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))
eventPublisher.Warning(ctx, "UpgradePathValidation", 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
}
Expand All @@ -240,14 +263,17 @@ SearchHeadCluster:
IndexerCluster:
if cr.GroupVersionKind().Kind == "IndexerCluster" {

// if manager client is not defined, then assign current client
if mgr.c == nil {
mgr.c = c
}

// check cluster info call using splunk rest api
clusterInfo, err := GetClusterInfoCall(ctx, mgr, false)
if err != nil {
return false, fmt.Errorf("could not get cluster info from cluster manager")
}
// check if cluster is multisite
if clusterInfo.MultiSite == "true" {
opts := []rclient.ListOption{
rclient.InNamespace(cr.GetNamespace()),
Expand All @@ -256,6 +282,7 @@ IndexerCluster:
if err != nil {
return false, err
}
// get sorted current indexer site list
sortedList, err := getIndexerClusterSortedSiteList(ctx, c, spec.ClusterManagerRef, indexerList)

preIdx := enterpriseApi.IndexerCluster{}
Expand All @@ -270,6 +297,7 @@ IndexerCluster:
}
}
if len(preIdx.Name) != 0 {
// check if previous indexer have completed before starting next one
image, _ := getCurrentImage(ctx, c, &preIdx, SplunkIndexer)
if preIdx.Status.Phase != enterpriseApi.PhaseReady || image != spec.Image {
return false, nil
Expand Down

0 comments on commit cba7f12

Please sign in to comment.