Jelajahi Sumber

secrets: enable secret inspect and rm by secret name

Signed-off-by: Evan Hazlett <ejhazlett@gmail.com>
Evan Hazlett 8 tahun lalu
induk
melakukan
e0e65b9a3b
3 mengubah file dengan 61 tambahan dan 3 penghapusan
  1. 16 2
      cli/command/secret/inspect.go
  2. 24 1
      cli/command/secret/remove.go
  3. 21 0
      cli/command/secret/utils.go

+ 16 - 2
cli/command/secret/inspect.go

@@ -34,9 +34,23 @@ func runSecretInspect(dockerCli *command.DockerCli, opts inspectOptions) error {
 	client := dockerCli.Client()
 	ctx := context.Background()
 
+	// attempt to lookup secret by name
+	secrets, err := getSecrets(client, ctx, []string{opts.name})
+	if err != nil {
+		return err
+	}
+
+	id := opts.name
+	for _, s := range secrets {
+		if s.Spec.Annotations.Name == opts.name {
+			id = s.ID
+			break
+		}
+	}
+
 	getRef := func(name string) (interface{}, []byte, error) {
-		return client.SecretInspectWithRaw(ctx, name)
+		return client.SecretInspectWithRaw(ctx, id)
 	}
 
-	return inspect.Inspect(dockerCli.Out(), []string{opts.name}, opts.format, getRef)
+	return inspect.Inspect(dockerCli.Out(), []string{id}, opts.format, getRef)
 }

+ 24 - 1
cli/command/secret/remove.go

@@ -31,7 +31,30 @@ func runSecretRemove(dockerCli *command.DockerCli, opts removeOptions) error {
 	client := dockerCli.Client()
 	ctx := context.Background()
 
-	for _, id := range opts.ids {
+	// attempt to lookup secret by name
+	secrets, err := getSecrets(client, ctx, opts.ids)
+	if err != nil {
+		return err
+	}
+
+	ids := opts.ids
+
+	names := make(map[string]int)
+	for _, id := range ids {
+		names[id] = 1
+	}
+
+	if len(secrets) > 0 {
+		ids = []string{}
+
+		for _, s := range secrets {
+			if _, ok := names[s.Spec.Annotations.Name]; ok {
+				ids = append(ids, s.ID)
+			}
+		}
+	}
+
+	for _, id := range ids {
 		if err := client.SecretRemove(ctx, id); err != nil {
 			return err
 		}

+ 21 - 0
cli/command/secret/utils.go

@@ -0,0 +1,21 @@
+package secret
+
+import (
+	"context"
+
+	"github.com/docker/docker/api/types"
+	"github.com/docker/docker/api/types/filters"
+	"github.com/docker/docker/api/types/swarm"
+	"github.com/docker/docker/client"
+)
+
+func getSecrets(client client.APIClient, ctx context.Context, names []string) ([]swarm.Secret, error) {
+	args := filters.NewArgs()
+	for _, n := range names {
+		args.Add("names", n)
+	}
+
+	return client.SecretList(ctx, types.SecretListOptions{
+		Filter: args,
+	})
+}