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.

cluster.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. package cluster
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/fluxcd/flux/policy"
  6. "github.com/fluxcd/flux/resource"
  7. "github.com/fluxcd/flux/ssh"
  8. )
  9. // Constants for workload ready status. These are defined here so that
  10. // no-one has to drag in Kubernetes dependencies to be able to use
  11. // them.
  12. const (
  13. StatusUnknown = "unknown"
  14. StatusError = "error"
  15. StatusReady = "ready"
  16. StatusUpdating = "updating"
  17. StatusStarted = "started"
  18. )
  19. // The things we can get from the running cluster. These used to form
  20. // the remote.Platform interface; but now we do more in the daemon so they
  21. // are distinct interfaces.
  22. type Cluster interface {
  23. // Get all of the services (optionally, from a specific namespace), excluding those
  24. AllWorkloads(ctx context.Context, maybeNamespace string) ([]Workload, error)
  25. SomeWorkloads(ctx context.Context, ids []resource.ID) ([]Workload, error)
  26. IsAllowedResource(resource.ID) bool
  27. Ping() error
  28. Export(ctx context.Context) ([]byte, error)
  29. Sync(SyncSet) error
  30. PublicSSHKey(regenerate bool) (ssh.PublicKey, error)
  31. }
  32. // RolloutStatus describes numbers of pods in different states and
  33. // the messages about unexpected rollout progress
  34. // a rollout status might be:
  35. // - in progress: Updated, Ready or Available numbers are not equal to Desired, or Outdated not equal to 0
  36. // - stuck: Messages contains info if deployment unavailable or exceeded its progress deadline
  37. // - complete: Updated, Ready and Available numbers are equal to Desired and Outdated equal to 0
  38. // See https://kubernetes.io/docs/concepts/workloads/controllers/deployment/#deployment-status
  39. type RolloutStatus struct {
  40. // Desired number of pods as defined in spec.
  41. Desired int32
  42. // Updated number of pods that are on the desired pod spec.
  43. Updated int32
  44. // Ready number of pods targeted by this deployment.
  45. Ready int32
  46. // Available number of available pods (ready for at least minReadySeconds) targeted by this deployment.
  47. Available int32
  48. // Outdated number of pods that are on a different pod spec.
  49. Outdated int32
  50. // Messages about unexpected rollout progress
  51. // if there's a message here, the rollout will not make progress without intervention
  52. Messages []string
  53. }
  54. // Workload describes a cluster resource that declares versioned images.
  55. type Workload struct {
  56. ID resource.ID
  57. Status string // A status summary for display
  58. // Is the controller considered read-only because it's under the
  59. // control of the platform. In the case of Kubernetes, we simply
  60. // omit these controllers; but this may not always be the case.
  61. IsSystem bool
  62. // If this workload was created _because_ of another, antecedent
  63. // resource through some mechanism (like an operator, or custom
  64. // resource controller), we try to record the ID of that resource
  65. // in this field.
  66. Antecedent resource.ID
  67. Labels map[string]string
  68. Policies policy.Set
  69. Rollout RolloutStatus
  70. // Errors during the recurring sync from the Git repository to the
  71. // cluster will surface here.
  72. SyncError error
  73. Containers ContainersOrExcuse
  74. }
  75. // Sometimes we care if we can't find the containers for a service,
  76. // sometimes we just want the information we can get.
  77. type ContainersOrExcuse struct {
  78. Excuse string
  79. Containers []resource.Container
  80. }
  81. func (s Workload) ContainersOrNil() []resource.Container {
  82. return s.Containers.Containers
  83. }
  84. func (s Workload) ContainersOrError() ([]resource.Container, error) {
  85. var err error
  86. if s.Containers.Excuse != "" {
  87. err = errors.New(s.Containers.Excuse)
  88. }
  89. return s.Containers.Containers, err
  90. }