k8s.io/client-go@v0.31.1/examples/in-cluster-client-configuration/main.go (about)

     1  /*
     2  Copyright 2016 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  // Note: the example only works with the code within the same release/branch.
    18  package main
    19  
    20  import (
    21  	"context"
    22  	"fmt"
    23  	"time"
    24  
    25  	"k8s.io/apimachinery/pkg/api/errors"
    26  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    27  	"k8s.io/client-go/kubernetes"
    28  	"k8s.io/client-go/rest"
    29  	//
    30  	// Uncomment to load all auth plugins
    31  	// _ "k8s.io/client-go/plugin/pkg/client/auth"
    32  	//
    33  	// Or uncomment to load specific auth plugins
    34  	// _ "k8s.io/client-go/plugin/pkg/client/auth/azure"
    35  	// _ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
    36  	// _ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
    37  )
    38  
    39  func main() {
    40  	// creates the in-cluster config
    41  	config, err := rest.InClusterConfig()
    42  	if err != nil {
    43  		panic(err.Error())
    44  	}
    45  	// creates the clientset
    46  	clientset, err := kubernetes.NewForConfig(config)
    47  	if err != nil {
    48  		panic(err.Error())
    49  	}
    50  	for {
    51  		// get pods in all the namespaces by omitting namespace
    52  		// Or specify namespace to get pods in particular namespace
    53  		pods, err := clientset.CoreV1().Pods("").List(context.TODO(), metav1.ListOptions{})
    54  		if err != nil {
    55  			panic(err.Error())
    56  		}
    57  		fmt.Printf("There are %d pods in the cluster\n", len(pods.Items))
    58  
    59  		// Examples for error handling:
    60  		// - Use helper functions e.g. errors.IsNotFound()
    61  		// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
    62  		_, err = clientset.CoreV1().Pods("default").Get(context.TODO(), "example-xxxxx", metav1.GetOptions{})
    63  		if errors.IsNotFound(err) {
    64  			fmt.Printf("Pod example-xxxxx not found in default namespace\n")
    65  		} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
    66  			fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
    67  		} else if err != nil {
    68  			panic(err.Error())
    69  		} else {
    70  			fmt.Printf("Found example-xxxxx pod in default namespace\n")
    71  		}
    72  
    73  		time.Sleep(10 * time.Second)
    74  	}
    75  }