Get hands-on experience with 20+ free Google Cloud products and $300 in free credit for new customers.

Support for downward API like functionality in Cloudrun

ns2
Bronze 3
Bronze 3

Does Cloud Run support K8s Downward API like functionality ? 
https://kubernetes.io/docs/concepts/workloads/pods/downward-api/

Primarily my use-case is to fetch the Service Id, Service name, namespace etc from the running container.
Does Cloud Run support access to this data from the running containers ? 

0 7 404
7 REPLIES 7

Hi, Cloud Run has a lot of this information in environment variables. It's documented here:

https://cloud.google.com/run/docs/container-contract

ns2
Bronze 3
Bronze 3

Thanks @knet .
My use case needs some additional information like IP address, labels. Something equivalent to attached image. Can you please let me know if its possible to fetch this info from the running containers(using API or something equivalent).

Cloudrun.png

There's also a metadata server, documented on the same page.

I don't think there's a way to get labels information, sorry. 

For IP address: Cloud Run services don't get their own IP address; instead, they each get a URL which is served from a shared IP pool. The URLs aren't easy to predict or discover at the moment, we're working on a solution in this space.

Actually, you can get both these pieces of information (labels and URL)  by issuing a get request for the current service to the Cloud Run API: https://cloud.google.com/run/docs/reference/rest/v2/projects.locations.services/get

ns2
Bronze 3
Bronze 3

Thanks @knet . I tried to use Cloud Client Library to access these APIs as suggested in the docs.
However, it appears that authentication is failing for my API.
Attached error message for your reference.

Please find my code snippet below. Can you please share some pointers on how to proceed with authentication

 

package main                                                                                                            
                                                                                                                        
import (                                                                                                                
        "context"                                                                                                       
        "fmt"                                                                                                           
        run "cloud.google.com/go/run/apiv2"                                                                             
        runpb "cloud.google.com/go/run/apiv2/runpb"                                                                     
)                                                                                                                       
                                                                                                           
func GetService() {                                                                                                     
        ctx := context.Background()                                                                                     
        c, err := run.NewServicesClient(ctx)                                                                            
        if err != nil {                                                                                                 
                fmt.Printf("Failure in NewServicesClient\n")                                                            
                return                                                                                                  
        }                                                                                                               
        defer c.Close()                                                                                                 
                                                                                                                        
        req := &runpb.GetServiceRequest{                                                                                
                Name: "xxxxxxxxxxxxxxxxxxxxx"                                                                           
        }                                                                                                               
        resp, err := c.GetService(ctx, req)                                                                             
        if err != nil {                                                                                                 
                fmt.Printf("Failure in Getservice %s\n",err)                                                            
                return                                                                                                                                                                                                                          
        }                                                                                                               
}                                                                                                                       
                                                                                                                        
func main() {                                                                                                                                                                                                                                  
   GetService()                                                                                                                                                                                                                                
}                 

 


Untitled.png

I'm afraid that's not my area of expertise 😞 Maybe someone else on here can help?

ns2
Bronze 3
Bronze 3

Thanks @knet for suggesting the pointers so far..

Top Solution Authors