Skip to content

Commit 33f2b04

Browse files
n3wscottVille Aikas
authored and
Ville Aikas
committed
First example using the pretty context builder. (#1403)
* First example using the log context builder. * No format. * Remote controller logging class, not needed yet. * Fixing cr date. * Adding function documentation. * Kind is always type Kind. * Add comment for type. * I feel so pretty. Oh so pretty. Pretty log lines. * Renaming files, fixing receiver names. * Finish controller.go.
1 parent 7852917 commit 33f2b04

File tree

3 files changed

+232
-33
lines changed

3 files changed

+232
-33
lines changed

pkg/controller/controller.go

Lines changed: 13 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ import (
4141
servicecatalogclientset "github.com/kubernetes-incubator/service-catalog/pkg/client/clientset_generated/clientset/typed/servicecatalog/v1beta1"
4242
informers "github.com/kubernetes-incubator/service-catalog/pkg/client/informers_generated/externalversions/servicecatalog/v1beta1"
4343
listers "github.com/kubernetes-incubator/service-catalog/pkg/client/listers_generated/servicecatalog/v1beta1"
44+
pretty "github.com/kubernetes-incubator/service-catalog/pkg/pretty"
4445
)
4546

4647
const (
@@ -247,16 +248,14 @@ func worker(queue workqueue.RateLimitingInterface, resourceType string, maxRetri
247248
// a brokerClient to use for that method given an ServiceInstance.
248249
// Sets ClusterServiceClassRef and/or ClusterServicePlanRef if they haven't been already set.
249250
func (c *controller) getClusterServiceClassPlanAndClusterServiceBroker(instance *v1beta1.ServiceInstance) (*v1beta1.ClusterServiceClass, *v1beta1.ClusterServicePlan, string, osb.Client, error) {
251+
pcb := pretty.NewContextBuilder(pretty.ServiceInstance, instance.Namespace, instance.Name)
250252
serviceClass, err := c.serviceClassLister.Get(instance.Spec.ClusterServiceClassRef.Name)
251253
if err != nil {
252254
s := fmt.Sprintf(
253255
"References a non-existent ClusterServiceClass (K8S: %q ExternalName: %q)",
254256
instance.Spec.ClusterServiceClassRef.Name, instance.Spec.ExternalClusterServiceClassName,
255257
)
256-
glog.Infof(
257-
`ServiceInstance "%v/%v": %s`,
258-
instance.Namespace, instance.Name, s,
259-
)
258+
glog.Info(pcb.Message(s))
260259
c.updateServiceInstanceCondition(
261260
instance,
262261
v1beta1.ServiceInstanceConditionReady,
@@ -274,10 +273,7 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBroker(instance
274273
"References a non-existent ClusterServicePlan (K8S: %q ExternalName: %q) on ClusterServiceClass (K8S: %q ExternalName: %q)",
275274
instance.Spec.ClusterServicePlanName, instance.Spec.ExternalClusterServicePlanName, serviceClass.Name, serviceClass.Spec.ExternalName,
276275
)
277-
glog.Warningf(
278-
`ServiceInstance "%s/%s": %s`,
279-
instance.Namespace, instance.Name, s,
280-
)
276+
glog.Warning(pcb.Message(s))
281277
c.updateServiceInstanceCondition(
282278
instance,
283279
v1beta1.ServiceInstanceConditionReady,
@@ -292,10 +288,7 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBroker(instance
292288
broker, err := c.brokerLister.Get(serviceClass.Spec.ClusterServiceBrokerName)
293289
if err != nil {
294290
s := fmt.Sprintf("References a non-existent broker %q", serviceClass.Spec.ClusterServiceBrokerName)
295-
glog.Warningf(
296-
`ServiceInstance "%s/%s": %s`,
297-
instance.Namespace, instance.Name, s,
298-
)
291+
glog.Warning(pcb.Message(s))
299292
c.updateServiceInstanceCondition(
300293
instance,
301294
v1beta1.ServiceInstanceConditionReady,
@@ -310,10 +303,7 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBroker(instance
310303
authConfig, err := getAuthCredentialsFromClusterServiceBroker(c.kubeClient, broker)
311304
if err != nil {
312305
s := fmt.Sprintf("Error getting broker auth credentials for broker %q: %s", broker.Name, err)
313-
glog.Infof(
314-
`ServiceInstance "%v/%v": %s`,
315-
instance.Namespace, instance.Name, s,
316-
)
306+
glog.Info(pcb.Message(s))
317307
c.updateServiceInstanceCondition(
318308
instance,
319309
v1beta1.ServiceInstanceConditionReady,
@@ -327,7 +317,8 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBroker(instance
327317

328318
clientConfig := NewClientConfigurationForBroker(broker, authConfig)
329319

330-
glog.V(4).Infof("Creating client for ClusterServiceBroker %v, URL: %v", broker.Name, broker.Spec.URL)
320+
s := fmt.Sprintf("Creating client for ClusterServiceBroker %v, URL: %v", broker.Name, broker.Spec.URL)
321+
glog.V(4).Info(pcb.Message(s))
331322
brokerClient, err := c.brokerClientCreateFunc(clientConfig)
332323
if err != nil {
333324
return nil, nil, "", nil, err
@@ -341,16 +332,14 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBroker(instance
341332
// a brokerclient to use for a given ServiceInstance.
342333
// Sets ClusterServiceClassRef and/or ClusterServicePlanRef if they haven't been already set.
343334
func (c *controller) getClusterServiceClassPlanAndClusterServiceBrokerForServiceBinding(instance *v1beta1.ServiceInstance, binding *v1beta1.ServiceBinding) (*v1beta1.ClusterServiceClass, *v1beta1.ClusterServicePlan, string, osb.Client, error) {
335+
pcb := pretty.NewContextBuilder(pretty.ServiceInstance, instance.Namespace, instance.Name)
344336
serviceClass, err := c.serviceClassLister.Get(instance.Spec.ClusterServiceClassRef.Name)
345337
if err != nil {
346338
s := fmt.Sprintf(
347339
"References a non-existent ClusterServiceClass (K8S: %q ExternalName: %q)",
348340
instance.Spec.ClusterServiceClassRef.Name, instance.Spec.ExternalClusterServiceClassName,
349341
)
350-
glog.Warningf(
351-
`ServiceBinding "%s/%s": %s`,
352-
instance.Namespace, instance.Name, s,
353-
)
342+
glog.Warning(pcb.Message(s))
354343
c.updateServiceBindingCondition(
355344
binding,
356345
v1beta1.ServiceBindingConditionReady,
@@ -368,10 +357,7 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBrokerForService
368357
"References a non-existent ClusterServicePlan (K8S: %q ExternalName: %q) on ClusterServiceClass (K8S: %q ExternalName: %q)",
369358
instance.Spec.ClusterServicePlanName, instance.Spec.ExternalClusterServicePlanName, serviceClass.Name, serviceClass.Spec.ExternalName,
370359
)
371-
glog.Warningf(
372-
`ServiceBinding "%s/%s": %s`,
373-
instance.Namespace, instance.Name, s,
374-
)
360+
glog.Warning(pcb.Message(s))
375361
c.updateServiceBindingCondition(
376362
binding,
377363
v1beta1.ServiceBindingConditionReady,
@@ -386,10 +372,7 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBrokerForService
386372
broker, err := c.brokerLister.Get(serviceClass.Spec.ClusterServiceBrokerName)
387373
if err != nil {
388374
s := fmt.Sprintf("References a non-existent ClusterServiceBroker %q", serviceClass.Spec.ClusterServiceBrokerName)
389-
glog.Warningf(
390-
`ServiceBinding "%s/%s": %s`,
391-
instance.Namespace, instance.Name, s,
392-
)
375+
glog.Warning(pcb.Message(s))
393376
c.updateServiceBindingCondition(
394377
binding,
395378
v1beta1.ServiceBindingConditionReady,
@@ -404,10 +387,7 @@ func (c *controller) getClusterServiceClassPlanAndClusterServiceBrokerForService
404387
authConfig, err := getAuthCredentialsFromClusterServiceBroker(c.kubeClient, broker)
405388
if err != nil {
406389
s := fmt.Sprintf("Error getting broker auth credentials for broker %q: %s", broker.Name, err)
407-
glog.Warningf(
408-
`ServiceBinding "%s/%s": %s`,
409-
instance.Namespace, instance.Name, s,
410-
)
390+
glog.Warning(pcb.Message(s))
411391
c.updateServiceBindingCondition(
412392
binding,
413393
v1beta1.ServiceBindingConditionReady,

pkg/pretty/context_builder.go

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pretty
18+
19+
import (
20+
"fmt"
21+
)
22+
23+
// Kind is used for the enum of the Type of object we are building context for.
24+
type Kind int
25+
26+
// Names of Types to use when creating pretty messages.
27+
const (
28+
ServiceInstance Kind = 1
29+
)
30+
31+
func (k Kind) String() string {
32+
switch k {
33+
case ServiceInstance:
34+
return "ServiceInstance"
35+
default:
36+
return ""
37+
}
38+
}
39+
40+
// ContextBuilder allows building up pretty message lines with context
41+
// that is important for debugging and tracing. This class helps create log
42+
// line formatting consistency. Pretty lines should be in the form:
43+
// <Kind> "<Namespace>/<Name>": <message>
44+
type ContextBuilder struct {
45+
Kind Kind
46+
Namespace string
47+
Name string
48+
}
49+
50+
// NewContextBuilder returns a new ContextBuilder that can be used to format messages in the
51+
// form `<Kind> "<Namespace>/<Name>": <message>`.
52+
// kind, namespace, name are all optional.
53+
func NewContextBuilder(kind Kind, namespace string, name string) *ContextBuilder {
54+
lb := new(ContextBuilder)
55+
lb.Kind = kind
56+
lb.Namespace = namespace
57+
lb.Name = name
58+
return lb
59+
}
60+
61+
// SetKind sets the kind to use in the source context for messages.
62+
func (pcb *ContextBuilder) SetKind(k Kind) *ContextBuilder {
63+
pcb.Kind = k
64+
return pcb
65+
}
66+
67+
// SetNamespace sets the namespace to use in the source context for messages.
68+
func (pcb *ContextBuilder) SetNamespace(n string) *ContextBuilder {
69+
pcb.Namespace = n
70+
return pcb
71+
}
72+
73+
// SetName sets the name to use in the source context for messages.
74+
func (pcb *ContextBuilder) SetName(n string) *ContextBuilder {
75+
pcb.Name = n
76+
return pcb
77+
}
78+
79+
// Message returns a string with message prepended with the current source context.
80+
func (pcb *ContextBuilder) Message(msg string) string {
81+
if pcb.Kind > 0 || pcb.Namespace != "" || pcb.Name != "" {
82+
return fmt.Sprintf(`%s: %s`, pcb, msg)
83+
}
84+
return msg
85+
}
86+
87+
// TODO(n3wscott): Support <type> (K8S: <K8S-Type-Name> ExternalName: <External-Type-Name>)
88+
89+
func (pcb ContextBuilder) String() string {
90+
s := ""
91+
space := ""
92+
if pcb.Kind > 0 {
93+
s += fmt.Sprintf("%s", pcb.Kind)
94+
space = " "
95+
}
96+
if pcb.Namespace != "" && pcb.Name != "" {
97+
s += fmt.Sprintf(`%s"%s/%s"`, space, pcb.Namespace, pcb.Name)
98+
} else if pcb.Namespace != "" {
99+
s += fmt.Sprintf(`%s"%s"`, space, pcb.Namespace)
100+
} else if pcb.Name != "" {
101+
s += fmt.Sprintf(`%s"%s"`, space, pcb.Name)
102+
}
103+
return s
104+
}

pkg/pretty/context_builder_test.go

Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/*
2+
Copyright 2017 The Kubernetes Authors.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
*/
16+
17+
package pretty
18+
19+
import (
20+
"testing"
21+
)
22+
23+
func TestPrettyContextBuilderKind(t *testing.T) {
24+
pcb := ContextBuilder{}
25+
26+
pcb.SetKind(ServiceInstance)
27+
28+
e := "ServiceInstance"
29+
g := pcb.String()
30+
if g != e {
31+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
32+
}
33+
}
34+
35+
func TestPrettyContextBuilderNamespace(t *testing.T) {
36+
pcb := ContextBuilder{}
37+
38+
pcb.SetNamespace("Namespace")
39+
40+
e := `"Namespace"`
41+
g := pcb.String()
42+
if g != e {
43+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
44+
}
45+
}
46+
47+
func TestPrettyContextBuilderName(t *testing.T) {
48+
pcb := ContextBuilder{}
49+
50+
pcb.SetName("Name")
51+
52+
e := `"Name"`
53+
g := pcb.String()
54+
if g != e {
55+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
56+
}
57+
}
58+
59+
func TestPrettyContextBuilderKindAndNamespace(t *testing.T) {
60+
pcb := ContextBuilder{}
61+
62+
pcb.SetKind(ServiceInstance).SetNamespace("Namespace")
63+
64+
e := `ServiceInstance "Namespace"`
65+
g := pcb.String()
66+
if g != e {
67+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
68+
}
69+
}
70+
71+
func TestPrettyContextBuilderKindAndName(t *testing.T) {
72+
pcb := ContextBuilder{}
73+
74+
pcb.SetKind(ServiceInstance).SetName("Name")
75+
76+
e := `ServiceInstance "Name"`
77+
g := pcb.String()
78+
if g != e {
79+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
80+
}
81+
}
82+
83+
func TestPrettyContextBuilderKindNamespaceName(t *testing.T) {
84+
pcb := ContextBuilder{}
85+
86+
pcb.SetKind(ServiceInstance).SetNamespace("Namespace").SetName("Name")
87+
88+
e := `ServiceInstance "Namespace/Name"`
89+
g := pcb.String()
90+
if g != e {
91+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
92+
}
93+
}
94+
95+
func TestPrettyContextBuilderMsg(t *testing.T) {
96+
pcb := ContextBuilder{}
97+
98+
e := `Msg`
99+
g := pcb.Message("Msg")
100+
if g != e {
101+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
102+
}
103+
}
104+
105+
func TestPrettyContextBuilderContextAndMsg(t *testing.T) {
106+
pcb := ContextBuilder{}
107+
108+
pcb.SetKind(ServiceInstance).SetNamespace("Namespace").SetName("Name")
109+
110+
e := `ServiceInstance "Namespace/Name": Msg`
111+
g := pcb.Message("Msg")
112+
if g != e {
113+
t.Fatalf("Unexpected value of ContextBuilder String; expected %v, got %v", e, g)
114+
}
115+
}

0 commit comments

Comments
 (0)