2019-12-24 17:45:17 +00:00
|
|
|
// Package client provides an implementation of a restricted subset of kubernetes API client
|
2019-11-02 13:25:10 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2019-11-15 13:41:40 +00:00
|
|
|
"bytes"
|
2019-11-02 13:25:10 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"errors"
|
2019-12-17 16:09:51 +00:00
|
|
|
"io"
|
2019-11-02 13:25:10 +00:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"path"
|
2020-04-23 13:53:42 +01:00
|
|
|
"regexp"
|
2019-12-24 17:45:17 +00:00
|
|
|
"strings"
|
2019-11-02 13:25:10 +00:00
|
|
|
|
2020-07-27 13:22:00 +01:00
|
|
|
"github.com/micro/go-micro/v3/logger"
|
|
|
|
"github.com/micro/go-micro/v3/util/kubernetes/api"
|
2019-11-02 13:25:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2019-11-15 13:41:40 +00:00
|
|
|
// path to kubernetes service account token
|
2019-11-02 13:25:10 +00:00
|
|
|
serviceAccountPath = "/var/run/secrets/kubernetes.io/serviceaccount"
|
|
|
|
// ErrReadNamespace is returned when the names could not be read from service account
|
|
|
|
ErrReadNamespace = errors.New("Could not read namespace from service account secret")
|
2019-12-24 17:45:17 +00:00
|
|
|
// DefaultImage is default micro image
|
|
|
|
DefaultImage = "micro/go-micro"
|
2020-04-23 13:53:42 +01:00
|
|
|
// DefaultNamespace is the default k8s namespace
|
|
|
|
DefaultNamespace = "default"
|
2019-11-02 13:25:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Client ...
|
|
|
|
type client struct {
|
|
|
|
opts *api.Options
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:45:17 +00:00
|
|
|
// Kubernetes client
|
|
|
|
type Client interface {
|
|
|
|
// Create creates new API resource
|
2020-04-23 13:53:42 +01:00
|
|
|
Create(*Resource, ...CreateOption) error
|
2020-07-16 23:33:11 +08:00
|
|
|
// Get queries API resources
|
2020-04-23 13:53:42 +01:00
|
|
|
Get(*Resource, ...GetOption) error
|
2019-12-24 17:45:17 +00:00
|
|
|
// Update patches existing API object
|
2020-04-23 13:53:42 +01:00
|
|
|
Update(*Resource, ...UpdateOption) error
|
2019-12-24 17:45:17 +00:00
|
|
|
// Delete deletes API resource
|
2020-04-23 13:53:42 +01:00
|
|
|
Delete(*Resource, ...DeleteOption) error
|
2019-12-24 17:45:17 +00:00
|
|
|
// List lists API resources
|
2020-04-23 13:53:42 +01:00
|
|
|
List(*Resource, ...ListOption) error
|
2019-12-24 17:45:17 +00:00
|
|
|
// Log gets log for a pod
|
|
|
|
Log(*Resource, ...LogOption) (io.ReadCloser, error)
|
2019-12-27 20:08:46 +00:00
|
|
|
// Watch for events
|
|
|
|
Watch(*Resource, ...WatchOption) (Watcher, error)
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-23 13:53:42 +01:00
|
|
|
// Create creates new API object
|
|
|
|
func (c *client) Create(r *Resource, opts ...CreateOption) error {
|
2020-04-23 18:10:13 +01:00
|
|
|
options := CreateOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2020-04-23 13:53:42 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
b := new(bytes.Buffer)
|
|
|
|
if err := renderTemplate(r.Kind, b, r.Value); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-11-02 13:25:10 +00:00
|
|
|
return api.NewRequest(c.opts).
|
2019-11-15 13:41:40 +00:00
|
|
|
Post().
|
|
|
|
SetHeader("Content-Type", "application/yaml").
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace(options.Namespace).
|
2019-11-15 13:41:40 +00:00
|
|
|
Resource(r.Kind).
|
|
|
|
Body(b).
|
2019-11-02 13:25:10 +00:00
|
|
|
Do().
|
|
|
|
Error()
|
|
|
|
}
|
2019-11-07 07:44:57 +00:00
|
|
|
|
2020-04-23 13:53:42 +01:00
|
|
|
var (
|
|
|
|
nameRegex = regexp.MustCompile("[^a-zA-Z0-9]+")
|
|
|
|
)
|
|
|
|
|
|
|
|
// SerializeResourceName removes all spacial chars from a string so it
|
|
|
|
// can be used as a k8s resource name
|
|
|
|
func SerializeResourceName(ns string) string {
|
|
|
|
return nameRegex.ReplaceAllString(ns, "-")
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
// Get queries API objects and stores the result in r
|
2020-04-23 13:53:42 +01:00
|
|
|
func (c *client) Get(r *Resource, opts ...GetOption) error {
|
2020-04-23 16:22:41 +01:00
|
|
|
options := GetOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2020-04-23 13:53:42 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
return api.NewRequest(c.opts).
|
2019-11-07 07:44:57 +00:00
|
|
|
Get().
|
2019-11-15 13:41:40 +00:00
|
|
|
Resource(r.Kind).
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace(options.Namespace).
|
|
|
|
Params(&api.Params{LabelSelector: options.Labels}).
|
2019-11-07 07:44:57 +00:00
|
|
|
Do().
|
2019-11-15 13:41:40 +00:00
|
|
|
Into(r.Value)
|
|
|
|
}
|
2019-11-07 07:44:57 +00:00
|
|
|
|
2019-12-24 17:33:05 +00:00
|
|
|
// Log returns logs for a pod
|
|
|
|
func (c *client) Log(r *Resource, opts ...LogOption) (io.ReadCloser, error) {
|
2020-04-23 16:22:41 +01:00
|
|
|
options := LogOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2019-12-24 17:33:05 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
2019-12-20 23:16:05 +00:00
|
|
|
}
|
2019-12-24 17:33:05 +00:00
|
|
|
|
2019-12-17 16:09:51 +00:00
|
|
|
req := api.NewRequest(c.opts).
|
|
|
|
Get().
|
2019-12-24 17:33:05 +00:00
|
|
|
Resource(r.Kind).
|
2019-12-17 16:09:51 +00:00
|
|
|
SubResource("log").
|
2020-04-23 13:53:42 +01:00
|
|
|
Name(r.Name).
|
|
|
|
Namespace(options.Namespace)
|
2019-12-17 16:09:51 +00:00
|
|
|
|
2019-12-24 17:33:05 +00:00
|
|
|
if options.Params != nil {
|
|
|
|
req.Params(&api.Params{Additional: options.Params})
|
2019-12-20 23:16:05 +00:00
|
|
|
}
|
|
|
|
|
2019-12-17 16:09:51 +00:00
|
|
|
resp, err := req.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-12-20 23:16:05 +00:00
|
|
|
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
|
|
|
|
resp.Body.Close()
|
|
|
|
return nil, errors.New(resp.Request.URL.String() + ": " + resp.Status)
|
|
|
|
}
|
2019-12-17 16:09:51 +00:00
|
|
|
return resp.Body, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
// Update updates API object
|
2020-04-23 13:53:42 +01:00
|
|
|
func (c *client) Update(r *Resource, opts ...UpdateOption) error {
|
2020-04-23 16:22:41 +01:00
|
|
|
options := UpdateOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2020-04-23 13:53:42 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
req := api.NewRequest(c.opts).
|
|
|
|
Patch().
|
|
|
|
SetHeader("Content-Type", "application/strategic-merge-patch+json").
|
|
|
|
Resource(r.Kind).
|
2020-04-23 13:53:42 +01:00
|
|
|
Name(r.Name).
|
|
|
|
Namespace(options.Namespace)
|
2019-11-15 13:41:40 +00:00
|
|
|
|
|
|
|
switch r.Kind {
|
|
|
|
case "service":
|
2019-11-26 22:28:08 +00:00
|
|
|
req.Body(r.Value.(*Service))
|
2019-11-15 13:41:40 +00:00
|
|
|
case "deployment":
|
2019-11-26 22:28:08 +00:00
|
|
|
req.Body(r.Value.(*Deployment))
|
2019-12-27 20:08:46 +00:00
|
|
|
case "pod":
|
|
|
|
req.Body(r.Value.(*Pod))
|
2019-11-15 13:41:40 +00:00
|
|
|
default:
|
|
|
|
return errors.New("unsupported resource")
|
|
|
|
}
|
|
|
|
|
|
|
|
return req.Do().Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete removes API object
|
2020-04-23 13:53:42 +01:00
|
|
|
func (c *client) Delete(r *Resource, opts ...DeleteOption) error {
|
2020-04-23 16:22:41 +01:00
|
|
|
options := DeleteOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2020-04-23 13:53:42 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
return api.NewRequest(c.opts).
|
|
|
|
Delete().
|
|
|
|
Resource(r.Kind).
|
|
|
|
Name(r.Name).
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace(options.Namespace).
|
2019-11-15 13:41:40 +00:00
|
|
|
Do().
|
|
|
|
Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// List lists API objects and stores the result in r
|
2020-04-23 13:53:42 +01:00
|
|
|
func (c *client) List(r *Resource, opts ...ListOption) error {
|
2020-04-23 16:22:41 +01:00
|
|
|
options := ListOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2020-04-23 13:53:42 +01:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-04-23 18:08:02 +01:00
|
|
|
return c.Get(r, GetNamespace(options.Namespace))
|
2019-11-07 07:44:57 +00:00
|
|
|
}
|
2019-12-24 17:45:17 +00:00
|
|
|
|
2019-12-27 20:08:46 +00:00
|
|
|
// Watch returns an event stream
|
|
|
|
func (c *client) Watch(r *Resource, opts ...WatchOption) (Watcher, error) {
|
2020-04-23 16:22:41 +01:00
|
|
|
options := WatchOptions{
|
|
|
|
Namespace: c.opts.Namespace,
|
|
|
|
}
|
2019-12-27 20:08:46 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the watch param
|
|
|
|
params := &api.Params{Additional: map[string]string{
|
|
|
|
"watch": "true",
|
|
|
|
}}
|
|
|
|
|
|
|
|
// get options params
|
|
|
|
if options.Params != nil {
|
|
|
|
for k, v := range options.Params {
|
|
|
|
params.Additional[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
req := api.NewRequest(c.opts).
|
|
|
|
Get().
|
|
|
|
Resource(r.Kind).
|
|
|
|
Name(r.Name).
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace(options.Namespace).
|
2019-12-27 20:08:46 +00:00
|
|
|
Params(params)
|
|
|
|
|
|
|
|
return newWatcher(req)
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:45:17 +00:00
|
|
|
// NewService returns default micro kubernetes service definition
|
2020-04-23 13:53:42 +01:00
|
|
|
func NewService(name, version, typ, namespace string) *Service {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("kubernetes default service: name: %s, version: %s", name, version)
|
|
|
|
}
|
2019-12-24 17:45:17 +00:00
|
|
|
|
|
|
|
Labels := map[string]string{
|
|
|
|
"name": name,
|
|
|
|
"version": version,
|
|
|
|
"micro": typ,
|
|
|
|
}
|
|
|
|
|
|
|
|
svcName := name
|
|
|
|
if len(version) > 0 {
|
|
|
|
// API service object name joins name and version over "-"
|
|
|
|
svcName = strings.Join([]string{name, version}, "-")
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:22:41 +01:00
|
|
|
if len(namespace) == 0 {
|
|
|
|
namespace = DefaultNamespace
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:45:17 +00:00
|
|
|
Metadata := &Metadata{
|
|
|
|
Name: svcName,
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace: SerializeResourceName(namespace),
|
2019-12-24 17:45:17 +00:00
|
|
|
Version: version,
|
|
|
|
Labels: Labels,
|
|
|
|
}
|
|
|
|
|
|
|
|
Spec := &ServiceSpec{
|
|
|
|
Type: "ClusterIP",
|
|
|
|
Selector: Labels,
|
|
|
|
Ports: []ServicePort{{
|
2020-03-18 18:27:29 +00:00
|
|
|
"service-port", 8080, "",
|
2019-12-24 17:45:17 +00:00
|
|
|
}},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Service{
|
|
|
|
Metadata: Metadata,
|
|
|
|
Spec: Spec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewService returns default micro kubernetes deployment definition
|
2020-04-23 13:53:42 +01:00
|
|
|
func NewDeployment(name, version, typ, namespace string) *Deployment {
|
2020-03-11 20:55:39 +03:00
|
|
|
if logger.V(logger.TraceLevel, logger.DefaultLogger) {
|
|
|
|
logger.Tracef("kubernetes default deployment: name: %s, version: %s", name, version)
|
|
|
|
}
|
2019-12-24 17:45:17 +00:00
|
|
|
|
|
|
|
Labels := map[string]string{
|
|
|
|
"name": name,
|
|
|
|
"version": version,
|
|
|
|
"micro": typ,
|
|
|
|
}
|
|
|
|
|
|
|
|
depName := name
|
|
|
|
if len(version) > 0 {
|
|
|
|
// API deployment object name joins name and version over "-"
|
|
|
|
depName = strings.Join([]string{name, version}, "-")
|
|
|
|
}
|
|
|
|
|
2020-04-23 16:22:41 +01:00
|
|
|
if len(namespace) == 0 {
|
|
|
|
namespace = DefaultNamespace
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:45:17 +00:00
|
|
|
Metadata := &Metadata{
|
|
|
|
Name: depName,
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace: SerializeResourceName(namespace),
|
2019-12-24 17:45:17 +00:00
|
|
|
Version: version,
|
|
|
|
Labels: Labels,
|
|
|
|
Annotations: map[string]string{},
|
|
|
|
}
|
|
|
|
|
|
|
|
// enable go modules by default
|
|
|
|
env := EnvVar{
|
|
|
|
Name: "GO111MODULE",
|
|
|
|
Value: "on",
|
|
|
|
}
|
|
|
|
|
|
|
|
Spec := &DeploymentSpec{
|
|
|
|
Replicas: 1,
|
|
|
|
Selector: &LabelSelector{
|
|
|
|
MatchLabels: Labels,
|
|
|
|
},
|
|
|
|
Template: &Template{
|
|
|
|
Metadata: Metadata,
|
|
|
|
PodSpec: &PodSpec{
|
|
|
|
Containers: []Container{{
|
|
|
|
Name: name,
|
2020-02-06 09:17:10 +00:00
|
|
|
Image: DefaultImage,
|
2019-12-24 17:45:17 +00:00
|
|
|
Env: []EnvVar{env},
|
2020-07-09 16:29:01 +01:00
|
|
|
Command: []string{},
|
2020-02-06 11:15:30 +00:00
|
|
|
Ports: []ContainerPort{{
|
2019-12-24 17:45:17 +00:00
|
|
|
Name: "service-port",
|
|
|
|
ContainerPort: 8080,
|
|
|
|
}},
|
|
|
|
}},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &Deployment{
|
|
|
|
Metadata: Metadata,
|
|
|
|
Spec: Spec,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 20:08:46 +00:00
|
|
|
// NewLocalClient returns a client that can be used with `kubectl proxy`
|
|
|
|
func NewLocalClient(hosts ...string) *client {
|
|
|
|
if len(hosts) == 0 {
|
|
|
|
hosts[0] = "http://localhost:8001"
|
2019-12-24 17:45:17 +00:00
|
|
|
}
|
|
|
|
return &client{
|
|
|
|
opts: &api.Options{
|
|
|
|
Client: http.DefaultClient,
|
2019-12-27 20:08:46 +00:00
|
|
|
Host: hosts[0],
|
2019-12-24 17:45:17 +00:00
|
|
|
Namespace: "default",
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-27 20:08:46 +00:00
|
|
|
// NewClusterClient creates a Kubernetes client for use from within a k8s pod.
|
|
|
|
func NewClusterClient() *client {
|
2019-12-24 17:45:17 +00:00
|
|
|
host := "https://" + os.Getenv("KUBERNETES_SERVICE_HOST") + ":" + os.Getenv("KUBERNETES_SERVICE_PORT")
|
|
|
|
|
|
|
|
s, err := os.Stat(serviceAccountPath)
|
|
|
|
if err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
logger.Fatal(err)
|
2019-12-24 17:45:17 +00:00
|
|
|
}
|
|
|
|
if s == nil || !s.IsDir() {
|
2020-03-11 20:55:39 +03:00
|
|
|
logger.Fatal(errors.New("service account not found"))
|
2019-12-24 17:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
token, err := ioutil.ReadFile(path.Join(serviceAccountPath, "token"))
|
|
|
|
if err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
logger.Fatal(err)
|
2019-12-24 17:45:17 +00:00
|
|
|
}
|
|
|
|
t := string(token)
|
|
|
|
|
|
|
|
crt, err := CertPoolFromFile(path.Join(serviceAccountPath, "ca.crt"))
|
|
|
|
if err != nil {
|
2020-03-11 20:55:39 +03:00
|
|
|
logger.Fatal(err)
|
2019-12-24 17:45:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c := &http.Client{
|
|
|
|
Transport: &http.Transport{
|
|
|
|
TLSClientConfig: &tls.Config{
|
|
|
|
RootCAs: crt,
|
|
|
|
},
|
|
|
|
DisableCompression: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
return &client{
|
|
|
|
opts: &api.Options{
|
|
|
|
Client: c,
|
|
|
|
Host: host,
|
|
|
|
BearerToken: &t,
|
2020-04-23 13:53:42 +01:00
|
|
|
Namespace: DefaultNamespace,
|
2019-12-24 17:45:17 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|