github.com/splunk/dan1-qbec@v0.7.3/internal/commands/wait.go (about) 1 /* 2 Copyright 2019 Splunk Inc. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package commands 18 19 import ( 20 "fmt" 21 "sync" 22 "time" 23 24 "github.com/pkg/errors" 25 "github.com/splunk/qbec/internal/model" 26 "github.com/splunk/qbec/internal/sio" 27 "github.com/splunk/qbec/internal/types" 28 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 29 "k8s.io/apimachinery/pkg/runtime/schema" 30 "k8s.io/apimachinery/pkg/watch" 31 "k8s.io/client-go/dynamic" 32 ) 33 34 // waitListener listens to rollout status updates and provides feedback to the user. 35 type waitListener struct { 36 start time.Time // start time using which relative progress times are printed 37 displayNameFn func(meta model.K8sMeta) string // MUST produce distinct strings for each object, name used as internal key 38 l sync.Mutex // locks concurrent access to field below 39 remaining map[string]bool // objects not yet marked "done" 40 } 41 42 func (w *waitListener) since() time.Duration { 43 return time.Since(w.start).Round(time.Second) 44 } 45 46 // OnInit implements the interface method and prints the name of all objects on which we ware waiting 47 func (w *waitListener) OnInit(objects []model.K8sMeta) { 48 w.start = time.Now() 49 w.remaining = map[string]bool{} 50 sio.Noticef("waiting for readiness of %d objects\n", len(objects)) 51 for _, o := range objects { 52 name := w.displayNameFn(o) 53 w.remaining[name] = true 54 sio.Printf(" - %s\n", w.displayNameFn(o)) 55 } 56 sio.Println() 57 } 58 59 // OnStatusChange prints the updated status of the object and removes it from the internal list of remaining items 60 // if the status is marked done. 61 func (w *waitListener) OnStatusChange(object model.K8sMeta, rs types.RolloutStatus) { 62 w.l.Lock() 63 defer w.l.Unlock() 64 if rs.Done { 65 name := w.displayNameFn(object) 66 delete(w.remaining, name) 67 sio.Noticef("✓ %-6s: %s :: %s (%d remaining)\n", w.since(), w.displayNameFn(object), rs.Description, len(w.remaining)) 68 return 69 } 70 sio.Debugf(" %-6s: %s :: %s\n", w.since(), w.displayNameFn(object), rs.Description) 71 } 72 73 // OnError prints the error for the object to console. 74 func (w *waitListener) OnError(object model.K8sMeta, err error) { 75 w.l.Lock() 76 defer w.l.Unlock() 77 sio.Errorf("%-6s: %s :: %v\n", w.since(), w.displayNameFn(object), err) 78 } 79 80 // OnEnd prints a list of objects that are not marked complete. 81 func (w *waitListener) OnEnd(err error) { 82 w.l.Lock() 83 defer w.l.Unlock() 84 sio.Println() 85 if len(w.remaining) > 0 { 86 sio.Printf("%s: rollout not complete for the following %d objects\n", w.since(), len(w.remaining)) 87 for name := range w.remaining { 88 sio.Printf(" - %s\n", name) 89 } 90 } 91 if err == nil { 92 sio.Noticef("✓ %s: rollout complete\n", w.since()) 93 return 94 } 95 } 96 97 type resourceInterfaceProvider func(gvk schema.GroupVersionKind, namespace string) (dynamic.ResourceInterface, error) 98 99 func waitWatcher(ri resourceInterfaceProvider, obj model.K8sMeta) (watch.Interface, error) { 100 in, err := ri(obj.GroupVersionKind(), obj.GetNamespace()) 101 if err != nil { 102 return nil, errors.Wrap(err, "get resource provider") 103 } 104 _, err = in.Get(obj.GetName(), metav1.GetOptions{}) 105 if err != nil { // object must exist 106 return nil, errors.Wrap(err, "get object") 107 } 108 watchXface, err := in.Watch(metav1.ListOptions{ 109 FieldSelector: fmt.Sprintf(`metadata.name=%s`, obj.GetName()), // XXX: escaping 110 }) 111 if err != nil { // XXX: implement fallback to poll with get if watch has permissions issues 112 return nil, errors.Wrap(err, "get watch interface") 113 } 114 return watchXface, nil 115 }