talos/pkg/provision/providers/vm/loadbalancer.go
Andrey Smirnov 9379cf9ee1 refactor: expose provision as public package
This change is only moving packages and updating import paths.

Goal: expose `internal/pkg/provision` as `pkg/provision` to enable other
projects to import Talos provisioning library.

As cluster checks are almost always required as part of provisioning
process, package `internal/pkg/cluster` was also made public as
`pkg/cluster`.

Other changes were direct dependencies discovered by `importvet` which
were updated.

Public packages (useful, general purpose packages with stable API):

* `internal/pkg/conditions` -> `pkg/conditions`
* `internal/pkg/tail` -> `pkg/tail`

Private packages (used only on provisioning library internally):

* `internal/pkg/inmemhttp` -> `pkg/provision/internal/inmemhttp`
* `internal/pkg/kernel/vmlinuz` -> `pkg/provision/internal/vmlinuz`
* `internal/pkg/cniutils` -> `pkg/provision/internal/cniutils`

Signed-off-by: Andrey Smirnov <smirnov.andrey@gmail.com>
2020-08-12 05:12:05 -07:00

72 lines
1.7 KiB
Go

// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package vm
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"strconv"
"strings"
"syscall"
"github.com/talos-systems/talos/pkg/provision"
)
const (
lbPid = "lb.pid"
lbLog = "lb.log"
)
// CreateLoadBalancer creates load balancer.
func (p *Provisioner) CreateLoadBalancer(state *State, clusterReq provision.ClusterRequest) error {
pidPath := state.GetRelativePath(lbPid)
logFile, err := os.OpenFile(state.GetRelativePath(lbLog), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0o666)
if err != nil {
return err
}
defer logFile.Close() //nolint: errcheck
masterNodes := clusterReq.Nodes.MasterNodes()
masterIPs := make([]string, len(masterNodes))
for i := range masterIPs {
masterIPs[i] = masterNodes[i].IP.String()
}
args := []string{
"loadbalancer-launch",
"--loadbalancer-addr", clusterReq.Network.GatewayAddr.String(),
"--loadbalancer-upstreams", strings.Join(masterIPs, ","),
}
cmd := exec.Command(clusterReq.SelfExecutable, args...)
cmd.Stdout = logFile
cmd.Stderr = logFile
cmd.SysProcAttr = &syscall.SysProcAttr{
Setsid: true, // daemonize
}
if err = cmd.Start(); err != nil {
return err
}
if err = ioutil.WriteFile(pidPath, []byte(strconv.Itoa(cmd.Process.Pid)), os.ModePerm); err != nil {
return fmt.Errorf("error writing LB PID file: %w", err)
}
return nil
}
// DestroyLoadBalancer destoys load balancer.
func (p *Provisioner) DestroyLoadBalancer(state *State) error {
pidPath := state.GetRelativePath(lbPid)
return stopProcessByPidfile(pidPath)
}