fix k8s issues (#1349)
This commit is contained in:
parent
0449138f61
commit
ca8684a886
@ -27,7 +27,7 @@ type kubernetes struct {
|
|||||||
|
|
||||||
// getService queries kubernetes for micro service
|
// getService queries kubernetes for micro service
|
||||||
// NOTE: this function is not thread-safe
|
// NOTE: this function is not thread-safe
|
||||||
func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, error) {
|
func (k *kubernetes) getService(labels map[string]string) ([]*service, error) {
|
||||||
// get the service status
|
// get the service status
|
||||||
serviceList := new(client.ServiceList)
|
serviceList := new(client.ServiceList)
|
||||||
r := &client.Resource{
|
r := &client.Resource{
|
||||||
@ -61,7 +61,7 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
|
|||||||
}
|
}
|
||||||
|
|
||||||
// service map
|
// service map
|
||||||
svcMap := make(map[string]*runtime.Service)
|
svcMap := make(map[string]*service)
|
||||||
|
|
||||||
// collect info from kubernetes service
|
// collect info from kubernetes service
|
||||||
for _, kservice := range serviceList.Items {
|
for _, kservice := range serviceList.Items {
|
||||||
@ -71,15 +71,18 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
|
|||||||
version := kservice.Metadata.Labels["version"]
|
version := kservice.Metadata.Labels["version"]
|
||||||
|
|
||||||
// save as service
|
// save as service
|
||||||
svcMap[name+version] = &runtime.Service{
|
svcMap[name+version] = &service{
|
||||||
Name: name,
|
Service: &runtime.Service{
|
||||||
Version: version,
|
Name: name,
|
||||||
Metadata: make(map[string]string),
|
Version: version,
|
||||||
|
Metadata: make(map[string]string),
|
||||||
|
},
|
||||||
|
kservice: &kservice,
|
||||||
}
|
}
|
||||||
|
|
||||||
// copy annotations metadata into service metadata
|
// copy annotations metadata into service metadata
|
||||||
for k, v := range kservice.Metadata.Annotations {
|
for k, v := range kservice.Metadata.Annotations {
|
||||||
svcMap[name+version].Metadata[k] = v
|
svcMap[name+version].Service.Metadata[k] = v
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -99,9 +102,9 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
|
|||||||
|
|
||||||
// set the service name, version and source
|
// set the service name, version and source
|
||||||
// based on existing annotations we stored
|
// based on existing annotations we stored
|
||||||
svc.Name = kdep.Metadata.Annotations["name"]
|
svc.Service.Name = kdep.Metadata.Annotations["name"]
|
||||||
svc.Version = kdep.Metadata.Annotations["version"]
|
svc.Service.Version = kdep.Metadata.Annotations["version"]
|
||||||
svc.Source = kdep.Metadata.Annotations["source"]
|
svc.Service.Source = kdep.Metadata.Annotations["source"]
|
||||||
|
|
||||||
// delete from metadata
|
// delete from metadata
|
||||||
delete(kdep.Metadata.Annotations, "name")
|
delete(kdep.Metadata.Annotations, "name")
|
||||||
@ -110,7 +113,7 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
|
|||||||
|
|
||||||
// copy all annotations metadata into service metadata
|
// copy all annotations metadata into service metadata
|
||||||
for k, v := range kdep.Metadata.Annotations {
|
for k, v := range kdep.Metadata.Annotations {
|
||||||
svc.Metadata[k] = v
|
svc.Service.Metadata[k] = v
|
||||||
}
|
}
|
||||||
|
|
||||||
// get the status from the pods
|
// get the status from the pods
|
||||||
@ -130,12 +133,14 @@ func (k *kubernetes) getService(labels map[string]string) ([]*runtime.Service, e
|
|||||||
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
if logger.V(logger.DebugLevel, logger.DefaultLogger) {
|
||||||
logger.Debugf("Runtime setting %s service deployment status: %v", name, status)
|
logger.Debugf("Runtime setting %s service deployment status: %v", name, status)
|
||||||
}
|
}
|
||||||
svc.Metadata["status"] = status
|
svc.Service.Metadata["status"] = status
|
||||||
|
// save deployment
|
||||||
|
svc.kdeploy = &kdep
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// collect all the services and return
|
// collect all the services and return
|
||||||
services := make([]*runtime.Service, 0, len(serviceList.Items))
|
services := make([]*service, 0, len(serviceList.Items))
|
||||||
|
|
||||||
for _, service := range svcMap {
|
for _, service := range svcMap {
|
||||||
services = append(services, service)
|
services = append(services, service)
|
||||||
@ -311,7 +316,17 @@ func (k *kubernetes) Read(opts ...runtime.ReadOption) ([]*runtime.Service, error
|
|||||||
labels["micro"] = options.Type
|
labels["micro"] = options.Type
|
||||||
}
|
}
|
||||||
|
|
||||||
return k.getService(labels)
|
srvs, err := k.getService(labels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var services []*runtime.Service
|
||||||
|
for _, service := range srvs {
|
||||||
|
services = append(services, service.Service)
|
||||||
|
}
|
||||||
|
|
||||||
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// List the managed services
|
// List the managed services
|
||||||
@ -327,26 +342,64 @@ func (k *kubernetes) List() ([]*runtime.Service, error) {
|
|||||||
logger.Debugf("Runtime listing all micro services")
|
logger.Debugf("Runtime listing all micro services")
|
||||||
}
|
}
|
||||||
|
|
||||||
return k.getService(labels)
|
srvs, err := k.getService(labels)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var services []*runtime.Service
|
||||||
|
for _, service := range srvs {
|
||||||
|
services = append(services, service.Service)
|
||||||
|
}
|
||||||
|
|
||||||
|
return services, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Update the service in place
|
// Update the service in place
|
||||||
func (k *kubernetes) Update(s *runtime.Service) error {
|
func (k *kubernetes) Update(s *runtime.Service) error {
|
||||||
// create new kubernetes micro service
|
// get the existing service
|
||||||
opts := runtime.CreateOptions{
|
// set the default labels
|
||||||
Type: k.options.Type,
|
labels := map[string]string{
|
||||||
|
"micro": k.options.Type,
|
||||||
}
|
}
|
||||||
|
|
||||||
// set image
|
if len(s.Name) > 0 {
|
||||||
opts.Image = k.getImage(s, opts)
|
labels["name"] = client.Format(s.Name)
|
||||||
|
}
|
||||||
|
|
||||||
// new pseudo service
|
if len(s.Version) > 0 {
|
||||||
service := newService(s, opts)
|
labels["version"] = s.Version
|
||||||
|
}
|
||||||
|
|
||||||
// update build time annotation
|
// get the existing service
|
||||||
service.kdeploy.Spec.Template.Metadata.Annotations["build"] = time.Now().Format(time.RFC3339)
|
services, err := k.getService(labels)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return service.Update(k.client)
|
// update the relevant services
|
||||||
|
for _, service := range services {
|
||||||
|
// nil check
|
||||||
|
if service.kdeploy.Metadata == nil || service.kdeploy.Metadata.Annotations == nil {
|
||||||
|
md := new(client.Metadata)
|
||||||
|
md.Annotations = make(map[string]string)
|
||||||
|
service.kdeploy.Metadata = md
|
||||||
|
}
|
||||||
|
|
||||||
|
// update metadata
|
||||||
|
for k, v := range s.Metadata {
|
||||||
|
service.kdeploy.Metadata.Annotations[k] = v
|
||||||
|
}
|
||||||
|
// update build time annotation
|
||||||
|
service.kdeploy.Spec.Template.Metadata.Annotations["build"] = time.Now().Format(time.RFC3339)
|
||||||
|
|
||||||
|
// update the service
|
||||||
|
if err := service.Update(k.client); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Delete removes a service
|
// Delete removes a service
|
||||||
|
Loading…
x
Reference in New Issue
Block a user