Svc metadata (#972)

* Added service metadata

* Added metadata to runtime service

* Add Annotations metadata to service metadata

* Add micro/micro as default service owners

* Update runtime/kubernetes/client/kubernetes.go

Change comment

Co-Authored-By: Jake Sanders <i@am.so-aweso.me>
This commit is contained in:
Milos Gajdos 2019-11-22 17:10:00 +00:00 committed by Asim Aslam
parent 8dc3fb964e
commit 38e29c5101
10 changed files with 209 additions and 124 deletions

View File

@ -152,10 +152,5 @@ func (c *client) List(r *Resource) error {
"micro": "service",
}
return api.NewRequest(c.opts).
Get().
Resource(r.Kind).
Params(&api.Params{LabelSelector: labels}).
Do().
Into(r.Value)
return c.Get(r, labels)
}

View File

@ -50,7 +50,6 @@ func DefaultService(name, version string) *Service {
if len(version) > 0 {
// API service object name joins name and version over "-"
svcName = strings.Join([]string{name, version}, "-")
}
Metadata := &Metadata{
@ -84,26 +83,32 @@ func DefaultDeployment(name, version, source string) *Deployment {
"micro": "service",
}
// API deployment object name joins name and version over "="
depName := strings.Join([]string{name, version}, "-")
depName := name
if len(version) > 0 {
// API deployment object name joins name and version over "-"
depName = strings.Join([]string{name, version}, "-")
}
Metadata := &Metadata{
Name: depName,
Namespace: "default",
Version: version,
Labels: Labels,
Annotations: map[string]string{
"source": source,
"owner": "micro",
"group": "micro",
},
}
// TODO: we need to figure out this version stuff
// might be worth adding Build to runtime.Service
// might have to add Build to runtime.Service
buildTime, err := strconv.ParseInt(version, 10, 64)
if err == nil {
buildUnixTimeUTC := time.Unix(buildTime, 0)
Metadata.Annotations = map[string]string{
"build": buildUnixTimeUTC.Format(time.RFC3339),
}
Metadata.Annotations["build"] = buildUnixTimeUTC.Format(time.RFC3339)
} else {
log.Debugf("Runtime could not parse build: %v", err)
log.Debugf("could not parse build: %v", err)
}
// enable go modules by default

View File

@ -17,6 +17,12 @@ metadata:
{{ $key }}: "{{ $value }}"
{{- end }}
{{- end }}
annotations:
{{- with .Metadata.Annotations }}
{{- range $key, $value := . }}
{{ $key }}: "{{ $value }}"
{{- end }}
{{- end }}
spec:
replicas: {{ .Spec.Replicas }}
selector:

View File

@ -103,6 +103,13 @@ type DeploymentSpec struct {
Template *Template `json:"template,omitempty"`
}
// DeploymentCondition describes the state of deployment
type DeploymentCondition struct {
Type string `json:"type"`
Reason string `json:"reason,omitempty"`
Message string `json:"message,omitempty"`
}
// DeploymentStatus is returned when querying deployment
type DeploymentStatus struct {
Replicas int `json:"replicas,omitempty"`
@ -110,6 +117,7 @@ type DeploymentStatus struct {
ReadyReplicas int `json:"readyReplicas,omitempty"`
AvailableReplicas int `json:"availableReplicas,omitempty"`
UnavailableReplicas int `json:"unavailableReplicas,omitempty"`
Conditions []DeploymentCondition `json:"conditions,omitempty"`
}
// Deployment is Kubernetes deployment

View File

@ -109,6 +109,90 @@ func (k *kubernetes) Create(s *runtime.Service, opts ...runtime.CreateOption) er
return nil
}
// getMicroService queries kubernetes for micro service
// NOTE: this function is not thread-safe
func (k *kubernetes) getMicroService(labels map[string]string) ([]*runtime.Service, error) {
// get the service status
serviceList := new(client.ServiceList)
r := &client.Resource{
Kind: "service",
Value: serviceList,
}
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,
}
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 {
name := kservice.Metadata.Labels["name"]
version := kservice.Metadata.Labels["version"]
svcMap[name] = &runtime.Service{
Name: name,
Version: version,
Metadata: make(map[string]string),
}
// copy annotations metadata into service metadata
for k, v := range kservice.Metadata.Annotations {
svcMap[name].Metadata[k] = v
}
}
// collect additional info from kubernetes deployment
for _, kdep := range depList.Items {
name := kdep.Metadata.Labels["name"]
if svc, ok := svcMap[name]; ok {
// set the service source
svc.Source = kdep.Metadata.Annotations["source"]
// copy all annotations metadata into service metadata
for k, v := range kdep.Metadata.Annotations {
svc.Metadata[k] = v
}
// parse out deployment status
if len(kdep.Status.Conditions) > 0 {
status := kdep.Status.Conditions[0].Type
// 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
if build, ok := kdep.Metadata.Annotations["build"]; ok {
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))
for _, service := range svcMap {
services = append(services, service)
}
return services, nil
}
// Get returns all instances of given service
func (k *kubernetes) Get(name string, opts ...runtime.GetOption) ([]*runtime.Service, error) {
k.Lock()
@ -119,11 +203,12 @@ func (k *kubernetes) Get(name string, opts ...runtime.GetOption) ([]*runtime.Ser
return nil, errors.New("missing service name")
}
// set the default label
// set the default labels
labels := map[string]string{
"micro": "service",
"name": name,
}
var options runtime.GetOptions
for _, o := range opts {
o(&options)
@ -136,25 +221,21 @@ func (k *kubernetes) Get(name string, opts ...runtime.GetOption) ([]*runtime.Ser
log.Debugf("Runtime querying service %s", name)
serviceList := new(client.ServiceList)
r := &client.Resource{
Kind: "service",
Value: serviceList,
}
if err := k.client.Get(r, labels); err != nil {
return nil, err
return k.getMicroService(labels)
}
services := make([]*runtime.Service, 0, len(serviceList.Items))
for _, kservice := range serviceList.Items {
service := &runtime.Service{
Name: kservice.Metadata.Name,
Version: kservice.Metadata.Version,
}
services = append(services, service)
// List the managed services
func (k *kubernetes) List() ([]*runtime.Service, error) {
k.Lock()
defer k.Unlock()
labels := map[string]string{
"micro": "service",
}
return services, nil
log.Debugf("Runtime listing all micro services")
return k.getMicroService(labels)
}
// Update the service in place
@ -202,39 +283,6 @@ func (k *kubernetes) Delete(s *runtime.Service) error {
return nil
}
// List the managed services
func (k *kubernetes) List() ([]*runtime.Service, error) {
serviceList := new(client.ServiceList)
r := &client.Resource{
Kind: "service",
Value: serviceList,
}
if err := k.client.List(r); err != nil {
return nil, err
}
log.Debugf("Runtime found %d micro services", len(serviceList.Items))
services := make([]*runtime.Service, 0, len(serviceList.Items))
for _, service := range serviceList.Items {
buildTime, err := time.Parse(time.RFC3339, service.Metadata.Annotations["build"])
if err != nil {
log.Debugf("Runtime error parsing build time for %s: %v", service.Metadata.Name, err)
continue
}
// add the service to the list of services
svc := &runtime.Service{
Name: service.Metadata.Name,
Version: fmt.Sprintf("%d", buildTime.Unix()),
}
services = append(services, svc)
}
return services, nil
}
// run runs the runtime management loop
func (k *kubernetes) run(events <-chan runtime.Event) {
t := time.NewTicker(time.Second * 10)

View File

@ -88,4 +88,6 @@ type Service struct {
Exec []string
// Version of the service
Version string
// Metadata stores metadata
Metadata map[string]string
}

View File

@ -19,6 +19,7 @@ func toProto(s *runtime.Service) *pb.Service {
Source: s.Source,
Path: s.Path,
Exec: s.Exec,
Metadata: s.Metadata,
}
}
@ -29,6 +30,7 @@ func toService(s *pb.Service) *runtime.Service {
Source: s.Source,
Path: s.Path,
Exec: s.Exec,
Metadata: s.Metadata,
}
}

View File

@ -31,6 +31,8 @@ type Service struct {
Path string `protobuf:"bytes,4,opt,name=path,proto3" json:"path,omitempty"`
// command to execute
Exec []string `protobuf:"bytes,5,rep,name=exec,proto3" json:"exec,omitempty"`
// service metadata
Metadata map[string]string `protobuf:"bytes,6,rep,name=metadata,proto3" json:"metadata,omitempty" protobuf_key:"bytes,1,opt,name=key,proto3" protobuf_val:"bytes,2,opt,name=value,proto3"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
XXX_sizecache int32 `json:"-"`
@ -96,6 +98,13 @@ func (m *Service) GetExec() []string {
return nil
}
func (m *Service) GetMetadata() map[string]string {
if m != nil {
return m.Metadata
}
return nil
}
type CreateOptions struct {
// command to pass in
Command []string `protobuf:"bytes,1,rep,name=command,proto3" json:"command,omitempty"`
@ -570,6 +579,7 @@ func (m *ListResponse) GetServices() []*Service {
func init() {
proto.RegisterType((*Service)(nil), "go.micro.runtime.Service")
proto.RegisterMapType((map[string]string)(nil), "go.micro.runtime.Service.MetadataEntry")
proto.RegisterType((*CreateOptions)(nil), "go.micro.runtime.CreateOptions")
proto.RegisterType((*CreateRequest)(nil), "go.micro.runtime.CreateRequest")
proto.RegisterType((*CreateResponse)(nil), "go.micro.runtime.CreateResponse")
@ -587,32 +597,36 @@ func init() {
func init() { proto.RegisterFile("runtime.proto", fileDescriptor_86e2dd377c869464) }
var fileDescriptor_86e2dd377c869464 = []byte{
// 425 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0xc9, 0xcb, 0xd3, 0x40,
0x14, 0xff, 0xda, 0xd4, 0x44, 0x5f, 0x8c, 0x94, 0x39, 0xc8, 0x58, 0x5c, 0xca, 0x1c, 0xc4, 0x53,
0x0e, 0x2d, 0x0a, 0x9e, 0x8d, 0xf4, 0x62, 0x11, 0x52, 0x04, 0xaf, 0x31, 0x7d, 0x68, 0xc0, 0x64,
0x62, 0x66, 0x52, 0xbd, 0x79, 0xf2, 0xff, 0x96, 0xd9, 0xba, 0xa4, 0x93, 0xef, 0xd2, 0xdb, 0xbc,
0xa5, 0xbf, 0xf7, 0x5b, 0x4a, 0x20, 0xe9, 0xfa, 0x46, 0x56, 0x35, 0xa6, 0x6d, 0xc7, 0x25, 0x27,
0xf3, 0xef, 0x3c, 0xad, 0xab, 0xb2, 0xe3, 0xa9, 0xed, 0xb3, 0xdf, 0x10, 0xed, 0xb0, 0x3b, 0x54,
0x25, 0x12, 0x02, 0xb3, 0xa6, 0xa8, 0x91, 0x4e, 0x96, 0x93, 0x37, 0x8f, 0x72, 0xfd, 0x26, 0x14,
0xa2, 0x03, 0x76, 0xa2, 0xe2, 0x0d, 0x9d, 0xea, 0xb6, 0x2b, 0xc9, 0x53, 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xf7, 0xc5, 0xed, 0xb8, 0x0e, 0xc5, 0xe2, 0x26, 0x10, 0x7f, 0xaa, 0x84,
0x33, 0x94, 0x7d, 0x84, 0xc7, 0xa6, 0xbc, 0xc9, 0x85, 0xd5, 0xbf, 0x00, 0xa2, 0xdc, 0x4c, 0xc9,
0x16, 0x42, 0x93, 0x35, 0x19, 0xfd, 0x7f, 0xd8, 0xeb, 0x8b, 0xe5, 0xf8, 0x82, 0xa5, 0x7b, 0x47,
0x32, 0x08, 0x36, 0x28, 0x89, 0x3f, 0x54, 0x07, 0xf4, 0x62, 0x64, 0x7a, 0x44, 0xd9, 0x42, 0x68,
0x0c, 0xf6, 0x91, 0xba, 0x08, 0xd0, 0x47, 0x6a, 0x90, 0x8d, 0x86, 0x33, 0xbe, 0xfa, 0xe0, 0x2e,
0x72, 0xf3, 0xc1, 0x0d, 0x22, 0xb9, 0x23, 0x1b, 0x98, 0xa9, 0x14, 0x88, 0x47, 0xc6, 0x59, 0x58,
0x8b, 0x97, 0x63, 0x63, 0x07, 0xf4, 0x2d, 0xd4, 0xdf, 0xb3, 0xf5, 0xff, 0x00, 0x00, 0x00, 0xff,
0xff, 0x2f, 0x63, 0x84, 0x1c, 0xe0, 0x04, 0x00, 0x00,
// 485 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xa4, 0x54, 0xcb, 0x6f, 0x94, 0x40,
0x18, 0x2f, 0xcb, 0x16, 0xea, 0x87, 0x98, 0xcd, 0xc4, 0x98, 0x71, 0xe3, 0x63, 0xc3, 0x41, 0x7b,
0xe2, 0xb0, 0x8d, 0xc6, 0xc7, 0xb1, 0x34, 0x7b, 0x71, 0x63, 0x42, 0x63, 0xe2, 0x75, 0x64, 0xbf,
0x28, 0xb1, 0x30, 0xc8, 0x0c, 0x1b, 0x7b, 0xf2, 0xe4, 0x1f, 0xed, 0xcd, 0xcc, 0x6b, 0xdb, 0xa5,
0xd0,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80, 0x43, 0xa4, 0x7a, 0xd9, 0x68, 0x38, 0xe3, 0xeb, 0x10, 0xdc, 0x5e, 0x6e, 0x43, 0x70, 0xbd,
0x48, 0x8e, 0xc8, 0x0a, 0xa6, 0x2a, 0x05, 0x32, 0x20, 0xe3, 0x56, 0x58, 0xf3, 0x17, 0x63, 0x6b,
0x07, 0xf4, 0x2d, 0xd0, 0xbf, 0xd4, 0xb3, 0xff, 0x01, 0x00, 0x00, 0xff, 0xff, 0xbc, 0xce, 0x6e,
0x31, 0x63, 0x05, 0x00, 0x00,
}

View File

@ -21,6 +21,8 @@ message Service {
string path = 4;
// command to execute
repeated string exec = 5;
// service metadata
map<string,string> metadata = 6;
}
message CreateOptions {

View File

@ -60,6 +60,7 @@ func (s *svc) Create(svc *runtime.Service, opts ...runtime.CreateOption) error {
Name: svc.Name,
Version: svc.Version,
Source: svc.Source,
Metadata: svc.Metadata,
},
Options: &pb.CreateOptions{
Command: options.Command,
@ -103,6 +104,7 @@ func (s *svc) Get(name string, opts ...runtime.GetOption) ([]*runtime.Service, e
Source: service.Source,
Path: service.Path,
Exec: service.Exec,
Metadata: service.Metadata,
}
services = append(services, svc)
}
@ -160,6 +162,7 @@ func (s *svc) List() ([]*runtime.Service, error) {
Source: service.Source,
Path: service.Path,
Exec: service.Exec,
Metadata: service.Metadata,
}
services = append(services, svc)
}