GitOps for k8s
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

update.go 1.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. package kubernetes
  2. import (
  3. "bytes"
  4. "os/exec"
  5. "strings"
  6. "github.com/pkg/errors"
  7. "github.com/weaveworks/flux"
  8. "github.com/weaveworks/flux/image"
  9. )
  10. // updatePodController takes the body of a resource definition
  11. // (specified in YAML), the ID of a particular resource and container
  12. // therein, and the name of the new image that should be put in the
  13. // definition (in the format "repo.org/group/name:tag") for that
  14. // resource and container. It returns a new resource definition body
  15. // where all references to the old image have been replaced with the
  16. // new one.
  17. //
  18. // This function has many additional requirements that are likely in flux. Read
  19. // the source to learn about them.
  20. func updatePodController(file []byte, resource flux.ResourceID, container string, newImageID image.Ref) ([]byte, error) {
  21. namespace, kind, name := resource.Components()
  22. if _, ok := resourceKinds[strings.ToLower(kind)]; !ok {
  23. return nil, UpdateNotSupportedError(kind)
  24. }
  25. args := []string{"--namespace", namespace, "--kind", kind, "--name", name}
  26. args = append(args, "--container", container, "--image", newImageID.String())
  27. println("TRACE:", "kubeyaml", strings.Join(args, " "))
  28. cmd := exec.Command("kubeyaml", args...)
  29. out := &bytes.Buffer{}
  30. errOut := &bytes.Buffer{}
  31. cmd.Stdin = bytes.NewBuffer(file)
  32. cmd.Stdout = out
  33. cmd.Stderr = errOut
  34. if err := cmd.Run(); err != nil {
  35. return nil, errors.Wrap(err, errOut.String())
  36. }
  37. return out.Bytes(), nil
  38. }