64 lines
2.0 KiB
Go
64 lines
2.0 KiB
Go
|
|
package main
|
||
|
|
|
||
|
|
import (
|
||
|
|
"fmt"
|
||
|
|
"k8s.io/api/core/v1"
|
||
|
|
"k8s.io/apimachinery/pkg/api/resource"
|
||
|
|
"log"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"k8s.io/apimachinery/pkg/api/errors"
|
||
|
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||
|
|
"k8s.io/client-go/kubernetes"
|
||
|
|
"k8s.io/client-go/rest"
|
||
|
|
)
|
||
|
|
|
||
|
|
func main() {
|
||
|
|
config, err := rest.InClusterConfig()
|
||
|
|
if err != nil {
|
||
|
|
panic(err.Error())
|
||
|
|
}
|
||
|
|
clientset, err := kubernetes.NewForConfig(config)
|
||
|
|
if err != nil {
|
||
|
|
panic(err.Error())
|
||
|
|
}
|
||
|
|
for {
|
||
|
|
namespaces, err := clientset.CoreV1().Namespaces().List(metav1.ListOptions{})
|
||
|
|
if err != nil {
|
||
|
|
panic(err.Error())
|
||
|
|
}
|
||
|
|
|
||
|
|
for _, ns := range namespaces.Items {
|
||
|
|
fmt.Printf("Checking for LimitRange named extreme-request-defaults in namespace '%v'\n", ns.Name)
|
||
|
|
if limitRange, err := clientset.CoreV1().LimitRanges(ns.Name).List(metav1.ListOptions{FieldSelector:"metadata.name=extreme-request-defaults"}); err == nil && len(limitRange.Items) == 0 {
|
||
|
|
fmt.Printf("Trying to create LimitaRange\n")
|
||
|
|
limits := []v1.LimitRangeItem{
|
||
|
|
{
|
||
|
|
DefaultRequest:v1.ResourceList{"memory": *resource.NewQuantity(1024*1024*1024*1024, resource.BinarySI)},
|
||
|
|
},
|
||
|
|
}
|
||
|
|
if _, err := clientset.CoreV1().LimitRanges(ns.Name).Create(&v1.LimitRange{Spec: v1.LimitRangeSpec{Limits: limits}}); err != nil {
|
||
|
|
log.Printf("Unable to create LimitRange in namespace '%v'\n", ns.Name)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
fmt.Printf("There are %d pods in the cluster\n", len(namespaces.Items))
|
||
|
|
|
||
|
|
// Examples for error handling:
|
||
|
|
// - Use helper functions like e.g. errors.IsNotFound()
|
||
|
|
// - And/or cast to StatusError and use its properties like e.g. ErrStatus.Message
|
||
|
|
_, err = clientset.CoreV1().Pods("default").Get("example-xxxxx", metav1.GetOptions{})
|
||
|
|
if errors.IsNotFound(err) {
|
||
|
|
fmt.Printf("Pod not found\n")
|
||
|
|
} else if statusError, isStatus := err.(*errors.StatusError); isStatus {
|
||
|
|
fmt.Printf("Error getting pod %v\n", statusError.ErrStatus.Message)
|
||
|
|
} else if err != nil {
|
||
|
|
panic(err.Error())
|
||
|
|
} else {
|
||
|
|
fmt.Printf("Found pod\n")
|
||
|
|
}
|
||
|
|
|
||
|
|
time.Sleep(10 * time.Second)
|
||
|
|
}
|
||
|
|
}
|