Skip to content

Commit

Permalink
adding per cr changes
Browse files Browse the repository at this point in the history
Signed-off-by: Vivek Reddy <vivekrsplunk@github.com>
  • Loading branch information
Vivek Reddy committed Jan 10, 2025
1 parent 4803790 commit 4eb8128
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 11 deletions.
24 changes: 23 additions & 1 deletion pkg/splunk/enterprise/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -1262,20 +1262,42 @@ func getManualUpdateStatus(ctx context.Context, client splcommon.ControllerClien
data := configMap.Data[cr.GetObjectKind().GroupVersionKind().Kind]
result = extractFieldFromConfigMapData(statusRegex, data)
if result == "on" {
scopedLog.Info("Namespace configMap value is set to", "name", configMapName, "data", result)
return result
}
} else {
scopedLog.Error(err, "Unable to get namespace specific configMap", "name", configMapName)
}

namespacedName = types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())}
/*
namespacedName = types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())}
CrconfigMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
if err == nil {
scopedLog.Info("Custom configMap value is set to", "name", configMapName, "data", CrconfigMap.Data)
data := CrconfigMap.Data["manualUpdate"]
return data
} else {
scopedLog.Error(err, "Unable to get custom specific configMap", "name", configMapName)
}
*/
return "off"
}

// getManualUpdatePerCrStatus extracts the status field from the configMap data
func getManualUpdatePerCrStatus(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject, configMapName string) string {
reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("getManualUpdatePerCrStatus").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())

namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())}
CrconfigMap, err := splctrl.GetConfigMap(ctx, client, namespacedName)
if err == nil {
scopedLog.Info("Custom configMap value is set to", "name", configMapName, "data", CrconfigMap.Data)
data := CrconfigMap.Data["manualUpdate"]
return data
} else {
scopedLog.Error(err, "Unable to get custom specific configMap", "name", configMapName)
}

return "off"
}

Expand Down
47 changes: 37 additions & 10 deletions pkg/splunk/enterprise/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -1466,31 +1466,43 @@ func isAppRepoPollingEnabled(appStatusContext *enterpriseApi.AppDeploymentContex

// shouldCheckAppRepoStatus
func shouldCheckAppRepoStatus(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject, appStatusContext *enterpriseApi.AppDeploymentContext, kind string, turnOffManualChecking *bool) bool {
reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("shouldCheckAppRepoStatus")
// If polling is disabled, check if manual update is on.
if !isAppRepoPollingEnabled(appStatusContext) {
configMapName := GetSplunkManualAppUpdateConfigMapName(cr.GetNamespace())

// Check if we need to manually check for app updates for this CR kind
scopedLog.Info("checking if namespace specific configmap contains manualUpdate settings")
if getManualUpdateStatus(ctx, client, cr, configMapName) == "on" {
scopedLog.Info("namespace specific configmap contains manualUpdate is set to on", "configMapName", configMapName)
// There can be more than 1 CRs of this kind. We should only
// turn off the status once all the CRs have finished the reconciles
if getManualUpdateRefCount(ctx, client, cr, configMapName) == 1 {
*turnOffManualChecking = true
}
return true
} else {
configMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
if getManualUpdateStatus(ctx, client, cr, configMapName) == "on" {
*turnOffManualChecking = true
return true
}
}
} else {
return HasAppRepoCheckTimerExpired(ctx, appStatusContext)
}
return false
}

func IsManualUpdateSetInCRConfig(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject, appStatusContext *enterpriseApi.AppDeploymentContext, kind string, turnOffManualChecking *bool) bool {
reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("shouldCheckAppRepoStatusPerCR")

configMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
scopedLog.Info("checking if per CR specific configmap contains manualUpdate settings")
if getManualUpdatePerCrStatus(ctx, client, cr, configMapName) == "on" {
scopedLog.Info("CR specific configmap contains manualUpdate is set to on", "configMapName", configMapName)
//*turnOffManualChecking = true
return true
}
return false
}

// getCleanObjectDigest returns only hexa-decimal portion of a string
// Ex. '\"b38a8f911e2b43982b71a979fe1d3c3f\"' is converted to b38a8f911e2b43982b71a979fe1d3c3f
func getCleanObjectDigest(rawObjectDigest *string) (*string, error) {
Expand Down Expand Up @@ -1568,6 +1580,15 @@ func updateManualAppUpdateConfigMapLocked(ctx context.Context, client splcommon.
}
}

return nil
}

