|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "io" |
| 9 | + "net" |
| 10 | + "strings" |
| 11 | + |
| 12 | + "github.com/containers/podman/v6/pkg/api/grpcpb" |
| 13 | + "github.com/containers/podman/v6/pkg/bindings" |
| 14 | + "github.com/containers/podman/v6/pkg/domain/entities" |
| 15 | + "github.com/spf13/cobra" |
| 16 | + "go.podman.io/common/pkg/completion" |
| 17 | + "google.golang.org/grpc" |
| 18 | + "google.golang.org/grpc/credentials/insecure" |
| 19 | + reflectionv1 "google.golang.org/grpc/reflection/grpc_reflection_v1" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + noopDescription = `Call the no-op GRPC endpoint.` |
| 24 | + noopCmd = &cobra.Command{ |
| 25 | + Use: "noop [arg]", |
| 26 | + Args: cobra.MaximumNArgs(1), |
| 27 | + Short: "Call the no-op GRPC endpoint", |
| 28 | + Long: noopDescription, |
| 29 | + RunE: noop, |
| 30 | + ValidArgsFunction: completion.AutocompleteNone, |
| 31 | + Example: `podman-testing noop`, |
| 32 | + } |
| 33 | + lsDescription = `List GRPC endpoints.` |
| 34 | + lsCmd = &cobra.Command{ |
| 35 | + Use: "ls [arg]", |
| 36 | + Args: cobra.MaximumNArgs(1), |
| 37 | + Short: "Call an RPC endpoint", |
| 38 | + Long: lsDescription, |
| 39 | + RunE: ls, |
| 40 | + ValidArgsFunction: completion.AutocompleteNone, |
| 41 | + Example: `podman-testing ls`, |
| 42 | + } |
| 43 | +) |
| 44 | + |
| 45 | +func init() { |
| 46 | + mainCmd.AddCommand(noopCmd) |
| 47 | + mainCmd.AddCommand(lsCmd) |
| 48 | +} |
| 49 | + |
| 50 | +func ls(_ *cobra.Command, args []string) error { |
| 51 | + if podmanConfig.EngineMode != entities.TunnelMode { |
| 52 | + return errors.New("only available in remote mode") |
| 53 | + } |
| 54 | + ctx, grpcClient, err := getGrpcClient() |
| 55 | + if err != nil { |
| 56 | + return fmt.Errorf("setting up grpc client for podman service: %w", err) |
| 57 | + } |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("setting up grpc client for podman service: %w", err) |
| 60 | + } |
| 61 | + reflectionClient := reflectionv1.NewServerReflectionClient(grpcClient) |
| 62 | + if reflectionClient == nil { |
| 63 | + return fmt.Errorf("setting up client for reflection grpc service: %w", err) |
| 64 | + } |
| 65 | + info, err := reflectionClient.ServerReflectionInfo(ctx) |
| 66 | + if err != nil { |
| 67 | + return fmt.Errorf("reflection grpc service: %w", err) |
| 68 | + } |
| 69 | + ls := "" |
| 70 | + if len(args) > 1 { |
| 71 | + ls = args[1] |
| 72 | + } |
| 73 | + err = info.Send(&reflectionv1.ServerReflectionRequest{ |
| 74 | + MessageRequest: &reflectionv1.ServerReflectionRequest_ListServices{ |
| 75 | + ListServices: ls, |
| 76 | + }, |
| 77 | + }) |
| 78 | + if err != nil { |
| 79 | + return fmt.Errorf("reflection grpc service: %w", err) |
| 80 | + } |
| 81 | + err = info.CloseSend() |
| 82 | + if err != nil { |
| 83 | + return fmt.Errorf("reflection grpc service: %w", err) |
| 84 | + } |
| 85 | + var response reflectionv1.ServerReflectionResponse |
| 86 | + err = info.RecvMsg(&response) |
| 87 | + for err == nil { |
| 88 | + var out []byte |
| 89 | + out, err = json.Marshal(response.GetListServicesResponse()) |
| 90 | + if err != nil { |
| 91 | + return fmt.Errorf("encoding response from grpc service: %w", err) |
| 92 | + } |
| 93 | + fmt.Println(string(out)) |
| 94 | + response.Reset() |
| 95 | + err = info.RecvMsg(&response) |
| 96 | + } |
| 97 | + if !errors.Is(err, io.EOF) { |
| 98 | + return fmt.Errorf("unexpected grpc protocol error: %w", err) |
| 99 | + } |
| 100 | + return nil |
| 101 | +} |
| 102 | + |
| 103 | +func noop(_ *cobra.Command, args []string) error { |
| 104 | + var out []byte |
| 105 | + switch podmanConfig.EngineMode { |
| 106 | + case entities.TunnelMode: |
| 107 | + ctx, grpcClient, err := getGrpcClient() |
| 108 | + if err != nil { |
| 109 | + return fmt.Errorf("setting up grpc client for podman service: %w", err) |
| 110 | + } |
| 111 | + noopClient := grpcpb.NewNoopClient(grpcClient) |
| 112 | + if noopClient == nil { |
| 113 | + return fmt.Errorf("setting up client for noop grpc service: %w", err) |
| 114 | + } |
| 115 | + var request grpcpb.NoopRequest |
| 116 | + if encoded := strings.Join(args, ""); len(encoded) > 0 { |
| 117 | + if err := json.Unmarshal([]byte(encoded), &request); err != nil { |
| 118 | + return fmt.Errorf("parsing client request contents for noop grpc service: %w", err) |
| 119 | + } |
| 120 | + } |
| 121 | + response, err := noopClient.Noop(ctx, &request) |
| 122 | + if err != nil { |
| 123 | + return fmt.Errorf("noop grpc service: %w", err) |
| 124 | + } |
| 125 | + out, err = json.Marshal(response) |
| 126 | + if err != nil { |
| 127 | + return fmt.Errorf("encoding response from grpc service: %w", err) |
| 128 | + } |
| 129 | + default: |
| 130 | + return errors.New("only available in remote mode") |
| 131 | + } |
| 132 | + fmt.Println(string(out)) |
| 133 | + return nil |
| 134 | +} |
| 135 | + |
| 136 | +func getGrpcClient() (context.Context, *grpc.ClientConn, error) { |
| 137 | + ctx, err := bindings.NewConnection(mainContext, podmanConfig.URI) |
| 138 | + if err != nil { |
| 139 | + return nil, nil, fmt.Errorf("connecting to podman service: %w", err) |
| 140 | + } |
| 141 | + client, err := bindings.GetClient(ctx) |
| 142 | + if err != nil { |
| 143 | + return nil, nil, fmt.Errorf("obtaining client handle for podman service: %w", err) |
| 144 | + } |
| 145 | + onlyPodmanSystemServiceDialer := grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) { return client.GetDialer(ctx) }) |
| 146 | + withoutEncryption := grpc.WithTransportCredentials(insecure.NewCredentials()) |
| 147 | + grpcClient, err := grpc.NewClient(podmanConfig.URI, onlyPodmanSystemServiceDialer, withoutEncryption) |
| 148 | + if err != nil { |
| 149 | + return nil, nil, fmt.Errorf("setting up grpc client for podman service: %w", err) |
| 150 | + } |
| 151 | + return ctx, grpcClient, err |
| 152 | +} |
0 commit comments