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

Fix bug where we could fail to emit user requested secrets or configmaps #3925

Merged
merged 1 commit into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
53 changes: 53 additions & 0 deletions v2/internal/controllers/edge_case_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,13 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"

documentdb "github.com/Azure/azure-service-operator/v2/api/documentdb/v1api20210515"
network "github.com/Azure/azure-service-operator/v2/api/network/v1api20201101"
resources "github.com/Azure/azure-service-operator/v2/api/resources/v1api20200601"
"github.com/Azure/azure-service-operator/v2/internal/testcommon"
"github.com/Azure/azure-service-operator/v2/internal/util/to"
"github.com/Azure/azure-service-operator/v2/pkg/common/annotations"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime"
"github.com/Azure/azure-service-operator/v2/pkg/genruntime/conditions"
)

Expand Down Expand Up @@ -277,3 +280,53 @@ func Test_Owner_IsMutableIfNotSuccessfullyCreated(t *testing.T) {
// Delete the account
tc.DeleteResourceAndWait(acct)
}

func Test_CreateCosmosAccountWithSkipReconcile_SecretsAreWritten(t *testing.T) {
t.Parallel()
tc := globalTestContext.ForTest(t)

rg := tc.CreateTestResourceGroupAndWait()
cosmosSecret1 := "keys1"

// Custom namer because cosmosdb accounts have stricter name
// requirements - no hyphens allowed.
// Create a Cosmos DB account
offerType := documentdb.DatabaseAccountOfferType_Standard
kind := documentdb.DatabaseAccount_Kind_Spec_GlobalDocumentDB
acct := &documentdb.DatabaseAccount{
ObjectMeta: tc.MakeObjectMetaWithName(tc.NoSpaceNamer.GenerateName("sqlacct")),
Spec: documentdb.DatabaseAccount_Spec{
Location: tc.AzureRegion,
Owner: testcommon.AsOwner(rg),
Kind: &kind,
DatabaseAccountOfferType: &offerType,
Locations: []documentdb.Location{
{
LocationName: tc.AzureRegion,
},
},
OperatorSpec: &documentdb.DatabaseAccountOperatorSpec{
Secrets: &documentdb.DatabaseAccountOperatorSecrets{
DocumentEndpoint: &genruntime.SecretDestination{
Name: cosmosSecret1,
Key: "endpoint",
},
},
},
},
}

cosmosSecret2 := "keys2"
skipAcct := acct.DeepCopy()
skipAcct.Spec.AzureName = skipAcct.Name
skipAcct.Name = skipAcct.Name + "-skip" // So we don't collide
skipAcct.Spec.OperatorSpec.Secrets.DocumentEndpoint.Name = cosmosSecret2
skipAcct.Annotations = map[string]string{
annotations.ReconcilePolicy: string(annotations.ReconcilePolicySkip),
}

tc.CreateResourcesAndWait(acct, skipAcct)

tc.ExpectSecretHasKeys(cosmosSecret1, "endpoint")
tc.ExpectSecretHasKeys(cosmosSecret2, "endpoint")
}

Large diffs are not rendered by default.

8 changes: 7 additions & 1 deletion v2/pkg/genruntime/configmaps/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func (c *Collector) errIfKeyExists(val *v1.ConfigMap, key string) error {
// been added going to the same config map (but with a different key) the new key is merged into the
// existing map.
func (c *Collector) AddValue(dest *genruntime.ConfigMapDestination, value string) {
if dest == nil || value == "" {
if dest == nil {
return
}

if value == "" {
// A dest was provided, but we couldn't find the key to match. This is an error
c.errors = append(c.errors, errors.Errorf("could not find value to save to '%s'", dest.String()))
return
}

Expand Down
16 changes: 16 additions & 0 deletions v2/pkg/genruntime/configmaps/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func TestCollector_DestinationsDifferentConfigMap_DoesNotMerge(t *testing.T) {
g.Expect(result[1].Data["bar"]).To(Equal("value2"))
}

func TestCollector_MissingValue_ReturnsError(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

destination1 := &genruntime.ConfigMapDestination{
Name: "myconfig",
Key: "foo",
}

collector := configmaps.NewCollector("ns")
collector.AddValue(destination1, "")

_, err := collector.Values()
g.Expect(err).To(HaveOccurred())
}

func TestCollector_SameDestinationSameKey_ReturnsError(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
Expand Down
8 changes: 7 additions & 1 deletion v2/pkg/genruntime/secrets/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,13 @@ func (c *Collector) errIfKeyExists(val *v1.Secret, key string) error {
// been added going to the same secret (but with a different key) the new key is merged into the
// existing secret.
func (c *Collector) AddValue(dest *genruntime.SecretDestination, value string) {
if dest == nil || value == "" {
if dest == nil {
return
}

if value == "" {
// A dest was provided, but we couldn't find the key to match. This is an error
c.errors = append(c.errors, errors.Errorf("could not find secret to save to '%s'", dest.String()))
return
}

Expand Down
16 changes: 16 additions & 0 deletions v2/pkg/genruntime/secrets/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,22 @@ func TestCollector_DestinationsDifferentSecret_DoesNotMerge(t *testing.T) {
g.Expect(result[1].StringData["bar"]).To(Equal("secret2"))
}

func TestCollector_MissingValue_ReturnsError(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)

destination1 := &genruntime.SecretDestination{
Name: "mysecret",
Key: "foo",
}

collector := secrets.NewCollector("ns")
collector.AddValue(destination1, "")

_, err := collector.Values()
g.Expect(err).To(HaveOccurred())
}

func TestCollector_SameDestinationSameKey_ReturnsError(t *testing.T) {
t.Parallel()
g := NewGomegaWithT(t)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ spec:
# Optional: Save the keys for the storage account into a Kubernetes secret
operatorSpec:
secrets:
appInsightsInstrumentationKey:
name: workspace-secret
matthchr marked this conversation as resolved.
Show resolved Hide resolved
key: appInsightsInstrumentationKey
containerRegistryPassword:
name: workspace-secret
key: containerRegistryPassword
containerRegistryPassword2:
name: workspace-secret
key: containerRegistryPassword2
containerRegistryUserName:
name: workspace-secret
key: containerRegistryUserName
# appInsightsInstrumentationKey:
# name: workspace-secret
# key: appInsightsInstrumentationKey
# containerRegistryPassword:
# name: workspace-secret
# key: containerRegistryPassword
# containerRegistryPassword2:
# name: workspace-secret
# key: containerRegistryPassword2
# containerRegistryUserName:
# name: workspace-secret
# key: containerRegistryUserName
primaryNotebookAccessKey:
name: workspace-secret
key: primaryNotebookAccessKey
Expand Down
Loading