@@ -2,9 +2,11 @@ package secret
22
33import (
44 "context"
5+ "fmt"
56 "strings"
67
78 "github.com/docker/docker/api/types"
9+ "github.com/docker/docker/api/types/swarm"
810 "github.com/docker/docker/client"
911 "github.com/gdamore/tcell/v2"
1012 "github.com/jr-k/d4s/internal/dao/common"
@@ -22,11 +24,12 @@ func NewManager(cli *client.Client, ctx context.Context) *Manager {
2224
2325// Secret Model
2426type Secret struct {
25- ID string
26- Name string
27- Created string
28- Updated string
29- Labels string
27+ ID string
28+ Name string
29+ Services int
30+ Created string
31+ Updated string
32+ Labels string
3033}
3134
3235func (s Secret ) GetID () string { return s .ID }
@@ -35,7 +38,8 @@ func (s Secret) GetCells() []string {
3538 if len (id ) > 12 {
3639 id = id [:12 ]
3740 }
38- return []string {id , s .Name , s .Created , s .Updated , s .Labels }
41+ servicesStr := fmt .Sprintf ("%d" , s .Services )
42+ return []string {id , s .Name , servicesStr , s .Created , s .Updated , s .Labels }
3943}
4044
4145func (s Secret ) GetStatusColor () (tcell.Color , tcell.Color ) {
@@ -48,6 +52,8 @@ func (s Secret) GetColumnValue(column string) string {
4852 return s .ID
4953 case "name" :
5054 return s .Name
55+ case "services" :
56+ return fmt .Sprintf ("%d" , s .Services )
5157 case "created" :
5258 return s .Created
5359 case "updated" :
@@ -72,16 +78,30 @@ func (m *Manager) List() ([]common.Resource, error) {
7278 return nil , err
7379 }
7480
81+ // Count services per secret
82+ counts := make (map [string ]int )
83+ services , err := m .cli .ServiceList (m .ctx , types.ServiceListOptions {})
84+ if err == nil {
85+ for _ , svc := range services {
86+ if svc .Spec .TaskTemplate .ContainerSpec != nil {
87+ for _ , secretRef := range svc .Spec .TaskTemplate .ContainerSpec .Secrets {
88+ counts [secretRef .SecretID ]++
89+ }
90+ }
91+ }
92+ }
93+
7594 var res []common.Resource
7695 for _ , s := range list {
7796 labels := formatLabels (s .Spec .Labels )
7897
7998 res = append (res , Secret {
80- ID : s .ID ,
81- Name : s .Spec .Name ,
82- Created : common .FormatTime (s .CreatedAt .Unix ()),
83- Updated : common .FormatTime (s .UpdatedAt .Unix ()),
84- Labels : labels ,
99+ ID : s .ID ,
100+ Name : s .Spec .Name ,
101+ Services : counts [s .ID ],
102+ Created : common .FormatTime (s .CreatedAt .Unix ()),
103+ Updated : common .FormatTime (s .UpdatedAt .Unix ()),
104+ Labels : labels ,
85105 })
86106 }
87107 return res , nil
@@ -91,6 +111,16 @@ func (m *Manager) Remove(id string) error {
91111 return m .cli .SecretRemove (m .ctx , id )
92112}
93113
114+ func (m * Manager ) Create (name string , data []byte ) error {
115+ _ , err := m .cli .SecretCreate (m .ctx , swarm.SecretSpec {
116+ Annotations : swarm.Annotations {
117+ Name : name ,
118+ },
119+ Data : data ,
120+ })
121+ return err
122+ }
123+
94124func formatLabels (labels map [string ]string ) string {
95125 if len (labels ) == 0 {
96126 return "-"
0 commit comments