Skip to content

Commit ba46943

Browse files
authored
fix(sharding): preserve shared account source reference (#10785)
1 parent 6d7846f commit ba46943

2 files changed

Lines changed: 45 additions & 38 deletions

File tree

controllers/apps/cluster/transformer_cluster_sharding_account.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,22 +332,21 @@ func (t *clusterShardingAccountTransformer) rewriteSystemAccount(transCtx *clust
332332
SecretRefRevision: revision,
333333
}
334334

335-
// update sharding
336-
for i, sharding := range transCtx.shardings {
335+
for _, sharding := range transCtx.shardings {
337336
if sharding.Name == shardingName {
338337
for _, account := range sharding.Template.SystemAccounts {
339338
if account.Name == accountName {
340339
newAccount.Disabled = account.Disabled
341340
break
342341
}
343342
}
344-
transCtx.shardings[i].Template.SystemAccounts =
345-
upsertSystemAccount(transCtx.shardings[i].Template.SystemAccounts, newAccount)
346343
break
347344
}
348345
}
349346

350-
// update sharding components
347+
// Rewrite only the expanded component specs and keep the sharding declaration
348+
// unchanged, so a user-provided source Secret remains the source on the next
349+
// reconcile. shardingComps and shardingCompsWithTpl share component pointers.
351350
shardingComps := transCtx.shardingComps[shardingName]
352351
for i := range shardingComps {
353352
shardingComps[i].SystemAccounts = upsertSystemAccount(shardingComps[i].SystemAccounts, newAccount)

controllers/apps/cluster/transformer_cluster_sharding_test.go

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -529,8 +529,12 @@ var _ = Describe("cluster sharding shared transformers", func() {
529529
transCtx := newTransformContext(referenced)
530530
transCtx.componentDefs = map[string]*appsv1.ComponentDefinition{compDefName: newComponentDefinition()}
531531
transCtx.shardings = []*appsv1.ClusterSharding{sharding}
532+
comps := []*appsv1.ClusterComponentSpec{{Name: "shard-0"}, {Name: "shard-1"}}
532533
transCtx.shardingComps = map[string][]*appsv1.ClusterComponentSpec{
533-
shardingName: {{Name: "shard-0"}, {Name: "shard-1"}},
534+
shardingName: comps,
535+
}
536+
transCtx.shardingCompsWithTpl = map[string]map[string][]*appsv1.ClusterComponentSpec{
537+
shardingName: {"": comps},
534538
}
535539

536540
managed, err := transformer.newSystemAccountSecret(transCtx, sharding, accountName)
@@ -539,11 +543,12 @@ var _ = Describe("cluster sharding shared transformers", func() {
539543
Expect(managed.Data).ShouldNot(HaveKey("minio-password"))
540544

541545
transformer.rewriteSystemAccount(transCtx, shardingName, accountName, "revision")
542-
rewritten := transCtx.shardings[0].Template.SystemAccounts[0]
543-
Expect(rewritten.SecretRef.Name).Should(Equal(managed.Name))
544-
Expect(rewritten.SecretRef.Password).Should(BeEmpty())
546+
declared := transCtx.shardings[0].Template.SystemAccounts[0]
547+
Expect(declared.SecretRef.Name).Should(Equal("custom-password-key"))
548+
Expect(declared.SecretRef.Password).Should(Equal("minio-password"))
545549
for _, comp := range transCtx.shardingComps[shardingName] {
546550
Expect(comp.SystemAccounts).Should(HaveLen(1))
551+
Expect(comp.SystemAccounts[0].SecretRef.Name).Should(Equal(managed.Name))
547552
Expect(comp.SystemAccounts[0].SecretRef.Password).Should(BeEmpty())
548553
}
549554
})
@@ -743,7 +748,8 @@ var _ = Describe("cluster sharding shared transformers", func() {
743748
Expect(err).ShouldNot(HaveOccurred())
744749
Expect(managedRevision).Should(Equal(sourceSecretRevision(source, passwordKey)))
745750
Expect(managedRevision).ShouldNot(Equal("opaque-revision"))
746-
Expect(sharding.Template.SystemAccounts[0].SecretRefRevision).Should(Equal(managedRevision))
751+
Expect(sharding.Template.SystemAccounts[0].SecretRef.Name).Should(Equal("source-account"))
752+
Expect(sharding.Template.SystemAccounts[0].SecretRefRevision).Should(Equal("opaque-revision"))
747753
for _, comp := range transCtx.shardingComps[shardingName] {
748754
Expect(comp.SystemAccounts[0].SecretRefRevision).Should(Equal(managedRevision))
749755
}
@@ -784,9 +790,7 @@ var _ = Describe("cluster sharding shared transformers", func() {
784790
transCtx := newTransformContext(managed)
785791
transCtx.componentDefs = map[string]*appsv1.ComponentDefinition{compDefName: newComponentDefinition()}
786792
transCtx.shardings = []*appsv1.ClusterSharding{sharding}
787-
transCtx.shardingComps = map[string][]*appsv1.ClusterComponentSpec{
788-
shardingName: {{Name: "shard-0"}},
789-
}
793+
setShardingComps(transCtx, sharding)
790794
graphCli := model.NewGraphClient(transCtx.Client)
791795
transCtx.Client = graphCli
792796
dag := graph.NewDAG()
@@ -862,7 +866,7 @@ var _ = Describe("cluster sharding shared transformers", func() {
862866
Expect(graphCli.IsAction(dag, managed, model.ActionDeletePtr())).Should(BeTrue())
863867
})
864868

865-
It("rewrites shared system accounts on sharding and generated shard components", func() {
869+
It("rewrites shared system accounts only on generated shard components", func() {
866870
transformer := &clusterShardingAccountTransformer{}
867871
disabled := true
868872
sharding := newSharding()
@@ -871,35 +875,45 @@ var _ = Describe("cluster sharding shared transformers", func() {
871875
Name: accountName,
872876
Disabled: &disabled,
873877
SecretRef: &appsv1.ProvisionSecretRef{
878+
Name: "source-account",
874879
Password: "password-key",
875880
},
876881
},
877882
}
878883
transCtx := newTransformContext()
879884
transCtx.shardings = []*appsv1.ClusterSharding{sharding}
880-
transCtx.shardingComps = map[string][]*appsv1.ClusterComponentSpec{
881-
shardingName: {
882-
{Name: "shard-0"},
883-
{Name: "shard-1"},
884-
},
885-
}
885+
setShardingComps(transCtx, sharding)
886+
(&clusterNormalizationTransformer{}).writeBackCompNShardingSpecs(transCtx)
887+
Expect(transCtx.Cluster.Spec.Shardings).Should(HaveLen(1))
888+
Expect(transCtx.Cluster.Spec.Shardings[0].ShardingDef).Should(Equal("sharddef"))
886889

887890
transformer.rewriteSystemAccount(transCtx, shardingName, accountName, "revision")
888-
rewritten := transCtx.shardings[0].Template.SystemAccounts[0]
889-
Expect(ptr.Deref(rewritten.Disabled, false)).Should(BeTrue())
890-
Expect(rewritten.SecretRef.Name).Should(Equal(shardingAccountSecretName(clusterName, shardingName, accountName)))
891-
Expect(rewritten.SecretRef.Namespace).Should(Equal(namespace))
892-
Expect(rewritten.SecretRef.Password).Should(BeEmpty())
893-
Expect(rewritten.SecretRefRevision).Should(Equal("revision"))
891+
declared := transCtx.shardings[0].Template.SystemAccounts[0]
892+
Expect(ptr.Deref(declared.Disabled, false)).Should(BeTrue())
893+
Expect(declared.SecretRef.Name).Should(Equal("source-account"))
894+
Expect(declared.SecretRef.Password).Should(Equal("password-key"))
895+
Expect(declared.SecretRefRevision).Should(BeEmpty())
896+
persisted := transCtx.Cluster.Spec.Shardings[0].Template.SystemAccounts[0]
897+
Expect(persisted.SecretRef.Name).Should(Equal("source-account"))
898+
Expect(persisted.SecretRef.Password).Should(Equal("password-key"))
899+
Expect(persisted.SecretRefRevision).Should(BeEmpty())
894900
for _, comp := range transCtx.shardingComps[shardingName] {
895901
Expect(comp.SystemAccounts).Should(HaveLen(1))
896-
Expect(comp.SystemAccounts[0].SecretRef.Name).Should(Equal(rewritten.SecretRef.Name))
902+
Expect(ptr.Deref(comp.SystemAccounts[0].Disabled, false)).Should(BeTrue())
903+
Expect(comp.SystemAccounts[0].SecretRef.Name).
904+
Should(Equal(shardingAccountSecretName(clusterName, shardingName, accountName)))
905+
Expect(comp.SystemAccounts[0].SecretRef.Namespace).Should(Equal(namespace))
906+
Expect(comp.SystemAccounts[0].SecretRef.Password).Should(BeEmpty())
907+
Expect(comp.SystemAccounts[0].SecretRefRevision).Should(Equal("revision"))
897908
}
898909

899910
transformer.rewriteSystemAccount(transCtx, shardingName, "monitor", "monitor-revision")
900-
Expect(transCtx.shardings[0].Template.SystemAccounts).Should(HaveLen(2))
901-
Expect(transCtx.shardings[0].Template.SystemAccounts[1].Name).Should(Equal("monitor"))
902-
Expect(ptr.Deref(transCtx.shardings[0].Template.SystemAccounts[1].Disabled, true)).Should(BeFalse())
911+
Expect(transCtx.shardings[0].Template.SystemAccounts).Should(HaveLen(1))
912+
for _, comp := range transCtx.shardingComps[shardingName] {
913+
Expect(comp.SystemAccounts).Should(HaveLen(2))
914+
Expect(comp.SystemAccounts[1].Name).Should(Equal("monitor"))
915+
Expect(ptr.Deref(comp.SystemAccounts[1].Disabled, true)).Should(BeFalse())
916+
}
903917
})
904918

905919
It("reconciles only sharding definitions marked with shared system accounts", func() {
@@ -919,16 +933,10 @@ var _ = Describe("cluster sharding shared transformers", func() {
919933
transCtx.shardingDefs = map[string]*appsv1.ShardingDefinition{
920934
"sharddef": newShardingDefinition(),
921935
}
922-
transCtx.shardingComps = map[string][]*appsv1.ClusterComponentSpec{
923-
shardingName: {
924-
{Name: "shard-0"},
925-
},
926-
}
936+
setShardingComps(transCtx, sharding)
927937

928938
Expect(transformer.reconcileShardingAccounts(transCtx, nil, nil)).Should(Succeed())
929-
Expect(transCtx.shardings[0].Template.SystemAccounts).Should(HaveLen(1))
930-
Expect(transCtx.shardings[0].Template.SystemAccounts[0].SecretRef.Name).
931-
Should(Equal(shardingAccountSecretName(clusterName, shardingName, accountName)))
939+
Expect(transCtx.shardings[0].Template.SystemAccounts).Should(BeEmpty())
932940
Expect(transCtx.shardingComps[shardingName][0].SystemAccounts).Should(HaveLen(1))
933941
})
934942

0 commit comments

Comments
 (0)