From 6d1c29115e4bd0a38235f550f9ed6efa005ed679 Mon Sep 17 00:00:00 2001 From: Nick Santos Date: Thu, 31 Oct 2024 17:53:25 -0400 Subject: [PATCH] registry: improve error handling on port status (#365) noticed while poking around on https://github.com/tilt-dev/tilt/issues/6457 i'm not sure this is actually the underlying problem but it's good to clean up anyway. Signed-off-by: Nick Santos --- pkg/api/generated.deepcopy.go | 5 +++ pkg/api/types.go | 3 ++ pkg/cmd/get.go | 2 +- pkg/registry/registry.go | 13 +++++--- pkg/registry/registry_test.go | 61 +++++++++++++++++++++++++++++++++++ 5 files changed, 79 insertions(+), 5 deletions(-) diff --git a/pkg/api/generated.deepcopy.go b/pkg/api/generated.deepcopy.go index e76bc95..77b8d0f 100644 --- a/pkg/api/generated.deepcopy.go +++ b/pkg/api/generated.deepcopy.go @@ -267,6 +267,11 @@ func (in *RegistryStatus) DeepCopyInto(out *RegistryStatus) { *out = make([]string, len(*in)) copy(*out, *in) } + if in.Warnings != nil { + in, out := &in.Warnings, &out.Warnings + *out = make([]string, len(*in)) + copy(*out, *in) + } return } diff --git a/pkg/api/types.go b/pkg/api/types.go index cf987ca..708e9f2 100644 --- a/pkg/api/types.go +++ b/pkg/api/types.go @@ -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. diff --git a/pkg/cmd/get.go b/pkg/cmd/get.go index cfd4082..8178b3c 100644 --- a/pkg/cmd/get.go +++ b/pkg/cmd/get.go @@ -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) } diff --git a/pkg/registry/registry.go b/pkg/registry/registry.go index 70a6221..8dbf749 100644 --- a/pkg/registry/registry.go +++ b/pkg/registry/registry.go @@ -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, @@ -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, }, } @@ -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 diff --git a/pkg/registry/registry_test.go b/pkg/registry/registry_test.go index 56084ed..3c722a9 100644 --- a/pkg/registry/registry_test.go +++ b/pkg/registry/registry_test.go @@ -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() @@ -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()