Skip to content

Commit ff810c1

Browse files
authored
crane: add serve subcommand (#1586)
1 parent 57f010d commit ff810c1

File tree

9 files changed

+163
-799
lines changed

9 files changed

+163
-799
lines changed

.golangci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ issues:
1414
linters:
1515
enable:
1616
- asciicheck
17-
- deadcode
1817
- depguard
1918
- errorlint
2019
- gofmt
@@ -28,6 +27,7 @@ linters:
2827
- tparallel
2928
- unconvert
3029
- unparam
30+
- unused
3131
- whitespace
3232

3333
disable:

cmd/crane/cmd/root.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ func New(use, short string, options []crane.Option) *cobra.Command {
9494
},
9595
}
9696

97-
commands := []*cobra.Command{
97+
root.AddCommand(
9898
NewCmdAppend(&options),
9999
NewCmdAuth(options, "crane", "auth"),
100100
NewCmdBlob(&options),
@@ -117,9 +117,8 @@ func New(use, short string, options []crane.Option) *cobra.Command {
117117
NewCmdTag(&options),
118118
NewCmdValidate(&options),
119119
NewCmdVersion(),
120-
}
121-
122-
root.AddCommand(commands...)
120+
newCmdRegistry(),
121+
)
123122

124123
root.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "Enable debug logs")
125124
root.PersistentFlags().BoolVar(&insecure, "insecure", false, "Allow image references to be fetched without TLS")

cmd/crane/cmd/serve.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
// Copyright 2023 Google LLC All Rights Reserved.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package cmd
16+
17+
import (
18+
"errors"
19+
"fmt"
20+
"log"
21+
"net"
22+
"net/http"
23+
"os"
24+
"time"
25+
26+
"github.com/spf13/cobra"
27+
28+
"github.com/google/go-containerregistry/pkg/registry"
29+
)
30+
31+
func newCmdRegistry() *cobra.Command {
32+
cmd := &cobra.Command{
33+
Use: "registry",
34+
}
35+
cmd.AddCommand(newCmdServe())
36+
return cmd
37+
}
38+
39+
func newCmdServe() *cobra.Command {
40+
return &cobra.Command{
41+
Use: "serve",
42+
Short: "Serve an in-memory registry implementation",
43+
Long: `This sub-command serves an in-memory registry implementation on port :8080 (or $PORT)
44+
45+
The command blocks while the server accepts pushes and pulls.
46+
47+
Contents are only stored in memory, and when the process exits, pushed data is lost.`,
48+
Args: cobra.NoArgs,
49+
RunE: func(cmd *cobra.Command, _ []string) error {
50+
ctx := cmd.Context()
51+
52+
port := os.Getenv("PORT")
53+
if port == "" {
54+
port = "0"
55+
}
56+
listener, err := net.Listen("tcp", "localhost:"+port)
57+
if err != nil {
58+
log.Fatalln(err)
59+
}
60+
porti := listener.Addr().(*net.TCPAddr).Port
61+
port = fmt.Sprintf("%d", porti)
62+
63+
s := &http.Server{
64+
ReadHeaderTimeout: 5 * time.Second, // prevent slowloris, quiet linter
65+
Handler: registry.New(),
66+
}
67+
log.Printf("serving on port %s", port)
68+
69+
errCh := make(chan error)
70+
go func() { errCh <- s.Serve(listener) }()
71+
72+
<-ctx.Done()
73+
log.Println("shutting down...")
74+
if err := s.Shutdown(ctx); err != nil {
75+
return err
76+
}
77+
78+
if err := <-errCh; !errors.Is(err, http.ErrServerClosed) {
79+
return err
80+
}
81+
return nil
82+
},
83+
}
84+
}

cmd/crane/doc/crane.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/crane/doc/crane_registry.md

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

cmd/crane/doc/crane_registry_serve.md

Lines changed: 35 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)