@@ -1185,6 +1185,115 @@ func TestValidatePatches(t *testing.T) {
1185
1185
},
1186
1186
wantErr : true ,
1187
1187
},
1188
+ {
1189
+ name : "pass if jsonPatch uses a user-defined variable which is defined" ,
1190
+ clusterClass : clusterv1.ClusterClass {
1191
+ Spec : clusterv1.ClusterClassSpec {
1192
+ ControlPlane : clusterv1.ControlPlaneClass {
1193
+ LocalObjectTemplate : clusterv1.LocalObjectTemplate {
1194
+ Ref : & corev1.ObjectReference {
1195
+ APIVersion : "controlplane.cluster.x-k8s.io/v1beta1" ,
1196
+ Kind : "ControlPlaneTemplate" ,
1197
+ },
1198
+ },
1199
+ },
1200
+ Patches : []clusterv1.ClusterClassPatch {
1201
+ {
1202
+ Name : "patch1" ,
1203
+ Definitions : []clusterv1.PatchDefinition {
1204
+ {
1205
+ Selector : clusterv1.PatchSelector {
1206
+ APIVersion : "controlplane.cluster.x-k8s.io/v1beta1" ,
1207
+ Kind : "ControlPlaneTemplate" ,
1208
+ MatchResources : clusterv1.PatchSelectorMatch {
1209
+ ControlPlane : true ,
1210
+ },
1211
+ },
1212
+ JSONPatches : []clusterv1.JSONPatch {
1213
+ {
1214
+ Op : "add" ,
1215
+ Path : "/spec/template/spec/" ,
1216
+ ValueFrom : & clusterv1.JSONPatchValue {
1217
+ Variable : pointer .String ("variableName" ),
1218
+ },
1219
+ },
1220
+ },
1221
+ },
1222
+ },
1223
+ },
1224
+ },
1225
+ Variables : []clusterv1.ClusterClassVariable {
1226
+ {
1227
+ Name : "variableName" ,
1228
+ Required : true ,
1229
+ Schema : clusterv1.VariableSchema {
1230
+ OpenAPIV3Schema : clusterv1.JSONSchemaProps {
1231
+ Type : "string" ,
1232
+ },
1233
+ },
1234
+ },
1235
+ },
1236
+ },
1237
+ },
1238
+ wantErr : false ,
1239
+ },
1240
+ {
1241
+ name : "pass if jsonPatch uses a nested user-defined variable which is defined" ,
1242
+ clusterClass : clusterv1.ClusterClass {
1243
+ Spec : clusterv1.ClusterClassSpec {
1244
+ ControlPlane : clusterv1.ControlPlaneClass {
1245
+ LocalObjectTemplate : clusterv1.LocalObjectTemplate {
1246
+ Ref : & corev1.ObjectReference {
1247
+ APIVersion : "controlplane.cluster.x-k8s.io/v1beta1" ,
1248
+ Kind : "ControlPlaneTemplate" ,
1249
+ },
1250
+ },
1251
+ },
1252
+ Patches : []clusterv1.ClusterClassPatch {
1253
+ {
1254
+ Name : "patch1" ,
1255
+ Definitions : []clusterv1.PatchDefinition {
1256
+ {
1257
+ Selector : clusterv1.PatchSelector {
1258
+ APIVersion : "controlplane.cluster.x-k8s.io/v1beta1" ,
1259
+ Kind : "ControlPlaneTemplate" ,
1260
+ MatchResources : clusterv1.PatchSelectorMatch {
1261
+ ControlPlane : true ,
1262
+ },
1263
+ },
1264
+ JSONPatches : []clusterv1.JSONPatch {
1265
+ {
1266
+ Op : "add" ,
1267
+ Path : "/spec/template/spec/" ,
1268
+ ValueFrom : & clusterv1.JSONPatchValue {
1269
+ Variable : pointer .String ("variableName.nestedField" ),
1270
+ },
1271
+ },
1272
+ },
1273
+ },
1274
+ },
1275
+ },
1276
+ },
1277
+ Variables : []clusterv1.ClusterClassVariable {
1278
+ {
1279
+ Name : "variableName" ,
1280
+ Required : true ,
1281
+ Schema : clusterv1.VariableSchema {
1282
+ OpenAPIV3Schema : clusterv1.JSONSchemaProps {
1283
+ Type : "object" ,
1284
+ Properties : map [string ]clusterv1.JSONSchemaProps {
1285
+ "nestedField" : {
1286
+ Type : "string" ,
1287
+ },
1288
+ },
1289
+ },
1290
+ },
1291
+ },
1292
+ },
1293
+ },
1294
+ },
1295
+ wantErr : false ,
1296
+ },
1188
1297
{
1189
1298
name : "error if jsonPatch uses a builtin variable which is not defined" ,
1190
1299
clusterClass : clusterv1.ClusterClass {
@@ -1628,3 +1737,35 @@ func Test_validateSelectors(t *testing.T) {
1628
1737
})
1629
1738
}
1630
1739
}
1740
+
1741
+ func TestGetVariableName (t * testing.T ) {
1742
+ tests := []struct {
1743
+ name string
1744
+ variable string
1745
+ variableName string
1746
+ }{
1747
+ {
1748
+ name : "simple variable" ,
1749
+ variable : "variableA" ,
1750
+ variableName : "variableA" ,
1751
+ },
1752
+ {
1753
+ name : "variable object" ,
1754
+ variable : "variableObject.field" ,
1755
+ variableName : "variableObject" ,
1756
+ },
1757
+ {
1758
+ name : "variable array" ,
1759
+ variable : "variableArray[0]" ,
1760
+ variableName : "variableArray" ,
1761
+ },
1762
+ }
1763
+
1764
+ for _ , tt := range tests {
1765
+ t .Run (tt .name , func (t * testing.T ) {
1766
+ g := NewWithT (t )
1767
+
1768
+ g .Expect (getVariableName (tt .variable )).To (Equal (tt .variableName ))
1769
+ })
1770
+ }
1771
+ }
0 commit comments