Merge pull request #993 from micro/k8s-fixes

Add fixes for label selector and skipping things that don't match
This commit is contained in:
Asim Aslam 2019-11-26 22:32:58 +00:00 committed by GitHub
commit 7318807dce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 20 additions and 6 deletions

View File

@ -119,7 +119,14 @@ func (r *Request) Body(in interface{}) *Request {
// Params isused to set paramters on a request // Params isused to set paramters on a request
func (r *Request) Params(p *Params) *Request { func (r *Request) Params(p *Params) *Request {
for k, v := range p.LabelSelector { for k, v := range p.LabelSelector {
r.params.Add("labelSelector", k+"="+v) // create new key=value pair
value := fmt.Sprintf("%s=%s", k, v)
// check if there's an existing value
if label := r.params.Get("labelSelector"); len(label) > 0 {
value = fmt.Sprintf("%s,%s", label, value)
}
// set and overwrite the value
r.params.Set("labelSelector", value)
} }
return r return r

View File

@ -126,9 +126,9 @@ func (c *client) Update(r *Resource) error {
switch r.Kind { switch r.Kind {
case "service": case "service":
req.Body(r.Value.(*Service).Spec) req.Body(r.Value.(*Service))
case "deployment": case "deployment":
req.Body(r.Value.(*Deployment).Spec) req.Body(r.Value.(*Deployment))
default: default:
return errors.New("unsupported resource") return errors.New("unsupported resource")
} }
@ -151,6 +151,5 @@ func (c *client) List(r *Resource) error {
labels := map[string]string{ labels := map[string]string{
"micro": "service", "micro": "service",
} }
return c.Get(r, labels) return c.Get(r, labels)
} }

View File

@ -199,10 +199,13 @@ func (k *kubernetes) run(events <-chan runtime.Event) {
continue continue
} }
// format the name
name := client.Format(event.Service)
// set the default labels // set the default labels
labels := map[string]string{ labels := map[string]string{
"micro": "service", "micro": "service",
"name": event.Service, "name": name,
} }
if len(event.Version) > 0 { if len(event.Version) > 0 {
@ -225,6 +228,11 @@ func (k *kubernetes) run(events <-chan runtime.Event) {
// technically we should not receive multiple versions but hey ho // technically we should not receive multiple versions but hey ho
for _, service := range deployed.Items { for _, service := range deployed.Items {
// check the name matches
if service.Metadata.Name != name {
continue
}
// update build time annotation // update build time annotation
if service.Spec.Template.Metadata.Annotations == nil { if service.Spec.Template.Metadata.Annotations == nil {
service.Spec.Template.Metadata.Annotations = make(map[string]string) service.Spec.Template.Metadata.Annotations = make(map[string]string)
@ -242,7 +250,7 @@ func (k *kubernetes) run(events <-chan runtime.Event) {
// update the build time // update the build time
service.Spec.Template.Metadata.Annotations["build"] = event.Timestamp.Format(time.RFC3339) service.Spec.Template.Metadata.Annotations["build"] = event.Timestamp.Format(time.RFC3339)
log.Debugf("Runtime updating service: %s", event.Service) log.Debugf("Runtime updating service: %s deployment: %s", event.Service, service.Metadata.Name)
if err := k.client.Update(deploymentResource(&service)); err != nil { if err := k.client.Update(deploymentResource(&service)); err != nil {
log.Debugf("Runtime failed to update service %s: %v", event.Service, err) log.Debugf("Runtime failed to update service %s: %v", event.Service, err)
continue continue