Skip to content

Instantly share code, notes, and snippets.

@ahmetb
Last active August 21, 2020 05:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahmetb/d611f4be3005657cccd7d768011f55f7 to your computer and use it in GitHub Desktop.
Save ahmetb/d611f4be3005657cccd7d768011f55f7 to your computer and use it in GitHub Desktop.
Use Knative API with Cloud Run client library (KUBECONFIG authentication)
package main
import (
"context"
"fmt"
"net/http"
"google.golang.org/api/option"
"google.golang.org/api/run/v1"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
)
func main() {
kc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(
clientcmd.NewDefaultClientConfigLoadingRules(),
&clientcmd.ConfigOverrides{})
kubeconfig, err := kc.ClientConfig()
if err != nil {
panic(err)
}
tr, err := rest.TransportFor(kubeconfig)
if err != nil {
panic(err)
}
hc := &http.Client{Transport: tr}
ctx := context.Background()
runService, err := run.NewService(ctx,
option.WithHTTPClient(hc),
option.WithEndpoint(kubeconfig.Host))
if err != nil {
panic(err)
}
// List Services
resp, err := runService.Namespaces.Services.List("namespaces/default").Do()
if err != nil {
panic(err)
}
fmt.Printf("%d kservices found\n", len(resp.Items))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment