Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions extraresources.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,10 @@ func (e *ExtraResourcesRequirement) ToResourceSelector() *fnv1.ResourceSelector
out.Match = &fnv1.ResourceSelector_MatchLabels{
MatchLabels: &fnv1.MatchLabels{Labels: e.MatchLabels},
}
return out
}

out.Match = &fnv1.ResourceSelector_MatchName{
MatchName: e.MatchName,
} else {
out.Match = &fnv1.ResourceSelector_MatchName{
MatchName: e.MatchName,
}
}

if e.Namespace != "" {
Expand Down
87 changes: 87 additions & 0 deletions extraresources_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package main

import (
"testing"

"github.com/google/go-cmp/cmp"
"google.golang.org/protobuf/testing/protocmp"

fnv1 "github.com/crossplane/function-sdk-go/proto/v1"
)

func TestExtraResourcesRequirementToResourceSelector(t *testing.T) {
ns := "test"

cases := map[string]struct {
reason string
e ExtraResourcesRequirement
want *fnv1.ResourceSelector
}{
"MatchLabelsWithNamespace": {
reason: "Namespace must be set on the selector when MatchLabels is used, not just MatchName.",
e: ExtraResourcesRequirement{
APIVersion: "example.org/v1",
Kind: "CoolExtraResource",
MatchLabels: map[string]string{
"cool": "true",
},
Namespace: ns,
},
want: &fnv1.ResourceSelector{
ApiVersion: "example.org/v1",
Kind: "CoolExtraResource",
Match: &fnv1.ResourceSelector_MatchLabels{
MatchLabels: &fnv1.MatchLabels{Labels: map[string]string{
"cool": "true",
}},
},
Namespace: &ns,
},
},
"MatchLabelsWithoutNamespace": {
reason: "Namespace must be left unset when it is empty.",
e: ExtraResourcesRequirement{
APIVersion: "example.org/v1",
Kind: "CoolExtraResource",
MatchLabels: map[string]string{
"cool": "true",
},
},
want: &fnv1.ResourceSelector{
ApiVersion: "example.org/v1",
Kind: "CoolExtraResource",
Match: &fnv1.ResourceSelector_MatchLabels{
MatchLabels: &fnv1.MatchLabels{Labels: map[string]string{
"cool": "true",
}},
},
},
},
"MatchNameWithNamespace": {
reason: "Namespace must still be set when MatchName is used.",
e: ExtraResourcesRequirement{
APIVersion: "example.org/v1",
Kind: "CoolExtraResource",
MatchName: "cool-extra-resource",
Namespace: ns,
},
want: &fnv1.ResourceSelector{
ApiVersion: "example.org/v1",
Kind: "CoolExtraResource",
Match: &fnv1.ResourceSelector_MatchName{
MatchName: "cool-extra-resource",
},
Namespace: &ns,
},
},
}

for name, tc := range cases {
t.Run(name, func(t *testing.T) {
got := tc.e.ToResourceSelector()
if diff := cmp.Diff(tc.want, got, protocmp.Transform()); diff != "" {
t.Errorf("\n%s\nToResourceSelector(...): -want, +got:\n%s", tc.reason, diff)
}
})
}
}
Loading