|
|
@ -39,6 +39,13 @@ type statusable interface {
|
|
|
|
GetStatusConditions() *[]metav1.Condition
|
|
|
|
GetStatusConditions() *[]metav1.Condition
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type StatusChecker struct {
|
|
|
|
|
|
|
|
client client.Client
|
|
|
|
|
|
|
|
timeout time.Duration
|
|
|
|
|
|
|
|
objRefs []object.ObjMetadata
|
|
|
|
|
|
|
|
statusPoller *polling.StatusPoller
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func isReady(ctx context.Context, kubeClient client.Client,
|
|
|
|
func isReady(ctx context.Context, kubeClient client.Client,
|
|
|
|
namespacedName types.NamespacedName, object statusable) wait.ConditionFunc {
|
|
|
|
namespacedName types.NamespacedName, object statusable) wait.ConditionFunc {
|
|
|
|
return func() (bool, error) {
|
|
|
|
return func() (bool, error) {
|
|
|
@ -63,3 +70,101 @@ func isReady(ctx context.Context, kubeClient client.Client,
|
|
|
|
return false, nil
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (sc *StatusChecker) New(kubeConfig *rest.Config, timeout time.Duration) error {
|
|
|
|
|
|
|
|
restMapper, err := apiutil.NewDynamicRESTMapper(kubeConfig)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
client, err := client.New(kubeConfig, client.Options{Mapper: restMapper})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
statusPoller := polling.NewStatusPoller(client, restMapper)
|
|
|
|
|
|
|
|
sc.client = client
|
|
|
|
|
|
|
|
sc.statusPoller = statusPoller
|
|
|
|
|
|
|
|
sc.timeout = timeout
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (sc *StatusChecker) AddChecks(components []string) error {
|
|
|
|
|
|
|
|
var componentRefs []object.ObjMetadata
|
|
|
|
|
|
|
|
for _, deployment := range components {
|
|
|
|
|
|
|
|
objMeta, err := object.CreateObjMetadata(rootArgs.namespace, deployment, schema.GroupKind{Group: "apps", Kind: "Deployment"})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
componentRefs = append(componentRefs, objMeta)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
sc.objRefs = componentRefs
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (sc *StatusChecker) Assess(pollInterval time.Duration) error {
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), sc.timeout)
|
|
|
|
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
opts := polling.Options{PollInterval: pollInterval, UseCache: true}
|
|
|
|
|
|
|
|
eventsChan := sc.statusPoller.Poll(ctx, sc.objRefs, opts)
|
|
|
|
|
|
|
|
coll := collector.NewResourceStatusCollector(sc.objRefs)
|
|
|
|
|
|
|
|
done := coll.ListenWithObserver(eventsChan, collector.ObserverFunc(
|
|
|
|
|
|
|
|
func(statusCollector *collector.ResourceStatusCollector, e event.Event) {
|
|
|
|
|
|
|
|
var rss []*event.ResourceStatus
|
|
|
|
|
|
|
|
for _, rs := range statusCollector.ResourceStatuses {
|
|
|
|
|
|
|
|
rss = append(rss, rs)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
desired := status.CurrentStatus
|
|
|
|
|
|
|
|
aggStatus := aggregator.AggregateStatus(rss, desired)
|
|
|
|
|
|
|
|
if aggStatus == desired {
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
return
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}),
|
|
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
<-done
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if coll.Error != nil {
|
|
|
|
|
|
|
|
return coll.Error
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if ctx.Err() == context.DeadlineExceeded {
|
|
|
|
|
|
|
|
ids := []string{}
|
|
|
|
|
|
|
|
for _, rs := range coll.ResourceStatuses {
|
|
|
|
|
|
|
|
if rs.Status != status.CurrentStatus {
|
|
|
|
|
|
|
|
id := sc.objMetadataToString(rs.Identifier)
|
|
|
|
|
|
|
|
ids = append(ids, id)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Errorf("Health check timed out for [%v]", strings.Join(ids, ", "))
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (sc *StatusChecker) toObjMetadata(cr []meta.NamespacedObjectKindReference) ([]object.ObjMetadata, error) {
|
|
|
|
|
|
|
|
oo := []object.ObjMetadata{}
|
|
|
|
|
|
|
|
for _, c := range cr {
|
|
|
|
|
|
|
|
// For backwards compatibility
|
|
|
|
|
|
|
|
if c.APIVersion == "" {
|
|
|
|
|
|
|
|
c.APIVersion = "apps/v1"
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gv, err := schema.ParseGroupVersion(c.APIVersion)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return []object.ObjMetadata{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
gk := schema.GroupKind{Group: gv.Group, Kind: c.Kind}
|
|
|
|
|
|
|
|
o, err := object.CreateObjMetadata(c.Namespace, c.Name, gk)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return []object.ObjMetadata{}, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
oo = append(oo, o)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
return oo, nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func (sc *StatusChecker) objMetadataToString(om object.ObjMetadata) string {
|
|
|
|
|
|
|
|
return fmt.Sprintf("%s '%s/%s'", om.GroupKind.Kind, om.Namespace, om.Name)
|
|
|
|
|
|
|
|
}
|
|
|
|