mirror of
https://github.com/openziti/zrok.git
synced 2025-06-25 12:12:32 +02:00
sdk enable; canary enabler (#935)
This commit is contained in:
parent
9fb97725f9
commit
70edfe190b
64
canary/enabler.go
Normal file
64
canary/enabler.go
Normal 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
14
controller/unbootstrap.go
Normal 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
|
||||||
|
}
|
36
sdk/golang/sdk/environment.go
Normal file
36
sdk/golang/sdk/environment.go
Normal 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
|
||||||
|
}
|
@ -2,6 +2,18 @@ package sdk
|
|||||||
|
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
|
type EnableRequest struct {
|
||||||
|
Host string
|
||||||
|
Description string
|
||||||
|
}
|
||||||
|
|
||||||
|
type Environment struct {
|
||||||
|
Host string
|
||||||
|
Description string
|
||||||
|
ZitiIdentity string
|
||||||
|
ZitiConfig string
|
||||||
|
}
|
||||||
|
|
||||||
type BackendMode string
|
type BackendMode string
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
Loading…
x
Reference in New Issue
Block a user