github.com/projectcalico/api@v0.0.0-20231218190037-9183ab93f33e/examples/list-gnp/main.go (about)

     1  // Copyright (c) 2021 Tigera, Inc. All rights reserved.
     2  
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"flag"
    20  	"fmt"
    21  
    22  	v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    23  	"k8s.io/client-go/tools/clientcmd"
    24  
    25  	"github.com/projectcalico/api/pkg/client/clientset_generated/clientset"
    26  )
    27  
    28  func main() {
    29  	// Create a new config based on kubeconfig file.
    30  	var kubeconfig *string
    31  	kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
    32  	flag.Parse()
    33  	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
    34  	if err != nil {
    35  		panic(err.Error())
    36  	}
    37  
    38  	// Build a clientset based on the provided kubeconfig file.
    39  	cs, err := clientset.NewForConfig(config)
    40  	if err != nil {
    41  		panic(err)
    42  	}
    43  
    44  	// List global network policies.
    45  	list, err := cs.ProjectcalicoV3().GlobalNetworkPolicies().List(context.Background(), v1.ListOptions{})
    46  	if err != nil {
    47  		panic(err)
    48  	}
    49  	for _, gnp := range list.Items {
    50  		fmt.Printf("%#v\n", gnp)
    51  	}
    52  }