Skip to content

Commit

Permalink
Fix sentinel refresh and nil pointer issue (#77)
Browse files Browse the repository at this point in the history
* fix sentinel refresh and nil pointer

* update

* avoid return nil sentinelETags
  • Loading branch information
linglingye001 authored Nov 1, 2024
1 parent 99cbeee commit 9997638
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 12 deletions.
5 changes: 5 additions & 0 deletions internal/controller/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (processor *AppConfigurationProviderProcessor) processFullReconciliation()
processor.RefreshOptions.ConfigMapSettingPopulated = true
processor.RefreshOptions.updatedKeyValueETags = updatedSettings.KeyValueETags
processor.RefreshOptions.updatedFeatureFlagETags = updatedSettings.FeatureFlagETags
processor.RefreshOptions.updatedSentinelETags = updatedSettings.SentinelETags
if processor.Provider.Spec.Secret != nil {
processor.RefreshOptions.SecretSettingPopulated = true
}
Expand Down Expand Up @@ -323,6 +324,10 @@ func (processor *AppConfigurationProviderProcessor) Finish() (ctrl.Result, error
processor.ReconciliationState.FeatureFlagETags = processor.RefreshOptions.updatedFeatureFlagETags
}

if processor.ShouldReconcile {
processor.ReconciliationState.SentinelETags = processor.RefreshOptions.updatedSentinelETags
}

if !processor.RefreshOptions.secretReferenceRefreshEnabled &&
!processor.RefreshOptions.keyValueRefreshEnabled &&
!processor.RefreshOptions.featureFlagRefreshEnabled {
Expand Down
32 changes: 20 additions & 12 deletions internal/loader/configuration_setting_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type TargetKeyValueSettings struct {
SecretReferences map[string]*TargetSecretReference
KeyValueETags map[acpv1.Selector][]*azcore.ETag
FeatureFlagETags map[acpv1.Selector][]*azcore.ETag
SentinelETags map[acpv1.Sentinel]*azcore.ETag
}

type TargetSecretReference struct {
Expand Down Expand Up @@ -113,6 +114,20 @@ func (csl *ConfigurationSettingLoader) CreateTargetSettings(ctx context.Context,
return nil, err
}

initializedSentinelETags := make(map[acpv1.Sentinel]*azcore.ETag)
if csl.Spec.Configuration.Refresh != nil &&
csl.Spec.Configuration.Refresh.Enabled &&
csl.Spec.Configuration.Refresh.Monitoring != nil {
sentinels := normalizeSentinels(csl.Spec.Configuration.Refresh.Monitoring.Sentinels)
eTags := make(map[acpv1.Sentinel]*azcore.ETag)
for _, sentinel := range sentinels {
eTags[sentinel] = nil
}
if _, initializedSentinelETags, err = csl.CheckAndRefreshSentinels(ctx, &csl.AzureAppConfigurationProvider, eTags); err != nil {
return nil, err
}
}

if csl.Spec.FeatureFlag != nil {
if rawSettings.FeatureFlagSettings, rawSettings.FeatureFlagETags, err = csl.getFeatureFlagSettings(ctx); err != nil {
return nil, err
Expand All @@ -130,6 +145,7 @@ func (csl *ConfigurationSettingLoader) CreateTargetSettings(ctx context.Context,
SecretReferences: rawSettings.SecretReferences,
KeyValueETags: rawSettings.KeyValueETags,
FeatureFlagETags: rawSettings.FeatureFlagETags,
SentinelETags: initializedSentinelETags,
}, nil
}

Expand Down Expand Up @@ -299,22 +315,14 @@ func (csl *ConfigurationSettingLoader) CheckAndRefreshSentinels(
provider *acpv1.AzureAppConfigurationProvider,
eTags map[acpv1.Sentinel]*azcore.ETag) (bool, map[acpv1.Sentinel]*azcore.ETag, error) {
sentinelChanged := false
if provider.Spec.Configuration.Refresh == nil {
return sentinelChanged, eTags, NewArgumentError("spec.configuration.refresh", fmt.Errorf("refresh is not specified"))
}
refreshedETags := make(map[acpv1.Sentinel]*azcore.ETag)
sentinels := normalizeSentinels(provider.Spec.Configuration.Refresh.Monitoring.Sentinels)

for _, sentinel := range sentinels {
if eTag, ok := eTags[sentinel]; ok {
// Initialize the updatedETags with the current eTags
refreshedETags[sentinel] = eTag
}
for sentinel, currentETag := range eTags {
refreshedETags[sentinel] = currentETag
settingsClient := csl.SettingsClient
if settingsClient == nil {
settingsClient = &SentinelSettingsClient{
sentinel: sentinel,
etag: eTags[sentinel],
etag: currentETag,
refreshInterval: provider.Spec.Configuration.Refresh.Interval,
}
}
Expand All @@ -323,7 +331,7 @@ func (csl *ConfigurationSettingLoader) CheckAndRefreshSentinels(
return false, eTags, err
}

if response.Settings != nil && response.Settings[0].ETag != nil {
if response != nil && response.Settings != nil && response.Settings[0].ETag != nil {
sentinelChanged = true
refreshedETags[sentinel] = response.Settings[0].ETag
}
Expand Down

0 comments on commit 9997638

Please sign in to comment.