mirror of
https://github.com/openziti/zrok.git
synced 2025-06-19 08:17:05 +02:00
enabler canary; enabling environments (#935)
This commit is contained in:
parent
70edfe190b
commit
ada9d7dba6
@ -10,7 +10,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type EnablerOptions struct {
|
type EnablerOptions struct {
|
||||||
Iterations int
|
Iterations uint
|
||||||
MinDwell time.Duration
|
MinDwell time.Duration
|
||||||
MaxDwell time.Duration
|
MaxDwell time.Duration
|
||||||
MinPacing time.Duration
|
MinPacing time.Duration
|
||||||
@ -18,15 +18,15 @@ type EnablerOptions struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Enabler struct {
|
type Enabler struct {
|
||||||
id uint
|
Id uint
|
||||||
opt *EnablerOptions
|
opt *EnablerOptions
|
||||||
root env_core.Root
|
root env_core.Root
|
||||||
Environments chan<- *sdk.Environment
|
Environments chan *sdk.Environment
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewEnabler(id uint, opt *EnablerOptions, root env_core.Root) *Enabler {
|
func NewEnabler(id uint, opt *EnablerOptions, root env_core.Root) *Enabler {
|
||||||
return &Enabler{
|
return &Enabler{
|
||||||
id: id,
|
Id: id,
|
||||||
opt: opt,
|
opt: opt,
|
||||||
root: root,
|
root: root,
|
||||||
Environments: make(chan *sdk.Environment, opt.Iterations),
|
Environments: make(chan *sdk.Environment, opt.Iterations),
|
||||||
@ -35,7 +35,7 @@ func NewEnabler(id uint, opt *EnablerOptions, root env_core.Root) *Enabler {
|
|||||||
|
|
||||||
func (e *Enabler) Run() {
|
func (e *Enabler) Run() {
|
||||||
defer close(e.Environments)
|
defer close(e.Environments)
|
||||||
defer logrus.Infof("#%d stopping", e.id)
|
defer logrus.Infof("#%d stopping", e.Id)
|
||||||
e.dwell()
|
e.dwell()
|
||||||
e.iterate()
|
e.iterate()
|
||||||
}
|
}
|
||||||
@ -50,9 +50,9 @@ func (e *Enabler) dwell() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (e *Enabler) iterate() {
|
func (e *Enabler) iterate() {
|
||||||
for i := 0; i < e.opt.Iterations; i++ {
|
for i := uint(0); i < e.opt.Iterations; i++ {
|
||||||
env, err := sdk.EnableEnvironment(e.root, &sdk.EnableRequest{
|
env, err := sdk.EnableEnvironment(e.root, &sdk.EnableRequest{
|
||||||
Host: fmt.Sprintf("canary_%d_%d", e.id, i),
|
Host: fmt.Sprintf("canary_%d_%d", e.Id, i),
|
||||||
Description: "canary.Enabler",
|
Description: "canary.Enabler",
|
||||||
})
|
})
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
84
cmd/zrok/testCanaryEnabler.go
Normal file
84
cmd/zrok/testCanaryEnabler.go
Normal file
@ -0,0 +1,84 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/openziti/zrok/canary"
|
||||||
|
"github.com/openziti/zrok/environment"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"math/rand"
|
||||||
|
"time"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
testCanaryCmd.AddCommand(newTestCanaryEnabler().cmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
type testCanaryEnabler struct {
|
||||||
|
cmd *cobra.Command
|
||||||
|
enablers uint
|
||||||
|
iterations uint
|
||||||
|
minPreDelay time.Duration
|
||||||
|
maxPreDelay time.Duration
|
||||||
|
minDwell time.Duration
|
||||||
|
maxDwell time.Duration
|
||||||
|
minPacing time.Duration
|
||||||
|
maxPacing time.Duration
|
||||||
|
}
|
||||||
|
|
||||||
|
func newTestCanaryEnabler() *testCanaryEnabler {
|
||||||
|
cmd := &cobra.Command{
|
||||||
|
Use: "enabler",
|
||||||
|
Short: "Enable a canary enabling environments",
|
||||||
|
Args: cobra.NoArgs,
|
||||||
|
}
|
||||||
|
command := &testCanaryEnabler{cmd: cmd}
|
||||||
|
cmd.Run = command.run
|
||||||
|
cmd.Flags().UintVarP(&command.enablers, "enablers", "e", 1, "Number of concurrent enablers to start")
|
||||||
|
cmd.Flags().UintVarP(&command.iterations, "iterations", "i", 1, "Number of iterations")
|
||||||
|
cmd.Flags().DurationVar(&command.minDwell, "min-dwell", 1*time.Second, "Minimum dwell time")
|
||||||
|
cmd.Flags().DurationVar(&command.maxDwell, "max-dwell", 1*time.Second, "Maximum dwell time")
|
||||||
|
cmd.Flags().DurationVar(&command.minPacing, "min-pacing", 0, "Minimum pacing time")
|
||||||
|
cmd.Flags().DurationVar(&command.maxPacing, "max-pacing", 0, "Maximum pacing time")
|
||||||
|
return command
|
||||||
|
}
|
||||||
|
|
||||||
|
func (cmd *testCanaryEnabler) run(_ *cobra.Command, _ []string) {
|
||||||
|
root, err := environment.LoadRoot()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
var enablers []*canary.Enabler
|
||||||
|
for i := uint(0); i < cmd.enablers; i++ {
|
||||||
|
preDelay := cmd.maxPreDelay.Milliseconds()
|
||||||
|
preDelayDelta := cmd.maxPreDelay.Milliseconds() - cmd.minPreDelay.Milliseconds()
|
||||||
|
if preDelayDelta > 0 {
|
||||||
|
preDelay = int64(rand.Intn(int(preDelayDelta))) + cmd.minPreDelay.Milliseconds()
|
||||||
|
time.Sleep(time.Duration(preDelay) * time.Millisecond)
|
||||||
|
}
|
||||||
|
|
||||||
|
enablerOpts := &canary.EnablerOptions{
|
||||||
|
Iterations: cmd.iterations,
|
||||||
|
MinDwell: cmd.minDwell,
|
||||||
|
MaxDwell: cmd.maxDwell,
|
||||||
|
MinPacing: cmd.minPacing,
|
||||||
|
MaxPacing: cmd.maxPacing,
|
||||||
|
}
|
||||||
|
enabler := canary.NewEnabler(i, enablerOpts, root)
|
||||||
|
enablers = append(enablers, enabler)
|
||||||
|
go enabler.Run()
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, enabler := range enablers {
|
||||||
|
enablerLoop:
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case env, ok := <-enabler.Environments:
|
||||||
|
if !ok {
|
||||||
|
break enablerLoop
|
||||||
|
}
|
||||||
|
logrus.Infof("enabler #%d: %v", enabler.Id, env.ZitiIdentity)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -8,10 +8,6 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func EnableEnvironment(env env_core.Root, request *EnableRequest) (*Environment, error) {
|
func EnableEnvironment(env env_core.Root, request *EnableRequest) (*Environment, error) {
|
||||||
if env.IsEnabled() {
|
|
||||||
return nil, errors.New("environment is already enabled!")
|
|
||||||
}
|
|
||||||
|
|
||||||
zrok, err := env.Client()
|
zrok, err := env.Client()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, errors.Wrap(err, "could not create zrok client")
|
return nil, errors.Wrap(err, "could not create zrok client")
|
||||||
|
Loading…
x
Reference in New Issue
Block a user