Skip to content

Commit 104b642

Browse files
committed
Add ability to show proxy/domain labels
1 parent 26a336e commit 104b642

File tree

3 files changed

+69
-18
lines changed

3 files changed

+69
-18
lines changed

cmd/domain.go

Lines changed: 30 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,24 @@ import (
1919

2020
func fmtDomain(r *v1alpha.Domain) {
2121
t := pretty.Table{
22-
Header: buildDomainHeader(),
22+
Header: buildDomainHeader(false),
2323
Rows: pretty.Rows{
24-
buildDomainRow(r),
24+
buildDomainRow(r, false),
2525
},
2626
}
2727
t.Print()
2828
}
2929

30-
func buildDomainHeader() pretty.Header {
30+
func buildDomainHeader(labels bool) pretty.Header {
31+
if labels {
32+
return pretty.Header{
33+
"NAME",
34+
"HOSTNAMES",
35+
"STATUS",
36+
"AGE",
37+
"LABELS",
38+
}
39+
}
3140
return pretty.Header{
3241
"NAME",
3342
"HOSTNAMES",
@@ -36,21 +45,26 @@ func buildDomainHeader() pretty.Header {
3645
}
3746
}
3847

39-
func buildDomainRow(r *v1alpha.Domain) []interface{} {
48+
func buildDomainRow(r *v1alpha.Domain, labels bool) (res []interface{}) {
4049
if r.Spec.Style == v1alpha.DomainStyleMagic {
41-
return []interface{}{
50+
res = []interface{}{
4251
r.Name,
4352
fmt.Sprintf("%s.apoxy.io", r.Spec.MagicKey),
4453
r.Status.Phase,
4554
pretty.SinceString(r.CreationTimestamp.Time),
4655
}
56+
} else {
57+
res = []interface{}{
58+
r.Name,
59+
strings.Join(r.Spec.Hostnames, ", "),
60+
r.Status.Phase,
61+
pretty.SinceString(r.CreationTimestamp.Time),
62+
}
4763
}
48-
return []interface{}{
49-
r.Name,
50-
strings.Join(r.Spec.Hostnames, ", "),
51-
r.Status.Phase,
52-
pretty.SinceString(r.CreationTimestamp.Time),
64+
if labels {
65+
res = append(res, labelsToString(r.Labels))
5366
}
67+
return
5468
}
5569

5670
func getOrCreateMagicProxy(c *rest.APIClient, name string) error {
@@ -96,6 +110,8 @@ func GetDomain(name string) error {
96110
return nil
97111
}
98112

113+
var showDomainLabels bool
114+
99115
func ListDomains() error {
100116
c, err := defaultAPIClient()
101117
if err != nil {
@@ -106,10 +122,10 @@ func ListDomains() error {
106122
return err
107123
}
108124
t := pretty.Table{
109-
Header: buildDomainHeader(),
125+
Header: buildDomainHeader(showDomainLabels),
110126
}
111127
for _, p := range r.Items {
112-
t.Rows = append(t.Rows, buildDomainRow(&p))
128+
t.Rows = append(t.Rows, buildDomainRow(&p, showDomainLabels))
113129
}
114130
t.Print()
115131
return nil
@@ -307,6 +323,8 @@ func init() {
307323
BoolVar(&domainRandomName, "random", false, "Generate a random name of the domain.")
308324
getDomainCmd.PersistentFlags().
309325
BoolVarP(&listProxies, "proxies", "p", false, "List the proxies this domain points to.")
326+
listDomainCmd.PersistentFlags().
327+
BoolVar(&showDomainLabels, "show-labels", false, "Print the domain's labels.")
310328
// TODO: add flags for domain config as raw envoy config
311329

312330
domainCmd.AddCommand(getDomainCmd, listDomainCmd, createDomainCmd, deleteDomainCmd)

cmd/proxy.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,25 @@ import (
1515

1616
func fmtProxy(r *v1alpha.Proxy) {
1717
t := pretty.Table{
18-
Header: buildProxyHeader(),
18+
Header: buildProxyHeader(false),
1919
Rows: pretty.Rows{
20-
buildProxyRow(r),
20+
buildProxyRow(r, false),
2121
},
2222
}
2323
t.Print()
2424
}
2525

26-
func buildProxyHeader() pretty.Header {
26+
func buildProxyHeader(labels bool) pretty.Header {
27+
if labels {
28+
return pretty.Header{
29+
"NAME",
30+
"PROVIDER",
31+
"STATUS",
32+
"ADDRESS",
33+
"AGE",
34+
"LABELS",
35+
}
36+
}
2737
return pretty.Header{
2838
"NAME",
2939
"PROVIDER",
@@ -33,7 +43,17 @@ func buildProxyHeader() pretty.Header {
3343
}
3444
}
3545

36-
func buildProxyRow(r *v1alpha.Proxy) []interface{} {
46+
func buildProxyRow(r *v1alpha.Proxy, labels bool) []interface{} {
47+
if labels {
48+
return []interface{}{
49+
r.Name,
50+
r.Spec.Provider,
51+
r.Status.Phase,
52+
r.Status.Address,
53+
pretty.SinceString(r.CreationTimestamp.Time),
54+
labelsToString(r.Labels),
55+
}
56+
}
3757
return []interface{}{
3858
r.Name,
3959
r.Spec.Provider,
@@ -56,6 +76,8 @@ func GetProxy(name string) error {
5676
return nil
5777
}
5878

79+
var showProxyLabels bool
80+
5981
func ListProxies(opts ...metav1.ListOptions) error {
6082
c, err := defaultAPIClient()
6183
if err != nil {
@@ -71,10 +93,10 @@ func ListProxies(opts ...metav1.ListOptions) error {
7193
return err
7294
}
7395
t := pretty.Table{
74-
Header: buildProxyHeader(),
96+
Header: buildProxyHeader(showProxyLabels),
7597
}
7698
for _, p := range r.Items {
77-
t.Rows = append(t.Rows, buildProxyRow(&p))
99+
t.Rows = append(t.Rows, buildProxyRow(&p, showProxyLabels))
78100
}
79101
t.Print()
80102
return nil
@@ -191,6 +213,8 @@ var deleteProxyCmd = &cobra.Command{
191213
func init() {
192214
createProxyCmd.PersistentFlags().
193215
StringVarP(&proxyFile, "filename", "f", "", "The file that contains the configuration to create.")
216+
listProxyCmd.PersistentFlags().
217+
BoolVar(&showProxyLabels, "show-labels", false, "Print the proxy's labels.")
194218
// TODO: add flags for proxy config as raw envoy config
195219

196220
proxyCmd.AddCommand(getProxyCmd, listProxyCmd, createProxyCmd, deleteProxyCmd)

cmd/root.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,15 @@ func yamlStringToJSONString(yamlString string) (string, error) {
9393
return string(jsonBytes), nil
9494
}
9595

96+
// labelsToString converts a map of labels to a string.
97+
func labelsToString(labels map[string]string) string {
98+
var l []string
99+
for k, v := range labels {
100+
l = append(l, fmt.Sprintf("%s=%s", k, v))
101+
}
102+
return strings.Join(l, ",")
103+
}
104+
96105
// GenerateDocs generates the docs in the docs folder.
97106
func GenerateDocs() {
98107
anchorLinks := func(s string) string {

0 commit comments

Comments
 (0)