func updateCrSpecificManualAppUpdateConfigMap(ctx context.Context, client splcommon.ControllerClient, cr splcommon.MetaObject, appStatusContext *enterpriseApi.AppDeploymentContext, kind string, turnOffManualChecking bool) error {

reqLogger := log.FromContext(ctx)
scopedLog := reqLogger.WithName("updateManualAppUpdateConfigMap").WithValues("name", cr.GetName(), "namespace", cr.GetNamespace())
configMapName := GetSplunkManualAppUpdateConfigMapName(cr.GetNamespace())
namespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: configMapName}
// now check namespace specific configmap if it contains manualUpdate settings
crScopedConfigMapName := fmt.Sprintf(perCrConfigMapNameStr, KindToInstanceString(cr.GroupVersionKind().Kind), cr.GetName())
crNamespacedName := types.NamespacedName{Namespace: cr.GetNamespace(), Name: crScopedConfigMapName}
Expand All @@ -1576,7 +1597,7 @@ func updateManualAppUpdateConfigMapLocked(ctx context.Context, client splcommon.
scopedLog.Error(err, "Unable to get configMap", "name", namespacedName.Name)
return err
}
if turnOffManualChecking {
if configMap.Data["manualUpdate"] == "on" {
scopedLog.Info("Turning off manual checking of apps update in per CR configmap", "Kind", kind)
configMap.Data["manualUpdate"] = "off"
}
Expand All @@ -1586,8 +1607,7 @@ func updateManualAppUpdateConfigMapLocked(ctx context.Context, client splcommon.
scopedLog.Error(err, "Could not update the per CR configMap", "name", crNamespacedName.Name)
return err
}

return nil
return err
}

// initAndCheckAppInfoStatus initializes the RemoteDataClients and checks the status of apps on remote storage.
Expand All @@ -1611,7 +1631,9 @@ func initAndCheckAppInfoStatus(ctx context.Context, client splcommon.ControllerC
kind := cr.GetObjectKind().GroupVersionKind().Kind

//check if the apps need to be downloaded from remote storage
if shouldCheckAppRepoStatus(ctx, client, cr, appStatusContext, kind, &turnOffManualChecking) || !reflect.DeepEqual(appStatusContext.AppFrameworkConfig, *appFrameworkConf) {
if shouldCheckAppRepoStatus(ctx, client, cr, appStatusContext, kind, &turnOffManualChecking) ||
IsManualUpdateSetInCRConfig(ctx, client, cr, appStatusContext, kind, &turnOffManualChecking) ||
!reflect.DeepEqual(appStatusContext.AppFrameworkConfig, *appFrameworkConf) {

if appStatusContext.IsDeploymentInProgress {
scopedLog.Info("App installation is already in progress. Not checking for any latest app repo changes")
Expand Down Expand Up @@ -1665,6 +1687,11 @@ func initAndCheckAppInfoStatus(ctx context.Context, client splcommon.ControllerC
scopedLog.Error(err, "failed to update the manual app udpate configMap")
return err
}
err = updateCrSpecificManualAppUpdateConfigMap(ctx, client, cr, appStatusContext, kind, turnOffManualChecking)
if err != nil {
scopedLog.Error(err, "failed to update the manual app udpate CR specific configMap")
return err
}
}
}

Expand Down

0 comments on commit 4eb8128

Please sign in to comment.