enabler canary; enabling environments (#935)

This commit is contained in:
Michael Quigley 2025-04-16 17:00:14 -04:00
parent 70edfe190b
commit ada9d7dba6
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 91 additions and 11 deletions

View File

@ -10,7 +10,7 @@ import (
)
type EnablerOptions struct {
Iterations int
Iterations uint
MinDwell time.Duration
MaxDwell time.Duration
MinPacing time.Duration
@ -18,15 +18,15 @@ type EnablerOptions struct {
}
type Enabler struct {
id uint
Id uint
opt *EnablerOptions
root env_core.Root
Environments chan<- *sdk.Environment
Environments chan *sdk.Environment
}
func NewEnabler(id uint, opt *EnablerOptions, root env_core.Root) *Enabler {
return &Enabler{
id: id,
Id: id,
opt: opt,
root: root,
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() {
defer close(e.Environments)
defer logrus.Infof("#%d stopping", e.id)
defer logrus.Infof("#%d stopping", e.Id)
e.dwell()
e.iterate()
}
@ -50,9 +50,9 @@ func (e *Enabler) dwell() {
}
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{
Host: fmt.Sprintf("canary_%d_%d", e.id, i),
Host: fmt.Sprintf("canary_%d_%d", e.Id, i),
Description: "canary.Enabler",
})
if err == nil {

View 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)
}
}
}
}

View File

@ -8,10 +8,6 @@ import (
)
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()
if err != nil {
return nil, errors.Wrap(err, "could not create zrok client")