From 1314027e1aee237dbac9c2e73c030e63fee5e172 Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Sun, 17 Mar 2024 14:50:58 +0100 Subject: [PATCH 1/2] feat(composition): update all examples, make input configurable Signed-off-by: Christopher Haar --- .gitignore | 1 + Makefile | 27 +-- README.md | 270 +---------------------- api/README.md | 2 - api/composition.yaml | 59 ----- apis/composition.yaml | 23 ++ {api => apis}/definition.yaml | 22 +- examples/composition-cidrhost.yaml | 20 -- examples/composition-cidrnetmask.yaml | 18 -- examples/composition-cidrsubnet.yaml | 21 -- examples/composition-cidrsubnetloop.yaml | 22 -- examples/composition-cidrsubnets.yaml | 19 -- examples/functions.yaml | 9 +- examples/xr-cidrhost.yaml | 8 +- examples/xr-cidrnetmask.yaml | 9 + examples/xr-cidrsubnet.yaml | 10 +- examples/xr-cidrsubnetloop.yaml | 11 +- examples/xr-cidrsubnets.yaml | 8 +- examples/xr.yaml | 5 - fn.go | 46 ++-- validate.go | 7 +- 21 files changed, 109 insertions(+), 508 deletions(-) delete mode 100644 api/README.md delete mode 100644 api/composition.yaml create mode 100644 apis/composition.yaml rename {api => apis}/definition.yaml (81%) delete mode 100644 examples/composition-cidrhost.yaml delete mode 100644 examples/composition-cidrnetmask.yaml delete mode 100644 examples/composition-cidrsubnet.yaml delete mode 100644 examples/composition-cidrsubnetloop.yaml delete mode 100644 examples/composition-cidrsubnets.yaml create mode 100644 examples/xr-cidrnetmask.yaml delete mode 100644 examples/xr.yaml diff --git a/.gitignore b/.gitignore index 012f680..f05fb31 100644 --- a/.gitignore +++ b/.gitignore @@ -23,3 +23,4 @@ go.work .golangci.yml *.xpkg function-cidr +.vscode \ No newline at end of file diff --git a/Makefile b/Makefile index 08a1bd8..1bef1c0 100644 --- a/Makefile +++ b/Makefile @@ -38,26 +38,13 @@ test: ## Run Code Tests go test -v -cover . render: ## Render Examples, Requires make debug first - crossplane beta render \ - examples/xr-cidrhost.yaml \ - examples/composition-cidrhost.yaml \ - examples/functions.yaml - crossplane beta render \ - examples/xr.yaml \ - examples/composition-cidrnetmask.yaml \ - examples/functions.yaml - crossplane beta render \ - examples/xr-cidrsubnet.yaml \ - examples/composition-cidrsubnet.yaml \ - examples/functions.yaml - crossplane beta render \ - examples/xr-cidrsubnets.yaml \ - examples/composition-cidrsubnets.yaml \ - examples/functions.yaml - crossplane beta render \ - examples/xr-cidrsubnetloop.yaml \ - examples/composition-cidrsubnetloop.yaml \ - examples/functions.yaml + @for file in examples/xr-*.yaml; do \ + echo "Rendering $$file..."; \ + crossplane beta render \ + "$$file" \ + apis/composition.yaml \ + examples/functions.yaml; \ + done debug: ## Run CIDR Function For Rendering Examples go run . --insecure --debug diff --git a/README.md b/README.md index 99872de..b3ef38d 100644 --- a/README.md +++ b/README.md @@ -28,9 +28,7 @@ cat < 0 { + cidrFunc, err = oxr.Resource.GetString(input.CidrFunc) + if err != nil { + response.Fatal(rsp, errors.Wrapf(err, "cannot get cidrFunc from field %s for %s", input.CidrFunc, oxr.Resource.GetKind())) + return rsp, nil + } + } + + switch cidrFunc { // cidrhost calculates the host CIDR from a prefix and a host number. // https://developer.hashicorp.com/terraform/language/functions/cidrhost case "cidrhost": @@ -90,10 +98,12 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ response.Fatal(rsp, errors.Wrapf(err, "cannot calculate CIDR host number for %s", oxr.Resource.GetKind())) return rsp, nil } - field := "status.atFunction.cidr.host" - if len(input.OutputField) > 0 { - field = input.OutputField + + field, err := oxr.Resource.GetString(input.OutputField) + if err != nil { + field = "status.atFunction.cidr.host" } + err = dxr.Resource.SetString(field, host) if err != nil { response.Fatal(rsp, errors.Wrapf(err, "cannot set field %s to %s for %s", field, host, oxr.Resource.GetKind())) @@ -108,9 +118,9 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ response.Fatal(rsp, errors.Wrapf(err, "cannot calculate CIDR netmask for %s", oxr.Resource.GetKind())) return rsp, nil } - field := "status.atFunction.cidr.netmask" - if len(input.OutputField) > 0 { - field = input.OutputField + field, err := oxr.Resource.GetString(input.OutputField) + if err != nil { + field = "status.atFunction.cidr.netmask" } err = dxr.Resource.SetString(field, netmask) if err != nil { @@ -144,9 +154,9 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ response.Fatal(rsp, errors.Wrapf(err, "cannot calculate subnet CIDR for %s", oxr.Resource.GetKind())) return rsp, nil } - field := "status.atFunction.cidr.subnet" - if len(input.OutputField) > 0 { - field = input.OutputField + field, err := oxr.Resource.GetString(input.OutputField) + if err != nil { + field = "status.atFunction.cidr.subnet" } err = dxr.Resource.SetString(field, string(cidr)) if err != nil { @@ -177,9 +187,9 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ for _, cidr := range cidrs { cidrSubnetsStringArray = append(cidrSubnetsStringArray, string(cidr)) } - field := "status.atFunction.cidr.subnets" - if len(input.OutputField) > 0 { - field = input.OutputField + field, err := oxr.Resource.GetString(input.OutputField) + if err != nil { + field = "status.atFunction.cidr.subnets" } err = dxr.Resource.SetValue(field, cidrSubnetsStringArray) if err != nil { @@ -242,9 +252,9 @@ func (f *Function) RunFunction(_ context.Context, req *fnv1beta1.RunFunctionRequ } cidrSubnetLoopStringArray = append(cidrSubnetLoopStringArray, string(cidr)) } - field := "status.atFunction.cidr.subnetloop" - if len(input.OutputField) > 0 { - field = input.OutputField + field, err := oxr.Resource.GetString(input.OutputField) + if err != nil { + field = "status.atFunction.cidr.subnets" } err = dxr.Resource.SetValue(field, cidrSubnetLoopStringArray) if err != nil { diff --git a/validate.go b/validate.go index 1429e47..2e7765d 100644 --- a/validate.go +++ b/validate.go @@ -138,7 +138,12 @@ func ValidateParameters(p *v1beta1.Parameters, oxr *resource.Composite) *field.E return fieldError } - switch p.CidrFunc { + cidrFunc, err := oxr.Resource.GetString(p.CidrFunc) + if err != nil { + return field.Required(field.NewPath("parameters"), "cidrFunc is required") + } + + switch cidrFunc { case "cidrhost": return ValidateCidrHostParameters(p, *oxr) case "cidrnetmask": From 83eee36e65a2f63ef74ef94a9a07cc5e061dac7e Mon Sep 17 00:00:00 2001 From: Christopher Haar Date: Sun, 17 Mar 2024 14:54:15 +0100 Subject: [PATCH 2/2] feat(composition): update all examples, make input configurable Signed-off-by: Christopher Haar --- apis/composition.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apis/composition.yaml b/apis/composition.yaml index 01d9f26..2ac96c4 100644 --- a/apis/composition.yaml +++ b/apis/composition.yaml @@ -5,7 +5,7 @@ metadata: spec: compositeTypeRef: apiVersion: platform.upbound.io/v1alpha1 - kind: XVPC + kind: XCIDR mode: Pipeline pipeline: - step: cidr