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

Refresh enhancement #85

Merged
merged 4 commits into from
Jan 17, 2025
Merged
Show file tree
Hide file tree
Changes from 3 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
15 changes: 11 additions & 4 deletions internal/controller/appconfigurationprovider_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func (reconciler *AzureAppConfigurationProviderReconciler) Reconcile(ctx context

/* Create ConfigMap from key-value settings */
if processor.RefreshOptions.ConfigMapSettingPopulated {
result, err := reconciler.createOrUpdateConfigMap(ctx, provider, processor.Settings)
result, err := reconciler.createOrUpdateConfigMap(ctx, &existingConfigMap, provider, processor.Settings)
if err != nil {
return result, nil
}
Expand All @@ -273,7 +273,7 @@ func (reconciler *AzureAppConfigurationProviderReconciler) Reconcile(ctx context
}
}

result, err := reconciler.createOrUpdateSecrets(ctx, provider, processor)
result, err := reconciler.createOrUpdateSecrets(ctx, provider, processor, existingSecrets)
if err != nil {
return result, nil
}
Expand Down Expand Up @@ -369,8 +369,14 @@ func (reconciler *AzureAppConfigurationProviderReconciler) requeueWhenGetSetting

func (reconciler *AzureAppConfigurationProviderReconciler) createOrUpdateConfigMap(
ctx context.Context,
existingConfigMap *corev1.ConfigMap,
provider *acpv1.AzureAppConfigurationProvider,
settings *loader.TargetKeyValueSettings) (reconcile.Result, error) {
if !shouldCreateOrUpdateConfigMap(existingConfigMap, provider.Spec.Target.ConfigMapData, settings.ConfigMapSettings) {
klog.V(5).Infof("Skip updating the configMap %q in %q namespace since data is not changed", provider.Spec.Target.ConfigMapName, provider.Namespace)
return reconcile.Result{}, nil
}

configMapObj := &corev1.ConfigMap{
ObjectMeta: metav1.ObjectMeta{
Name: provider.Spec.Target.ConfigMapName,
Expand Down Expand Up @@ -415,7 +421,8 @@ func (reconciler *AzureAppConfigurationProviderReconciler) createOrUpdateConfigM
func (reconciler *AzureAppConfigurationProviderReconciler) createOrUpdateSecrets(
ctx context.Context,
provider *acpv1.AzureAppConfigurationProvider,
processor *AppConfigurationProviderProcessor) (reconcile.Result, error) {
processor *AppConfigurationProviderProcessor,
existingSecrets map[string]corev1.Secret) (reconcile.Result, error) {
if len(processor.Settings.SecretSettings) == 0 {
klog.V(3).Info("No secret settings are fetched from Azure AppConfiguration")
}
Expand All @@ -430,7 +437,7 @@ func (reconciler *AzureAppConfigurationProviderReconciler) createOrUpdateSecrets
}

for secretName, secret := range processor.Settings.SecretSettings {
if !shouldCreateOrUpdate(processor, secretName) {
if !shouldCreateOrUpdateSecret(processor, secretName, existingSecrets) {
if _, ok := reconciler.ProvidersReconcileState[namespacedName].ExistingK8sSecrets[secretName]; ok {
processor.Settings.K8sSecrets[secretName].SecretResourceVersion = reconciler.ProvidersReconcileState[namespacedName].ExistingK8sSecrets[secretName].SecretResourceVersion
}
Expand Down
156 changes: 17 additions & 139 deletions internal/controller/appconfigurationprovider_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
acpv1 "azappconfig/provider/api/v1"

"github.com/Azure/azure-sdk-for-go/sdk/azcore"
"github.com/Azure/azure-sdk-for-go/sdk/security/keyvault/azsecrets"
"github.com/golang/mock/gomock"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
Expand Down Expand Up @@ -1107,11 +1106,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
secretResult["testSecretKey"] = []byte("testSecretValue")

secretName := "secret-to-be-modified"
var fakeId azsecrets.ID = "fakeSecretId"
secretMetadata := make(map[string]loader.KeyVaultSecretMetadata)
secretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{
SecretId: &fakeId,
}
secretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{}

allSettings := &loader.TargetKeyValueSettings{
SecretSettings: map[string]corev1.Secret{
Expand Down Expand Up @@ -1214,7 +1210,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Expect(string(secret.Data["testSecretKey"])).Should(Equal("testSecretValue"))
Expect(secret.Type).Should(Equal(corev1.SecretType("Opaque")))
Expect(secret.Annotations["azconfig.io/LastReconcileTime"]).ShouldNot(Equal(secretLastReconcileTime))
Expect(configmap.Annotations["azconfig.io/LastReconcileTime"]).ShouldNot(Equal(configmapLastReconcileTime))
// Since no data change in configMap, the last reconcile time should not change
Expect(configmap.Annotations["azconfig.io/LastReconcileTime"]).Should(Equal(configmapLastReconcileTime))

_ = k8sClient.Delete(ctx, configProvider)
})
Expand All @@ -1230,11 +1227,8 @@ var _ = Describe("AppConfiguationProvider controller", func() {
secretResult["testSecretKey"] = []byte("testSecretValue")

secretName := "secret-to-be-deleted"
var fakeId azsecrets.ID = "fakeSecretId"
secretMetadata := make(map[string]loader.KeyVaultSecretMetadata)
secretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{
SecretId: &fakeId,
}
secretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{}

allSettings := &loader.TargetKeyValueSettings{
SecretSettings: map[string]corev1.Secret{
Expand Down Expand Up @@ -1340,7 +1334,7 @@ var _ = Describe("AppConfiguationProvider controller", func() {
Expect(string(secret.Data["testSecretKey"])).Should(Equal("testSecretValue"))
Expect(secret.Type).Should(Equal(corev1.SecretType("Opaque")))
Expect(secret.Annotations["azconfig.io/LastReconcileTime"]).ShouldNot(Equal(secretLastReconcileTime))
Expect(configmap.Annotations["azconfig.io/LastReconcileTime"]).ShouldNot(Equal(configmapLastReconcileTime))
Expect(configmap.Annotations["azconfig.io/LastReconcileTime"]).Should(Equal(configmapLastReconcileTime))

_ = k8sClient.Delete(ctx, configProvider)
})
Expand All @@ -1356,10 +1350,11 @@ var _ = Describe("AppConfiguationProvider controller", func() {
secretResult["testSecretKey"] = []byte("testSecretValue")

secretName := "secret-to-be-refreshed-3"
var fakeId azsecrets.ID = "fakeSecretId"
secretMetadata := make(map[string]loader.KeyVaultSecretMetadata)
secretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{
SecretId: &fakeId,
HostName: "fake-host",
SecretVersion: "",
SecretName: "fake-secret",
}

allSettings := &loader.TargetKeyValueSettings{
Expand Down Expand Up @@ -1449,10 +1444,11 @@ var _ = Describe("AppConfiguationProvider controller", func() {
},
}

var newFakeId azsecrets.ID = "newFakeSecretId"
newSecretMetadata := make(map[string]loader.KeyVaultSecretMetadata)
newSecretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{
SecretId: &newFakeId,
HostName: "fake-host",
SecretVersion: "",
SecretName: "fake-secret",
}
mockedSecretReference := make(map[string]*loader.TargetK8sSecretMetadata)
mockedSecretReference[secretName] = &loader.TargetK8sSecretMetadata{
Expand Down Expand Up @@ -1665,126 +1661,6 @@ var _ = Describe("AppConfiguationProvider controller", func() {
_ = k8sClient.Delete(ctx, configProvider)
})

It("Should refresh configMap when both configuration.refresh and featureFlag.refresh enabled", func() {
By("When selected feattureFlags updated in Azure App Configuration")
mapResult := make(map[string]string)
keyValueEtags := make(map[acpv1.Selector][]*azcore.ETag)
featureFlagEtags := make(map[acpv1.Selector][]*azcore.ETag)
mapResult["filestyle.json"] = "{\"aKey\":\"testValue\",\"feature_management\":{\"feature_flags\":[{\"id\": \"testFeatureFlag\",\"enabled\": true,\"conditions\": {\"client_filters\": []}}]}}"

allSettings := &loader.TargetKeyValueSettings{
ConfigMapSettings: mapResult,
KeyValueETags: keyValueEtags,
FeatureFlagETags: featureFlagEtags,
}

mapResult2 := make(map[string]string)
mapResult2["filestyle.json"] = "{\"aKey\":\"testValue\",\"feature_management\":{\"feature_flags\":[{\"id\": \"testFeatureFlag\",\"enabled\": false,\"conditions\": {\"client_filters\": []}}]}}"

allSettings2 := &loader.TargetKeyValueSettings{
ConfigMapSettings: mapResult2,
KeyValueETags: keyValueEtags,
FeatureFlagETags: featureFlagEtags,
}

mockConfigurationSettings.EXPECT().CreateTargetSettings(gomock.Any(), gomock.Any()).Return(allSettings, nil)

ctx := context.Background()
providerName := "test-appconfigurationprovider-7a"
configMapName := "file-style-configmap-to-be-created-7a"
wildcard := "*"
configProvider := &acpv1.AzureAppConfigurationProvider{
TypeMeta: metav1.TypeMeta{
APIVersion: "appconfig.kubernetes.config/v1",
Kind: "AzureAppConfigurationProvider",
},
ObjectMeta: metav1.ObjectMeta{
Name: providerName,
Namespace: ProviderNamespace,
Labels: map[string]string{"foo": "fooValue", "bar": "barValue"},
},
Spec: acpv1.AzureAppConfigurationProviderSpec{
Endpoint: &EndpointName,
Target: acpv1.ConfigurationGenerationParameters{
ConfigMapName: configMapName,
ConfigMapData: &acpv1.ConfigMapDataOptions{
Type: "json",
Key: "filestyle.json",
},
},
Configuration: acpv1.AzureAppConfigurationKeyValueOptions{
Refresh: &acpv1.DynamicConfigurationRefreshParameters{
Interval: "5s",
Enabled: true,
},
},
FeatureFlag: &acpv1.AzureAppConfigurationFeatureFlagOptions{
Selectors: []acpv1.Selector{
{
KeyFilter: &wildcard,
},
},
Refresh: &acpv1.FeatureFlagRefreshSettings{
Interval: "5s",
Enabled: true,
},
},
},
}
Expect(k8sClient.Create(ctx, configProvider)).Should(Succeed())
time.Sleep(time.Second * 5) //Wait few seconds to wait the second round reconcile complete
configmapLookupKey := types.NamespacedName{Name: configMapName, Namespace: ProviderNamespace}
configmap := &corev1.ConfigMap{}

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Name).Should(Equal(configMapName))
Expect(configmap.Namespace).Should(Equal(ProviderNamespace))
Expect(configmap.Labels["foo"]).Should(Equal("fooValue"))
Expect(configmap.Labels["bar"]).Should(Equal("barValue"))
Expect(configmap.Data["filestyle.json"]).Should(Equal("{\"aKey\":\"testValue\",\"feature_management\":{\"feature_flags\":[{\"id\": \"testFeatureFlag\",\"enabled\": true,\"conditions\": {\"client_filters\": []}}]}}"))
Expect(len(configmap.Data)).Should(Equal(1))

mockConfigurationSettings.EXPECT().CheckPageETags(gomock.Any(), gomock.Any()).Return(false, nil).Times(2)

time.Sleep(5 * time.Second)

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Name).Should(Equal(configMapName))
Expect(configmap.Namespace).Should(Equal(ProviderNamespace))
Expect(configmap.Labels["foo"]).Should(Equal("fooValue"))
Expect(configmap.Labels["bar"]).Should(Equal("barValue"))
Expect(configmap.Data["filestyle.json"]).Should(Equal("{\"aKey\":\"testValue\",\"feature_management\":{\"feature_flags\":[{\"id\": \"testFeatureFlag\",\"enabled\": true,\"conditions\": {\"client_filters\": []}}]}}"))
Expect(len(configmap.Data)).Should(Equal(1))

mockConfigurationSettings.EXPECT().CheckPageETags(gomock.Any(), gomock.Any()).Return(true, nil)
mockConfigurationSettings.EXPECT().RefreshFeatureFlagSettings(gomock.Any(), gomock.Any()).Return(allSettings2, nil)
mockConfigurationSettings.EXPECT().CheckPageETags(gomock.Any(), gomock.Any()).Return(false, nil)

time.Sleep(5 * time.Second)

Eventually(func() bool {
err := k8sClient.Get(ctx, configmapLookupKey, configmap)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(configmap.Name).Should(Equal(configMapName))
Expect(configmap.Namespace).Should(Equal(ProviderNamespace))
Expect(configmap.Labels["foo"]).Should(Equal("fooValue"))
Expect(configmap.Labels["bar"]).Should(Equal("barValue"))
Expect(configmap.Data["filestyle.json"]).Should(Equal("{\"aKey\":\"testValue\",\"feature_management\":{\"feature_flags\":[{\"id\": \"testFeatureFlag\",\"enabled\": false,\"conditions\": {\"client_filters\": []}}]}}"))
Expect(len(configmap.Data)).Should(Equal(1))

_ = k8sClient.Delete(ctx, configProvider)
})

It("Feature flag refresh can work with secret refresh", func() {
By("By enabling refresh on secret and feature flag")
mapResult := make(map[string]string)
Expand All @@ -1794,10 +1670,11 @@ var _ = Describe("AppConfiguationProvider controller", func() {
secretResult["testSecretKey"] = []byte("testSecretValue")

secretName := "secret-to-be-refreshed-4"
var fakeId azsecrets.ID = "fakeSecretId"
secretMetadata := make(map[string]loader.KeyVaultSecretMetadata)
secretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{
SecretId: &fakeId,
HostName: "fake-host",
SecretVersion: "",
SecretName: "fake-secret",
}

allSettings := &loader.TargetKeyValueSettings{
Expand Down Expand Up @@ -1932,10 +1809,11 @@ var _ = Describe("AppConfiguationProvider controller", func() {
},
}

var newFakeId azsecrets.ID = "newFakeSecretId"
newSecretMetadata := make(map[string]loader.KeyVaultSecretMetadata)
newSecretMetadata["testSecretKey"] = loader.KeyVaultSecretMetadata{
SecretId: &newFakeId,
HostName: "fake-host",
SecretVersion: "",
SecretName: "fake-secret",
}
mockedSecretReference := make(map[string]*loader.TargetK8sSecretMetadata)
mockedSecretReference[secretName] = &loader.TargetK8sSecretMetadata{
Expand Down
22 changes: 8 additions & 14 deletions internal/controller/processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -227,13 +227,7 @@ func (processor *AppConfigurationProviderProcessor) processSecretReferenceRefres

// Only resolve the secret references that not specified the secret version
secretReferencesToSolve := make(map[string]*loader.TargetK8sSecretMetadata)
copiedSecretReferences := make(map[string]*loader.TargetK8sSecretMetadata)
for secretName, k8sSecret := range reconcileState.ExistingK8sSecrets {
copiedSecretReferences[secretName] = &loader.TargetK8sSecretMetadata{
Type: k8sSecret.Type,
SecretsKeyVaultMetadata: make(map[string]loader.KeyVaultSecretMetadata),
}

for key, secretMetadata := range k8sSecret.SecretsKeyVaultMetadata {
if secretMetadata.SecretVersion == "" {
if secretReferencesToSolve[secretName] == nil {
Expand All @@ -244,7 +238,6 @@ func (processor *AppConfigurationProviderProcessor) processSecretReferenceRefres
}
secretReferencesToSolve[secretName].SecretsKeyVaultMetadata[key] = secretMetadata
}
copiedSecretReferences[secretName].SecretsKeyVaultMetadata[key] = secretMetadata
}
}

Expand All @@ -253,19 +246,20 @@ func (processor *AppConfigurationProviderProcessor) processSecretReferenceRefres
return err
}

secrets := make(map[string]corev1.Secret)
for key, secret := range existingSecrets {
secrets[key] = *secret.DeepCopy()
}

for secretName, resolvedSecret := range resolvedSecrets.SecretSettings {
existingSecret, ok := existingSecrets[secretName]
existingSecret, ok := secrets[secretName]
if ok {
maps.Copy(existingSecret.Data, resolvedSecret.Data)
}
}

for secretName, k8sSecret := range resolvedSecrets.K8sSecrets {
maps.Copy(copiedSecretReferences[secretName].SecretsKeyVaultMetadata, k8sSecret.SecretsKeyVaultMetadata)
}

processor.Settings.SecretSettings = existingSecrets
processor.Settings.K8sSecrets = copiedSecretReferences
processor.Settings.SecretSettings = secrets
processor.Settings.K8sSecrets = reconcileState.ExistingK8sSecrets
processor.RefreshOptions.SecretSettingPopulated = true

// Update next refresh time only if settings updated successfully
Expand Down
Loading
Loading