Skip to content

Commit

Permalink
Implement support to disable registry filtering
Browse files Browse the repository at this point in the history
  • Loading branch information
phillebaba committed Feb 29, 2024
1 parent c9343bf commit 9ab3818
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 10 deletions.
13 changes: 9 additions & 4 deletions pkg/oci/containerd.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type Containerd struct {
registryConfigPath string
}

func NewContainerd(sock, namespace, registryConfigPath string, registries []url.URL) (*Containerd, error) {
listFilter, eventFilter := createFilters(registries)
func NewContainerd(sock, namespace, registryConfigPath string, filterRegistries []url.URL) (*Containerd, error) {
listFilter, eventFilter := createFilters(filterRegistries)
return &Containerd{
clientGetter: func() (*containerd.Client, error) {
return containerd.New(sock, containerd.WithDefaultNamespace(namespace))
Expand Down Expand Up @@ -417,12 +417,17 @@ func getEventImage(e typeurl.Any) (string, EventType, error) {
}
}

func createFilters(registries []url.URL) (string, string) {
func createFilters(filterRegistries []url.URL) (string, string) {
registryHosts := []string{}
for _, registry := range registries {
for _, registry := range filterRegistries {
registryHosts = append(registryHosts, strings.ReplaceAll(registry.Host, `.`, `\\.`))
}
listFilter := fmt.Sprintf(`name~="^(%s)/"`, strings.Join(registryHosts, "|"))
if len(registryHosts) == 0 {
// Filter images that do not have a registry in it's reference,
// as we cant mirror images without registries.
listFilter = `name~="^.+/"`
}
eventFilter := fmt.Sprintf(`topic~="/images/create|/images/update|/images/delete",event.%s`, listFilter)
return listFilter, eventFilter
}
Expand Down
11 changes: 5 additions & 6 deletions pkg/oci/containerd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,19 +80,18 @@ func TestCreateFilter(t *testing.T) {
registries []string
}{
{
name: "only registries",
name: "with registry filtering",
registries: []string{"https://docker.io", "https://gcr.io"},
expectedListFilter: `name~="^(docker\\.io|gcr\\.io)/"`,
expectedEventFilter: `topic~="/images/create|/images/update|/images/delete",event.name~="^(docker\\.io|gcr\\.io)/"`,
},
{
name: "additional image filtes",
registries: []string{"https://docker.io", "https://gcr.io"},
expectedListFilter: `name~="^(docker\\.io|gcr\\.io)/"`,
expectedEventFilter: `topic~="/images/create|/images/update|/images/delete",event.name~="^(docker\\.io|gcr\\.io)/"`,
name: "without registry filtering",
registries: []string{},
expectedListFilter: `name~="^.+/"`,
expectedEventFilter: `topic~="/images/create|/images/update|/images/delete",event.name~="^.+/"`,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
listFilter, eventFilter := createFilters(stringListToUrlList(t, tt.registries))
Expand Down

0 comments on commit 9ab3818

Please sign in to comment.