Skip to content

Commit

Permalink
registry: improve error handling on port status (#365)
Browse files Browse the repository at this point in the history
noticed while poking around on tilt-dev/tilt#6457

i'm not sure this is actually the underlying problem
but it's good to clean up anyway.

Signed-off-by: Nick Santos <[email protected]>
  • Loading branch information
nicks authored Oct 31, 2024
1 parent 438c182 commit 6d1c291
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/api/generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions pkg/api/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ type RegistryStatus struct {

// Image for the running container.
Image string `json:"image,omitempty" yaml:"image,omitempty"`

// Warnings that occurred when reporting the registry status.
Warnings []string `json:"warnings,omitempty" yaml:"warnings,omitempty"`
}

// RegistryList is a list of Registrys.
Expand Down
2 changes: 1 addition & 1 deletion pkg/cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ func (o *GetOptions) registriesAsTable(registries []api.Registry) runtime.Object
}

hostAddress := "none"
if registry.Status.HostPort != 0 {
if registry.Status.ListenAddress != "" && registry.Status.HostPort != 0 {
hostAddress = fmt.Sprintf("%s:%d", registry.Status.ListenAddress, registry.Status.HostPort)
}

Expand Down
13 changes: 9 additions & 4 deletions pkg/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,11 @@ func (c *Controller) List(ctx context.Context, options ListOptions) (*api.Regist
}
sort.Strings(networks)

listenAddress, hostPort, containerPort := c.ipAndPortsFrom(container.Ports)
var warnings []string
listenAddress, hostPort, containerPort, err := c.ipAndPortsFrom(container.Ports)
if err != nil {
warnings = append(warnings, fmt.Sprintf("Unexpected registry ports: %+v", container.Ports))
}

registry := &api.Registry{
TypeMeta: typeMeta,
Expand All @@ -167,6 +171,7 @@ func (c *Controller) List(ctx context.Context, options ListOptions) (*api.Regist
Labels: container.Labels,
Image: container.Image,
Env: env,
Warnings: warnings,
},
}

Expand All @@ -181,13 +186,13 @@ func (c *Controller) List(ctx context.Context, options ListOptions) (*api.Regist
}, nil
}

func (c *Controller) ipAndPortsFrom(ports []types.Port) (listenAddress string, hostPort int, containerPort int) {
func (c *Controller) ipAndPortsFrom(ports []types.Port) (listenAddress string, hostPort int, containerPort int, err error) {
for _, port := range ports {
if port.PrivatePort == 5000 {
return port.IP, int(port.PublicPort), int(port.PrivatePort)
return port.IP, int(port.PublicPort), int(port.PrivatePort), nil
}
}
return "unknown", 0, 0
return "", 0, 0, fmt.Errorf("could not find registry port")
}

// Compare the desired registry against the existing registry, and reconcile
Expand Down
61 changes: 61 additions & 0 deletions pkg/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,35 @@ func kindRegistryCustomImage() types.Container {
}
}

func registryBadPorts() types.Container {
return types.Container{
ID: "a815c0ec15f1f7430bd402e3fffe65026dd692a1a99861a52b3e30ad6e253a08",
Names: []string{"/kind-registry"},
Image: DefaultRegistryImageRef,
ImageID: "sha256:2d4f4b5309b1e41b4f83ae59b44df6d673ef44433c734b14c1c103ebca82c116",
Command: "/entrypoint.sh /etc/docker/registry/config.yml",
Created: 1603483645,
Labels: map[string]string{"dev.tilt.ctlptl.role": "registry"},
Ports: []types.Port{
types.Port{IP: "127.0.0.1", PrivatePort: 5001, PublicPort: 5002, Type: "tcp"},
},
SizeRw: 0,
SizeRootFs: 0,
State: "running",
Status: "Up 2 hours",
NetworkSettings: &types.SummaryNetworkSettings{
Networks: map[string]*network.EndpointSettings{
"bridge": &network.EndpointSettings{
IPAddress: "172.0.1.2",
},
"kind": &network.EndpointSettings{
IPAddress: "172.0.1.3",
},
},
},
}
}

func TestListRegistries(t *testing.T) {
f := newFixture(t)
defer f.TearDown()
Expand Down Expand Up @@ -182,6 +211,38 @@ func TestListRegistries(t *testing.T) {
}, list.Items[2])
}

func TestListRegistries_badPorts(t *testing.T) {
f := newFixture(t)
defer f.TearDown()

regWithoutLabels := kindRegistryLoopback()
regWithoutLabels.Labels = nil

f.docker.containers = []types.Container{registryBadPorts()}

list, err := f.c.List(context.Background(), ListOptions{})
require.NoError(t, err)

require.Len(t, list.Items, 1)
assert.Equal(t, api.Registry{
TypeMeta: typeMeta,
Name: "kind-registry",
Status: api.RegistryStatus{
CreationTimestamp: metav1.Time{Time: time.Unix(1603483645, 0)},
IPAddress: "172.0.1.2",
Networks: []string{"bridge", "kind"},
ContainerID: "a815c0ec15f1f7430bd402e3fffe65026dd692a1a99861a52b3e30ad6e253a08",
State: "running",
Labels: map[string]string{"dev.tilt.ctlptl.role": "registry"},
Image: DefaultRegistryImageRef,
Env: []string{"REGISTRY_STORAGE_DELETE_ENABLED=true", "PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"},
Warnings: []string{
"Unexpected registry ports: [{IP:127.0.0.1 PrivatePort:5001 PublicPort:5002 Type:tcp}]",
},
},
}, list.Items[0])
}

func TestGetRegistry(t *testing.T) {
f := newFixture(t)
defer f.TearDown()
Expand Down

0 comments on commit 6d1c291

Please sign in to comment.