Skip to content

Commit ad7a8d2

Browse files
author
Kumar Gaurav Sharma
committed
Extend ack-generate to generate slice with elements of type Kubernetes Secret
Related issue: aws-controllers-k8s/community#828 With this code change, when a field of type slice is configured as secret type inside Generator config, then the generated CRD yaml allows specifying multiple k8s secret values for that field in input yaml and generated sdk code supplies these values to the resource API input field as slice type.
1 parent cac5654 commit ad7a8d2

File tree

6 files changed

+96
-18
lines changed

6 files changed

+96
-18
lines changed

pkg/generate/code/set_sdk.go

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,13 @@ func SetSDK(
236236
sourceAdaptedVarName += "." + f.Names.Camel
237237
sourceFieldPath := f.Names.Camel
238238

239-
if r.IsSecretField(memberName) {
239+
memberShapeRef, _ := inputShape.MemberRefs[memberName]
240+
memberShape := memberShapeRef.Shape
241+
242+
if r.IsSecretField(memberName) &&
243+
memberShape.Type != "list" &&
244+
memberShape.Type != "structure" &&
245+
memberShape.Type != "map" {
240246
out += setSDKForSecret(
241247
cfg, r,
242248
memberName,
@@ -247,9 +253,6 @@ func SetSDK(
247253
continue
248254
}
249255

250-
memberShapeRef, _ := inputShape.MemberRefs[memberName]
251-
memberShape := memberShapeRef.Shape
252-
253256
// we construct variables containing temporary storage for sub-elements
254257
// and sub-fields that are structs. Names of fields are "f" appended by
255258
// the 0-based index of the field within the set of the target struct's
@@ -770,6 +773,16 @@ func setSDKForContainer(
770773
indentLevel,
771774
)
772775
default:
776+
if r.IsSecretField(sourceFieldPath) {
777+
return setSDKForSecret(
778+
cfg, r,
779+
"",
780+
targetVarName,
781+
sourceVarName,
782+
indentLevel,
783+
)
784+
}
785+
773786
return setSDKForScalar(
774787
cfg, r,
775788
targetFieldName,
@@ -833,10 +846,17 @@ func setSDKForSecret(
833846
// res.SetMasterUserPassword(tmpSecret)
834847
// }
835848
out += fmt.Sprintf("%s\tif tmpSecret != \"\" {\n", indent)
836-
out += fmt.Sprintf(
837-
"%s\t\t%s.Set%s(%s)\n",
838-
indent, targetVarName, targetFieldName, secVar,
839-
)
849+
if targetFieldName == "" {
850+
out += fmt.Sprintf(
851+
"%s\t\t%s = %s\n",
852+
indent, targetVarName, secVar,
853+
)
854+
} else {
855+
out += fmt.Sprintf(
856+
"%s\t\t%s.Set%s(%s)\n",
857+
indent, targetVarName, targetFieldName, secVar,
858+
)
859+
}
840860
out += fmt.Sprintf("%s\t}\n", indent)
841861
// }
842862
out += fmt.Sprintf("%s}\n", indent)
@@ -974,14 +994,16 @@ func setSDKForSlice(
974994
//
975995
// f0elem.SetMyField(*f0iter)
976996
containerFieldName := ""
997+
sourceAttributePath := sourceFieldPath
977998
if targetShape.MemberRef.Shape.Type == "structure" {
978999
containerFieldName = targetFieldName
1000+
sourceAttributePath = sourceFieldPath+"."
9791001
}
9801002
out += setSDKForContainer(
9811003
cfg, r,
9821004
containerFieldName,
9831005
elemVarName,
984-
sourceFieldPath+".",
1006+
sourceAttributePath,
9851007
iterVarName,
9861008
&targetShape.MemberRef,
9871009
indentLevel+1,

pkg/generate/code/set_sdk_test.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1109,6 +1109,49 @@ func TestSetSDK_Elasticache_ReplicationGroup_Update_Override_Values(t *testing.T
11091109
)
11101110
}
11111111

1112+
func TestSetSDK_Elasticache_User_Create_Override_Values(t *testing.T) {
1113+
assert := assert.New(t)
1114+
require := require.New(t)
1115+
1116+
g := testutil.NewGeneratorForService(t, "elasticache")
1117+
1118+
crd := testutil.GetCRDByName(t, g, "User")
1119+
require.NotNil(crd)
1120+
1121+
expected := `
1122+
if r.ko.Spec.AccessString != nil {
1123+
res.SetAccessString(*r.ko.Spec.AccessString)
1124+
}
1125+
if r.ko.Spec.NoPasswordRequired != nil {
1126+
res.SetNoPasswordRequired(*r.ko.Spec.NoPasswordRequired)
1127+
}
1128+
if r.ko.Spec.Passwords != nil {
1129+
f3 := []*string{}
1130+
for _, f3iter := range r.ko.Spec.Passwords {
1131+
var f3elem string
1132+
if f3iter != nil {
1133+
tmpSecret, err := rm.rr.SecretValueFromReference(ctx, f3iter)
1134+
if err != nil {
1135+
return nil, err
1136+
}
1137+
if tmpSecret != "" {
1138+
f3elem = tmpSecret
1139+
}
1140+
}
1141+
f3 = append(f3, &f3elem)
1142+
}
1143+
res.SetPasswords(f3)
1144+
}
1145+
if r.ko.Spec.UserID != nil {
1146+
res.SetUserId(*r.ko.Spec.UserID)
1147+
}
1148+
`
1149+
assert.Equal(
1150+
expected,
1151+
code.SetSDK(crd.Config(), crd, model.OpTypeUpdate, "r.ko", "res", 1),
1152+
)
1153+
}
1154+
11121155
func TestSetSDK_RDS_DBInstance_Create(t *testing.T) {
11131156
assert := assert.New(t)
11141157
require := require.New(t)

pkg/generate/elasticache_test.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,13 @@ func TestElasticache_ValidateAuthTokenIsSecret(t *testing.T) {
274274

275275
assert := assert.New(t)
276276
assert.Equal("*ackv1alpha1.SecretKeyReference", crd.SpecFields["AuthToken"].GoType)
277-
assert.Equal("ackv1alpha1.SecretKeyReference", crd.SpecFields["AuthToken"].GoTypeElem)
277+
assert.Equal("SecretKeyReference", crd.SpecFields["AuthToken"].GoTypeElem)
278278
assert.Equal("*ackv1alpha1.SecretKeyReference", crd.SpecFields["AuthToken"].GoTypeWithPkgName)
279+
280+
crd = getCRDByName("User", crds)
281+
require.NotNil(crd)
282+
283+
assert.Equal("[]*ackv1alpha1.SecretKeyReference", crd.SpecFields["Passwords"].GoType)
284+
assert.Equal("SecretKeyReference", crd.SpecFields["Passwords"].GoTypeElem)
285+
assert.Equal("[]*ackv1alpha1.SecretKeyReference", crd.SpecFields["Passwords"].GoTypeWithPkgName)
279286
}

pkg/generate/testdata/models/apis/elasticache/0000-00-00/generator.yaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ resources:
1818
from:
1919
operation: DescribeEvents
2020
path: Events
21+
User:
22+
fields:
23+
Passwords:
24+
is_secret: true
2125
ReplicationGroup:
2226
update_conditions_custom_method_name: CustomUpdateConditions
2327
exceptions:
@@ -126,7 +130,6 @@ ignore:
126130
- GlobalReplicationGroup
127131
- CacheCluster
128132
- CacheSecurityGroup
129-
- User
130133
- UserGroup
131134
field_paths:
132135
- DescribeSnapshotsInput.CacheClusterId

pkg/model/field.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,8 @@ func NewField(
9595
shape = shapeRef.Shape
9696
}
9797

98-
if cfg != nil && cfg.IsSecret {
99-
gt = "*ackv1alpha1.SecretKeyReference"
100-
gte = "ackv1alpha1.SecretKeyReference"
101-
gtwp = "*ackv1alpha1.SecretKeyReference"
102-
} else if shape != nil {
103-
gte, gt, gtwp = cleanGoType(crd.sdkAPI, crd.cfg, shape)
98+
if shape != nil {
99+
gte, gt, gtwp = cleanGoType(crd.sdkAPI, crd.cfg, shape, cfg)
104100
} else {
105101
gte = "string"
106102
gt = "*string"

pkg/model/types.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ func cleanGoType(
2929
api *SDKAPI,
3030
cfg *ackgenconfig.Config,
3131
shape *awssdkmodel.Shape,
32+
fieldCfg *ackgenconfig.FieldConfig,
3233
) (string, string, string) {
3334
// There are shapes that are called things like DBProxyStatus that are
3435
// fields in a DBProxy CRD... we need to ensure the type names don't
@@ -48,20 +49,26 @@ func cleanGoType(
4849
} else if shape.Type == "list" {
4950
// If it's a list type, where the element is a structure, we need to
5051
// set the GoType to the cleaned-up Camel-cased name
51-
mgte, mgt, _ := cleanGoType(api, cfg, shape.MemberRef.Shape)
52+
mgte, mgt, mgtwp := cleanGoType(api, cfg, shape.MemberRef.Shape, fieldCfg)
5253
cleanNames := names.New(mgte)
5354
gte = cleanNames.Camel
5455
if api.HasConflictingTypeName(mgte, cfg) {
5556
gte += "_SDK"
5657
}
5758

5859
gt = "[]" + mgt
60+
gtwp = "[]" + mgtwp
5961
} else if shape.Type == "timestamp" {
6062
// time.Time needs to be converted to apimachinery/metav1.Time
6163
// otherwise there is no DeepCopy support
6264
gtwp = "*metav1.Time"
6365
gte = "metav1.Time"
6466
gt = "*metav1.Time"
67+
} else if fieldCfg != nil && fieldCfg.IsSecret {
68+
gt = "*ackv1alpha1.SecretKeyReference"
69+
gte = "SecretKeyReference"
70+
gtwp = "*ackv1alpha1.SecretKeyReference"
71+
return gte, gt, gtwp
6572
}
6673

6774
// Replace the type part of the full type-with-package-name with the

0 commit comments

Comments
 (0)