Skip to content
This repository has been archived by the owner on Sep 5, 2024. It is now read-only.

Commit

Permalink
Merge pull request #716 from cbron/normalize-weights
Browse files Browse the repository at this point in the history
Normalize computed weights and switch to duration
  • Loading branch information
cbron committed Nov 8, 2019
2 parents 44f4d9a + c0fb3e6 commit 969baf2
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 176 deletions.
38 changes: 19 additions & 19 deletions cli/cmd/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package create
import (
"fmt"
"strconv"
"time"

"github.com/pkg/errors"
"github.com/rancher/rio/cli/cmd/weight"
"github.com/rancher/rio/cli/pkg/clicontext"
"github.com/rancher/rio/cli/pkg/types"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/rio/pkg/riofile/stringers"
"github.com/rancher/wrangler/pkg/kv"
Expand Down Expand Up @@ -66,8 +69,7 @@ type Create struct {
P_Ports []string `desc:"Publish a container's port(s) (format: svcport:containerport/protocol)"`
Privileged bool `desc:"Run container with privilege"`
ReadOnly bool `desc:"Mount the container's root filesystem as read only"`
RolloutInterval int `desc:"Rollout interval in seconds"`
RolloutIncrement int `desc:"Rollout increment value"`
RolloutDuration string `desc:"How long the rollout should take" default:"0s"`
RequestTimeoutSeconds int `desc:"Set request timeout in seconds"`
Secret []string `desc:"Secrets to inject to the service (format: name[/key]:target)"`
Template bool `desc:"If true new version is created per git commit. If false update in-place"`
Expand Down Expand Up @@ -98,20 +100,6 @@ func (c *Create) RunCallback(ctx *clicontext.CLIContext, cb func(service *riov1.
return service, ctx.Create(service)
}

func (c *Create) setRollout(spec *riov1.ServiceSpec) {
if c.RolloutIncrement > 0 || c.RolloutInterval > 0 {
spec.RolloutConfig = &riov1.RolloutConfig{
Increment: c.RolloutIncrement,
IntervalSeconds: c.RolloutInterval,
}
} else if c.Template {
spec.RolloutConfig = &riov1.RolloutConfig{
Increment: 5,
IntervalSeconds: 5,
}
}
}

func (c *Create) setDNS(spec *riov1.ServiceSpec) (err error) {
if len(c.DNS) > 0 || len(c.DNSSearch) > 0 || len(c.DNSOption) > 0 || c.HostDNS {
spec.DNS = &riov1.DNS{
Expand Down Expand Up @@ -199,11 +187,23 @@ func (c *Create) ToService(ctx *clicontext.CLIContext, args []string) (*riov1.Se
}

if c.Weight > 0 {
spec.Weight = &c.Weight
duration, err := time.ParseDuration(c.RolloutDuration)
if err != nil {
return nil, err
}
tempResource := types.Resource{
Name: name,
App: r.App,
Namespace: r.Namespace,
}
weight, rc, err := weight.GenerateWeightAndRolloutConfig(ctx, tempResource, c.Weight, duration, false)
if err != nil {
return nil, err
}
spec.Weight = &weight
spec.RolloutConfig = rc
}

c.setRollout(&spec)

if err := c.setDNS(&spec); err != nil {
return nil, err
}
Expand Down
58 changes: 12 additions & 46 deletions cli/cmd/promote/promote.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@ package promote

import (
"errors"
"fmt"
"time"

"github.com/rancher/rio/cli/cmd/util"
"github.com/rancher/rio/cli/cmd/weight"
"github.com/rancher/rio/cli/pkg/clicontext"
"github.com/rancher/rio/cli/pkg/types"
riov1 "github.com/rancher/rio/pkg/apis/rio.cattle.io/v1"
"github.com/rancher/wrangler/pkg/merr"
"k8s.io/apimachinery/pkg/runtime"
)

type Promote struct {
Increment int `desc:"Amount of weight to increment on each interval" default:"5"`
Interval int `desc:"Interval seconds between each increment" default:"5"`
Pause bool `desc:"Whether to pause rollout or continue it. Default to false" default:"false"`
Duration string `desc:"How long the rollout should take" default:"0s"`
Pause bool `desc:"Whether to pause rollout or continue it. Default to false" default:"false"`
}

func (p *Promote) Run(ctx *clicontext.CLIContext) error {
Expand All @@ -25,46 +20,17 @@ func (p *Promote) Run(ctx *clicontext.CLIContext) error {
return errors.New("at least one argument is needed")
}
serviceName := arg.First()
rolloutConfig := &riov1.RolloutConfig{
Pause: p.Pause,
Increment: p.Increment,
IntervalSeconds: p.Interval,
}
return PerformPromote(ctx, serviceName, rolloutConfig)
}

func PerformPromote(ctx *clicontext.CLIContext, serviceName string, rolloutConfig *riov1.RolloutConfig) error {
var allErrors []error
svcs, err := util.ListAppServicesFromServiceName(ctx, serviceName)
resource, err := ctx.ByID(serviceName)
if err != nil {
return err
}
versionToPromote := ctx.ParseID(serviceName).Version
if versionToPromote == "" {
return errors.New("invalid version specified")
duration, err := time.ParseDuration(p.Duration)
if err != nil {
return err
}
for _, s := range svcs {
err := ctx.UpdateResource(types.Resource{
Namespace: s.Namespace,
Name: s.Name,
App: s.Spec.App,
Version: s.Spec.Version,
Type: types.ServiceType,
}, func(obj runtime.Object) error {
s := obj.(*riov1.Service)
if s.Spec.Weight == nil {
s.Spec.Weight = new(int)
}
s.Spec.RolloutConfig = rolloutConfig
if s.Spec.Version == versionToPromote {
*s.Spec.Weight = 100
fmt.Printf("%s promoted\n", s.Name)
} else {
*s.Spec.Weight = 0
}
return nil
})
allErrors = append(allErrors, err)
promoteWeight, rolloutConfig, err := weight.GenerateWeightAndRolloutConfig(ctx, resource, 100, duration, p.Pause)
if err != nil {
return err
}
return merr.NewErrors(allErrors...)
return weight.PromoteService(ctx, resource, rolloutConfig, promoteWeight)
}
15 changes: 15 additions & 0 deletions cli/cmd/ps/ps_services.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ps

import (
"math"
"strings"

webhookv1 "github.com/rancher/gitwatcher/pkg/apis/gitwatcher.cattle.io/v1"
Expand All @@ -17,6 +18,7 @@ import (
type ServiceData struct {
Name string
Service *riov1.Service
Weight int
Deployment *appsv1.Deployment
DaemonSet *appsv1.DaemonSet
Namespace string
Expand Down Expand Up @@ -116,6 +118,18 @@ func (p *Ps) services(ctx *clicontext.CLIContext) error {
if service.(*riov1.Service).Spec.Template && !p.A_All {
continue
}

weight := 0
if service.(*riov1.Service).Status.ComputedWeight != nil && *service.(*riov1.Service).Status.ComputedWeight > 0 {
totalWeight := 0
for _, subService := range services {
if service.(*riov1.Service).Spec.App == subService.(*riov1.Service).Spec.App && subService.(*riov1.Service).Status.ComputedWeight != nil {
totalWeight += *subService.(*riov1.Service).Status.ComputedWeight
}
}
weight = int(math.Round((float64(*service.(*riov1.Service).Status.ComputedWeight) / float64(totalWeight)) / 0.01)) // round to nearest percent
}

var gitwatcher *webhookv1.GitWatcher
gitwatchers, err := ctx.Gitwatcher.GitWatchers(service.(*riov1.Service).Namespace).List(metav1.ListOptions{})
if err == nil {
Expand All @@ -128,6 +142,7 @@ func (p *Ps) services(ctx *clicontext.CLIContext) error {
}
output = append(output, ServiceData{
Service: service.(*riov1.Service),
Weight: weight,
Namespace: service.(*riov1.Service).Namespace,
Pods: podMap[service.(*riov1.Service).Namespace+"/"+service.(*riov1.Service).Name],
GitWatcher: gitwatcher,
Expand Down
6 changes: 5 additions & 1 deletion cli/cmd/util/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,11 @@ func ListAppServicesFromServiceName(ctx *clicontext.CLIContext, serviceName stri

svc := service.Object.(*riov1.Service)
app, _ := services.AppAndVersion(svc)
return ListAppServicesFromAppName(ctx, namespace, app)

}

func ListAppServicesFromAppName(ctx *clicontext.CLIContext, namespace, appName string) ([]riov1.Service, error) {
svcs, err := ctx.Rio.Services(namespace).List(metav1.ListOptions{})
if err != nil {
return []riov1.Service{}, err
Expand All @@ -27,7 +31,7 @@ func ListAppServicesFromServiceName(ctx *clicontext.CLIContext, serviceName stri
var revisions []riov1.Service
for _, rev := range svcs.Items {
revApp, _ := services.AppAndVersion(&rev)
if revApp == app {
if revApp == appName {
revisions = append(revisions, rev)
}
}
Expand Down
Loading

0 comments on commit 969baf2

Please sign in to comment.