Last active
August 21, 2020 05:06
-
-
Save ahmetb/d611f4be3005657cccd7d768011f55f7 to your computer and use it in GitHub Desktop.
Use Knative API with Cloud Run client library (KUBECONFIG authentication)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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