Below is the code(with invalid kubernetes cluster URL - 10.xx.xx.xx sitting in google cloud) :
package main
import (
"fmt"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
)
func NewK8sClient(masterUrl, kubeconfigPath string) (kubernetes.Interface, error) {
// use the current context in kubeconfig
fmt.Println("NewK8sClient() - Invoke BuildConfigFromFlags()")
config, err := clientcmd.BuildConfigFromFlags(masterUrl, kubeconfigPath)
if err != nil {
return nil, err
}
// create the clientset
return kubernetes.NewForConfig(config)
}
func main() {
fmt.Println("main() - Invoke NewK8sClient()")
clientSet, err := NewK8sClient("https://10.xx.xx.xx:443", "./conf.yaml")
if err != nil {
fmt.Println(err)
}
fmt.Println(clientSet)
}
to recognize kind:ClientConfig config yaml(./conf.yml) for authentication:
kind: ClientConfig
apiVersion: authentication.gke.io/v2alpha1
spec:
name: dev-corp
server: https://10.x.x.x:443
certificateAuthorityData: ccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
authentication:
- name: oidc
oidc:
clientID: aaaaad3-9aa1-33c8-dd0-ddddd6b5bf5
clientSecret: ccccccccccccccccc-
issuerURI: https://login.microsoftonline.com/aaaa92-aab7-bbfa-cccf-ddaaaaaaaa/v2.0
kubectlRedirectURI: http://localhost:12345/callback
cloudConsoleRedirectURI: http://console.cloud.google.com/kubernetes/oidc
scopes: offline_access,profile
userClaim: upn
userPrefix: '-'
groupsClaim: groups
preferredAuthentication: oidc
Below is the error:
$ go run main.go
main() - Invoke NewK8sClient()
NewK8sClient() - Invoke BuildConfigFromFlags()
error loading config file "./conf.yaml": no kind "ClientConfig" is registered for version "authentication.gke.io/v2alpha1" in scheme "pkg/runtime/scheme.go:100"
<nil>
$
Environment in which, above code is executed, has:
kubectl oidc login --login-config ./conf.yml --cluster cluster-1
succeeded
As mentioned here:
kubectl and other Kubernetes clients require an authentication plugin, gke-gcloud-auth-plugin, which uses the Client-go Credential Plugins framework to provide authentication tokens to communicate with GKE clusters.
So, we are using client-go library, in the above code: https://github.com/kubernetes/client-go
How to load kind:ClientConfig config yaml for authentication(OIDC based authentication)?
Just to add more info... kind:Config config yaml is working, which is certificate based authentication(not OIDC based authentication)