mirror of https://github.com/knative/client.git
fix(service update): Retry an update in case of a conflict. (#240)
* fix(service update): Retry an update in case of a conflict. Is related to #224 and should fix one flake. * chore(service): Fixed retry-loop for kn service create/update
This commit is contained in:
parent
9f62341dcf
commit
e1614f95e6
|
|
@ -19,6 +19,11 @@ import (
|
||||||
"github.com/spf13/cobra"
|
"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 {
|
func NewServiceCommand(p *commands.KnParams) *cobra.Command {
|
||||||
serviceCmd := &cobra.Command{
|
serviceCmd := &cobra.Command{
|
||||||
Use: "service",
|
Use: "service",
|
||||||
|
|
|
||||||
|
|
@ -147,6 +147,8 @@ func createService(client v1alpha1.KnClient, service *serving_v1alpha1_api.Servi
|
||||||
}
|
}
|
||||||
|
|
||||||
func replaceService(client v1alpha1.KnClient, service *serving_v1alpha1_api.Service, namespace string, out io.Writer) error {
|
func replaceService(client v1alpha1.KnClient, service *serving_v1alpha1_api.Service, namespace string, out io.Writer) error {
|
||||||
|
var retries = 0
|
||||||
|
for {
|
||||||
existingService, err := client.GetService(service.Name)
|
existingService, err := client.GetService(service.Name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -154,10 +156,16 @@ func replaceService(client v1alpha1.KnClient, service *serving_v1alpha1_api.Serv
|
||||||
service.ResourceVersion = existingService.ResourceVersion
|
service.ResourceVersion = existingService.ResourceVersion
|
||||||
err = client.UpdateService(service)
|
err = client.UpdateService(service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Retry to update when a resource version conflict exists
|
||||||
|
if api_errors.IsConflict(err) && retries < MaxUpdateRetries {
|
||||||
|
retries++
|
||||||
|
continue
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
fmt.Fprintf(out, "Service '%s' successfully replaced in namespace '%s'.\n", service.Name, namespace)
|
fmt.Fprintf(out, "Service '%s' successfully replaced in namespace '%s'.\n", service.Name, namespace)
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func serviceExists(client v1alpha1.KnClient, name string, namespace string) (bool, error) {
|
func serviceExists(client v1alpha1.KnClient, name string, namespace string) (bool, error) {
|
||||||
|
|
|
||||||
|
|
@ -18,8 +18,10 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
"github.com/knative/client/pkg/kn/commands"
|
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
|
api_errors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
|
|
||||||
|
"github.com/knative/client/pkg/kn/commands"
|
||||||
)
|
)
|
||||||
|
|
||||||
func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
|
func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
|
||||||
|
|
@ -52,6 +54,8 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var retries = 0
|
||||||
|
for {
|
||||||
service, err := client.GetService(args[0])
|
service, err := client.GetService(args[0])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
|
@ -65,11 +69,16 @@ func NewServiceUpdateCommand(p *commands.KnParams) *cobra.Command {
|
||||||
|
|
||||||
err = client.UpdateService(service)
|
err = client.UpdateService(service)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// Retry to update when a resource version conflict exists
|
||||||
|
if api_errors.IsConflict(err) && retries < MaxUpdateRetries {
|
||||||
|
retries++
|
||||||
|
continue
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
fmt.Fprintf(cmd.OutOrStdout(), "Service '%s' updated in namespace '%s'.\n", args[0], namespace)
|
fmt.Fprintf(cmd.OutOrStdout(), "Service '%s' updated in namespace '%s'.\n", args[0], namespace)
|
||||||
return nil
|
return nil
|
||||||
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue