2019-11-15 15:41:40 +02:00
|
|
|
package kubernetes
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2019-11-26 15:49:52 +02:00
|
|
|
"time"
|
2019-11-15 15:41:40 +02:00
|
|
|
|
|
|
|
"github.com/micro/go-micro/runtime"
|
|
|
|
"github.com/micro/go-micro/runtime/kubernetes/client"
|
|
|
|
"github.com/micro/go-micro/util/log"
|
|
|
|
)
|
|
|
|
|
|
|
|
type service struct {
|
|
|
|
// service to manage
|
|
|
|
*runtime.Service
|
|
|
|
// Kubernetes service
|
|
|
|
kservice *client.Service
|
|
|
|
// Kubernetes deployment
|
|
|
|
kdeploy *client.Deployment
|
|
|
|
}
|
|
|
|
|
|
|
|
func newService(s *runtime.Service, c runtime.CreateOptions) *service {
|
2019-11-25 18:31:14 +02:00
|
|
|
// use pre-formatted name/version
|
|
|
|
name := client.Format(s.Name)
|
|
|
|
version := client.Format(s.Version)
|
2019-11-15 15:41:40 +02:00
|
|
|
|
2019-11-25 18:31:14 +02:00
|
|
|
kservice := client.NewService(name, version)
|
|
|
|
kdeploy := client.NewDeployment(name, version)
|
|
|
|
|
|
|
|
// 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 15:49:52 +02:00
|
|
|
// set a build timestamp to the current time
|
|
|
|
kdeploy.Metadata.Annotations["build"] = time.Now().Format(time.RFC3339)
|
|
|
|
|
2019-11-25 18:31:14 +02:00
|
|
|
// define the environment values used by the container
|
2019-11-15 15:41:40 +02: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 19:31:13 +02:00
|
|
|
// if environment has been supplied update deployment default environment
|
2019-11-15 15:41:40 +02:00
|
|
|
if len(env) > 0 {
|
2019-11-21 19:31:13 +02:00
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[0].Env = append(kdeploy.Spec.Template.PodSpec.Containers[0].Env, env...)
|
2019-11-15 15:41:40 +02:00
|
|
|
}
|
|
|
|
|
2019-11-21 19:31:13 +02:00
|
|
|
// if Exec/Command has been supplied override the default command
|
|
|
|
if len(s.Exec) > 0 {
|
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[0].Command = s.Exec
|
2019-11-25 18:31:14 +02:00
|
|
|
} else if len(c.Command) > 0 {
|
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[0].Command = c.Command
|
|
|
|
} else if len(s.Source) > 0 {
|
|
|
|
// default command for our k8s service should be source
|
|
|
|
kdeploy.Spec.Template.PodSpec.Containers[0].Command = []string{"go", "run", s.Source}
|
2019-11-15 15:41:40 +02: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
|
|
|
|
func (s *service) Start(k client.Kubernetes) error {
|
|
|
|
// 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)
|
|
|
|
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)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Stop(k client.Kubernetes) error {
|
|
|
|
// first attempt to delete service
|
|
|
|
if err := k.Delete(serviceResource(s.kservice)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to delete service: %v", err)
|
|
|
|
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)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *service) Update(k client.Kubernetes) error {
|
|
|
|
if err := k.Update(deploymentResource(s.kdeploy)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to update deployment: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := k.Update(serviceResource(s.kservice)); err != nil {
|
|
|
|
log.Debugf("Runtime failed to update service: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|