crd

package
v0.28.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 28, 2022 License: Apache-2.0 Imports: 1 Imported by: 0

README

CRD

Once an operator is installed and CRDs are applied, these commands help to verify CRDs are in place.

package main

import (
    "flag"
	"fmt"
	"log"
	"os"
	"path/filepath"

	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
	"k8s.io/client-go/kubernetes"
	"k8s.io/client-go/tools/clientcmd"

	"github.com/banzaicloud/operator-tools/pkg/crd"
)

func main() {
	var kubeconfig *string
	if home := homeDir(); home != "" {
		kubeconfig = flag.String("kubeconfig", filepath.Join(home, ".kube", "config"), "(optional) absolute path to the kubeconfig file")
	} else {
		kubeconfig = flag.String("kubeconfig", "", "absolute path to the kubeconfig file")
	}
	flag.Parse()

	config, err := clientcmd.BuildConfigFromFlags("", *kubeconfig)
	if err != nil {
		log.Fatal(err)
	}
	clientset, err := kubernetes.NewForConfig(config)
	if err != nil {
		log.Fatal(err)
	}

	c := crd.NewCRD(clientset)
	resources, err := c.ListAPIResources(metav1.GroupVersion{
		Group:   "monitoring.coreos.com",
		Version: "v1",
	})
	if err != nil {
		log.Fatal(err)
	}
	for _, resource := range resources {
		fmt.Println(resource.Name)
	}
	// Output:
	// alertmanagers
	// servicemonitors
	// prometheuses
	// prometheusrules
	// podmonitors

	has, err := c.HasAPIResource(metav1.GroupVersion{
		Group:   "monitoring.coreos.com",
		Version: "v1",
	}, "servicemonitors")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Printf("%t\n", has)
	// Output: true
}


func homeDir() string {
	if h := os.Getenv("HOME"); h != "" {
		return h
	}
	return os.Getenv("USERPROFILE") // windows
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CRD

type CRD struct {
	// contains filtered or unexported fields
}

func NewCRD

func NewCRD(sri ServerResourcesInterface) *CRD

func (*CRD) HasAPIResource

func (c *CRD) HasAPIResource(groupVersion metav1.GroupVersion, name string) (bool, error)

HasAPIResource checks whether a given resource is registered under a given API group and resource or not.

func (*CRD) ListAPIResources

func (c *CRD) ListAPIResources(groupVersion metav1.GroupVersion) ([]metav1.APIResource, error)

ListAPIResources returns all API resources registered for a given API group and version.

type ServerResourcesInterface

type ServerResourcesInterface interface {
	// ServerResourcesForGroupVersion returns the supported resources for a group and version.
	ServerResourcesForGroupVersion(groupVersion string) (*metav1.APIResourceList, error)
}

ServerResourcesInterface has methods for obtaining supported resources on the API server

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL