2019-11-02 13:25:10 +00:00
|
|
|
// Package kubernetes implements kubernetes micro runtime
|
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2020-02-05 13:59:35 +00:00
|
|
|
"errors"
|
2019-11-02 13:25:10 +00:00
|
|
|
"fmt"
|
2020-02-05 13:59:35 +00:00
|
|
|
"strings"
|
2019-11-02 13:25:10 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/runtime"
|
|
|
|
"github.com/micro/go-micro/v2/util/kubernetes/client"
|
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2019-11-02 13:25:10 +00:00
|
|
|
)
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
// action to take on runtime service
|
|
|
|
type action int
|
|
|
|
|
2019-11-02 13:25:10 +00:00
|
|
|
type kubernetes struct {
|
|
|
|
sync.RWMutex
|
|
|
|
// options configure runtime
|
|
|
|
options runtime.Options
|
|
|
|
// indicates if we're running
|
|
|
|
running bool
|
|
|
|
// used to stop the runtime
|
|
|
|
closed chan bool
|
|
|
|
// client is kubernetes client
|
2019-12-24 17:51:30 +00:00
|
|
|
client client.Client
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
// getService queries kubernetes for micro service
|
2019-11-22 17:10:00 +00:00
|
|
|
// NOTE: this function is not thread-safe
|
2019-11-25 16:31:14 +00:00
|
|
|
func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, error) {
|
2019-11-22 17:10:00 +00:00
|
|
|
// get the service status
|
|
|
|
serviceList := new(client.ServiceList)
|
|
|
|
r := &client.Resource{
|
|
|
|
Kind: "service",
|
|
|
|
Value: serviceList,
|
|
|
|
}
|
2019-11-26 13:49:52 +00:00
|
|
|
|
|
|
|
// get the service from k8s
|
2019-11-22 17:10:00 +00:00
|
|
|
if err := k.client.Get(r, labels); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the deployment status
|
|
|
|
depList := new(client.DeploymentList)
|
|
|
|
d := &client.Resource{
|
|
|
|
Kind: "deployment",
|
|
|
|
Value: depList,
|
|
|
|
}
|
2019-11-26 13:49:52 +00:00
|
|
|
|
|
|
|
// get the deployment from k8s
|
2019-11-22 17:10:00 +00:00
|
|
|
if err := k.client.Get(d, labels); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// service map
|
|
|
|
svcMap := make(map[string]*runtime.Service)
|
|
|
|
|
|
|
|
// collect info from kubernetes service
|
|
|
|
for _, kservice := range serviceList.Items {
|
2019-11-25 16:31:14 +00:00
|
|
|
// name of the service
|
2019-11-22 17:10:00 +00:00
|
|
|
name := kservice.Metadata.Labels["name"]
|
2019-11-25 16:31:14 +00:00
|
|
|
// version of the service
|
2019-11-22 17:10:00 +00:00
|
|
|
version := kservice.Metadata.Labels["version"]
|
2019-11-25 16:31:14 +00:00
|
|
|
|
|
|
|
// save as service
|
|
|
|
svcMap[name+version] = &runtime.Service{
|
2019-11-22 17:10:00 +00:00
|
|
|
Name: name,
|
|
|
|
Version: version,
|
|
|
|
Metadata: make(map[string]string),
|
|
|
|
}
|
2019-11-25 16:31:14 +00:00
|
|
|
|
2019-11-22 17:10:00 +00:00
|
|
|
// copy annotations metadata into service metadata
|
|
|
|
for k, v := range kservice.Metadata.Annotations {
|
2019-11-25 16:31:14 +00:00
|
|
|
svcMap[name+version].Metadata[k] = v
|
2019-11-22 17:10:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect additional info from kubernetes deployment
|
|
|
|
for _, kdep := range depList.Items {
|
2019-11-25 16:31:14 +00:00
|
|
|
// name of the service
|
2019-11-22 17:10:00 +00:00
|
|
|
name := kdep.Metadata.Labels["name"]
|
2019-11-25 16:31:14 +00:00
|
|
|
// versio of the service
|
|
|
|
version := kdep.Metadata.Labels["version"]
|
|
|
|
|
|
|
|
// access existing service map based on name + version
|
|
|
|
if svc, ok := svcMap[name+version]; ok {
|
|
|
|
// we're expecting our own service name in metadata
|
|
|
|
if _, ok := kdep.Metadata.Annotations["name"]; !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// set the service name, version and source
|
|
|
|
// based on existing annotations we stored
|
|
|
|
svc.Name = kdep.Metadata.Annotations["name"]
|
|
|
|
svc.Version = kdep.Metadata.Annotations["version"]
|
2019-11-22 17:10:00 +00:00
|
|
|
svc.Source = kdep.Metadata.Annotations["source"]
|
2019-11-25 16:31:14 +00:00
|
|
|
|
|
|
|
// delete from metadata
|
|
|
|
delete(kdep.Metadata.Annotations, "name")
|
|
|
|
delete(kdep.Metadata.Annotations, "version")
|
|
|
|
delete(kdep.Metadata.Annotations, "source")
|
|
|
|
|
2019-11-22 17:10:00 +00:00
|
|
|
// copy all annotations metadata into service metadata
|
|
|
|
for k, v := range kdep.Metadata.Annotations {
|
|
|
|
svc.Metadata[k] = v
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
// parse out deployment status and inject into service metadata
|
2019-11-22 17:10:00 +00:00
|
|
|
if len(kdep.Status.Conditions) > 0 {
|
2020-01-10 21:54:28 +00:00
|
|
|
var status string
|
|
|
|
switch kdep.Status.Conditions[0].Type {
|
|
|
|
case "Available":
|
|
|
|
status = "running"
|
|
|
|
delete(svc.Metadata, "error")
|
|
|
|
case "Progressing":
|
|
|
|
status = "starting"
|
|
|
|
delete(svc.Metadata, "error")
|
|
|
|
default:
|
|
|
|
status = "error"
|
|
|
|
svc.Metadata["error"] = kdep.Status.Conditions[0].Message
|
|
|
|
}
|
2019-11-22 17:10:00 +00:00
|
|
|
// pick the last known condition type and mark the service status with it
|
|
|
|
log.Debugf("Runtime setting %s service deployment status: %v", name, status)
|
|
|
|
svc.Metadata["status"] = status
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse out deployment build
|
2019-11-26 17:33:41 +00:00
|
|
|
if build, ok := kdep.Spec.Template.Metadata.Annotations["build"]; ok {
|
2019-11-22 17:10:00 +00:00
|
|
|
buildTime, err := time.Parse(time.RFC3339, build)
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Runtime failed parsing build time for %s: %v", name, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
svc.Metadata["build"] = fmt.Sprintf("%d", buildTime.Unix())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if no build annotation is found, set it to current time
|
|
|
|
svc.Metadata["build"] = fmt.Sprintf("%d", time.Now().Unix())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// collect all the services and return
|
|
|
|
services := make([]*runtime.Service, 0, len(serviceList.Items))
|
2019-11-25 16:31:14 +00:00
|
|
|
|
2019-11-22 17:10:00 +00:00
|
|
|
for _, service := range svcMap {
|
|
|
|
services = append(services, service)
|
|
|
|
}
|
|
|
|
|
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// run runs the runtime management loop
|
|
|
|
func (k *kubernetes) run(events <-chan runtime.Event) {
|
|
|
|
t := time.NewTicker(time.Second * 10)
|
|
|
|
defer t.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-t.C:
|
|
|
|
// TODO: figure out what to do here
|
|
|
|
// - do we even need the ticker for k8s services?
|
|
|
|
case event := <-events:
|
|
|
|
// NOTE: we only handle Update events for now
|
|
|
|
log.Debugf("Runtime received notification event: %v", event)
|
|
|
|
switch event.Type {
|
|
|
|
case runtime.Update:
|
|
|
|
// only process if there's an actual service
|
|
|
|
// we do not update all the things individually
|
|
|
|
if len(event.Service) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-26 22:28:08 +00:00
|
|
|
// format the name
|
|
|
|
name := client.Format(event.Service)
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// set the default labels
|
|
|
|
labels := map[string]string{
|
2019-11-29 11:35:00 +00:00
|
|
|
"micro": k.options.Type,
|
2019-11-26 22:28:08 +00:00
|
|
|
"name": name,
|
2019-11-26 13:49:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(event.Version) > 0 {
|
|
|
|
labels["version"] = event.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the deployment status
|
|
|
|
deployed := new(client.DeploymentList)
|
|
|
|
|
|
|
|
// get the existing service rather than creating a new one
|
|
|
|
err := k.client.Get(&client.Resource{
|
|
|
|
Kind: "deployment",
|
|
|
|
Value: deployed,
|
|
|
|
}, labels)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Debugf("Runtime update failed to get service %s: %v", event.Service, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// technically we should not receive multiple versions but hey ho
|
|
|
|
for _, service := range deployed.Items {
|
2019-11-26 22:28:08 +00:00
|
|
|
// check the name matches
|
|
|
|
if service.Metadata.Name != name {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// update build time annotation
|
2019-11-26 14:56:23 +00:00
|
|
|
if service.Spec.Template.Metadata.Annotations == nil {
|
|
|
|
service.Spec.Template.Metadata.Annotations = make(map[string]string)
|
2019-11-26 17:33:41 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the existing build timestamp
|
|
|
|
if build, ok := service.Spec.Template.Metadata.Annotations["build"]; ok {
|
|
|
|
buildTime, err := time.Parse(time.RFC3339, build)
|
|
|
|
if err == nil && !event.Timestamp.After(buildTime) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-26 14:56:23 +00:00
|
|
|
}
|
2019-11-26 17:33:41 +00:00
|
|
|
|
|
|
|
// update the build time
|
2019-11-26 13:49:52 +00:00
|
|
|
service.Spec.Template.Metadata.Annotations["build"] = event.Timestamp.Format(time.RFC3339)
|
|
|
|
|
2019-11-26 22:28:08 +00:00
|
|
|
log.Debugf("Runtime updating service: %s deployment: %s", event.Service, service.Metadata.Name)
|
2019-11-26 13:49:52 +00:00
|
|
|
if err := k.client.Update(deploymentResource(&service)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to update service %s: %v", event.Service, err)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case <-k.closed:
|
|
|
|
log.Debugf("Runtime stopped")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init initializes runtime options
|
|
|
|
func (k *kubernetes) Init(opts ...runtime.Option) error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&k.options)
|
|
|
|
}
|
|
|
|
|
2020-02-05 13:59:35 +00:00
|
|
|
if strings.HasPrefix(k.options.Source, "github.com") {
|
|
|
|
return errors.New("invalid source provided to kubernetes runtime, expected docker image")
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Creates a service
|
|
|
|
func (k *kubernetes) Create(s *runtime.Service, opts ...runtime.CreateOption) error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
2019-11-29 11:35:00 +00:00
|
|
|
options := runtime.CreateOptions{
|
|
|
|
Type: k.options.Type,
|
|
|
|
}
|
2019-11-26 13:49:52 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:13:24 +00:00
|
|
|
// hackish
|
|
|
|
if len(options.Type) == 0 {
|
|
|
|
options.Type = k.options.Type
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// create new kubernetes micro service
|
|
|
|
service := newService(s, options)
|
|
|
|
|
2019-12-24 17:51:30 +00:00
|
|
|
// start the service
|
|
|
|
return service.Start(k.client)
|
2019-11-26 13:49:52 +00:00
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
// Read returns all instances of given service
|
2019-11-29 11:35:00 +00:00
|
|
|
func (k *kubernetes) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error) {
|
2019-11-02 13:25:10 +00:00
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
2019-11-22 17:10:00 +00:00
|
|
|
// set the default labels
|
2019-11-15 13:41:40 +00:00
|
|
|
labels := map[string]string{
|
2019-11-29 11:35:00 +00:00
|
|
|
"micro": k.options.Type,
|
2019-11-15 13:41:40 +00:00
|
|
|
}
|
2019-11-22 17:10:00 +00:00
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
var options runtime.ReadOptions
|
2019-11-15 13:41:40 +00:00
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
2019-11-02 13:25:10 +00:00
|
|
|
|
2019-11-29 11:35:00 +00:00
|
|
|
if len(options.Service) > 0 {
|
|
|
|
labels["name"] = client.Format(options.Service)
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
// add version to labels if a version has been supplied
|
|
|
|
if len(options.Version) > 0 {
|
|
|
|
labels["version"] = options.Version
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 11:35:00 +00:00
|
|
|
if len(options.Type) > 0 {
|
2019-11-29 13:05:18 +00:00
|
|
|
labels["micro"] = options.Type
|
2019-11-29 11:35:00 +00:00
|
|
|
}
|
2019-11-15 13:41:40 +00:00
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
return k.getService(labels)
|
2019-11-22 17:10:00 +00:00
|
|
|
}
|
2019-11-15 13:41:40 +00:00
|
|
|
|
2019-11-22 17:10:00 +00:00
|
|
|
// List the managed services
|
|
|
|
func (k *kubernetes) List() ([]*runtime.Service, error) {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
labels := map[string]string{
|
2019-11-29 11:35:00 +00:00
|
|
|
"micro": k.options.Type,
|
2019-11-15 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 17:10:00 +00:00
|
|
|
log.Debugf("Runtime listing all micro services")
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
return k.getService(labels)
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the service in place
|
|
|
|
func (k *kubernetes) Update(s *runtime.Service) error {
|
2019-11-15 13:41:40 +00:00
|
|
|
// create new kubernetes micro service
|
2019-11-29 11:35:00 +00:00
|
|
|
service := newService(s, runtime.CreateOptions{
|
|
|
|
Type: k.options.Type,
|
|
|
|
})
|
2019-11-15 13:41:40 +00:00
|
|
|
|
|
|
|
// update build time annotation
|
2019-11-26 13:49:52 +00:00
|
|
|
service.kdeploy.Spec.Template.Metadata.Annotations["build"] = time.Now().Format(time.RFC3339)
|
2019-11-15 13:41:40 +00:00
|
|
|
|
2019-12-24 17:51:30 +00:00
|
|
|
return service.Update(k.client)
|
2019-11-15 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// Delete removes a service
|
2019-11-15 13:41:40 +00:00
|
|
|
func (k *kubernetes) Delete(s *runtime.Service) error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
// create new kubernetes micro service
|
2019-11-29 11:35:00 +00:00
|
|
|
service := newService(s, runtime.CreateOptions{
|
|
|
|
Type: k.options.Type,
|
|
|
|
})
|
2019-11-15 13:41:40 +00:00
|
|
|
|
2019-12-24 17:51:30 +00:00
|
|
|
return service.Stop(k.client)
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// Start starts the runtime
|
2019-11-02 13:25:10 +00:00
|
|
|
func (k *kubernetes) Start() error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
// already running
|
|
|
|
if k.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// set running
|
|
|
|
k.running = true
|
|
|
|
k.closed = make(chan bool)
|
|
|
|
|
|
|
|
var events <-chan runtime.Event
|
2020-01-16 13:34:04 +00:00
|
|
|
if k.options.Scheduler != nil {
|
2019-11-02 13:25:10 +00:00
|
|
|
var err error
|
2020-01-16 13:34:04 +00:00
|
|
|
events, err = k.options.Scheduler.Notify()
|
2019-11-02 13:25:10 +00:00
|
|
|
if err != nil {
|
|
|
|
// TODO: should we bail here?
|
|
|
|
log.Debugf("Runtime failed to start update notifier")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
go k.run(events)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// Stop shuts down the runtime
|
2019-11-02 13:25:10 +00:00
|
|
|
func (k *kubernetes) Stop() error {
|
|
|
|
k.Lock()
|
|
|
|
defer k.Unlock()
|
|
|
|
|
|
|
|
if !k.running {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-k.closed:
|
|
|
|
return nil
|
|
|
|
default:
|
|
|
|
close(k.closed)
|
|
|
|
// set not running
|
|
|
|
k.running = false
|
2020-01-16 13:34:04 +00:00
|
|
|
// stop the scheduler
|
|
|
|
if k.options.Scheduler != nil {
|
|
|
|
return k.options.Scheduler.Close()
|
2019-11-02 13:25:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// String implements stringer interface
|
|
|
|
func (k *kubernetes) String() string {
|
|
|
|
return "kubernetes"
|
|
|
|
}
|
2019-11-26 13:49:52 +00:00
|
|
|
|
|
|
|
// NewRuntime creates new kubernetes runtime
|
|
|
|
func NewRuntime(opts ...runtime.Option) runtime.Runtime {
|
|
|
|
// get default options
|
2019-11-29 11:35:00 +00:00
|
|
|
options := runtime.Options{
|
|
|
|
// Create labels with type "micro": "service"
|
|
|
|
Type: "service",
|
|
|
|
}
|
2019-11-26 13:49:52 +00:00
|
|
|
|
|
|
|
// apply requested options
|
|
|
|
for _, o := range opts {
|
|
|
|
o(&options)
|
|
|
|
}
|
|
|
|
|
|
|
|
// kubernetes client
|
2019-12-27 20:08:46 +00:00
|
|
|
client := client.NewClusterClient()
|
2019-11-26 13:49:52 +00:00
|
|
|
|
|
|
|
return &kubernetes{
|
|
|
|
options: options,
|
|
|
|
closed: make(chan bool),
|
|
|
|
client: client,
|
|
|
|
}
|
|
|
|
}
|