Skip to content

Instantly share code, notes, and snippets.

@jsnouffer
Last active August 4, 2022 14:35
Show Gist options
  • Save jsnouffer/2a1d85e9cbe71965dca47aabd0edfce2 to your computer and use it in GitHub Desktop.
Save jsnouffer/2a1d85e9cbe71965dca47aabd0edfce2 to your computer and use it in GitHub Desktop.
package main
import (
"context"
"fmt"
v1 "k8s.io/api/apps/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
ctrl "sigs.k8s.io/controller-runtime"
)
func main() {
ctx := context.Background()
config := ctrl.GetConfigOrDie()
clientset := kubernetes.NewForConfigOrDie(config)
namespace := "default"
items, err := GetDeployments(clientset, ctx, namespace)
if err != nil {
fmt.Println(err)
} else {
for _, item := range items {
fmt.Printf("%+v\n", item)
}
}
}
func GetDeployments(clientset *kubernetes.Clientset, ctx context.Context,
namespace string) ([]v1.Deployment, error) {
list, err := clientset.AppsV1().Deployments(namespace).
List(ctx, metav1.ListOptions{})
if err != nil {
return nil, err
}
return list.Items, nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment