-
Notifications
You must be signed in to change notification settings - Fork 232
/
daemon.go
138 lines (126 loc) · 3.32 KB
/
daemon.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package cmd
import (
"fmt"
"net"
"net/http"
"os"
"path/filepath"
"regexp"
"runtime"
"time"
crcConfig "github.com/code-ready/crc/pkg/crc/config"
"github.com/code-ready/crc/pkg/crc/constants"
"github.com/code-ready/crc/pkg/crc/logging"
"github.com/code-ready/gvisor-tap-vsock/pkg/transport"
"github.com/code-ready/gvisor-tap-vsock/pkg/types"
"github.com/code-ready/gvisor-tap-vsock/pkg/virtualnetwork"
"github.com/docker/go-units"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(daemonCmd)
}
var daemonCmd = &cobra.Command{
Use: "daemon",
Short: "Run the crc daemon",
Long: "Run the crc daemon",
Hidden: true,
RunE: func(cmd *cobra.Command, args []string) error {
// setup separate logging for daemon
logging.CloseLogging()
logging.InitLogrus(logging.LogLevel, constants.DaemonLogFilePath)
go runDaemon()
var endpoints []string
if runtime.GOOS == "windows" {
endpoints = append(endpoints, transport.DefaultURL)
} else {
_ = os.Remove(constants.NetworkSocketPath)
endpoints = append(endpoints, fmt.Sprintf("unix://%s", constants.NetworkSocketPath))
if runtime.GOOS == "linux" {
endpoints = append(endpoints, transport.DefaultURL)
}
}
err := run(&types.Configuration{
Debug: false, // never log packets
CaptureFile: captureFile(),
MTU: 4000, // Large packets slightly improve the performance. Less small packets.
Subnet: "192.168.127.0/24",
GatewayIP: constants.VSockGateway,
GatewayMacAddress: "\x5A\x94\xEF\xE4\x0C\xDD",
DNS: []types.Zone{
{
Name: "apps-crc.testing.",
DefaultIP: net.ParseIP("192.168.127.2"),
},
{
Name: "crc.testing.",
Records: []types.Record{
{
Name: "gateway",
IP: net.ParseIP("192.168.127.1"),
},
{
Name: "api",
IP: net.ParseIP("192.168.127.2"),
},
{
Name: "api-int",
IP: net.ParseIP("192.168.127.2"),
},
{
Regexp: regexp.MustCompile("crc-(.*?)-master-0"),
IP: net.ParseIP("192.168.126.11"),
},
},
},
},
Forwards: map[string]string{
fmt.Sprintf(":%d", constants.VsockSSHPort): "192.168.127.2:22",
":6443": "192.168.127.2:6443",
":443": "192.168.127.2:443",
},
}, endpoints)
return err
},
}
func captureFile() string {
if !isDebugLog() {
return ""
}
return filepath.Join(constants.CrcBaseDir, "capture.pcap")
}
func run(configuration *types.Configuration, endpoints []string) error {
vn, err := virtualnetwork.New(configuration)
if err != nil {
return err
}
log.Info("waiting for clients...")
errCh := make(chan error)
for _, endpoint := range endpoints {
log.Infof("listening %s", endpoint)
ln, err := transport.Listen(endpoint)
if err != nil {
return errors.Wrap(err, "cannot listen")
}
go func() {
if err := http.Serve(ln, vn.Mux()); err != nil {
errCh <- err
}
}()
}
if isDebugLog() {
go func() {
for {
fmt.Printf("%v sent to the VM, %v received from the VM\n", units.HumanSize(float64(vn.BytesSent())), units.HumanSize(float64(vn.BytesReceived())))
time.Sleep(5 * time.Second)
}
}()
}
return <-errCh
}
func newConfig() (crcConfig.Storage, error) {
config, _, err := newViperConfig()
return config, err
}