Skip to content

Commit 06e0102

Browse files
committed
encapsulate SetSDK for Secret fields
Encapsulates the code that generates Go code for setting an SDK Input shape from the value of a SecretKeyReference in a separate `setSDKForSecret` function in `pkg/generate/code` and creates a `CRD.IsSecretField` that accepts a field path and returns whether the field at that field path is a SecretKeyReference.
1 parent e8be3ca commit 06e0102

File tree

2 files changed

+82
-17
lines changed

2 files changed

+82
-17
lines changed

pkg/generate/code/set_sdk.go

Lines changed: 71 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,6 @@ func SetSDK(
165165
}
166166

167167
opConfig, override := cfg.OverrideValues(op.Name)
168-
fieldConfigs := cfg.ResourceFields(r.Names.Original)
169168
for memberIndex, memberName := range inputShape.MemberNames() {
170169
if r.UnpacksAttributesMap() && memberName == "Attributes" {
171170
continue
@@ -190,22 +189,6 @@ func SetSDK(
190189
}
191190
}
192191

193-
fc, ok := fieldConfigs[memberName]
194-
if ok && fc.IsSecret {
195-
out += fmt.Sprintf("%sif %s.Spec.%s != nil {\n", indent, sourceVarName, memberName)
196-
out += fmt.Sprintf("%s%stmpSecret, err := rm.rr.SecretValueFromReference(ctx, %s.Spec.%s)\n", indent,
197-
indent, sourceVarName, memberName)
198-
out += fmt.Sprintf("%s%sif err != nil {\n", indent, indent)
199-
out += fmt.Sprintf("%s%s%sreturn nil, err\n", indent, indent, indent)
200-
out += fmt.Sprintf("%s%s}\n", indent, indent)
201-
out += fmt.Sprintf("%s%sif tmpSecret != \"\" {\n", indent, indent)
202-
out += fmt.Sprintf("%s%s%s%s.Set%s(%s)\n", indent, indent, indent,
203-
targetVarName, memberName, "tmpSecret")
204-
out += fmt.Sprintf("%s%s}\n", indent, indent)
205-
out += fmt.Sprintf("%s}\n", indent)
206-
continue
207-
}
208-
209192
if r.IsPrimaryARNField(memberName) {
210193
// if ko.Status.ACKResourceMetadata != nil && ko.Status.ACKResourceMetadata.ARN != nil {
211194
// res.SetTopicArn(string(*ko.Status.ACKResourceMetadata.ARN))
@@ -252,6 +235,17 @@ func SetSDK(
252235
}
253236
sourceAdaptedVarName += "." + f.Names.Camel
254237

238+
if r.IsSecretField(memberName) {
239+
out += setSDKForSecret(
240+
cfg, r,
241+
memberName,
242+
targetVarName,
243+
sourceAdaptedVarName,
244+
indentLevel,
245+
)
246+
continue
247+
}
248+
255249
memberShapeRef, _ := inputShape.MemberRefs[memberName]
256250
memberShape := memberShapeRef.Shape
257251

@@ -777,6 +771,66 @@ func setSDKForContainer(
777771
}
778772
}
779773

774+
// setSDKForSecret returns a string of Go code that sets a target variable to
775+
// the value of a Secret when the type of the source variable is a
776+
// SecretKeyReference.
777+
//
778+
// The Go code output from this function looks like this:
779+
//
780+
// if ko.Spec.MasterUserPassword != nil {
781+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, ko.Spec.MasterUserPassword)
782+
// if err != nil {
783+
// return nil, err
784+
// }
785+
// if tmpSecret != "" {
786+
// res.SetMasterUserPassword(tmpSecret)
787+
// }
788+
// }
789+
func setSDKForSecret(
790+
cfg *ackgenconfig.Config,
791+
r *model.CRD,
792+
// The name of the SDK Shape field we're setting
793+
targetFieldName string,
794+
// The variable name that we want to set a value on
795+
targetVarName string,
796+
// The CR field that we access our source value from
797+
sourceVarName string,
798+
indentLevel int,
799+
) string {
800+
out := ""
801+
indent := strings.Repeat("\t", indentLevel)
802+
secVar := "tmpSecret"
803+
804+
// if ko.Spec.MasterUserPassword != nil {
805+
out += fmt.Sprintf(
806+
"%sif %s != nil {\n",
807+
indent, sourceVarName,
808+
)
809+
// tmpSecret, err := rm.rr.SecretValueFromReference(ctx, ko.Spec.MasterUserPassword)
810+
out += fmt.Sprintf(
811+
"%s\t%s, err := rm.rr.SecretValueFromReference(ctx, %s)\n",
812+
indent, secVar, sourceVarName,
813+
)
814+
// if err != nil {
815+
// return nil, err
816+
// }
817+
out += fmt.Sprintf("%s\tif err != nil {\n", indent)
818+
out += fmt.Sprintf("%s\t\treturn nil, err\n", indent)
819+
out += fmt.Sprintf("%s\t}\n", indent)
820+
// if tmpSecret != "" {
821+
// res.SetMasterUserPassword(tmpSecret)
822+
// }
823+
out += fmt.Sprintf("%s\tif tmpSecret != \"\" {\n", indent)
824+
out += fmt.Sprintf(
825+
"%s\t\t%s.Set%s(%s)\n",
826+
indent, targetVarName, targetFieldName, secVar,
827+
)
828+
out += fmt.Sprintf("%s\t}\n", indent)
829+
// }
830+
out += fmt.Sprintf("%s}\n", indent)
831+
return out
832+
}
833+
780834
// setSDKForStruct returns a string of Go code that sets a target variable
781835
// value to a source variable when the type of the source variable is a struct.
782836
func setSDKForStruct(

pkg/model/crd.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,17 @@ func (r *CRD) IsPrimaryARNField(fieldName string) bool {
292292
strings.EqualFold(fieldName, r.Names.Original+"arn")
293293
}
294294

295+
// IsSecretField returns true if the supplied field *path* refers to a Field
296+
// that is a SecretKeyReference
297+
func (r *CRD) IsSecretField(path string) bool {
298+
fConfigs := r.cfg.ResourceFields(r.Names.Original)
299+
fConfig, found := fConfigs[path]
300+
if found {
301+
return fConfig.IsSecret
302+
}
303+
return false
304+
}
305+
295306
// SetOutputCustomMethodName returns custom set output operation as *string for
296307
// given operation on custom resource, if specified in generator config
297308
func (r *CRD) SetOutputCustomMethodName(

0 commit comments

Comments
 (0)