From b95d6834a4e441d21c7616886821e98ff8dabcd7 Mon Sep 17 00:00:00 2001 From: Taylor Fahlman Date: Thu, 18 Jul 2019 15:58:37 -0400 Subject: [PATCH] Get current route on update, and use resourceVersions --- pkg/metrics/service.go | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) diff --git a/pkg/metrics/service.go b/pkg/metrics/service.go index 17c9a32..b720858 100644 --- a/pkg/metrics/service.go +++ b/pkg/metrics/service.go @@ -215,19 +215,26 @@ func createOrUpdateService(ctx context.Context, client client.Client, s *v1.Serv //createOrUpdateRoute is a function which creates or updates the route for the service object. func createOrUpdateRoute(ctx context.Context, client client.Client, r *routev1.Route) (*routev1.Route, error) { - err := client.Create(ctx, r) - if err != nil { - if k8serr.IsAlreadyExists(err) { + if err := client.Create(ctx, r); err != nil { + if err != nil { + if !k8serr.IsAlreadyExists(err) { + return nil, err + } + + existingRoute := &routev1.Route{} + err := client.Get(ctx, types.NamespacedName{ + Name: r.Name, + Namespace: r.Namespace, + }, existingRoute) // update the Route - if rUpdateErr := client.Update(ctx, r); rUpdateErr != nil { - log.Info("Error creating metrics route", "Error", rUpdateErr.Error()) - return nil, rUpdateErr + r.ResourceVersion = existingRoute.ResourceVersion + if err = client.Update(ctx, r); err != nil { + log.Info("Error creating metrics route", "Error", err.Error()) + return nil, err } - log.Info("Metrics route object updated", "Route.Name", r.Name, "Route.Namespace", r.Namespace) - return nil, nil + log.Info("Metrics Route object updated Route.Name %v and Route.Namespace %v", r.Name, r.Namespace) + return existingRoute, nil } - log.Info("Error creating metrics route", "Error", err.Error()) - return nil, err } log.Info("Metrics Route object Created", "Route.Name", r.Name, "Route.Namespace", r.Namespace)