* First commit: outline of K8s runtime package * Added poller. Added auto-updater into default runtime * Added build and updated Poller interface * Added comments and NewRuntime that accepts Options * DefaultPoller; Runtime options * First commit to add Kubernetes cruft * Add comments * Add micro- prefix to K8s runtime service names * Get rid of import cycles. Move K8s runtime into main runtime package * Major refactoring: Poller replaced by Notifier POller has been replaced by Notifier which returns a channel of events that can be consumed and acted upon. * Added runtime configuration options * K8s runtime is now Kubernetes runtime in dedicated pkg. Naming kung-fu. * Fix typo in command. * Fixed typo * Dont Delete service when runtime stops. runtime.Stop stops services; no need to double-stop * Track runtime services * Parse Unix timestamps properly * Added deployments into K8s client. Debug logging
		
			
				
	
	
		
			95 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			95 lines
		
	
	
		
			1.7 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package api
 | 
						|
 | 
						|
import (
 | 
						|
	"encoding/json"
 | 
						|
	"errors"
 | 
						|
	"io/ioutil"
 | 
						|
	"net/http"
 | 
						|
 | 
						|
	"github.com/micro/go-micro/util/log"
 | 
						|
)
 | 
						|
 | 
						|
// Errors ...
 | 
						|
var (
 | 
						|
	ErrNotFound = errors.New("kubernetes: not found")
 | 
						|
	ErrDecode   = errors.New("kubernetes: error decoding")
 | 
						|
	ErrOther    = errors.New("kubernetes: unknown error")
 | 
						|
)
 | 
						|
 | 
						|
// Status is an object that is returned when a request
 | 
						|
// failed or delete succeeded.
 | 
						|
// type Status struct {
 | 
						|
// 	Kind    string `json:"kind"`
 | 
						|
// 	Status  string `json:"status"`
 | 
						|
// 	Message string `json:"message"`
 | 
						|
// 	Reason  string `json:"reason"`
 | 
						|
// 	Code    int    `json:"code"`
 | 
						|
// }
 | 
						|
 | 
						|
// Response ...
 | 
						|
type Response struct {
 | 
						|
	res *http.Response
 | 
						|
	err error
 | 
						|
 | 
						|
	body []byte
 | 
						|
}
 | 
						|
 | 
						|
// Error returns an error
 | 
						|
func (r *Response) Error() error {
 | 
						|
	return r.err
 | 
						|
}
 | 
						|
 | 
						|
// StatusCode returns status code for response
 | 
						|
func (r *Response) StatusCode() int {
 | 
						|
	return r.res.StatusCode
 | 
						|
}
 | 
						|
 | 
						|
// Into decode body into `data`
 | 
						|
func (r *Response) Into(data interface{}) error {
 | 
						|
	if r.err != nil {
 | 
						|
		return r.err
 | 
						|
	}
 | 
						|
 | 
						|
	defer r.res.Body.Close()
 | 
						|
	decoder := json.NewDecoder(r.res.Body)
 | 
						|
	err := decoder.Decode(&data)
 | 
						|
	if err != nil {
 | 
						|
		return ErrDecode
 | 
						|
	}
 | 
						|
 | 
						|
	return r.err
 | 
						|
}
 | 
						|
 | 
						|
func newResponse(res *http.Response, err error) *Response {
 | 
						|
	r := &Response{
 | 
						|
		res: res,
 | 
						|
		err: err,
 | 
						|
	}
 | 
						|
 | 
						|
	if err != nil {
 | 
						|
		return r
 | 
						|
	}
 | 
						|
 | 
						|
	if r.res.StatusCode == http.StatusOK ||
 | 
						|
		r.res.StatusCode == http.StatusCreated ||
 | 
						|
		r.res.StatusCode == http.StatusNoContent {
 | 
						|
		// Non error status code
 | 
						|
		return r
 | 
						|
	}
 | 
						|
 | 
						|
	if r.res.StatusCode == http.StatusNotFound {
 | 
						|
		r.err = ErrNotFound
 | 
						|
		return r
 | 
						|
	}
 | 
						|
 | 
						|
	log.Logf("kubernetes: request failed with code %v", r.res.StatusCode)
 | 
						|
 | 
						|
	b, err := ioutil.ReadAll(r.res.Body)
 | 
						|
	if err == nil {
 | 
						|
		log.Log("kubernetes: request failed with body:")
 | 
						|
		log.Log(string(b))
 | 
						|
	}
 | 
						|
	r.err = ErrOther
 | 
						|
	return r
 | 
						|
}
 |