TUN-10390: Call prechecks

Final run method, which runs cloudlflared pre-checks for both the normal startup procedure, as well as cloudflared diag.

For cloudflared diag, this produces a new json output to which is added to the final zip file.

Also added in a new flag to prevent this from running all the time, at least for now until we are 100% sure this works as intended. We will later remove this flag, only leaving in `--no-prechecks`, so this runs by default for everyone using cloudflared.

Tested pre-checks locally with origintunneld. The results show all pre-checks succeeding. In this case, it ran with only 1 region, since locally we run it with `--edge origintunneld1:7844`.

![Screenshot 2026-05-07 at 13.19.19.png](/uploads/8d0031d7c819d8a761707fe9d845667f/Screenshot_2026-05-07_at_13.19.19.png){width=900 height=217}
This commit is contained in:
Miguel da Costa Martins Marcelino
2026-05-07 17:27:58 +00:00
parent 22a955f7bb
commit a67c583bf1
7 changed files with 126 additions and 2 deletions

View File

@@ -34,4 +34,5 @@ const (
cliConfigurationBaseName = "cli-configuration.json"
configurationBaseName = "configuration.json"
taskResultBaseName = "task-result.json"
prechecksBaseName = "prechecks.json"
)

View File

@@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/url"
"os"
"path/filepath"
@@ -16,6 +17,8 @@ import (
"github.com/rs/zerolog"
network "github.com/cloudflare/cloudflared/diagnostic/network"
"github.com/cloudflare/cloudflared/edgediscovery/allregions"
"github.com/cloudflare/cloudflared/prechecks"
)
const (
@@ -32,6 +35,7 @@ const (
networkInformationJobName = "network information"
cliConfigurationJobName = "cli configuration"
configurationJobName = "configuration"
prechecksJobName = "connectivity pre-checks"
)
// Struct used to hold the results of different routines executing the network collection.
@@ -92,6 +96,7 @@ type Options struct {
Address string
ContainerID string
PodID string
Region string
Toggles Toggles
}
@@ -230,7 +235,7 @@ func networkInformationCollectors() (rawNetworkCollector, jsonNetworkCollector c
}
func rawNetworkInformationWriter(resultMap map[string]networkCollectionResult) (string, error) {
// nolint: gosec
// nolint: gosec // Intentionally creating a temporary diagnostic file in the OS temp directory.
networkDumpHandle, err := os.Create(filepath.Join(os.TempDir(), rawNetworkBaseName))
if err != nil {
return "", ErrCreatingTemporaryFile
@@ -372,6 +377,7 @@ func resolveInstanceBaseURL(
func createJobs(
client *httpClient,
tunnel *TunnelState,
region string,
diagContainer string,
diagPod string,
noDiagSystem bool,
@@ -434,11 +440,55 @@ func createJobs(
fn: collectFromEndpointAdapter(client.GetTunnelConfiguration, configurationBaseName),
bypass: false,
},
{
jobName: prechecksJobName,
fn: collectPrechecks(region),
bypass: noDiagNetwork,
},
}
return jobs
}
// collectPrechecks runs connectivity pre-checks and writes the results to a JSON file.
func collectPrechecks(region string) collectFunc {
return func(ctx context.Context) (string, error) {
cfg := prechecks.Config{
Region: region,
IPVersion: allregions.Auto,
Timeout: defaultTimeout,
}
// Create a no-op logger since we don't want to spam logs during diagnostic collection
log := zerolog.New(io.Discard)
dialers := prechecks.RunDialers{
DNSResolver: &prechecks.EdgeDNSResolver{Log: &log},
TCPDialer: &prechecks.EdgeTCPDialer{},
QUICDialer: &prechecks.EdgeQUICDialer{},
ManagementDialer: &prechecks.NetManagementDialer{Dialer: net.Dialer{}},
}
emptyCert := ""
report := prechecks.Run(ctx, emptyCert, cfg, &log, dialers)
// Write the report to a JSON file
// nolint: gosec
dumpHandle, err := os.Create(filepath.Join(os.TempDir(), prechecksBaseName))
if err != nil {
return "", ErrCreatingTemporaryFile
}
defer func() { _ = dumpHandle.Close() }()
encoder := newFormattedEncoder(dumpHandle)
if err := encoder.Encode(report); err != nil {
return dumpHandle.Name(), fmt.Errorf("error encoding prechecks report: %w", err)
}
return dumpHandle.Name(), nil
}
}
func createTaskReport(taskReport map[string]taskResult) (string, error) {
// nolint: gosec
dumpHandle, err := os.Create(filepath.Join(os.TempDir(), taskResultBaseName))
@@ -527,6 +577,7 @@ func RunDiagnostic(
jobs := createJobs(
client,
tunnel,
options.Region,
options.ContainerID,
options.PodID,
options.Toggles.NoDiagSystem,