config.go 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. package convert // import "github.com/docker/docker/daemon/cluster/convert"
  2. import (
  3. swarmtypes "github.com/docker/docker/api/types/swarm"
  4. types "github.com/docker/docker/api/types/swarm"
  5. gogotypes "github.com/gogo/protobuf/types"
  6. swarmapi "github.com/moby/swarmkit/v2/api"
  7. )
  8. // ConfigFromGRPC converts a grpc Config to a Config.
  9. func ConfigFromGRPC(s *swarmapi.Config) swarmtypes.Config {
  10. config := swarmtypes.Config{
  11. ID: s.ID,
  12. Spec: swarmtypes.ConfigSpec{
  13. Annotations: annotationsFromGRPC(s.Spec.Annotations),
  14. Data: s.Spec.Data,
  15. },
  16. }
  17. config.Version.Index = s.Meta.Version.Index
  18. // Meta
  19. config.CreatedAt, _ = gogotypes.TimestampFromProto(s.Meta.CreatedAt)
  20. config.UpdatedAt, _ = gogotypes.TimestampFromProto(s.Meta.UpdatedAt)
  21. if s.Spec.Templating != nil {
  22. config.Spec.Templating = &types.Driver{
  23. Name: s.Spec.Templating.Name,
  24. Options: s.Spec.Templating.Options,
  25. }
  26. }
  27. return config
  28. }
  29. // ConfigSpecToGRPC converts Config to a grpc Config.
  30. func ConfigSpecToGRPC(s swarmtypes.ConfigSpec) swarmapi.ConfigSpec {
  31. spec := swarmapi.ConfigSpec{
  32. Annotations: swarmapi.Annotations{
  33. Name: s.Name,
  34. Labels: s.Labels,
  35. },
  36. Data: s.Data,
  37. }
  38. if s.Templating != nil {
  39. spec.Templating = &swarmapi.Driver{
  40. Name: s.Templating.Name,
  41. Options: s.Templating.Options,
  42. }
  43. }
  44. return spec
  45. }
  46. // ConfigReferencesFromGRPC converts a slice of grpc ConfigReference to ConfigReference
  47. func ConfigReferencesFromGRPC(s []*swarmapi.ConfigReference) []*swarmtypes.ConfigReference {
  48. refs := []*swarmtypes.ConfigReference{}
  49. for _, r := range s {
  50. ref := &swarmtypes.ConfigReference{
  51. ConfigID: r.ConfigID,
  52. ConfigName: r.ConfigName,
  53. }
  54. if t, ok := r.Target.(*swarmapi.ConfigReference_File); ok {
  55. ref.File = &swarmtypes.ConfigReferenceFileTarget{
  56. Name: t.File.Name,
  57. UID: t.File.UID,
  58. GID: t.File.GID,
  59. Mode: t.File.Mode,
  60. }
  61. }
  62. refs = append(refs, ref)
  63. }
  64. return refs
  65. }