sigs.k8s.io/prow@v0.0.0-20240503223140-c5e374dc7eb1/pkg/resultstore/client.go (about)

     1  /*
     2  Copyright 2023 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package resultstore
    18  
    19  import (
    20  	"context"
    21  	"crypto/x509"
    22  	"fmt"
    23  
    24  	"google.golang.org/genproto/googleapis/devtools/resultstore/v2"
    25  	"google.golang.org/grpc"
    26  	"google.golang.org/grpc/credentials"
    27  	"google.golang.org/grpc/credentials/oauth"
    28  )
    29  
    30  // TODO: have client connect itself? Move to flagutils?
    31  const ResultStoreAddress = "resultstore.googleapis.com:443"
    32  
    33  // Connect returns a ResultStore GRPC client connection.
    34  func Connect(ctx context.Context) (*grpc.ClientConn, error) {
    35  	pool, err := x509.SystemCertPool()
    36  	if err != nil {
    37  		return nil, fmt.Errorf("system cert pool: %w", err)
    38  	}
    39  	creds := credentials.NewClientTLSFromCert(pool, "")
    40  	const scope = "https://www.googleapis.com/auth/cloud-platform"
    41  	perRPC, err := oauth.NewApplicationDefault(ctx, scope)
    42  	if err != nil {
    43  		return nil, fmt.Errorf("create oauth: %w", err)
    44  	}
    45  	conn, err := grpc.Dial(
    46  		ResultStoreAddress,
    47  		grpc.WithTransportCredentials(creds),
    48  		grpc.WithPerRPCCredentials(perRPC),
    49  	)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	return conn, nil
    54  }
    55  
    56  type Client struct {
    57  	upload resultstore.ResultStoreUploadClient
    58  }
    59  
    60  // NewClient returns a new ResultStore client.
    61  func NewClient(conn *grpc.ClientConn) *Client {
    62  	return &Client{
    63  		upload: resultstore.NewResultStoreUploadClient(conn),
    64  	}
    65  }
    66  
    67  func (c *Client) UploadClient() resultstore.ResultStoreUploadClient {
    68  	return c.upload
    69  }