Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set label filter to nil when it's empty string #70

Merged
merged 6 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion api/v1/azureappconfigurationprovider_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ type RefreshMonitoring struct {
type Sentinel struct {
Key string `json:"key"`
// +kubebuilder:default="\x00"
Label string `json:"label,omitempty"`
Label *string `json:"label,omitempty"`
}

// ConfigMapDataOptions defines the options of generating ConfigMap data
Expand Down
9 changes: 8 additions & 1 deletion api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

61 changes: 41 additions & 20 deletions internal/controller/appconfigurationprovider_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -628,6 +628,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
mockConfigurationSettings.EXPECT().RefreshKeyValueSettings(gomock.Any(), gomock.Any(), gomock.Any()).Return(allSettings2, nil)

ctx := context.Background()
testKey := "testKey"
testLabel := "testLabel"
providerName := "refresh-appconfigurationprovider-2"
configMapName := "configmap-to-be-refresh-2"
configProvider := &acpv1.AzureAppConfigurationProvider{
Expand All @@ -651,7 +653,7 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Enabled: true,
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{Key: "testKey", Label: "testLabel"},
{Key: testKey, Label: &testLabel},
},
},
},
Expand Down Expand Up @@ -783,6 +785,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
mockConfigurationSettings.EXPECT().CheckAndRefreshSentinels(gomock.Any(), gomock.Any(), gomock.Any()).Return(false, nil, nil)

ctx := context.Background()
testNewKey := "testNewKey"
testNewLabel := "testNewLabel"
providerName := "refresh-appconfigurationprovider-2a"
configMapName := "configmap-to-be-refresh-2a"
configProvider := &acpv1.AzureAppConfigurationProvider{
Expand All @@ -806,7 +810,7 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Enabled: true,
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{Key: "testNewKey", Label: "testNewLabel"},
{Key: testNewKey, Label: &testNewLabel},
},
},
},
Expand Down Expand Up @@ -857,6 +861,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings, nil)

