Files
zt/zttest/ztlab/component_zt_tunnel.go
zeekay 448c41866f Merge upstream/main: sync with openziti (15 commits)
Upstream changes include:
- Add get/set db version commands (#3579)
- Fix link dial race condition (#3566)
- Update channel version to v4.3.5
- Retire ingress-nginx in favor of Traefik (#3582)
- Appease golangci-lint (#3594)
- Fix flaky certchecker and link tests
- Enable verbose logging for Go SDK apps
- Improve router quickstart crash recovery

Conflicts resolved keeping hanzozt branding while incorporating
all upstream code improvements.
2026-02-22 00:49:18 -08:00

232 lines
5.7 KiB
Go

/*
Copyright 2019 NetFoundry Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package ztlab
import (
"fmt"
"strings"
"github.com/hanzozt/fablab/kernel/model"
"github.com/hanzozt/foundation/v2/versions"
"github.com/hanzozt/zt/zttest/ztlab/stagezt"
"github.com/sirupsen/logrus"
)
var _ model.ComponentType = (*ZitiTunnelType)(nil)
type ZitiTunnelMode int
const (
ZitiTunnelModeTproxy ZitiTunnelMode = 0
ZitiTunnelModeProxy ZitiTunnelMode = 1
ZitiTunnelModeHost ZitiTunnelMode = 2
ZitiTunnelActionsReEnroll = "reEnroll"
)
func (self ZitiTunnelMode) String() string {
if self == ZitiTunnelModeTproxy {
return "tproxy"
}
if self == ZitiTunnelModeProxy {
return "proxy"
}
if self == ZitiTunnelModeHost {
return "host"
}
return "invalid"
}
type ZitiTunnelType struct {
Mode ZitiTunnelMode
Version string
LocalPath string
ConfigPathF func(c *model.Component) string
Count uint8
DefaultRouterConnections uint8
ControlRouterConnections uint8
EnableSdkFlowControl bool
Verbose bool
}
func (self *ZitiTunnelType) Label() string {
return "zt-tunnel"
}
func (self *ZitiTunnelType) GetVersion() string {
return self.Version
}
func (self *ZitiTunnelType) SetVersion(version string) {
self.Version = version
}
func (self *ZitiTunnelType) GetActions() map[string]model.ComponentAction {
return map[string]model.ComponentAction{
ZitiTunnelActionsReEnroll: model.ComponentActionF(self.ReEnroll),
}
}
func (self *ZitiTunnelType) InitType(*model.Component) {
canonicalizeGoAppVersion(&self.Version)
if self.Count < 1 {
self.Count = 1
}
if self.DefaultRouterConnections == 0 {
self.DefaultRouterConnections = 1
}
if self.DefaultRouterConnections > 5 {
self.DefaultRouterConnections = 5
}
if self.ControlRouterConnections > 1 {
self.ControlRouterConnections = 1
}
if self.DefaultRouterConnections > 1 {
self.EnableSdkFlowControl = true
}
}
func (self *ZitiTunnelType) Dump() any {
return map[string]string{
"type_id": "zt-tunnel",
"version": self.Version,
"local_path": self.LocalPath,
}
}
func (self *ZitiTunnelType) StageFiles(r model.Run, c *model.Component) error {
return stagezt.StageZitiOnce(r, c, self.Version, self.LocalPath)
}
func (self *ZitiTunnelType) InitializeHost(_ model.Run, c *model.Component) error {
if self.Mode == ZitiTunnelModeTproxy {
return setupDnsForTunneler(c)
}
return nil
}
func (self *ZitiTunnelType) getProcessFilter(c *model.Component) func(string) bool {
return getZitiProcessFilter(c, "tunnel")
}
func (self *ZitiTunnelType) IsRunning(_ model.Run, c *model.Component) (bool, error) {
pids, err := c.GetHost().FindProcesses(self.getProcessFilter(c))
if err != nil {
return false, err
}
return len(pids) > 0, nil
}
func (self *ZitiTunnelType) GetConfigPath(c *model.Component) string {
if self.ConfigPathF != nil {
return self.ConfigPathF(c)
}
return fmt.Sprintf("/home/%s/fablab/cfg/%s.json", c.GetHost().GetSshUser(), c.Id)
}
func (self *ZitiTunnelType) Start(_ model.Run, c *model.Component) error {
pids, err := c.GetHost().FindProcesses(self.getProcessFilter(c))
if err != nil {
return err
}
if len(pids) >= int(self.Count) {
fmt.Printf("zt tunnel(s) %s already started\n", c.Id)
return nil
}
count := int(self.Count) - len(pids)
start := 0
if len(pids) > 0 {
start = int(self.Count)
}
for n := range count {
if err = self.StartIndividual(c, start+n); err != nil {
return err
}
}
return nil
}
func (self *ZitiTunnelType) StartIndividual(c *model.Component, idx int) error {
mode := self.Mode
user := c.GetHost().GetSshUser()
binaryPath := GetZitiBinaryPath(c, self.Version)
configPath := self.GetConfigPath(c)
logsPath := fmt.Sprintf("/home/%s/logs/%s-%v.log", user, c.Id, idx)
useSudo := ""
if mode == ZitiTunnelModeTproxy {
useSudo = "sudo"
}
supportsSdkFlowControl := self.Version == ""
if self.Version != "" {
v := versions.MustParseSemVer(self.Version)
supportsSdkFlowControl = v.CompareTo(versions.MustParseSemVer("v1.6.6")) >= 0
}
connectCfg := ""
if supportsSdkFlowControl {
if self.ControlRouterConnections > 0 {
connectCfg += fmt.Sprintf("--control-connections %d", self.ControlRouterConnections)
}
if self.DefaultRouterConnections > 1 {
connectCfg += fmt.Sprintf(" --default-connections %d", self.DefaultRouterConnections)
}
if self.EnableSdkFlowControl {
connectCfg += fmt.Sprintf(" --sdk-flow-control=%v", self.EnableSdkFlowControl)
}
}
verbose := ""
if self.Verbose {
verbose = "-v"
}
serviceCmd := fmt.Sprintf("%s %s tunnel %s %s %s --cli-agent-alias %s --log-formatter json -i %s > %s 2>&1 &",
useSudo, binaryPath, mode.String(), connectCfg, verbose, c.Id, configPath, logsPath)
value, err := c.Host.ExecLogged(
"rm -f "+logsPath,
serviceCmd)
if err != nil {
return err
}
if len(value) > 0 {
logrus.Infof("output [%s]", strings.Trim(value, " \t\r\n"))
}
return nil
}
func (self *ZitiTunnelType) Stop(_ model.Run, c *model.Component) error {
return c.GetHost().KillProcesses("-KILL", self.getProcessFilter(c))
}
func (self *ZitiTunnelType) ReEnroll(run model.Run, c *model.Component) error {
return reEnrollIdentity(run, c, GetZitiBinaryPath(c, self.Version), self.GetConfigPath(c))
}