title: Connect to a gRPC service on a GKE cluster from Cloud Build
body: <p>We’re using a managed Kubeflow Pipelines (KFP) instance, created with GCP’s AI Platform Pipelines, with a managed MySQL instance, created with Cloud SQL. We’re also using Cloud Build to build and run our KFP pipelines. We’d like to add a Cloud Build step that, after a KFP pipeline run finishes, runs a script that uses the MLMD API to query for that pipeline run’s metadata. We have an MLMD script that can successfully query for metadata when run manually on a GCP VM. The issue has been getting that script to run in Cloud Build.</p>
  <p>The first approach was to create the <code>mlmd.metadata_store.MetadataStore</code> object using MySQL connection information, e.g.,</p>
  <pre><code>connection_config = metadata_store_pb2.ConnectionConfig()
  connection_config.mysql.host = [IP address]
  connection_config.mysql.port = 3306
  connection_config.mysql.database = &quot;kubeflow_experiments_metadata_metadata&quot;
  connection_config.mysql.user = [user]
  connection_config.mysql.password = [password]
  store = mlmd.metadata_store.MetadataStore(connection_config)
  </code></pre>
  <p>This runs fine on a VM. It appears, however, that the Cloud SQL Proxy is required for this to work in Cloud Build. Using this <a href="https://codelabs.developers.google.com/codelabs/connecting-to-cloud-sql-with-cloud-functions#2" rel="nofollow noreferrer">Cloud Functions codelab</a> as an example, I was able to run a script in Cloud Build that used sqlalchemy to connect to MySQL via the proxy. The method for connecting sqlalchemy with the proxy, however, seems to be incompatible with the above MLMD API. It looks something like this:</p>
  <pre><code>driver_name = &quot;mysql+pymysql&quot;
  query_string = dict({&quot;unix_socket&quot;: &quot;/cloudsql/{}&quot;.format(connection_name)})
  db = sqlalchemy.create_engine(
      sqlalchemy.engine.url.URL(drivername=driver_name, username=[user], password=[password], database=&quot;kubeflow_experiments_metadata_metadata&quot;, query=query_string),
      pool_size=5,
      max_overflow=2,
      pool_timeout=30,
      pool_recycle=1800,
  )
  </code></pre>
  <p>The second approach uses the MLMD gRPC service that’s deployed with KFP. First, I port forward the service:</p>
  <pre><code>kubectl port-forward svc/metadata-grpc-service 8080:8080
  </code></pre>
  <p>then create the <code>mlmd.metadata_store.MetadataStore</code> using the MLMD gRPC API:</p>
  <pre><code>connection_config = metadata_store_pb2.MetadataStoreClientConfig(
      host=&quot;127.0.0.1&quot;,
      port=8080,
  )
  store = mlmd.metadata_store.MetadataStore(connection_config)
  </code></pre>
  <p>Again, this works fine on a VM. I’m not sure, however, how to connect to the gRPC service from Cloud Build. My experience with gRPC and Kubernetes is limited, so I wouldn’t be surprised if there was a straightforward solution.</p>
  <p>Any advice would be greatly appreciated!</p>