Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): sort composed resources in the render output #5030

Merged
merged 1 commit into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions cmd/crank/beta/render/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,7 @@ func (c *Cmd) Run(k *kong.Context, _ logging.Logger) error { //nolint:gocyclo //
for i := range out.ComposedResources {
fmt.Fprintln(k.Stdout, "---")
if err := s.Encode(&out.ComposedResources[i], os.Stdout); err != nil {
// TODO(negz): Use composed name annotation instead.
return errors.Wrapf(err, "cannot marshal composed resource %q to YAML", out.ComposedResources[i].GetName())
return errors.Wrapf(err, "cannot marshal composed resource %q to YAML", out.ComposedResources[i].GetAnnotations()[AnnotationKeyCompositionResourceName])
}
}

Expand Down
8 changes: 7 additions & 1 deletion cmd/crank/beta/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package render
import (
"context"
"encoding/json"
"sort"

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
Expand Down Expand Up @@ -82,7 +83,7 @@ type Outputs struct {
// appear ready?
}

// Render the desired XR and composed resources given the supplied inputs.
// Render the desired XR and composed resources, sorted by resource name, given the supplied inputs.
func Render(ctx context.Context, in Inputs) (Outputs, error) { //nolint:gocyclo // TODO(negz): Should we refactor to break this up a bit?
// Run our Functions.
conns := map[string]*grpc.ClientConn{}
Expand Down Expand Up @@ -216,6 +217,11 @@ func Render(ctx context.Context, in Inputs) (Outputs, error) { //nolint:gocyclo
desired = append(desired, *cd)
}

// Sort the resource names to ensure a deterministic ordering of returned composed resources.
sort.Slice(desired, func(i, j int) bool {
return desired[i].GetAnnotations()[AnnotationKeyCompositionResourceName] < desired[j].GetAnnotations()[AnnotationKeyCompositionResourceName]
})

xr := ucomposite.New()
if err := composite.FromStruct(xr, d.GetComposite().GetResource()); err != nil {
return Outputs{}, errors.Wrap(err, "cannot render desired composite resource")
Expand Down
49 changes: 43 additions & 6 deletions cmd/crank/beta/render/render_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,19 @@ func TestRender(t *testing.T) {
}`),
},
Resources: map[string]*fnv1beta1.Resource{
"cool-resource": {
"b-cool-resource": {
Resource: MustStructJSON(`{
"apiVersion": "test.crossplane.io/v1",
"kind": "Composed",
"apiVersion": "atest.crossplane.io/v1",
"kind": "AComposed",
"spec": {
"widgets": 9003
}
}`),
},
"a-cool-resource": {
Resource: MustStructJSON(`{
"apiVersion": "btest.crossplane.io/v1",
"kind": "BComposed",
"spec": {
"widgets": 9002
}
Expand Down Expand Up @@ -266,14 +275,14 @@ func TestRender(t *testing.T) {
{
Unstructured: unstructured.Unstructured{
Object: MustLoadJSON(`{
"apiVersion": "test.crossplane.io/v1",
"apiVersion": "btest.crossplane.io/v1",
"metadata": {
"generateName": "test-render-",
"labels": {
"crossplane.io/composite": "test-render"
},
"annotations": {
"crossplane.io/composition-resource-name": "cool-resource"
"crossplane.io/composition-resource-name": "a-cool-resource"
},
"ownerReferences": [{
"apiVersion": "nop.example.org/v1alpha1",
Expand All @@ -284,13 +293,41 @@ func TestRender(t *testing.T) {
"uid": ""
}]
},
"kind": "Composed",
"kind": "BComposed",
"spec": {
"widgets": 9002
}
}`),
},
},
{
Unstructured: unstructured.Unstructured{
Object: MustLoadJSON(`{
"apiVersion": "atest.crossplane.io/v1",
"metadata": {
"generateName": "test-render-",
"labels": {
"crossplane.io/composite": "test-render"
},
"annotations": {
"crossplane.io/composition-resource-name": "b-cool-resource"
},
"ownerReferences": [{
"apiVersion": "nop.example.org/v1alpha1",
"kind": "XNopResource",
"name": "test-render",
"blockOwnerDeletion": true,
"controller": true,
"uid": ""
}]
},
"kind": "AComposed",
"spec": {
"widgets": 9003
}
}`),
},
},
},
},
},
Expand Down