Skip to content

Commit 6c7b652

Browse files
committed
feat: Pass owning resource to SecretValueFromReference for cross-namespace notice
The runtime's SecretValueFromReference now takes the owning resource as an explicit acktypes.ConditionManager argument so it can set the cross-namespace deprecation ACK.Advisory condition on it (runtime mechanism 1b, replacing the context-stash). Update the generated SetSDK code to pass the resource handle: rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.MasterUserPassword) The resource variable is threaded through setSDKForContainer / SetSDKForStruct / setSDKForSlice / setSDKForMap / setSDKForUnion so secrets nested in collections still pass the top-level resource (not a loop iterator); at entry points it is derived from the leading token of the source variable path. The generated *resource type implements acktypes.ConditionManager. Companion to the runtime change; builds on #699.
1 parent f0dc0d8 commit 6c7b652

3 files changed

Lines changed: 86 additions & 20 deletions

File tree

pkg/generate/ack/controller.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ var (
124124
return code.SetSDK(r.Config(), r, ackmodel.OpTypeDelete, sourceVarName, targetVarName, indentLevel)
125125
},
126126
"GoCodeSetSDKForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceFieldPath string, sourceVarName string, indentLevel int) (string, error) {
127-
return code.SetSDKForStruct(r.Config(), r, targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, model.OpTypeList, indentLevel)
127+
// Empty resourceVarName: SetSDKForStruct derives it from the
128+
// leading token of sourceVarName when invoked as an entry point.
129+
return code.SetSDKForStruct(r.Config(), r, "", targetFieldName, targetVarName, targetShapeRef, sourceFieldPath, sourceVarName, model.OpTypeList, indentLevel)
128130
},
129131
"GoCodeSetResourceForStruct": func(r *ackmodel.CRD, targetFieldName string, targetVarName string, targetShapeRef *awssdkmodel.ShapeRef, sourceVarName string, sourceShapeRef *awssdkmodel.ShapeRef, indentLevel int) (string, error) {
130132
var setCfg *ackgenconfig.SetFieldConfig = nil

pkg/generate/code/set_sdk.go

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,16 @@ func SetSDK(
119119
out := "\n"
120120
indent := strings.Repeat("\t", indentLevel)
121121

122+
// The owning resource variable, used so secret fields can pass it to the
123+
// runtime's SecretValueFromReference (which sets the cross-namespace
124+
// deprecation ACK.Advisory condition on it). It is the leading token of
125+
// the source variable path (e.g. "r" from "r.ko"); the generated
126+
// *resource type implements acktypes.ConditionManager.
127+
resourceVarName := sourceVarName
128+
if dot := strings.IndexByte(resourceVarName, '.'); dot != -1 {
129+
resourceVarName = resourceVarName[:dot]
130+
}
131+
122132
// Check if there's an input wrapper field path configured. If so, we need
123133
// to create a wrapper struct and populate its fields from the CRD spec,
124134
// then assign the wrapper to the input shape's wrapper field.
@@ -402,6 +412,7 @@ func SetSDK(
402412
)
403413
containerOut, err := setSDKForContainer(
404414
cfg, r,
415+
resourceVarName,
405416
memberName,
406417
memberVarName,
407418
sourceFieldPath,
@@ -430,6 +441,7 @@ func SetSDK(
430441
if r.IsSecretField(memberName) {
431442
out += setSDKForSecret(
432443
cfg, r,
444+
resourceVarName,
433445
memberName,
434446
targetVarName,
435447
sourceAdaptedVarName,
@@ -1005,6 +1017,10 @@ func setSDKReadMany(
10051017
func setSDKForContainer(
10061018
cfg *ackgenconfig.Config,
10071019
r *model.CRD,
1020+
// The name of the variable holding the owning resource (a *resource,
1021+
// which implements acktypes.ConditionManager). Threaded down so secret
1022+
// fields nested in collections can pass it to SecretValueFromReference.
1023+
resourceVarName string,
10081024
// The name of the SDK Input shape member we're outputting for
10091025
targetFieldName string,
10101026
// The variable name that we want to set a value to
@@ -1023,6 +1039,7 @@ func setSDKForContainer(
10231039
case "structure":
10241040
return SetSDKForStruct(
10251041
cfg, r,
1042+
resourceVarName,
10261043
targetFieldName,
10271044
targetVarName,
10281045
targetShapeRef,
@@ -1034,6 +1051,7 @@ func setSDKForContainer(
10341051
case "list":
10351052
return setSDKForSlice(
10361053
cfg, r,
1054+
resourceVarName,
10371055
targetFieldName,
10381056
targetVarName,
10391057
targetShapeRef,
@@ -1045,6 +1063,7 @@ func setSDKForContainer(
10451063
case "map":
10461064
return setSDKForMap(
10471065
cfg, r,
1066+
resourceVarName,
10481067
targetFieldName,
10491068
targetVarName,
10501069
targetShapeRef,
@@ -1056,6 +1075,7 @@ func setSDKForContainer(
10561075
case "union":
10571076
return setSDKForUnion(
10581077
cfg, r,
1078+
resourceVarName,
10591079
targetFieldName,
10601080
targetVarName,
10611081
targetShapeRef,
@@ -1074,6 +1094,7 @@ func setSDKForContainer(
10741094
)
10751095
out += setSDKForSecret(
10761096
cfg, r,
1097+
resourceVarName,
10771098
"",
10781099
targetVarName,
10791100
sourceVarName,
@@ -1101,14 +1122,17 @@ func setSDKForContainer(
11011122
// the value of a Secret when the type of the source variable is a
11021123
// SecretKeyReference.
11031124
//
1104-
// Cross-namespace validation (and the Phase 1 deprecation warning) is
1105-
// performed inside the runtime's SecretValueFromReference, so it is not
1106-
// emitted here. This ensures every caller is covered, including custom
1107-
// update functions and hooks that call SecretValueFromReference directly.
1125+
// Cross-namespace validation (and the Phase 1 deprecation notice) is performed
1126+
// inside the runtime's SecretValueFromReference, so it is not emitted here.
1127+
// This ensures every caller is covered, including custom update functions and
1128+
// hooks that call SecretValueFromReference directly. The owning resource is
1129+
// passed as the second argument so the runtime can set the cross-namespace
1130+
// deprecation ACK.Advisory condition on it; it is derived from the source
1131+
// variable (e.g. "r" from "r.ko.Spec.MasterUserPassword").
11081132
//
11091133
// The Go code output from this function looks like this:
11101134
//
1111-
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, ko.Spec.MasterUserPassword)
1135+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.MasterUserPassword)
11121136
// if err != nil {
11131137
// return nil, ackrequeue.Needed(err)
11141138
// }
@@ -1122,6 +1146,11 @@ func setSDKForContainer(
11221146
func setSDKForSecret(
11231147
cfg *ackgenconfig.Config,
11241148
r *model.CRD,
1149+
// The name of the variable holding the owning resource (a
1150+
// *resource, which implements acktypes.ConditionManager). It is passed to
1151+
// SecretValueFromReference so the runtime can set the cross-namespace
1152+
// deprecation ACK.Advisory condition on it. Typically "r" or "desired".
1153+
resourceVarName string,
11251154
// The name of the SDK Shape field we're setting
11261155
targetFieldName string,
11271156
// The variable name that we want to set a value on
@@ -1138,12 +1167,14 @@ func setSDKForSecret(
11381167
// Cross-namespace validation for the secret reference is performed inside
11391168
// the runtime's SecretValueFromReference, so that every call site is
11401169
// covered (including custom update functions and hooks). No per-call
1141-
// validation is generated here.
1170+
// validation is generated here. The owning resource is passed so the
1171+
// runtime can set the cross-namespace deprecation ACK.Advisory condition
1172+
// on it.
11421173

1143-
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, ko.Spec.MasterUserPassword)
1174+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.MasterUserPassword)
11441175
out += fmt.Sprintf(
1145-
"%s\t%s, err := rm.rr.SecretValueFromReference(ctx, %s)\n",
1146-
indent, secVar, sourceVarName,
1176+
"%s\t%s, err := rm.rr.SecretValueFromReference(ctx, %s, %s)\n",
1177+
indent, secVar, resourceVarName, sourceVarName,
11471178
)
11481179
// if err != nil {
11491180
// return nil, ackrequeue.Needed(err)
@@ -1175,6 +1206,12 @@ func setSDKForSecret(
11751206
func SetSDKForStruct(
11761207
cfg *ackgenconfig.Config,
11771208
r *model.CRD,
1209+
// The name of the variable holding the owning resource (a *resource,
1210+
// which implements acktypes.ConditionManager). Threaded down so secret
1211+
// fields nested in this struct can pass it to SecretValueFromReference.
1212+
// When this is the entry point (e.g. the GoCodeSetSDKForStruct template
1213+
// helper), it is derived from the leading token of sourceVarName.
1214+
resourceVarName string,
11781215
// The name of the CR field we're outputting for
11791216
targetFieldName string,
11801217
// The variable name that we want to set a value to
@@ -1191,6 +1228,16 @@ func SetSDKForStruct(
11911228
out := ""
11921229
indent := strings.Repeat("\t", indentLevel)
11931230
targetShape := targetShapeRef.Shape
1231+
// When SetSDKForStruct is invoked as a top-level entry point (rather than
1232+
// recursively via setSDKForContainer), resourceVarName is not supplied by
1233+
// the caller. Fall back to the leading token of the source variable path
1234+
// (e.g. "r" from "r.ko.Spec.Foo"), which names the owning *resource.
1235+
if resourceVarName == "" {
1236+
resourceVarName = sourceVarName
1237+
if dot := strings.IndexByte(resourceVarName, '.'); dot != -1 {
1238+
resourceVarName = resourceVarName[:dot]
1239+
}
1240+
}
11941241

11951242
for memberIndex, memberName := range targetShape.MemberNames() {
11961243
memberShapeRef := targetShape.MemberRefs[memberName]
@@ -1248,6 +1295,7 @@ func SetSDKForStruct(
12481295
)
12491296
containerOut, err := setSDKForContainer(
12501297
cfg, r,
1298+
resourceVarName,
12511299
memberName,
12521300
memberVarName,
12531301
memberFieldPath,
@@ -1276,6 +1324,7 @@ func SetSDKForStruct(
12761324
if r.IsSecretField(memberFieldPath) {
12771325
out += setSDKForSecret(
12781326
cfg, r,
1327+
resourceVarName,
12791328
memberName,
12801329
targetVarName,
12811330
sourceAdaptedVarName,
@@ -1310,6 +1359,10 @@ func SetSDKForStruct(
13101359
func setSDKForSlice(
13111360
cfg *ackgenconfig.Config,
13121361
r *model.CRD,
1362+
// The name of the variable holding the owning resource (a *resource,
1363+
// which implements acktypes.ConditionManager). Threaded down so secret
1364+
// fields nested in this slice can pass it to SecretValueFromReference.
1365+
resourceVarName string,
13131366
// The name of the CR field we're outputting for
13141367
targetFieldName string,
13151368
// The variable name that we want to set a value to
@@ -1382,6 +1435,7 @@ func setSDKForSlice(
13821435
} else {
13831436
containerOut, err := setSDKForContainer(
13841437
cfg, r,
1438+
resourceVarName,
13851439
containerFieldName,
13861440
elemVarName,
13871441
sourceFieldPath,
@@ -1414,6 +1468,10 @@ func setSDKForSlice(
14141468
func setSDKForMap(
14151469
cfg *ackgenconfig.Config,
14161470
r *model.CRD,
1471+
// The name of the variable holding the owning resource (a *resource,
1472+
// which implements acktypes.ConditionManager). Threaded down so secret
1473+
// fields nested in this map can pass it to SecretValueFromReference.
1474+
resourceVarName string,
14171475
// The name of the CR field we're outputting for
14181476
targetFieldName string,
14191477
// The variable name that we want to set a value to
@@ -1481,6 +1539,7 @@ func setSDKForMap(
14811539
} else {
14821540
containerOut, err := setSDKForContainer(
14831541
cfg, r,
1542+
resourceVarName,
14841543
containerFieldName,
14851544
valVarName,
14861545
sourceFieldPath,
@@ -1844,6 +1903,10 @@ func resolveAWSMapValueType(valueType string) string {
18441903
func setSDKForUnion(
18451904
cfg *ackgenconfig.Config,
18461905
r *model.CRD,
1906+
// The name of the variable holding the owning resource (a *resource,
1907+
// which implements acktypes.ConditionManager). Threaded down so secret
1908+
// fields nested in this union can pass it to SecretValueFromReference.
1909+
resourceVarName string,
18471910
// The name of the CR field we're outputting for
18481911
targetFieldName string,
18491912
// The variable name that we want to set a value to
@@ -1929,6 +1992,7 @@ func setSDKForUnion(
19291992
)
19301993
containerOut, err := setSDKForContainer(
19311994
cfg, r,
1995+
resourceVarName,
19321996
memberName,
19331997
indexedVarName,
19341998
memberFieldPath,

pkg/generate/code/set_sdk_test.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func TestSetSDK_MemoryDB_User_Create(t *testing.T) {
105105
for _, f1f0iter := range r.ko.Spec.AuthenticationMode.Passwords {
106106
var f1f0elem string
107107
if f1f0iter != nil {
108-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f1f0iter)
108+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, f1f0iter)
109109
if err != nil {
110110
return nil, ackrequeue.Needed(err)
111111
}
@@ -208,7 +208,7 @@ func TestSetSDK_OpenSearch_Domain_Create(t *testing.T) {
208208
f3f4.MasterUserName = r.ko.Spec.AdvancedSecurityOptions.MasterUserOptions.MasterUserName
209209
}
210210
if r.ko.Spec.AdvancedSecurityOptions.MasterUserOptions.MasterUserPassword != nil {
211-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r.ko.Spec.AdvancedSecurityOptions.MasterUserOptions.MasterUserPassword)
211+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.AdvancedSecurityOptions.MasterUserOptions.MasterUserPassword)
212212
if err != nil {
213213
return nil, ackrequeue.Needed(err)
214214
}
@@ -841,7 +841,7 @@ func TestSetSDK_ECR_Repository_Create(t *testing.T) {
841841
// res.SetAtRestEncryptionEnabled(*r.ko.Spec.AtRestEncryptionEnabled)
842842
// }
843843
// if r.ko.Spec.AuthToken != nil {
844-
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r.ko.Spec.AuthToken)
844+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.AuthToken)
845845
// if err != nil {
846846
// return nil, ackrequeue.Needed(err)
847847
// }
@@ -1096,7 +1096,7 @@ func TestSetSDK_ECR_Repository_Create(t *testing.T) {
10961096
// expected := `
10971097
// res.SetApplyImmediately(true)
10981098
// if r.ko.Spec.AuthToken != nil {
1099-
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r.ko.Spec.AuthToken)
1099+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.AuthToken)
11001100
// if err != nil {
11011101
// return nil, ackrequeue.Needed(err)
11021102
// }
@@ -1218,7 +1218,7 @@ func TestSetSDK_ECR_Repository_Create(t *testing.T) {
12181218
// for _, f3iter := range r.ko.Spec.Passwords {
12191219
// var f3elem string
12201220
// if f3iter != nil {
1221-
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f3iter)
1221+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, f3iter)
12221222
// if err != nil {
12231223
// return nil, ackrequeue.Needed(err)
12241224
// }
@@ -1489,7 +1489,7 @@ func TestSetSDK_RDS_DBInstance_Create(t *testing.T) {
14891489
res.ManageMasterUserPassword = r.ko.Spec.ManageMasterUserPassword
14901490
}
14911491
if r.ko.Spec.MasterUserPassword != nil {
1492-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r.ko.Spec.MasterUserPassword)
1492+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.MasterUserPassword)
14931493
if err != nil {
14941494
return nil, ackrequeue.Needed(err)
14951495
}
@@ -1779,7 +1779,7 @@ func TestSetSDK_RDS_DBInstance_Update(t *testing.T) {
17791779
}
17801780
if delta.DifferentAt("Spec.MasterUserPassword") {
17811781
if r.ko.Spec.MasterUserPassword != nil {
1782-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r.ko.Spec.MasterUserPassword)
1782+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, r.ko.Spec.MasterUserPassword)
17831783
if err != nil {
17841784
return nil, ackrequeue.Needed(err)
17851785
}
@@ -2318,7 +2318,7 @@ func TestSetSDK_MQ_Broker_Create(t *testing.T) {
23182318
f18elem.Groups = aws.ToStringSlice(f18iter.Groups)
23192319
}
23202320
if f18iter.Password != nil {
2321-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f18iter.Password)
2321+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, f18iter.Password)
23222322
if err != nil {
23232323
return nil, ackrequeue.Needed(err)
23242324
}
@@ -4669,7 +4669,7 @@ func TestSetSDK_Lambda_Function_EnvironmentVariable_MapOfSecrets_Create(t *testi
46694669
for f4f0key, f4f0valiter := range r.ko.Spec.Environment.Variables {
46704670
var f4f0val string
46714671
if f4f0valiter != nil {
4672-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f4f0valiter)
4672+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, f4f0valiter)
46734673
if err != nil {
46744674
return nil, ackrequeue.Needed(err)
46754675
}
@@ -4806,7 +4806,7 @@ func TestSetSDK_Lambda_Function_EnvironmentVariable_MapOfSecrets_Update(t *testi
48064806
for f2f0key, f2f0valiter := range r.ko.Spec.Environment.Variables {
48074807
var f2f0val string
48084808
if f2f0valiter != nil {
4809-
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f2f0valiter)
4809+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, r, f2f0valiter)
48104810
if err != nil {
48114811
return nil, ackrequeue.Needed(err)
48124812
}

0 commit comments

Comments
 (0)