Lorenz Brun | 52f7f29 | 2020-06-24 16:42:02 +0200 | [diff] [blame] | 1 | // Copyright 2020 The Monogon Project Authors. |
| 2 | // |
| 3 | // SPDX-License-Identifier: Apache-2.0 |
| 4 | // |
| 5 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | // you may not use this file except in compliance with the License. |
| 7 | // You may obtain a copy of the License at |
| 8 | // |
| 9 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | // |
| 11 | // Unless required by applicable law or agreed to in writing, software |
| 12 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | // See the License for the specific language governing permissions and |
| 15 | // limitations under the License. |
| 16 | |
| 17 | package main |
| 18 | |
| 19 | import ( |
| 20 | "context" |
| 21 | "log" |
| 22 | "os" |
| 23 | "os/signal" |
| 24 | "syscall" |
| 25 | "time" |
| 26 | |
| 27 | grpcretry "github.com/grpc-ecosystem/go-grpc-middleware/retry" |
| 28 | "google.golang.org/grpc" |
| 29 | |
| 30 | "git.monogon.dev/source/nexantic.git/core/generated/api" |
| 31 | "git.monogon.dev/source/nexantic.git/core/internal/common" |
| 32 | "git.monogon.dev/source/nexantic.git/core/internal/launch" |
| 33 | ) |
| 34 | |
| 35 | func main() { |
| 36 | sigs := make(chan os.Signal, 1) |
| 37 | signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM) |
| 38 | ctx, cancel := context.WithCancel(context.Background()) |
| 39 | go func() { |
| 40 | <-sigs |
| 41 | cancel() |
| 42 | }() |
| 43 | sw0, vm0, err := launch.NewSocketPair() |
| 44 | if err != nil { |
| 45 | log.Fatalf("Failed to create network pipe: %v\n", err) |
| 46 | } |
| 47 | sw1, vm1, err := launch.NewSocketPair() |
| 48 | if err != nil { |
| 49 | log.Fatalf("Failed to create network pipe: %v\n", err) |
| 50 | } |
| 51 | |
| 52 | go func() { |
| 53 | if err := launch.Launch(ctx, launch.Options{ConnectToSocket: vm0, SerialPort: os.Stdout}); err != nil { |
| 54 | log.Fatalf("Failed to launch vm0: %v", err) |
| 55 | } |
| 56 | }() |
| 57 | nanoswitchPortMap := make(launch.PortMap) |
| 58 | identityPorts := []uint16{ |
| 59 | common.ExternalServicePort, |
| 60 | common.DebugServicePort, |
| 61 | common.KubernetesAPIPort, |
| 62 | } |
| 63 | for _, port := range identityPorts { |
| 64 | nanoswitchPortMap[port] = port |
| 65 | } |
| 66 | go func() { |
| 67 | opts := []grpcretry.CallOption{ |
| 68 | grpcretry.WithBackoff(grpcretry.BackoffExponential(100 * time.Millisecond)), |
| 69 | } |
| 70 | conn, err := nanoswitchPortMap.DialGRPC(common.ExternalServicePort, grpc.WithInsecure(), |
| 71 | grpc.WithUnaryInterceptor(grpcretry.UnaryClientInterceptor(opts...))) |
| 72 | if err != nil { |
| 73 | panic(err) |
| 74 | } |
| 75 | defer conn.Close() |
| 76 | cmc := api.NewClusterManagementClient(conn) |
| 77 | res, err := cmc.NewEnrolmentConfig(context.Background(), &api.NewEnrolmentConfigRequest{ |
| 78 | Name: "test", |
| 79 | }, grpcretry.WithMax(10)) |
| 80 | if err != nil { |
| 81 | log.Fatalf("Failed to get enrolment config: %v", err) |
| 82 | } |
| 83 | if err := launch.Launch(ctx, launch.Options{ConnectToSocket: vm1, EnrolmentConfig: res.EnrolmentConfig, SerialPort: os.Stdout}); err != nil { |
| 84 | log.Fatalf("Failed to launch vm1: %v", err) |
| 85 | } |
| 86 | }() |
| 87 | if err := launch.RunMicroVM(ctx, &launch.MicroVMOptions{ |
| 88 | SerialPort: os.Stdout, |
| 89 | KernelPath: "core/tools/ktest/linux-testing.elf", |
| 90 | InitramfsPath: "core/cmd/nanoswitch/initramfs.lz4", |
| 91 | ExtraNetworkInterfaces: []*os.File{sw0, sw1}, |
| 92 | PortMap: nanoswitchPortMap, |
| 93 | }); err != nil { |
| 94 | log.Fatalf("Failed to launch nanoswitch: %v", err) |
| 95 | } |
| 96 | } |