-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: add json logs test environment
Add an option to `talosctl cluster create` to start a JSON log receiver, and enabled it optionally. Enable in `integration-qemu`. See #9510 Signed-off-by: Andrey Smirnov <[email protected]>
- Loading branch information
Showing
12 changed files
with
214 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// 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 mgmt | ||
|
||
import ( | ||
"bufio" | ||
"log" | ||
"net" | ||
"net/netip" | ||
|
||
"github.com/spf13/cobra" | ||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
var jsonLogsLaunchCmdFlags struct { | ||
addr string | ||
} | ||
|
||
// jsonLogsLaunchCmd represents the kms-launch command. | ||
var jsonLogsLaunchCmd = &cobra.Command{ | ||
Use: "json-logs-launch", | ||
Short: "Internal command used by QEMU provisioner", | ||
Long: ``, | ||
Args: cobra.NoArgs, | ||
Hidden: true, | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
lis, err := net.Listen("tcp", jsonLogsLaunchCmdFlags.addr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("starting JSON logs server on %s", jsonLogsLaunchCmdFlags.addr) | ||
|
||
eg, ctx := errgroup.WithContext(cmd.Context()) | ||
|
||
eg.Go(func() error { | ||
for { | ||
conn, err := lis.Accept() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
go func() { | ||
defer conn.Close() //nolint:errcheck | ||
|
||
remoteAddr := conn.RemoteAddr().String() | ||
|
||
if addr, err := netip.ParseAddrPort(remoteAddr); err == nil { | ||
remoteAddr = addr.Addr().String() | ||
} | ||
|
||
scanner := bufio.NewScanner(conn) | ||
|
||
for scanner.Scan() { | ||
log.Printf("%s: %s", remoteAddr, scanner.Text()) | ||
} | ||
}() | ||
} | ||
}) | ||
|
||
eg.Go(func() error { | ||
<-ctx.Done() | ||
|
||
return lis.Close() | ||
}) | ||
|
||
return eg.Wait() | ||
}, | ||
} | ||
|
||
func init() { | ||
jsonLogsLaunchCmd.Flags().StringVar(&jsonLogsLaunchCmdFlags.addr, "addr", "localhost:3000", "JSON logs listen address") | ||
addCommand(jsonLogsLaunchCmd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -74,7 +74,7 @@ var kmsLaunchCmd = &cobra.Command{ | |
return nil | ||
}) | ||
|
||
return s.Serve(lis) | ||
return eg.Wait() | ||
}, | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
// 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 ( | ||
"crypto/rand" | ||
"fmt" | ||
"io" | ||
"os" | ||
"os/exec" | ||
"strconv" | ||
"syscall" | ||
|
||
"github.com/siderolabs/talos/pkg/provision" | ||
) | ||
|
||
const ( | ||
jsonLogsPid = "json-logs.pid" | ||
jsonLogsLog = "json-logs.log" | ||
) | ||
|
||
// CreateJSONLogs creates JSON logs server. | ||
func (p *Provisioner) CreateJSONLogs(state *State, clusterReq provision.ClusterRequest, options provision.Options) error { | ||
pidPath := state.GetRelativePath(jsonLogsPid) | ||
|
||
logFile, err := os.OpenFile(state.GetRelativePath(jsonLogsLog), os.O_APPEND|os.O_CREATE|os.O_RDWR, 0o666) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
defer logFile.Close() //nolint:errcheck | ||
|
||
key := make([]byte, 32) | ||
if _, err = io.ReadFull(rand.Reader, key); err != nil { | ||
return err | ||
} | ||
|
||
args := []string{ | ||
"json-logs-launch", | ||
"--addr", options.JSONLogsEndpoint, | ||
} | ||
|
||
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 = os.WriteFile(pidPath, []byte(strconv.Itoa(cmd.Process.Pid)), os.ModePerm); err != nil { | ||
return fmt.Errorf("error writing LB PID file: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// DestroyJSONLogs destroys JSON logs server. | ||
func (p *Provisioner) DestroyJSONLogs(state *State) error { | ||
pidPath := state.GetRelativePath(jsonLogsPid) | ||
|
||
return StopProcessByPidfile(pidPath) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters