Skip to content

Commit

Permalink
fix(service update): Retry an update in case of a conflict.
Browse files Browse the repository at this point in the history
Is related to #224 and should fix one flake.
  • Loading branch information
rhuss committed Jul 8, 2019
1 parent 314be82 commit d7e4133
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 26 deletions.
5 changes: 5 additions & 0 deletions pkg/kn/commands/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import (
"github.com/spf13/cobra"
)

const (
// How often to retry in case of an optimistic lock error when replacing a service (--force)
MaxUpdateRetries = 3
)

func NewServiceCommand(p *commands.KnParams) *cobra.Command {
serviceCmd := &cobra.Command{
Use: "service",
Expand Down
30 changes: 20 additions & 10 deletions pkg/kn/commands/service/service_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,17 +131,27 @@ func createService(client serving_v1alpha1_client.ServingV1alpha1Interface, serv
}

func replaceService(client serving_v1alpha1_client.ServingV1alpha1Interface, service *serving_v1alpha1_api.Service, namespace string, out io.Writer) error {
existingService, err := client.Services(namespace).Get(service.Name, v1.GetOptions{})
if err != nil {
return err
}
service.ResourceVersion = existingService.ResourceVersion
_, err = client.Services(namespace).Update(service)
if err != nil {
return err
var retries = 0
for true {
existingService, err := client.Services(namespace).Get(service.Name, v1.GetOptions{})
if err != nil {
return err
}
service.ResourceVersion = existingService.ResourceVersion
_, err = client.Services(namespace).Update(service)
if err != nil {
// Retry to update when a resource version conflict exists
if api_errors.IsConflict(err) && retries < MaxUpdateRetries {
retries++
continue
}
return err
}
fmt.Fprintf(out, "Service '%s' successfully replaced in namespace '%s'.\n", service.Name, namespace)
return nil
}
fmt.Fprintf(out, "Service '%s' successfully replaced in namespace '%s'.\n", service.Name, namespace)
return nil
// This line will be never reached, but go insists on having it
return fmt.Errorf("can not update service '%s' in namespace '%s'", service.Name, namespace)
}

func serviceExists(client serving_v1alpha1_client.ServingV1alpha1Interface, name string, namespace string) (bool, error) {
Expand Down
43 changes: 27 additions & 16 deletions pkg/kn/commands/service/service_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ import (
"errors"
"fmt"

"github.com/knative/client/pkg/kn/commands"
"github.com/spf13/cobra"
api_errors "k8s.io/apimachinery/pkg/api/errors"
v1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/knative/client/pkg/kn/commands"
)

func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
Expand Down Expand Up @@ -53,24 +55,33 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
return err
}

service, err := client.Services(namespace).Get(args[0], v1.GetOptions{})
if err != nil {
return err
}
service = service.DeepCopy()
var retries = 0
for true {
service, err := client.Services(namespace).Get(args[0], v1.GetOptions{})
if err != nil {
return err
}
service = service.DeepCopy()

err = editFlags.Apply(service, cmd)
if err != nil {
return err
}
err = editFlags.Apply(service, cmd)
if err != nil {
return err
}

_, err = client.Services(namespace).Update(service)
if err != nil {
return err
_, err = client.Services(namespace).Update(service)
if err != nil {
// Retry to update when a resource version conflict exists
if api_errors.IsConflict(err) && retries < MaxUpdateRetries {
retries++
continue
}
return err
}
fmt.Fprintf(cmd.OutOrStdout(), "Service '%s' updated in namespace '%s'.\n", args[0], namespace)
return nil
}

fmt.Fprintf(cmd.OutOrStdout(), "Service '%s' updated in namespace '%s'.\n", args[0], namespace)
return nil
// This line will be never reached, but go insists on having it
return fmt.Errorf("can not update service '%s' in namespace '%s'", args[0], namespace)
},
}

Expand Down

0 comments on commit d7e4133

Please sign in to comment.