|
|
@ -17,26 +17,22 @@ limitations under the License.
|
|
|
|
package configurator
|
|
|
|
package configurator
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
|
|
|
|
"fmt"
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"log"
|
|
|
|
"os/exec"
|
|
|
|
|
|
|
|
"regexp"
|
|
|
|
"regexp"
|
|
|
|
"strings"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/ghodss/yaml"
|
|
|
|
"github.com/kubernetes/deployment-manager/common"
|
|
|
|
"github.com/kubernetes/deployment-manager/common"
|
|
|
|
|
|
|
|
"github.com/kubernetes/deployment-manager/util"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(jackgr): Define an interface and a struct type for Configurator and move initialization to the caller.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
type Configurator struct {
|
|
|
|
type Configurator struct {
|
|
|
|
KubePath string
|
|
|
|
k util.Kubernetes
|
|
|
|
Arguments []string
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func NewConfigurator(kubectlPath string, arguments []string) *Configurator {
|
|
|
|
func NewConfigurator(kubernetes util.Kubernetes) *Configurator {
|
|
|
|
return &Configurator{kubectlPath, arguments}
|
|
|
|
return &Configurator{kubernetes}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// operation is an enumeration type for kubectl operations.
|
|
|
|
// operation is an enumeration type for kubectl operations.
|
|
|
@ -50,10 +46,6 @@ const (
|
|
|
|
ReplaceOperation operation = "replace"
|
|
|
|
ReplaceOperation operation = "replace"
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
func (o operation) String() string {
|
|
|
|
|
|
|
|
return string(o)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO(jackgr): Configure resources without dependencies in parallel.
|
|
|
|
// TODO(jackgr): Configure resources without dependencies in parallel.
|
|
|
|
|
|
|
|
|
|
|
|
// Error is an error type that captures errors from the multiple calls to kubectl
|
|
|
|
// Error is an error type that captures errors from the multiple calls to kubectl
|
|
|
@ -128,65 +120,64 @@ func (a *Configurator) Configure(c *common.Configuration, o operation) (string,
|
|
|
|
return strings.Join(output, "\n"), nil
|
|
|
|
return strings.Join(output, "\n"), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (a *Configurator) configureResource(resource *common.Resource, o operation) (string, error) {
|
|
|
|
func marshalResource(resource *common.Resource) (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 {
|
|
|
|
if len(resource.Properties) > 0 {
|
|
|
|
var err error
|
|
|
|
y, err := yaml.Marshal(resource.Properties)
|
|
|
|
y, err = yaml.Marshal(resource.Properties)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
if err != nil {
|
|
|
|
e := fmt.Errorf("yaml marshal failed for resource: %v: %v", resource.Name, err)
|
|
|
|
return "", fmt.Errorf("yaml marshal failed for resource: %v: %v", resource.Name, err)
|
|
|
|
resource.State = failState(e)
|
|
|
|
|
|
|
|
return "", e
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(y), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return "", nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(y) > 0 {
|
|
|
|
func (a *Configurator) configureResource(resource *common.Resource, o operation) (string, error) {
|
|
|
|
args = append(args, "-f", "-")
|
|
|
|
ret := ""
|
|
|
|
}
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
|
|
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 kubectl for resource: %v: %v", resource.Name, err)
|
|
|
|
|
|
|
|
resource.State = failState(e)
|
|
|
|
|
|
|
|
return "", e
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if err := cmd.Wait(); err != nil {
|
|
|
|
switch o {
|
|
|
|
// Treat delete special. If a delete is issued and a resource is not found, treat it as
|
|
|
|
case CreateOperation:
|
|
|
|
// success.
|
|
|
|
obj, err := marshalResource(resource)
|
|
|
|
if o == DeleteOperation && strings.HasSuffix(strings.TrimSpace(combined.String()), "not found") {
|
|
|
|
if err != nil {
|
|
|
|
log.Println(resource.Name + " not found, treating as success for delete")
|
|
|
|
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 {
|
|
|
|
} else {
|
|
|
|
e := fmt.Errorf("kubectl failed for resource: %v: %v: %v", resource.Name, err, combined.String())
|
|
|
|
resource.State = &common.ResourceState{Status: common.Created}
|
|
|
|
resource.State = failState(e)
|
|
|
|
|
|
|
|
return combined.String(), e
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, nil
|
|
|
|
|
|
|
|
case GetOperation:
|
|
|
|
|
|
|
|
return a.k.Get(resource.Name, resource.Type)
|
|
|
|
|
|
|
|
case DeleteOperation:
|
|
|
|
|
|
|
|
ret, err = a.k.Delete(resource.Name, resource.Type)
|
|
|
|
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
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 failState(e error) *common.ResourceState {
|
|
|
|
func failState(e error) *common.ResourceState {
|
|
|
|