Skip to content

Commit 58fb81a

Browse files
authored
feat: add labels to func describe (knative#2882)
return typed error on describe of uninitialized Function adds describe tests
1 parent f9bf9fe commit 58fb81a

File tree

4 files changed

+81
-3
lines changed

4 files changed

+81
-3
lines changed

cmd/describe.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package cmd
33
import (
44
"encoding/json"
55
"encoding/xml"
6+
"errors"
67
"fmt"
78
"io"
89
"os"
@@ -81,6 +82,9 @@ func runDescribe(cmd *cobra.Command, args []string, newClient ClientFactory) (er
8182
if err != nil {
8283
return err
8384
}
85+
if !f.Initialized() {
86+
return errors.New("function not found at this path and no name provided")
87+
}
8488
details, err = client.Describe(cmd.Context(), "", "", f)
8589
if err != nil {
8690
return err
@@ -153,6 +157,13 @@ func (i info) Human(w io.Writer) error {
153157
fmt.Fprintf(w, " %v %v %v\n", s.Source, s.Type, s.Broker)
154158
}
155159
}
160+
161+
if len(i.Labels) > 0 {
162+
fmt.Fprintln(w, "Labels:")
163+
for k, v := range i.Labels {
164+
fmt.Fprintf(w, " %v: %v\n", k, v)
165+
}
166+
}
156167
return nil
157168
}
158169

@@ -170,6 +181,12 @@ func (i info) Plain(w io.Writer) error {
170181
fmt.Fprintf(w, "Subscription %v %v %v\n", s.Source, s.Type, s.Broker)
171182
}
172183
}
184+
185+
if len(i.Labels) > 0 {
186+
for k, v := range i.Labels {
187+
fmt.Fprintf(w, "Label %v %v\n", k, v)
188+
}
189+
}
173190
return nil
174191
}
175192

cmd/describe_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,66 @@ package cmd
22

33
import (
44
"context"
5+
"strings"
56
"testing"
67

78
fn "knative.dev/func/pkg/functions"
89
"knative.dev/func/pkg/mock"
910
. "knative.dev/func/pkg/testing"
1011
)
1112

13+
// TestDescribe_Default ensures that running describe when there is no
14+
// function in the given directory fails correctly.
15+
func TestDescribe_Default(t *testing.T) {
16+
_ = FromTempDirectory(t)
17+
describer := mock.NewDescriber()
18+
19+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
20+
cmd.SetArgs([]string{})
21+
err := cmd.Execute()
22+
23+
if err == nil {
24+
t.Fatal("describing a nonexistent function should error")
25+
}
26+
if !strings.Contains(err.Error(), "function not found at this path and no name provided") {
27+
t.Fatalf("Unexpected error text returned: %v", err)
28+
}
29+
if describer.DescribeInvoked {
30+
t.Fatal("Describer incorrectly invoked")
31+
}
32+
}
33+
34+
// TestDescribe_Undeployed ensures that describing a function which exists,
35+
// but has not been deployed, does not error but rather delegates to the
36+
// deployer which will presumably describe it as being !deployed (See deployer
37+
// test suite)
38+
func TestDescribe_Undeployed(t *testing.T) {
39+
root := FromTempDirectory(t)
40+
41+
client := fn.New()
42+
_, err := client.Init(fn.Function{
43+
Name: "testfunc",
44+
Runtime: "go",
45+
Registry: TestRegistry,
46+
Root: root,
47+
})
48+
if err != nil {
49+
t.Fatal(err)
50+
}
51+
52+
describer := mock.NewDescriber()
53+
54+
cmd := NewDescribeCmd(NewTestClient(fn.WithDescriber(describer)))
55+
cmd.SetArgs([]string{})
56+
if err := cmd.Execute(); err != nil {
57+
t.Fatal(err)
58+
}
59+
60+
if !describer.DescribeInvoked {
61+
t.Fatal("Describer should have been invoked for any initialized function")
62+
}
63+
}
64+
1265
// TestDescribe_ByName ensures that describing a function by name invokes
1366
// the describer appropriately.
1467
func TestDescribe_ByName(t *testing.T) {

pkg/functions/client.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -973,17 +973,20 @@ func (c *Client) Describe(ctx context.Context, name, namespace string, f Functio
973973
return c.describer.Describe(ctx, name, namespace)
974974
}
975975

976-
// If the function's not initialized, then we can save some time and
977-
// fail fast.
976+
// Desribe Current Function
977+
// ------------------------
978978
if !f.Initialized() {
979-
return d, fmt.Errorf("function not initialized: %v", f.Root)
979+
return d, NewErrNotInitialized(f.Root)
980980
}
981981

982982
// If the function is undeployed, we can't describe it either.
983983
if f.Name == "" {
984984
return d, fmt.Errorf("unable to describe without a name. %v", ErrNameRequired)
985985
}
986986

987+
// If it has a populated deployed namespace, we can presume it's deployed
988+
// and attempt to describe.
989+
987990
return c.describer.Describe(ctx, f.Name, f.Deploy.Namespace)
988991
}
989992

pkg/knative/describer.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,5 +98,10 @@ func (d *Describer) Describe(ctx context.Context, name, namespace string) (descr
9898

9999
description.Subscriptions = subscriptions
100100

101+
// Populate labels from the service
102+
if service.Labels != nil {
103+
description.Labels = service.Labels
104+
}
105+
101106
return
102107
}

0 commit comments

Comments
 (0)