Skip to content

Commit 9f374d4

Browse files
committed
cagent pull --force
Signed-off-by: David Gageot <david.gageot@docker.com>
1 parent 33018a6 commit 9f374d4

6 files changed

Lines changed: 28 additions & 16 deletions

File tree

cmd/root/api.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ func (f *apiFlags) runAPICommand(cmd *cobra.Command, args []string) error {
126126
return
127127
case <-ticker.C:
128128
slog.Info("Auto-pulling OCI reference", "reference", agentsPath)
129-
if _, err := remote.Pull(ctx, agentsPath); err != nil {
129+
if _, err := remote.Pull(ctx, agentsPath, false); err != nil {
130130
slog.Error("Failed to auto-pull OCI reference", "reference", agentsPath, "error", err)
131131
continue
132132
}

cmd/root/pull.go

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,28 @@ import (
1515
"github.com/docker/cagent/pkg/telemetry"
1616
)
1717

18+
type pullFlags struct {
19+
force bool
20+
}
21+
1822
func newPullCmd() *cobra.Command {
19-
return &cobra.Command{
23+
var flags pullFlags
24+
25+
cmd := &cobra.Command{
2026
Use: "pull <registry-ref>",
2127
Short: "Pull an agent from an OCI registry",
2228
Long: "Pull an agent configuration file from an OCI registry",
2329
GroupID: "core",
2430
Args: cobra.ExactArgs(1),
25-
RunE: runPullCommand,
31+
RunE: flags.runPullCommand,
2632
}
33+
34+
cmd.PersistentFlags().BoolVar(&flags.force, "force", false, "Force pull even if the configuration already exists locally")
35+
36+
return cmd
2737
}
2838

29-
func runPullCommand(cmd *cobra.Command, args []string) error {
39+
func (f *pullFlags) runPullCommand(cmd *cobra.Command, args []string) error {
3040
telemetry.TrackCommand("pull", args)
3141

3242
ctx := cmd.Context()
@@ -37,7 +47,7 @@ func runPullCommand(cmd *cobra.Command, args []string) error {
3747
out.Println("Pulling agent", registryRef)
3848

3949
var opts []crane.Option
40-
_, err := remote.Pull(ctx, registryRef, opts...)
50+
_, err := remote.Pull(ctx, registryRef, f.force, opts...)
4151
if err != nil {
4252
return fmt.Errorf("failed to pull artifact: %w", err)
4353
}

pkg/agentfile/resolver.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ func Resolve(ctx context.Context, out *cli.Printer, agentFilename string) (strin
105105
a, err := FromStore(agentFilename)
106106
if err != nil {
107107
out.Println("Pulling agent", agentFilename)
108-
if _, pullErr := remote.Pull(ctx, agentFilename); pullErr != nil {
108+
if _, pullErr := remote.Pull(ctx, agentFilename, false); pullErr != nil {
109109
return "", fmt.Errorf("failed to pull OCI image %s: %w", agentFilename, pullErr)
110110
}
111111
// Retry after pull

pkg/remote/pull.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import (
1111
)
1212

1313
// Pull pulls an artifact from a registry and stores it in the content store
14-
func Pull(ctx context.Context, registryRef string, opts ...crane.Option) (string, error) {
14+
func Pull(ctx context.Context, registryRef string, force bool, opts ...crane.Option) (string, error) {
1515
opts = append(opts, crane.WithContext(ctx))
1616

1717
ref, err := name.ParseReference(registryRef)
@@ -30,12 +30,14 @@ func Pull(ctx context.Context, registryRef string, opts ...crane.Option) (string
3030
}
3131

3232
localRef := ref.Context().RepositoryStr() + ":" + ref.Identifier()
33-
if meta, metaErr := store.GetArtifactMetadata(localRef); metaErr == nil {
34-
if meta.Digest == remoteDigest {
35-
if !hasCagentAnnotation(meta.Annotations) {
36-
return "", fmt.Errorf("artifact %s found in store wasn't created by cagent", localRef)
33+
if !force {
34+
if meta, metaErr := store.GetArtifactMetadata(localRef); metaErr == nil {
35+
if meta.Digest == remoteDigest {
36+
if !hasCagentAnnotation(meta.Annotations) {
37+
return "", fmt.Errorf("artifact %s found in store wasn't created by cagent", localRef)
38+
}
39+
return meta.Digest, nil
3740
}
38-
return meta.Digest, nil
3941
}
4042
}
4143

@@ -49,7 +51,7 @@ func Pull(ctx context.Context, registryRef string, opts ...crane.Option) (string
4951
return "", fmt.Errorf("getting manifest from pulled image: %w", err)
5052
}
5153
if !hasCagentAnnotation(manifest.Annotations) {
52-
return "", fmt.Errorf("artifact %s found in store wasn't created by cagent", localRef)
54+
return "", fmt.Errorf("artifact %s wasn't created by cagent", localRef)
5355
}
5456

5557
digest, err := store.StoreArtifact(img, localRef)

pkg/remote/pull_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@ import (
1515
)
1616

1717
func TestPullNonExistentRegistry(t *testing.T) {
18-
_, err := Pull(t.Context(), "registry.example.com/non-existent:latest")
18+
_, err := Pull(t.Context(), "registry.example.com/non-existent:latest", false)
1919
require.Error(t, err)
2020
}
2121

2222
func TestPullWithOptions(t *testing.T) {
23-
_, err := Pull(t.Context(), "registry.example.com/test:latest", crane.Insecure)
23+
_, err := Pull(t.Context(), "registry.example.com/test:latest", false, crane.Insecure)
2424
require.Error(t, err)
2525
}
2626

pkg/server/server.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,7 @@ func (s *Server) pullAgent(c echo.Context) error {
695695
}
696696

697697
slog.Info("Pulling agent", "name", req.Name)
698-
_, err := remote.Pull(c.Request().Context(), req.Name)
698+
_, err := remote.Pull(c.Request().Context(), req.Name, false)
699699
if err != nil {
700700
slog.Error("Failed to pull agent", "name", req.Name, "error", err)
701701
return echo.NewHTTPError(http.StatusInternalServerError, "failed to pull agent")

0 commit comments

Comments
 (0)