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.

policies.go 1.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. package kubernetes
  2. import (
  3. "fmt"
  4. kresource "github.com/fluxcd/flux/cluster/kubernetes/resource"
  5. "github.com/fluxcd/flux/resource"
  6. )
  7. func (m *manifests) UpdateWorkloadPolicies(def []byte, id resource.ID, update resource.PolicyUpdate) ([]byte, error) {
  8. resources, err := m.ParseManifest(def, "stdin")
  9. if err != nil {
  10. return nil, err
  11. }
  12. res, ok := resources[id.String()]
  13. if !ok {
  14. return nil, fmt.Errorf("resource %s not found", id.String())
  15. }
  16. // This is the Kubernetes manifests implementation; panic if it's
  17. // not returning `KubeManifest`s.
  18. kres := res.(kresource.KubeManifest)
  19. workload, ok := res.(resource.Workload)
  20. if !ok {
  21. return nil, fmt.Errorf("resource %s does not have containers", id.String())
  22. }
  23. if err != nil {
  24. return nil, err
  25. }
  26. changes, err := resource.ChangesForPolicyUpdate(workload, update)
  27. if err != nil {
  28. return nil, err
  29. }
  30. var args []string
  31. for k, v := range changes {
  32. annotation, ok := kres.PolicyAnnotationKey(k)
  33. if !ok {
  34. annotation = fmt.Sprintf("%s%s", kresource.PolicyPrefix, k)
  35. }
  36. args = append(args, fmt.Sprintf("%s=%s", annotation, v))
  37. }
  38. ns, kind, name := id.Components()
  39. return (KubeYAML{}).Annotate(def, ns, kind, name, args...)
  40. }