NewWithFlags creates a new gRPC client connection, optionally port-forwarding to one of the hubble-relay pods, using flags to extract the required information.
(ctx context.Context, vp *viper.Viper)
| 124 | // NewWithFlags creates a new gRPC client connection, optionally port-forwarding to one of the |
| 125 | // hubble-relay pods, using flags to extract the required information. |
| 126 | func NewWithFlags(ctx context.Context, vp *viper.Viper) (*grpc.ClientConn, error) { |
| 127 | server := vp.GetString(config.KeyServer) |
| 128 | |
| 129 | if vp.GetBool(config.KeyPortForward) { |
| 130 | kubeContext := vp.GetString(config.KeyKubeContext) |
| 131 | kubeconfig := vp.GetString(config.KeyKubeconfig) |
| 132 | kubeNamespace := vp.GetString(config.KeyKubeNamespace) |
| 133 | localPort := vp.GetUint16(config.KeyPortForwardPort) |
| 134 | |
| 135 | pf, err := newPortForwarder(kubeContext, kubeconfig) |
| 136 | if err != nil { |
| 137 | return nil, fmt.Errorf("failed to create k8s port forwader: %w", err) |
| 138 | } |
| 139 | |
| 140 | // default to first port configured on the service when svcPort is set to 0 |
| 141 | res, err := pf.PortForwardService(ctx, kubeNamespace, "hubble-relay", int32(localPort), 0) |
| 142 | if err != nil { |
| 143 | return nil, fmt.Errorf("failed to port forward: %w", err) |
| 144 | } |
| 145 | |
| 146 | server = fmt.Sprintf("127.0.0.1:%d", res.ForwardedPort.Local) |
| 147 | logger.Logger.Debug("port-forward to hubble-relay pod running", logfields.Address, server) |
| 148 | } |
| 149 | |
| 150 | conn, err := New(server) |
| 151 | if err != nil { |
| 152 | return nil, err |
| 153 | } |
| 154 | |
| 155 | return conn, nil |
| 156 | } |
| 157 | |
| 158 | func newPortForwarder(context, kubeconfig string) (*portforward.PortForwarder, error) { |
| 159 | restClientGetter := genericclioptions.ConfigFlags{ |
no test coverage detected
searching dependent graphs…