Skip to content

Commit

Permalink
Merge pull request #125 from terraform-providers/f-pvc-improve-event-…
Browse files Browse the repository at this point in the history
…polling

resource/kubernetes_persistent_volume_claim: Improve event log polling for warns
  • Loading branch information
radeksimko authored Feb 26, 2018
2 parents 4052f5c + dcb3529 commit 66901c4
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 7 deletions.
18 changes: 12 additions & 6 deletions kubernetes/event_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
}
15 changes: 14 additions & 1 deletion kubernetes/resource_kubernetes_persistent_volume_claim.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}
Expand Down

0 comments on commit 66901c4

Please sign in to comment.