sdk enable; canary enabler (#935)

This commit is contained in:
Michael Quigley 2025-04-16 12:01:44 -04:00
parent 9fb97725f9
commit 70edfe190b
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
4 changed files with 126 additions and 0 deletions

64
canary/enabler.go Normal file
View File

@ -0,0 +1,64 @@
package canary
import (
"fmt"
"github.com/openziti/zrok/environment/env_core"
"github.com/openziti/zrok/sdk/golang/sdk"
"github.com/sirupsen/logrus"
"math/rand"
"time"
)
type EnablerOptions struct {
Iterations int
MinDwell time.Duration
MaxDwell time.Duration
MinPacing time.Duration
MaxPacing time.Duration
}
type Enabler struct {
id uint
opt *EnablerOptions
root env_core.Root
Environments chan<- *sdk.Environment
}
func NewEnabler(id uint, opt *EnablerOptions, root env_core.Root) *Enabler {
return &Enabler{
id: id,
opt: opt,
root: root,
Environments: make(chan *sdk.Environment, opt.Iterations),
}
}
func (e *Enabler) Run() {
defer close(e.Environments)
defer logrus.Infof("#%d stopping", e.id)
e.dwell()
e.iterate()
}
func (e *Enabler) dwell() {
dwell := e.opt.MinDwell.Milliseconds()
dwelta := e.opt.MaxDwell.Milliseconds() - e.opt.MinDwell.Milliseconds()
if dwelta > 0 {
dwell = int64(rand.Intn(int(dwelta)) + int(e.opt.MinDwell.Milliseconds()))
}
time.Sleep(time.Duration(dwell) * time.Millisecond)
}
func (e *Enabler) iterate() {
for i := 0; i < e.opt.Iterations; i++ {
env, err := sdk.EnableEnvironment(e.root, &sdk.EnableRequest{
Host: fmt.Sprintf("canary_%d_%d", e.id, i),
Description: "canary.Enabler",
})
if err == nil {
e.Environments <- env
} else {
logrus.Errorf("error creating canary environment: %v", err)
}
}
}

14
controller/unbootstrap.go Normal file
View File

@ -0,0 +1,14 @@
package controller
import (
"github.com/openziti/zrok/controller/config"
"github.com/openziti/zrok/controller/zrokEdgeSdk"
)
func Unbootstrap(cfg *config.Config) error {
_, err := zrokEdgeSdk.Client(cfg.Ziti)
if err != nil {
return err
}
return nil
}

View File

@ -0,0 +1,36 @@
package sdk
import (
httptransport "github.com/go-openapi/runtime/client"
"github.com/openziti/zrok/environment/env_core"
restEnvironment "github.com/openziti/zrok/rest_client_zrok/environment"
"github.com/pkg/errors"
)
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")
}
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Environment().AccountToken)
req := restEnvironment.NewEnableParams()
req.Body.Description = request.Description
req.Body.Host = request.Host
resp, err := zrok.Environment.Enable(req, auth)
if err != nil {
return nil, err
}
return &Environment{
Host: request.Host,
Description: request.Description,
ZitiIdentity: resp.Payload.Identity,
ZitiConfig: resp.Payload.Cfg,
}, nil
}

View File

@ -2,6 +2,18 @@ package sdk
import "time"
type EnableRequest struct {
Host string
Description string
}
type Environment struct {
Host string
Description string
ZitiIdentity string
ZitiConfig string
}
type BackendMode string
const (