2019-11-15 13:41:40 +00:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
2020-01-18 02:13:24 +00:00
|
|
|
"encoding/json"
|
2019-11-15 13:41:40 +00:00
|
|
|
"strings"
|
2019-11-26 13:49:52 +00:00
|
|
|
"time"
|
2019-11-15 13:41:40 +00:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/runtime"
|
|
|
|
"github.com/micro/go-micro/v2/util/kubernetes/api"
|
|
|
|
"github.com/micro/go-micro/v2/util/kubernetes/client"
|
|
|
|
"github.com/micro/go-micro/v2/util/log"
|
2019-11-15 13:41:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
// service to manage
|
|
|
|
*runtime.Service
|
|
|
|
// Kubernetes service
|
|
|
|
kservice *client.Service
|
|
|
|
// Kubernetes deployment
|
|
|
|
kdeploy *client.Deployment
|
|
|
|
}
|
|
|
|
|
2020-01-18 02:13:24 +00:00
|
|
|
func parseError(err error) *api.Status {
|
|
|
|
status := new(api.Status)
|
|
|
|
json.Unmarshal([]byte(err.Error()), &status)
|
|
|
|
return status
|
|
|
|
}
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
func newService(s *runtime.Service, c runtime.CreateOptions) *service {
|
2019-11-25 16:31:14 +00:00
|
|
|
// use pre-formatted name/version
|
|
|
|
name := client.Format(s.Name)
|
|
|
|
version := client.Format(s.Version)
|
2019-11-15 13:41:40 +00:00
|
|
|
|
2019-11-29 11:35:00 +00:00
|
|
|
kservice := client.NewService(name, version, c.Type)
|
|
|
|
kdeploy := client.NewDeployment(name, version, c.Type)
|
2019-11-25 16:31:14 +00:00
|
|
|
|
2020-02-06 09:17:10 +00:00
|
|
|
if len(s.Source) > 0 {
|
2020-02-06 11:12:40 +00:00
|
|
|
for i := range kdeploy.Spec.Template.PodSpec.Containers {
|
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[i].Image = s.Source
|
2020-02-06 11:28:34 +00:00
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[i].Command = []string{}
|
2020-02-06 12:17:16 +00:00
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[i].Args = []string{name}
|
2020-02-06 09:17:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
// attach our values to the deployment; name, version, source
|
|
|
|
kdeploy.Metadata.Annotations["name"] = s.Name
|
|
|
|
kdeploy.Metadata.Annotations["version"] = s.Version
|
|
|
|
kdeploy.Metadata.Annotations["source"] = s.Source
|
|
|
|
|
|
|
|
// associate owner:group to be later augmented
|
|
|
|
kdeploy.Metadata.Annotations["owner"] = "micro"
|
|
|
|
kdeploy.Metadata.Annotations["group"] = "micro"
|
|
|
|
|
2019-11-26 13:49:52 +00:00
|
|
|
// set a build timestamp to the current time
|
2019-11-26 17:33:41 +00:00
|
|
|
if kdeploy.Spec.Template.Metadata.Annotations == nil {
|
|
|
|
kdeploy.Spec.Template.Metadata.Annotations = make(map[string]string)
|
|
|
|
}
|
|
|
|
kdeploy.Spec.Template.Metadata.Annotations["build"] = time.Now().Format(time.RFC3339)
|
2019-11-26 13:49:52 +00:00
|
|
|
|
2019-11-25 16:31:14 +00:00
|
|
|
// define the environment values used by the container
|
2019-11-15 13:41:40 +00:00
|
|
|
env := make([]client.EnvVar, 0, len(c.Env))
|
|
|
|
for _, evar := range c.Env {
|
|
|
|
evarPair := strings.Split(evar, "=")
|
|
|
|
env = append(env, client.EnvVar{Name: evarPair[0], Value: evarPair[1]})
|
|
|
|
}
|
|
|
|
|
2019-11-21 17:31:13 +00:00
|
|
|
// if environment has been supplied update deployment default environment
|
2019-11-15 13:41:40 +00:00
|
|
|
if len(env) > 0 {
|
2019-11-21 17:31:13 +00:00
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[0].Env = append(kdeploy.Spec.Template.PodSpec.Containers[0].Env, env...)
|
2019-11-15 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
2019-11-29 11:35:00 +00:00
|
|
|
// specify the command to exec
|
|
|
|
if len(c.Command) > 0 {
|
2019-11-25 16:31:14 +00:00
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[0].Command = c.Command
|
2019-11-15 13:41:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &service{
|
|
|
|
Service: s,
|
|
|
|
kservice: kservice,
|
|
|
|
kdeploy: kdeploy,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func deploymentResource(d *client.Deployment) *client.Resource {
|
|
|
|
return &client.Resource{
|
|
|
|
Name: d.Metadata.Name,
|
|
|
|
Kind: "deployment",
|
|
|
|
Value: d,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func serviceResource(s *client.Service) *client.Resource {
|
|
|
|
return &client.Resource{
|
|
|
|
Name: s.Metadata.Name,
|
|
|
|
Kind: "service",
|
|
|
|
Value: s,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Start starts the Kubernetes service. It creates new kubernetes deployment and service API objects
|
2019-12-24 17:51:30 +00:00
|
|
|
func (s *service) Start(k client.Client) error {
|
2019-11-15 13:41:40 +00:00
|
|
|
// create deployment first; if we fail, we dont create service
|
|
|
|
if err := k.Create(deploymentResource(s.kdeploy)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to create deployment: %v", err)
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("error", err)
|
2020-01-18 02:13:24 +00:00
|
|
|
v := parseError(err)
|
|
|
|
if v.Reason == "AlreadyExists" {
|
|
|
|
return runtime.ErrAlreadyExists
|
|
|
|
}
|
2019-11-15 13:41:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// create service now that the deployment has been created
|
|
|
|
if err := k.Create(serviceResource(s.kservice)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to create service: %v", err)
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("error", err)
|
2020-01-18 02:13:24 +00:00
|
|
|
v := parseError(err)
|
|
|
|
if v.Reason == "AlreadyExists" {
|
|
|
|
return runtime.ErrAlreadyExists
|
|
|
|
}
|
2019-11-15 13:41:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("started", nil)
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:51:30 +00:00
|
|
|
func (s *service) Stop(k client.Client) error {
|
2019-11-15 13:41:40 +00:00
|
|
|
// first attempt to delete service
|
|
|
|
if err := k.Delete(serviceResource(s.kservice)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to delete service: %v", err)
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("error", err)
|
2019-11-15 13:41:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
// delete deployment once the service has been deleted
|
|
|
|
if err := k.Delete(deploymentResource(s.kdeploy)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to delete deployment: %v", err)
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("error", err)
|
2019-11-15 13:41:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("stopped", nil)
|
|
|
|
|
2019-11-15 13:41:40 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-24 17:51:30 +00:00
|
|
|
func (s *service) Update(k client.Client) error {
|
2019-11-15 13:41:40 +00:00
|
|
|
if err := k.Update(deploymentResource(s.kdeploy)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to update deployment: %v", err)
|
2020-01-10 21:54:28 +00:00
|
|
|
s.Status("error", err)
|
2019-11-15 13:41:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.Update(serviceResource(s.kservice)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to update service: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-01-10 21:54:28 +00:00
|
|
|
|
|
|
|
func (s *service) Status(status string, err error) {
|
|
|
|
if err == nil {
|
|
|
|
s.Metadata["status"] = status
|
|
|
|
return
|
|
|
|
}
|
|
|
|
s.Metadata["status"] = "error"
|
|
|
|
s.Metadata["error"] = err.Error()
|
|
|
|
}
|