ctx := context.Background()
testNewKey := "testNewKey"
testNewLabel := "testNewLabel"
providerName := "refresh-appconfigurationprovider-2b"
configMapName := "configmap-to-be-refresh-2b"
configProvider := &acpv1.AzureAppConfigurationProvider{
Expand All @@ -880,7 +886,7 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Enabled: false,
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{Key: "testNewKey", Label: "testNewLabel"},
{Key: testNewKey, Label: &testNewLabel},
},
},
},
Expand Down Expand Up @@ -942,6 +948,10 @@ var _ = Describe("AppConfiguationProvider controller", func() {
mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings2, nil)

ctx := context.Background()
testKeyOne := "testKeyOne"
testLabelOne := "testNewLabel"
testKeyTwo := "testKeyTwo"
testLabelTwo := "testLabel"
providerName := "refresh-appconfigurationprovider-2c"
configMapName := "configmap-to-be-refresh-2c"
configProvider := &acpv1.AzureAppConfigurationProvider{
Expand All @@ -965,8 +975,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Enabled: true,
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{Key: "testKeyOne", Label: "testNewLabel"},
{Key: "testKeyTwo", Label: "testLabel"},
{Key: testKeyOne, Label: &testLabelOne},
{Key: testKeyTwo, Label: &testLabelTwo},
},
},
},
Expand Down Expand Up @@ -2256,6 +2266,9 @@ var _ = Describe("AppConfiguationProvider controller", func() {
It("Should return error when duplicated sentinel key are set", func() {
configMapName := "test-configmap"
connectionStringReference := "fakeSecret"
testKey := "testKey"
testLabelOne := "testValue"
testLabelTwo := "testValue1"
configProviderSpec := acpv1.AzureAppConfigurationProviderSpec{
ConnectionStringReference: &connectionStringReference,
Target: acpv1.ConfigurationGenerationParameters{
Expand All @@ -2266,16 +2279,16 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{
Key: "testKey",
Label: "testValue",
Key: testKey,
Label: &testLabelOne,
},
{
Key: "testKey",
Label: "testValue1",
Key: testKey,
Label: &testLabelTwo,
},
{
Key: "testKey",
Label: "testValue",
Key: testKey,
Label: &testLabelOne,
},
},
},
Expand All @@ -2289,6 +2302,12 @@ var _ = Describe("AppConfiguationProvider controller", func() {
It("Should return no error when all sentinel are unique", func() {
configMapName := "test-configmap"
connectionStringReference := "fakeSecret"
testKey := "testKey"
testKey2 := "testKey2"
testKey3 := "testKey3"
testKey4 := "testKey4"
testLabel := "testValue"
emptyLabel := ""
configProviderSpec := acpv1.AzureAppConfigurationProviderSpec{
ConnectionStringReference: &connectionStringReference,
Target: acpv1.ConfigurationGenerationParameters{
Expand All @@ -2299,20 +2318,20 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{
Key: "testKey",
Label: "\x00",
Key: testKey,
Label: nil,
},
{
Key: "testKey",
Label: "testValue1",
Key: testKey2,
Label: &testLabel,
},
{
Key: "testKey2",
Label: "\x00",
Key: testKey3,
Label: nil,
},
{
Key: "testKey2",
Label: "",
Key: testKey4,
Label: &emptyLabel,
},
},
},
Expand Down Expand Up @@ -2382,6 +2401,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {

It("Should return error when configuration.refresh.interval is less than 1 second", func() {
configMapName := "test-configmap"
testKey := "testKey"
testLabel := "testLabel"
configProviderSpec := acpv1.AzureAppConfigurationProviderSpec{
Endpoint: &EndpointName,
Target: acpv1.ConfigurationGenerationParameters{
Expand All @@ -2393,7 +2414,7 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Enabled: true,
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{Key: "testKey", Label: "testLabel"},
{Key: testKey, Label: &testLabel},
},
},
},
Expand Down
40 changes: 40 additions & 0 deletions internal/loader/configuraiton_setting_loader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1216,6 +1216,7 @@ func TestGetFilters(t *testing.T) {
two := "two"
three := "three"
labelString := "test"
emptyLabel := ""
testSpec := acpv1.AzureAppConfigurationProviderSpec{
Configuration: acpv1.AzureAppConfigurationKeyValueOptions{
Selectors: []acpv1.Selector{
Expand Down Expand Up @@ -1324,6 +1325,45 @@ func TestGetFilters(t *testing.T) {
assert.Equal(t, "test", *filters6[0].LabelFilter)
assert.Equal(t, "one", *filters6[1].KeyFilter)
assert.Nil(t, filters6[1].LabelFilter)

testSpec7 := acpv1.AzureAppConfigurationProviderSpec{
Configuration: acpv1.AzureAppConfigurationKeyValueOptions{
Selectors: []acpv1.Selector{
{KeyFilter: &one, LabelFilter: &emptyLabel},
},
},
}

filters7 := GetKeyValueFilters(testSpec7)
assert.Len(t, filters7, 1)
assert.Equal(t, "one", *filters7[0].KeyFilter)
assert.Nil(t, filters7[0].LabelFilter)

testSpec8 := acpv1.AzureAppConfigurationProviderSpec{
Configuration: acpv1.AzureAppConfigurationKeyValueOptions{
Refresh: &acpv1.DynamicConfigurationRefreshParameters{
Monitoring: &acpv1.RefreshMonitoring{
Sentinels: []acpv1.Sentinel{
{
Key: one,
Label: nil,
},
{
Key: two,
Label: &emptyLabel,
},
},
},
},
},
}

sentinels := getSentinels(testSpec8.Configuration.Refresh.Monitoring.Sentinels)
assert.Len(t, sentinels, 2)
assert.Equal(t, "one", sentinels[0].Key)
assert.Nil(t, sentinels[0].Label)
assert.Equal(t, "two", sentinels[1].Key)
assert.Nil(t, sentinels[1].Label)
}

func TestCompare(t *testing.T) {
Expand Down
41 changes: 38 additions & 3 deletions internal/loader/configuration_setting_loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,8 +303,9 @@ func (csl *ConfigurationSettingLoader) CheckAndRefreshSentinels(
return sentinelChanged, eTags, NewArgumentError("spec.configuration.refresh", fmt.Errorf("refresh is not specified"))
}
refreshedETags := make(map[acpv1.Sentinel]*azcore.ETag)
sentinels := getSentinels(provider.Spec.Configuration.Refresh.Monitoring.Sentinels)

for _, sentinel := range 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
Expand Down Expand Up @@ -624,14 +625,14 @@ func GetSecret(ctx context.Context,
}

func GetKeyValueFilters(acpSpec acpv1.AzureAppConfigurationProviderSpec) []acpv1.Selector {
return deduplicateFilters(acpSpec.Configuration.Selectors)
return deduplicateFilters(processEmptyLabelFilter(acpSpec.Configuration.Selectors))
}

func GetFeatureFlagFilters(acpSpec acpv1.AzureAppConfigurationProviderSpec) []acpv1.Selector {
featureFlagFilters := make([]acpv1.Selector, 0)

if acpSpec.FeatureFlag != nil {
featureFlagFilters = deduplicateFilters(acpSpec.FeatureFlag.Selectors)
featureFlagFilters = deduplicateFilters(processEmptyLabelFilter(acpSpec.FeatureFlag.Selectors))
for i := 0; i < len(featureFlagFilters); i++ {
if featureFlagFilters[i].KeyFilter != nil {
prefixedFeatureFlagFilter := FeatureFlagKeyPrefix + *featureFlagFilters[i].KeyFilter
Expand All @@ -643,6 +644,40 @@ func GetFeatureFlagFilters(acpSpec acpv1.AzureAppConfigurationProviderSpec) []ac
return featureFlagFilters
}

func getSentinels(sentinels []acpv1.Sentinel) []acpv1.Sentinel {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rename to "normalizeSentinels" ?

var results []acpv1.Sentinel
for _, sentinel := range sentinels {
label := sentinel.Label
if sentinel.Label != nil && len(*sentinel.Label) == 0 {
label = nil
}

results = append(results, acpv1.Sentinel{
Key: sentinel.Key,
Label: label,
})

}
return results
}

func processEmptyLabelFilter(filters []acpv1.Selector) []acpv1.Selector {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rename to "normalizeLableFilter"?

var result []acpv1.Selector
for i := 0; i < len(filters); i++ {
labelFilter := filters[i].LabelFilter
if filters[i].LabelFilter != nil && len(*filters[i].LabelFilter) == 0 {
labelFilter = nil
}

result = append(result, acpv1.Selector{
KeyFilter: filters[i].KeyFilter,
LabelFilter: labelFilter,
})
}

return result
}

func deduplicateFilters(filters []acpv1.Selector) []acpv1.Selector {
var result []acpv1.Selector
findDuplicate := false
Expand Down
6 changes: 3 additions & 3 deletions internal/loader/settings_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,15 +88,15 @@ func (s *EtagSettingsClient) GetSettings(ctx context.Context, client *azappconfi
}

func (s *SentinelSettingsClient) GetSettings(ctx context.Context, client *azappconfig.Client) (*SettingsResponse, error) {
sentinelSetting, err := client.GetSetting(ctx, s.sentinel.Key, &azappconfig.GetSettingOptions{Label: &s.sentinel.Label, OnlyIfChanged: s.etag})
sentinelSetting, err := client.GetSetting(ctx, s.sentinel.Key, &azappconfig.GetSettingOptions{Label: s.sentinel.Label, OnlyIfChanged: s.etag})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to do the same as SelectorSettingsClient, if label is nil, set it to \x00

if err != nil {
var respErr *azcore.ResponseError
if errors.As(err, &respErr) {
var label string
if s.sentinel.Label == "\x00" { // NUL is escaped to \x00 in golang
if s.sentinel.Label == nil { // NUL is escaped to \x00 in golang
label = "no"
} else {
label = fmt.Sprintf("'%s'", s.sentinel.Label)
label = fmt.Sprintf("'%s'", *s.sentinel.Label)
}
switch respErr.StatusCode {
case 404:
Expand Down
Loading