func (a *Configurator) configureResource(resource *common.Resource, o operation) (string, error) { args := []string{o.String()} if o == GetOperation { args = append(args, "-o", "yaml") if resource.Type != "" { args = append(args, resource.Type) if resource.Name != "" { args = append(args, resource.Name) } } } var y []byte if len(resource.Properties) > 0 { var err error y, err = yaml.Marshal(resource.Properties) if err != nil { e := fmt.Errorf("yaml marshal failed for resource: %v: %v", resource.Name, err) resource.State = failState(e) return "", e } } if len(y) > 0 { args = append(args, "-f", "-") } args = append(args, a.Arguments...) cmd := exec.Command(a.KubePath, args...) cmd.Stdin = bytes.NewBuffer(y) // Combine stdout and stderr into a single dynamically resized buffer combined := &bytes.Buffer{} cmd.Stdout = combined cmd.Stderr = combined if err := cmd.Start(); err != nil { e := fmt.Errorf("cannot start kubetcl for resource: %v: %v", resource.Name, err) resource.State = failState(e) return "", e } if err := cmd.Wait(); err != nil { // Treat delete special. If a delete is issued and a resource is not found, treat it as // success. if o == DeleteOperation && strings.HasSuffix(strings.TrimSpace(combined.String()), "not found") { log.Println(resource.Name + " not found, treating as success for delete") } else { e := fmt.Errorf("kubetcl failed for resource: %v: %v: %v", resource.Name, err, combined.String()) resource.State = failState(e) return combined.String(), e } } log.Printf("kubectl succeeded for resource: %v: SysTime: %v UserTime: %v\n%v", resource.Name, cmd.ProcessState.SystemTime(), cmd.ProcessState.UserTime(), combined.String()) resource.State = &common.ResourceState{Status: common.Created} return combined.String(), nil }
func (a *Configurator) configureResource(resource *common.Resource, o operation) (string, error) { ret := "" var err error switch o { case CreateOperation: obj, err := marshalResource(resource) if err != nil { resource.State = failState(err) return "", err } ret, err = a.k.Create(obj) if err != nil { resource.State = failState(err) } else { resource.State = &common.ResourceState{Status: common.Created} } return ret, nil case ReplaceOperation: obj, err := marshalResource(resource) if err != nil { resource.State = failState(err) return "", err } ret, err = a.k.Replace(obj) if err != nil { resource.State = failState(err) } else { resource.State = &common.ResourceState{Status: common.Created} } return ret, nil case GetOperation: return a.k.Get(resource.Name, resource.Type) case DeleteOperation: obj, err := marshalResource(resource) if err != nil { resource.State = failState(err) return "", err } ret, err = a.k.Delete(obj) // Treat deleting a non-existent resource as success. if err != nil { if strings.HasSuffix(strings.TrimSpace(ret), "not found") { resource.State = &common.ResourceState{Status: common.Created} return ret, nil } resource.State = failState(err) } return ret, err default: return "", fmt.Errorf("invalid operation %s for resource: %v: %v", o, resource.Name, err) } }