provider/kubernetes: Provide more details about why PVC failed to bind

This commit is contained in:
Radek Simko 2017-06-02 14:18:35 +01:00
parent 483b849448
commit b6abf1f2bb
No known key found for this signature in database
GPG Key ID: 6823F3DCCE01BB19
1 changed files with 21 additions and 1 deletions

View File

@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"k8s.io/apimachinery/pkg/api/errors" "k8s.io/apimachinery/pkg/api/errors"
meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1" meta_v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/fields"
pkgApi "k8s.io/apimachinery/pkg/types" pkgApi "k8s.io/apimachinery/pkg/types"
api "k8s.io/kubernetes/pkg/api/v1" api "k8s.io/kubernetes/pkg/api/v1"
kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/clientset" kubernetes "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
@ -165,6 +166,7 @@ func resourceKubernetesPersistentVolumeClaimCreate(d *schema.ResourceData, meta
name := out.ObjectMeta.Name name := out.ObjectMeta.Name
if d.Get("wait_until_bound").(bool) { if d.Get("wait_until_bound").(bool) {
var lastEvent api.Event
stateConf := &resource.StateChangeConf{ stateConf := &resource.StateChangeConf{
Target: []string{"Bound"}, Target: []string{"Bound"},
Pending: []string{"Pending"}, Pending: []string{"Pending"},
@ -176,6 +178,20 @@ func resourceKubernetesPersistentVolumeClaimCreate(d *schema.ResourceData, meta
return out, "", err return out, "", err
} }
events, err := conn.CoreV1().Events(metadata.Namespace).List(meta_v1.ListOptions{
FieldSelector: fields.Set(map[string]string{
"involvedObject.name": metadata.Name,
"involvedObject.namespace": metadata.Namespace,
"involvedObject.kind": "PersistentVolumeClaim",
}).String(),
})
if err != nil {
return out, "", err
}
if len(events.Items) > 0 {
lastEvent = events.Items[0]
}
statusPhase := fmt.Sprintf("%v", out.Status.Phase) statusPhase := fmt.Sprintf("%v", out.Status.Phase)
log.Printf("[DEBUG] Persistent volume claim %s status received: %#v", out.Name, statusPhase) log.Printf("[DEBUG] Persistent volume claim %s status received: %#v", out.Name, statusPhase)
return out, statusPhase, nil return out, statusPhase, nil
@ -183,7 +199,11 @@ func resourceKubernetesPersistentVolumeClaimCreate(d *schema.ResourceData, meta
} }
_, err = stateConf.WaitForState() _, err = stateConf.WaitForState()
if err != nil { if err != nil {
return err reason := ""
if lastEvent.Reason != "" {
reason = fmt.Sprintf(". Reason: %s: %s", lastEvent.Reason, lastEvent.Message)
}
return fmt.Errorf("%s%s", err, reason)
} }
} }
log.Printf("[INFO] Persistent volume claim %s created", out.Name) log.Printf("[INFO] Persistent volume claim %s created", out.Name)