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

Adds events for route #358

Merged
merged 15 commits into from
Mar 15, 2018
Merged

Conversation

yanweiguo
Copy link
Contributor

@yanweiguo yanweiguo commented Mar 13, 2018

Adds following events:

  • Route:
    • Create service completes/fails
    • Create Ingress completes/fails
    • Create Istio route rule complets/fails
    • Update route traffic targets completes/fails
    • Receive invalid configuration(route label already set to another route)

Sample output:

$ kubectl describe route/route-example

...
Events:
  Type     Reason          Age                 From              Message
  ----     ------          ----                ----              -------
  Normal   Created         4m                 route-controller  Created service 'route-example-service'
  Normal   Created         4m                 route-controller  Created Ingress 'route-example-ela-ingress'
  Warning  CreationFailed  4m                 route-controller  Failed to create Istio route rule 'route-example-istio': routerules.config.istio.io "route-example-istio" already exists
  Normal   Created         4m                 route-controller  Created Istio route rule 'route-example-istio'
  Warning  UpdateFailed    4m                 route-controller  Failed to update Istio route rule 'route-example-istio': Operation cannot be fulfilled on routerules.config.istio.io "route-example-istio": the object has been modified; please apply your changes to the latest version and try again
  Normal   Updated         2m (x6 over 4m)  route-controller  Updated status for route 'route-example'
  Normal   Updated         4m (x8 over 4m)   route-controller  Updated Istio route rule 'route-example-istio'

Also improves the tests for controllers:

Closes #8 #12

@google-prow-robot google-prow-robot added the size/L Denotes a PR that changes 100-499 lines, ignoring generated files. label Mar 13, 2018
@yanweiguo
Copy link
Contributor Author

/retest

@@ -367,7 +353,7 @@ func (c *Controller) syncTrafficTargets(route *v1alpha1.Route) (*v1alpha1.Route,
}

