65 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
			
		
		
	
	
			65 lines
		
	
	
		
			1.6 KiB
		
	
	
	
		
			Go
		
	
	
	
| package k8sutil
 | |
| 
 | |
| import (
 | |
| 	"time"
 | |
| 
 | |
| 	apierrors "k8s.io/apimachinery/pkg/api/errors"
 | |
| 	"k8s.io/apimachinery/pkg/runtime/schema"
 | |
| 	"k8s.io/apimachinery/pkg/runtime/serializer"
 | |
| 	"k8s.io/client-go/kubernetes"
 | |
| 	"k8s.io/client-go/pkg/api"
 | |
| 	"k8s.io/client-go/rest"
 | |
| 	"k8s.io/client-go/tools/clientcmd"
 | |
| 
 | |
| 	"github.com/zalando-incubator/postgres-operator/pkg/util/constants"
 | |
| 	"github.com/zalando-incubator/postgres-operator/pkg/util/retryutil"
 | |
| )
 | |
| 
 | |
| func RestConfig(kubeConfig string, outOfCluster bool) (*rest.Config, error) {
 | |
| 	if outOfCluster {
 | |
| 		return clientcmd.BuildConfigFromFlags("", kubeConfig)
 | |
| 	}
 | |
| 
 | |
| 	return rest.InClusterConfig()
 | |
| }
 | |
| 
 | |
| func KubernetesClient(config *rest.Config) (client *kubernetes.Clientset, err error) {
 | |
| 	return kubernetes.NewForConfig(config)
 | |
| }
 | |
| 
 | |
| func ResourceAlreadyExists(err error) bool {
 | |
| 	return apierrors.IsAlreadyExists(err)
 | |
| }
 | |
| 
 | |
| func ResourceNotFound(err error) bool {
 | |
| 	return apierrors.IsNotFound(err)
 | |
| }
 | |
| 
 | |
| func KubernetesRestClient(cfg rest.Config) (rest.Interface, error) {
 | |
| 	cfg.GroupVersion = &schema.GroupVersion{
 | |
| 		Group:   constants.TPRGroup,
 | |
| 		Version: constants.TPRApiVersion,
 | |
| 	}
 | |
| 	cfg.APIPath = constants.K8sAPIPath
 | |
| 	cfg.NegotiatedSerializer = serializer.DirectCodecFactory{CodecFactory: api.Codecs}
 | |
| 
 | |
| 	return rest.RESTClientFor(&cfg)
 | |
| }
 | |
| 
 | |
| func WaitTPRReady(restclient rest.Interface, interval, timeout time.Duration, ns string) error {
 | |
| 	return retryutil.Retry(interval, timeout, func() (bool, error) {
 | |
| 		_, err := restclient.
 | |
| 			Get().
 | |
| 			Namespace(ns).
 | |
| 			Resource(constants.ResourceName).
 | |
| 			DoRaw()
 | |
| 		if err != nil {
 | |
| 			if ResourceNotFound(err) { // not set up yet. wait more.
 | |
| 				return false, nil
 | |
| 			}
 | |
| 			return false, err
 | |
| 		}
 | |
| 		return true, nil
 | |
| 	})
 | |
| }
 |