From dcb3529fba9e2480f19951013f97ba75041a8f32 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 26 Feb 2018 09:12:55 +0000 Subject: [PATCH] resource/kubernetes_persistent_volume_claim: Improve event log polling for warns --- kubernetes/event_helpers.go | 18 ++++++++++++------ ...ource_kubernetes_persistent_volume_claim.go | 15 ++++++++++++++- 2 files changed, 26 insertions(+), 7 deletions(-) diff --git a/kubernetes/event_helpers.go b/kubernetes/event_helpers.go index 5ba9ea256a..876abce3bb 100644 --- a/kubernetes/event_helpers.go +++ b/kubernetes/event_helpers.go @@ -12,11 +12,15 @@ import ( ) func getLastWarningsForObject(conn *kubernetes.Clientset, metadata meta_v1.ObjectMeta, kind string, limit int) ([]api.Event, error) { - fs := fields.Set(map[string]string{ - "involvedObject.name": metadata.Name, - "involvedObject.namespace": metadata.Namespace, - "involvedObject.kind": kind, - }).String() + m := map[string]string{ + "involvedObject.name": metadata.Name, + "involvedObject.kind": kind, + } + if metadata.Namespace != "" { + m["involvedObject.namespace"] = metadata.Namespace + } + + fs := fields.Set(m).String() log.Printf("[DEBUG] Looking up events via this selector: %q", fs) out, err := conn.CoreV1().Events(metadata.Namespace).List(meta_v1.ListOptions{ FieldSelector: fs, @@ -61,7 +65,9 @@ func getLastWarningsForObject(conn *kubernetes.Clientset, metadata meta_v1.Objec func stringifyEvents(events []api.Event) string { var output string for _, e := range events { - output += fmt.Sprintf("\n * %s: %s", e.Reason, e.Message) + output += fmt.Sprintf("\n * %s (%s): %s: %s", + e.InvolvedObject.Name, e.InvolvedObject.Kind, + e.Reason, e.Message) } return output } diff --git a/kubernetes/resource_kubernetes_persistent_volume_claim.go b/kubernetes/resource_kubernetes_persistent_volume_claim.go index 580a968434..87f6462810 100644 --- a/kubernetes/resource_kubernetes_persistent_volume_claim.go +++ b/kubernetes/resource_kubernetes_persistent_volume_claim.go @@ -190,10 +190,23 @@ func resourceKubernetesPersistentVolumeClaimCreate(d *schema.ResourceData, meta } _, err = stateConf.WaitForState() if err != nil { - lastWarnings, wErr := getLastWarningsForObject(conn, out.ObjectMeta, "PersistentVolumeClaim", 3) + var lastWarnings []api.Event + var wErr error + + lastWarnings, wErr = getLastWarningsForObject(conn, out.ObjectMeta, "PersistentVolumeClaim", 3) if wErr != nil { return wErr } + + if len(lastWarnings) == 0 { + lastWarnings, wErr = getLastWarningsForObject(conn, meta_v1.ObjectMeta{ + Name: out.Spec.VolumeName, + }, "PersistentVolume", 3) + if wErr != nil { + return wErr + } + } + return fmt.Errorf("%s%s", err, stringifyEvents(lastWarnings)) } }