assignments.go 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. package dispatcher
  2. import (
  3. "fmt"
  4. "github.com/docker/swarmkit/api"
  5. "github.com/docker/swarmkit/api/equality"
  6. "github.com/docker/swarmkit/api/validation"
  7. "github.com/docker/swarmkit/manager/drivers"
  8. "github.com/docker/swarmkit/manager/state/store"
  9. "github.com/sirupsen/logrus"
  10. )
  11. type typeAndID struct {
  12. id string
  13. objType api.ResourceType
  14. }
  15. type assignmentSet struct {
  16. dp *drivers.DriverProvider
  17. tasksMap map[string]*api.Task
  18. tasksUsingDependency map[typeAndID]map[string]struct{}
  19. changes map[typeAndID]*api.AssignmentChange
  20. log *logrus.Entry
  21. }
  22. func newAssignmentSet(log *logrus.Entry, dp *drivers.DriverProvider) *assignmentSet {
  23. return &assignmentSet{
  24. dp: dp,
  25. changes: make(map[typeAndID]*api.AssignmentChange),
  26. tasksMap: make(map[string]*api.Task),
  27. tasksUsingDependency: make(map[typeAndID]map[string]struct{}),
  28. log: log,
  29. }
  30. }
  31. func assignSecret(a *assignmentSet, readTx store.ReadTx, mapKey typeAndID, t *api.Task) {
  32. a.tasksUsingDependency[mapKey] = make(map[string]struct{})
  33. secret, err := a.secret(readTx, t, mapKey.id)
  34. if err != nil {
  35. a.log.WithFields(logrus.Fields{
  36. "resource.type": "secret",
  37. "secret.id": mapKey.id,
  38. "error": err,
  39. }).Debug("failed to fetch secret")
  40. return
  41. }
  42. a.changes[mapKey] = &api.AssignmentChange{
  43. Assignment: &api.Assignment{
  44. Item: &api.Assignment_Secret{
  45. Secret: secret,
  46. },
  47. },
  48. Action: api.AssignmentChange_AssignmentActionUpdate,
  49. }
  50. }
  51. func assignConfig(a *assignmentSet, readTx store.ReadTx, mapKey typeAndID) {
  52. a.tasksUsingDependency[mapKey] = make(map[string]struct{})
  53. config := store.GetConfig(readTx, mapKey.id)
  54. if config == nil {
  55. a.log.WithFields(logrus.Fields{
  56. "resource.type": "config",
  57. "config.id": mapKey.id,
  58. }).Debug("config not found")
  59. return
  60. }
  61. a.changes[mapKey] = &api.AssignmentChange{
  62. Assignment: &api.Assignment{
  63. Item: &api.Assignment_Config{
  64. Config: config,
  65. },
  66. },
  67. Action: api.AssignmentChange_AssignmentActionUpdate,
  68. }
  69. }
  70. func (a *assignmentSet) addTaskDependencies(readTx store.ReadTx, t *api.Task) {
  71. for _, resourceRef := range t.Spec.ResourceReferences {
  72. mapKey := typeAndID{objType: resourceRef.ResourceType, id: resourceRef.ResourceID}
  73. if len(a.tasksUsingDependency[mapKey]) == 0 {
  74. switch resourceRef.ResourceType {
  75. case api.ResourceType_SECRET:
  76. assignSecret(a, readTx, mapKey, t)
  77. case api.ResourceType_CONFIG:
  78. assignConfig(a, readTx, mapKey)
  79. default:
  80. a.log.WithField(
  81. "resource.type", resourceRef.ResourceType,
  82. ).Debug("invalid resource type for a task dependency, skipping")
  83. continue
  84. }
  85. }
  86. a.tasksUsingDependency[mapKey][t.ID] = struct{}{}
  87. }
  88. var secrets []*api.SecretReference
  89. container := t.Spec.GetContainer()
  90. if container != nil {
  91. secrets = container.Secrets
  92. }
  93. for _, secretRef := range secrets {
  94. secretID := secretRef.SecretID
  95. mapKey := typeAndID{objType: api.ResourceType_SECRET, id: secretID}
  96. if len(a.tasksUsingDependency[mapKey]) == 0 {
  97. assignSecret(a, readTx, mapKey, t)
  98. }
  99. a.tasksUsingDependency[mapKey][t.ID] = struct{}{}
  100. }
  101. var configs []*api.ConfigReference
  102. if container != nil {
  103. configs = container.Configs
  104. }
  105. for _, configRef := range configs {
  106. configID := configRef.ConfigID
  107. mapKey := typeAndID{objType: api.ResourceType_CONFIG, id: configID}
  108. if len(a.tasksUsingDependency[mapKey]) == 0 {
  109. assignConfig(a, readTx, mapKey)
  110. }
  111. a.tasksUsingDependency[mapKey][t.ID] = struct{}{}
  112. }
  113. }
  114. func (a *assignmentSet) releaseDependency(mapKey typeAndID, assignment *api.Assignment, taskID string) bool {
  115. delete(a.tasksUsingDependency[mapKey], taskID)
  116. if len(a.tasksUsingDependency[mapKey]) != 0 {
  117. return false
  118. }
  119. // No tasks are using the dependency anymore
  120. delete(a.tasksUsingDependency, mapKey)
  121. a.changes[mapKey] = &api.AssignmentChange{
  122. Assignment: assignment,
  123. Action: api.AssignmentChange_AssignmentActionRemove,
  124. }
  125. return true
  126. }
  127. func (a *assignmentSet) releaseTaskDependencies(t *api.Task) bool {
  128. var modified bool
  129. for _, resourceRef := range t.Spec.ResourceReferences {
  130. var assignment *api.Assignment
  131. switch resourceRef.ResourceType {
  132. case api.ResourceType_SECRET:
  133. assignment = &api.Assignment{
  134. Item: &api.Assignment_Secret{
  135. Secret: &api.Secret{ID: resourceRef.ResourceID},
  136. },
  137. }
  138. case api.ResourceType_CONFIG:
  139. assignment = &api.Assignment{
  140. Item: &api.Assignment_Config{
  141. Config: &api.Config{ID: resourceRef.ResourceID},
  142. },
  143. }
  144. default:
  145. a.log.WithField(
  146. "resource.type", resourceRef.ResourceType,
  147. ).Debug("invalid resource type for a task dependency, skipping")
  148. continue
  149. }
  150. mapKey := typeAndID{objType: resourceRef.ResourceType, id: resourceRef.ResourceID}
  151. if a.releaseDependency(mapKey, assignment, t.ID) {
  152. modified = true
  153. }
  154. }
  155. container := t.Spec.GetContainer()
  156. var secrets []*api.SecretReference
  157. if container != nil {
  158. secrets = container.Secrets
  159. }
  160. for _, secretRef := range secrets {
  161. secretID := secretRef.SecretID
  162. mapKey := typeAndID{objType: api.ResourceType_SECRET, id: secretID}
  163. assignment := &api.Assignment{
  164. Item: &api.Assignment_Secret{
  165. Secret: &api.Secret{ID: secretID},
  166. },
  167. }
  168. if a.releaseDependency(mapKey, assignment, t.ID) {
  169. modified = true
  170. }
  171. }
  172. var configs []*api.ConfigReference
  173. if container != nil {
  174. configs = container.Configs
  175. }
  176. for _, configRef := range configs {
  177. configID := configRef.ConfigID
  178. mapKey := typeAndID{objType: api.ResourceType_CONFIG, id: configID}
  179. assignment := &api.Assignment{
  180. Item: &api.Assignment_Config{
  181. Config: &api.Config{ID: configID},
  182. },
  183. }
  184. if a.releaseDependency(mapKey, assignment, t.ID) {
  185. modified = true
  186. }
  187. }
  188. return modified
  189. }
  190. func (a *assignmentSet) addOrUpdateTask(readTx store.ReadTx, t *api.Task) bool {
  191. // We only care about tasks that are ASSIGNED or higher.
  192. if t.Status.State < api.TaskStateAssigned {
  193. return false
  194. }
  195. if oldTask, exists := a.tasksMap[t.ID]; exists {
  196. // States ASSIGNED and below are set by the orchestrator/scheduler,
  197. // not the agent, so tasks in these states need to be sent to the
  198. // agent even if nothing else has changed.
  199. if equality.TasksEqualStable(oldTask, t) && t.Status.State > api.TaskStateAssigned {
  200. // this update should not trigger a task change for the agent
  201. a.tasksMap[t.ID] = t
  202. // If this task got updated to a final state, let's release
  203. // the dependencies that are being used by the task
  204. if t.Status.State > api.TaskStateRunning {
  205. // If releasing the dependencies caused us to
  206. // remove something from the assignment set,
  207. // mark one modification.
  208. return a.releaseTaskDependencies(t)
  209. }
  210. return false
  211. }
  212. } else if t.Status.State <= api.TaskStateRunning {
  213. // If this task wasn't part of the assignment set before, and it's <= RUNNING
  214. // add the dependencies it references to the assignment.
  215. // Task states > RUNNING are worker reported only, are never created in
  216. // a > RUNNING state.
  217. a.addTaskDependencies(readTx, t)
  218. }
  219. a.tasksMap[t.ID] = t
  220. a.changes[typeAndID{objType: api.ResourceType_TASK, id: t.ID}] = &api.AssignmentChange{
  221. Assignment: &api.Assignment{
  222. Item: &api.Assignment_Task{
  223. Task: t,
  224. },
  225. },
  226. Action: api.AssignmentChange_AssignmentActionUpdate,
  227. }
  228. return true
  229. }
  230. func (a *assignmentSet) removeTask(t *api.Task) bool {
  231. if _, exists := a.tasksMap[t.ID]; !exists {
  232. return false
  233. }
  234. a.changes[typeAndID{objType: api.ResourceType_TASK, id: t.ID}] = &api.AssignmentChange{
  235. Assignment: &api.Assignment{
  236. Item: &api.Assignment_Task{
  237. Task: &api.Task{ID: t.ID},
  238. },
  239. },
  240. Action: api.AssignmentChange_AssignmentActionRemove,
  241. }
  242. delete(a.tasksMap, t.ID)
  243. // Release the dependencies being used by this task.
  244. // Ignoring the return here. We will always mark this as a
  245. // modification, since a task is being removed.
  246. a.releaseTaskDependencies(t)
  247. return true
  248. }
  249. func (a *assignmentSet) message() api.AssignmentsMessage {
  250. var message api.AssignmentsMessage
  251. for _, change := range a.changes {
  252. message.Changes = append(message.Changes, change)
  253. }
  254. // The the set of changes is reinitialized to prepare for formation
  255. // of the next message.
  256. a.changes = make(map[typeAndID]*api.AssignmentChange)
  257. return message
  258. }
  259. // secret populates the secret value from raft store. For external secrets, the value is populated
  260. // from the secret driver.
  261. func (a *assignmentSet) secret(readTx store.ReadTx, task *api.Task, secretID string) (*api.Secret, error) {
  262. secret := store.GetSecret(readTx, secretID)
  263. if secret == nil {
  264. return nil, fmt.Errorf("secret not found")
  265. }
  266. if secret.Spec.Driver == nil {
  267. return secret, nil
  268. }
  269. d, err := a.dp.NewSecretDriver(secret.Spec.Driver)
  270. if err != nil {
  271. return nil, err
  272. }
  273. value, err := d.Get(&secret.Spec, task)
  274. if err != nil {
  275. return nil, err
  276. }
  277. if err := validation.ValidateSecretPayload(value); err != nil {
  278. return nil, err
  279. }
  280. // Assign the secret
  281. secret.Spec.Data = value
  282. return secret, nil
  283. }