// Then create the actual route rules.
glog.Infof("Creating istio route rules")
glog.Infof("Creating Istio route rules")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: no format args

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -389,26 +375,33 @@ func (c *Controller) syncTrafficTargets(route *v1alpha1.Route) (*v1alpha1.Route,
updated, err := c.updateStatus(route)
if err != nil {
glog.Warningf("Failed to update service status: %s", err)
c.recorder.Event(route, corev1.EventTypeWarning, "UpdateFailed", "Failed to update traffic targets")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@vaikas-google should we surface err here?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, makes sense, otherwise the user knows something went wrong, but not what. We should also add details here about which route failed or succeeded. Otherwise, messages of the form (from the PR description):
Normal Updated 29s (x6 over 1m) route-controller Updated traffic targets

don't provide enough information to figure out what happened.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added the err.

if err != nil {
if !apierrs.IsAlreadyExists(err) {
glog.Infof("Failed to create service: %s", err)
c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create service: %s", service.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include err here unless @vaikas-google thinks that's inappropriate.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -428,8 +421,13 @@ func (c *Controller) createOrUpdateIngress(route *v1alpha1.Route, ns string) err
return err
}
_, createErr := ic.Create(ingress)
if createErr != nil {
c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create Ingress: %s", ingress.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Include err?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -428,8 +421,13 @@ func (c *Controller) createOrUpdateIngress(route *v1alpha1.Route, ns string) err
return err
}
_, createErr := ic.Create(ingress)
if createErr != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prefer: if _, err := ic.Create(); err != nil {

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are a bunch of these if you are up for changing them :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -627,15 +627,21 @@ func (c *Controller) createOrUpdateRoutes(route *v1alpha1.Route, configMap map[s
return nil, err
}
routeRules = MakeRouteIstioRoutes(route, ns, revisionRoutes)
_, createErr := routeClient.Create(routeRules)
return revisionRoutes, createErr
if _, createErr := routeClient.Create(routeRules); createErr != nil {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: use err

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

_, createErr := routeClient.Create(routeRules)
return revisionRoutes, createErr
if _, createErr := routeClient.Create(routeRules); createErr != nil {
c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create Istio route rule: %s", routeRules.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include err?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

}

routeRules.Spec = MakeRouteIstioSpec(route, ns, revisionRoutes)
_, err = routeClient.Update(routeRules)
if err != nil {
if _, err := routeClient.Update(routeRules); err != nil {
c.recorder.Eventf(route, corev1.EventTypeWarning, "UpdateFailed", "Failed to update Istio route rule: %s", routeRules.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

include err?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added.

return nil, err
}
c.recorder.Eventf(route, corev1.EventTypeNormal, "Updated", "Updated Istio route rule: %s", routeRules.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to better understand the wisdom of the parameter in the "Updated" position.

Is this purely informational (terse) or expected to be more like an enumeration that clients can operate on programmatically? I'm trying to capture some of this for Status.Condition convention here, but am not sure what the wisdom is for events?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH, IDK neither. From the Event source code, it says Reason is machine understandable string that gives the reason for the transition into the object's current status.

Here are some samples of events created by k8s:

7m          7m           1         p-79de88cc-3791-4370-b6c0-5197d2cd1705-autoscaler-d7c5c6554z2rn.151be65f06790b80   Pod             spec.containers{autoscaler}      Normal    Pulled                  kubelet, gke-elafros-demo-default-pool-8d4847e8-xw93   Container image "gcr.io/yanweiguo-test/ela-autoscaler@sha256:ffc20211794a1ea1298a6584797db4f5317b892c0970bd8129e65d0b68542a2a" already present on machine
7m          7m           1         p-79de88cc-3791-4370-b6c0-5197d2cd1705-autoscaler-d7c5c6554z2rn.151be65f09dc92c2   Pod             spec.containers{autoscaler}      Normal    Created                 kubelet, gke-elafros-demo-default-pool-8d4847e8-xw93   Created container
7m          7m           1         p-79de88cc-3791-4370-b6c0-5197d2cd1705-autoscaler-d7c5c6554z2rn.151be65f10809f19   Pod             spec.containers{autoscaler}      Normal    Started                 kubelet, gke-elafros-demo-default-pool-8d4847e8-xw93  

I just followed that to use Created or Updated.

c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create Ingress: %s", ingress.Name)
return createErr
}
c.recorder.Eventf(route, corev1.EventTypeNormal, "Created", "Created Ingress: %s", ingress.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we make this "IngressCreated"? IDK what's typical here.

@@ -389,26 +375,33 @@ func (c *Controller) syncTrafficTargets(route *v1alpha1.Route) (*v1alpha1.Route,
updated, err := c.updateStatus(route)
if err != nil {
glog.Warningf("Failed to update service status: %s", err)
c.recorder.Event(route, corev1.EventTypeWarning, "UpdateFailed", "Failed to update traffic targets")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably, makes sense, otherwise the user knows something went wrong, but not what. We should also add details here about which route failed or succeeded. Otherwise, messages of the form (from the PR description):
Normal Updated 29s (x6 over 1m) route-controller Updated traffic targets

don't provide enough information to figure out what happened.

return nil, err
}

c.recorder.Event(route, corev1.EventTypeNormal, "Updated", "Updated traffic targets")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the route name to all these messages. The other messages are nice because you can see what happened and to which object.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -428,8 +421,13 @@ func (c *Controller) createOrUpdateIngress(route *v1alpha1.Route, ns string) err
return err
}
_, createErr := ic.Create(ingress)
if createErr != nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1 :)

config.Name, ela.RouteLabelKey, routeName)
c.recorder.Event(route, corev1.EventTypeWarning, "InvalidConfiguration", errMsg)
return errors.New(errMsg)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't actually know what the best practices here really are. I think logs/events have different retain policies and you could maybe have different rbac rules, etc. Perhaps we should file an issue on this.
At the very least we should clean up our logs:
#361

@google-prow-robot google-prow-robot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Mar 14, 2018
Copy link
Contributor

@vaikas vaikas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for doing this, the one request is global replace of '%s' with %q for fmt. arguments.

@@ -294,11 +294,11 @@ func (c *Controller) syncHandler(key string) error {
created, err := c.elaclientset.BuildV1alpha1().Builds(build.Namespace).Create(build)
if err != nil {
glog.Errorf("Failed to create Build:\n%+v\n%s", build, err)
c.recorder.Eventf(config, corev1.EventTypeWarning, "CreationFailed", "Failed to create Build: %s", build.Name)
c.recorder.Eventf(config, corev1.EventTypeWarning, "CreationFailed", "Failed to create Build '%s': %s", build.Name, err)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of '%s' could you change these to %q
https://golang.org/pkg/fmt/

%q a double-quoted string safely escaped with Go syntax

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Good to know this. Thanks!

return err
}
glog.Infof("Created Build:\n%+v", created.Name)
c.recorder.Eventf(config, corev1.EventTypeNormal, "Created", "Created Build: %s", created.Name)
c.recorder.Eventf(config, corev1.EventTypeNormal, "Created", "Created Build '%s'", created.Name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same here and elsewhere where you use '%s' format.

@@ -209,12 +209,12 @@ func (c *Controller) processNextWorkItem() bool {
// Run the syncHandler, passing it the namespace/name string of the
// Foo resource to be synced.
if err := c.syncHandler(key); err != nil {
return fmt.Errorf("error syncing '%s': %s", key, err.Error())
return fmt.Errorf("error syncing %q: %s", key, err.Error())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't use err.Error(), but should instead use %v and just err.

@@ -294,11 +294,11 @@ func (c *Controller) syncHandler(key string) error {
created, err := c.elaclientset.BuildV1alpha1().Builds(build.Namespace).Create(build)
if err != nil {
glog.Errorf("Failed to create Build:\n%+v\n%s", build, err)
c.recorder.Eventf(config, corev1.EventTypeWarning, "CreationFailed", "Failed to create Build: %s", build.Name)
c.recorder.Eventf(config, corev1.EventTypeWarning, "CreationFailed", "Failed to create Build %q: %s", build.Name, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use %v for err

@@ -328,10 +328,10 @@ func (c *Controller) syncHandler(key string) error {
created, err := c.elaclientset.ElafrosV1alpha1().Revisions(config.Namespace).Create(rev)
if err != nil {
glog.Errorf("Failed to create Revision:\n%+v\n%s", rev, err)
c.recorder.Eventf(config, corev1.EventTypeWarning, "CreationFailed", "Failed to create Revision: %s", rev.Name)
c.recorder.Eventf(config, corev1.EventTypeWarning, "CreationFailed", "Failed to create Revision %q: %s", rev.Name, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%v

@@ -375,10 +375,10 @@ func (c *Controller) syncTrafficTargets(route *v1alpha1.Route) (*v1alpha1.Route,
updated, err := c.updateStatus(route)
if err != nil {
glog.Warningf("Failed to update service status: %s", err)
c.recorder.Event(route, corev1.EventTypeWarning, "UpdateFailed", "Failed to update traffic targets")
c.recorder.Eventf(route, corev1.EventTypeWarning, "UpdateFailed", "Failed to update status for route '%s': %s", route.Name, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q and %v

return nil, err
}
c.recorder.Event(route, corev1.EventTypeNormal, "Updated", "Updated traffic targets")
c.recorder.Eventf(route, corev1.EventTypeNormal, "Updated", "Updated status for route '%s'", route.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q

return nil, err
}

c.recorder.Eventf(route, corev1.EventTypeNormal, "Updated", "Updated status for route '%s'", route.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q

if !apierrs.IsAlreadyExists(err) {
glog.Infof("Failed to create service: %s", err)
c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create service '%s': %s", service.Name, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q and %v

}
glog.Infof("Created service: %q", service.Name)
if newlyCreated {
c.recorder.Eventf(route, corev1.EventTypeNormal, "Created", "Created service '%s'", service.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q

if !apierrs.IsNotFound(err) {
return err
}
_, createErr := ic.Create(ingress)
if _, err := ic.Create(ingress); err != nil {
c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create Ingress '%s': %s", ingress.Name, err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q and %v

c.recorder.Eventf(route, corev1.EventTypeWarning, "CreationFailed", "Failed to create Ingress '%s': %s", ingress.Name, err)
return err
}
c.recorder.Eventf(route, corev1.EventTypeNormal, "Created", "Created Ingress '%s'", ingress.Name)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

%q

@yanweiguo
Copy link
Contributor Author

/retest

Copy link
Member

@mattmoor mattmoor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for bearing with my tidal wave of nits :)

I spotted one typo, but otherwise this LGTM.
-M

@@ -277,12 +277,12 @@ func (c *Controller) processNextWorkItem() bool {
// Run the syncHandler, passing it the namespace/name string of the
// Foo resource to be synced.
if err := c.syncHandler(key); err != nil {
return fmt.Errorf("error syncing '%s': %s", key, err.Error()), controller.PromLabelValueFailure
return fmt.Errorf("error syncing %q: %vs", key, err), controller.PromLabelValueFailure
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: %vs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

@@ -625,7 +625,7 @@ func TestMarkRevReadyUponEndpointBecomesReady(t *testing.T) {
// Look for the revision ready event.
h.OnCreate(&kubeClient.Fake, "events", func(obj runtime.Object) hooks.HookResult {
event := obj.(*corev1.Event)
expectedMessage := "Revision becomes ready upon endpoint 'test-endpoints' becoming ready"
expectedMessage := "Revision becomes ready upon endpoint \"test-endpoints\" becoming ready"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No change needed, but FYI: if you use `Strings "like" this` you can avoid escaping.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good to know. Thanks.

@mattmoor
Copy link
Member

/retest

@yanweiguo yanweiguo merged commit 6ee4002 into knative:master Mar 15, 2018
@yanweiguo yanweiguo deleted the add_events_for_route branch March 15, 2018 16:46
ReToCode pushed a commit to ReToCode/serving that referenced this pull request Aug 3, 2023
Co-authored-by: Kenjiro Nakayama <nakayamakenjiro@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants