Skip to content

Commit 70ebe3e

Browse files
feat: allow RGD resources to reference instance status fields
feat: allow RGD resources to reference instance status fields
1 parent 548e4db commit 70ebe3e

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

pkg/graph/builder.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -202,16 +202,14 @@ func (b *Builder) NewResourceGraphDefinition(originalCR *v1alpha1.ResourceGraphD
202202
}
203203
}
204204

205-
// include the instance spec schema in the context as "schema". This will let us
206-
// validate expressions such as ${schema.spec.someField}.
207-
//
208-
// not that we only include the spec and metadata fields, instance status references
209-
// are not allowed in RGDs (yet)
210-
schemaWithoutStatus, err := getSchemaWithoutStatus(instance.crd)
205+
// include the instance schema in the context as "schema". This will let us
206+
// validate expressions such as ${schema.spec.someField}, ${schema.metadata.name},
207+
// and ${schema.status.someField}.
208+
instanceSchema, err := getInstanceSchema(instance.crd)
211209
if err != nil {
212-
return nil, fmt.Errorf("failed to get schema without status: %w", err)
210+
return nil, fmt.Errorf("failed to get instance schema: %w", err)
213211
}
214-
schemas["schema"] = schemaWithoutStatus
212+
schemas["schema"] = instanceSchema
215213

216214
// First, build the dependency graph by inspecting CEL expressions.
217215
// This extracts all resource dependencies and validates that:
@@ -831,8 +829,10 @@ func validateReadyWhenExpressions(env *cel.Env, resource *Resource) error {
831829
return nil
832830
}
833831

834-
// getSchemaWithoutStatus returns a schema from the CRD with the status field removed.
835-
func getSchemaWithoutStatus(crd *extv1.CustomResourceDefinition) (*spec.Schema, error) {
832+
// getInstanceSchema returns a schema from the CRD including spec, status, and metadata fields.
833+
// This schema is used for CEL expression validation, allowing references to instance fields like
834+
// ${schema.spec.field}, ${schema.status.field}, and ${schema.metadata.name}.
835+
func getInstanceSchema(crd *extv1.CustomResourceDefinition) (*spec.Schema, error) {
836836
crdCopy := crd.DeepCopy()
837837

838838
// TODO(a-hilaly) expand this function when we start support CRD upgrades.
@@ -846,8 +846,6 @@ func getSchemaWithoutStatus(crd *extv1.CustomResourceDefinition) (*spec.Schema,
846846
openAPISchema.Properties = make(map[string]extv1.JSONSchemaProps)
847847
}
848848

849-
delete(openAPISchema.Properties, "status")
850-
851849
specSchema, err := schema.ConvertJSONSchemaPropsToSpecSchema(openAPISchema)
852850
if err != nil {
853851
return nil, err

pkg/graph/builder_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1766,6 +1766,42 @@ func TestGraphBuilder_CELTypeChecking(t *testing.T) {
17661766
},
17671767
wantErr: false,
17681768
},
1769+
{
1770+
name: "valid schema.status field reference",
1771+
resourceGraphDefinitionOpts: []generator.ResourceGraphDefinitionOption{
1772+
generator.WithSchema(
1773+
"Application", "v1alpha1",
1774+
map[string]interface{}{
1775+
"region": "string",
1776+
},
1777+
map[string]interface{}{
1778+
"vpcID": "${vpc.status.vpcID}",
1779+
},
1780+
),
1781+
generator.WithResource("vpc", map[string]interface{}{
1782+
"apiVersion": "ec2.services.k8s.aws/v1alpha1",
1783+
"kind": "VPC",
1784+
"metadata": map[string]interface{}{
1785+
"name": "app-vpc",
1786+
},
1787+
"spec": map[string]interface{}{
1788+
"cidrBlocks": []interface{}{"10.0.0.0/16"},
1789+
},
1790+
}, nil, nil),
1791+
generator.WithResource("subnet", map[string]interface{}{
1792+
"apiVersion": "ec2.services.k8s.aws/v1alpha1",
1793+
"kind": "Subnet",
1794+
"metadata": map[string]interface{}{
1795+
"name": "app-subnet",
1796+
},
1797+
"spec": map[string]interface{}{
1798+
"cidrBlock": "10.0.1.0/24",
1799+
"vpcID": "${schema.status.vpcID}",
1800+
},
1801+
}, nil, nil),
1802+
},
1803+
wantErr: false,
1804+
},
17691805
}
17701806

17711807
for _, tt := range tests {

0 commit comments

Comments
 (0)