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.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package kubernetes
  2. import (
  3. "fmt"
  4. "strings"
  5. kresource "github.com/fluxcd/flux/cluster/kubernetes/resource"
  6. "github.com/fluxcd/flux/image"
  7. "github.com/fluxcd/flux/resource"
  8. )
  9. // updateWorkloadContainer takes a YAML document stream (one or more
  10. // YAML docs, as bytes), a resource ID referring to a controller, a
  11. // container name, and the name of the new image that should be used
  12. // for the container. It returns a new YAML stream where the image for
  13. // the container has been replaced with the imageRef supplied.
  14. func updateWorkloadContainer(in []byte, resource resource.ID, container string, newImageID image.Ref) ([]byte, error) {
  15. namespace, kind, name := resource.Components()
  16. if _, ok := resourceKinds[strings.ToLower(kind)]; !ok {
  17. return nil, UpdateNotSupportedError(kind)
  18. }
  19. return (KubeYAML{}).Image(in, namespace, kind, name, container, newImageID.String())
  20. }
  21. // updateWorkloadImagePaths takes a YAML document stream (one or more
  22. // YAML docs, as bytes), a resource ID referring to a HelmRelease,
  23. // a ContainerImageMap, and the name of the new image that should be
  24. // applied to the mapped paths. It returns a new YAML stream where
  25. // the values of the paths have been replaced with the imageRef
  26. // supplied.
  27. func updateWorkloadImagePaths(in []byte,
  28. resource resource.ID, paths kresource.ContainerImageMap, newImageID image.Ref) ([]byte, error) {
  29. namespace, kind, name := resource.Components()
  30. // We only support HelmRelease resource kinds for now
  31. if kind != "helmrelease" {
  32. return nil, UpdateNotSupportedError(kind)
  33. }
  34. if m, ok := paths.MapImageRef(newImageID); ok {
  35. var args []string
  36. for k, v := range m {
  37. args = append(args, fmt.Sprintf("%s=%s", k, v))
  38. }
  39. return (KubeYAML{}).Set(in, namespace, kind, name, args...)
  40. }
  41. return nil, fmt.Errorf("failed to map paths %#v to %q for %q", paths, newImageID.String(), resource.String())
  42. }