Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update registry client v2 #31

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cmd/secrets-init-webhook/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"github.com/pkg/errors"
"net/http"
"os"
"runtime"
Expand All @@ -11,7 +12,7 @@ import (
"github.com/Masterminds/semver/v3"

"github.com/doitintl/kube-secrets-init/cmd/secrets-init-webhook/registry"
"github.com/pkg/errors"
_ "github.com/pkg/errors"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't really make sense if it's imported again above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

shit, I only wrote this so that the test would pass, since when I imported from your code this gave me an error

"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
log "github.com/sirupsen/logrus"
Expand Down Expand Up @@ -263,7 +264,7 @@ func (mw *mutatingWebhook) mutateContainers(containers []corev1.Container, podSp
// the container has no explicitly specified command
if len(args) == 0 {
c := container
imageConfig, err := mw.registry.GetImageConfig(mw.k8sClient, ns, &c, podSpec)
imageConfig, err := mw.registry.GetImageConfig(context.Background(), mw.k8sClient, ns, &c, podSpec)
if err != nil {
return false, err
}
Expand Down
19 changes: 10 additions & 9 deletions cmd/secrets-init-webhook/main_test.go
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
package main

import (
"context"
"fmt"
"reflect"
"testing"

"github.com/doitintl/kube-secrets-init/cmd/secrets-init-webhook/registry"
imagev1 "github.com/opencontainers/image-spec/specs-go/v1"
v1 "github.com/google/go-containerregistry/pkg/v1"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
fake "k8s.io/client-go/kubernetes/fake"
)

type MockRegistry struct {
Image imagev1.ImageConfig
Image v1.Config
}

//nolint:lll
func (r *MockRegistry) GetImageConfig(_ kubernetes.Interface, _ string, _ *corev1.Container, _ *corev1.PodSpec) (*imagev1.ImageConfig, error) {
func (r *MockRegistry) GetImageConfig(_ context.Context, _ kubernetes.Interface, _ string, _ *corev1.Container, _ *corev1.PodSpec) (*v1.Config, error) {
return &r.Image, nil
}

Expand Down Expand Up @@ -51,7 +52,7 @@ func Test_mutatingWebhook_mutateContainers(t *testing.T) {
fields: fields{
k8sClient: fake.NewSimpleClientset(),
registry: &MockRegistry{
Image: imagev1.ImageConfig{},
Image: v1.Config{},
},
provider: "aws",
image: secretsInitImage,
Expand Down Expand Up @@ -101,7 +102,7 @@ func Test_mutatingWebhook_mutateContainers(t *testing.T) {
}),
),
registry: &MockRegistry{
Image: imagev1.ImageConfig{},
Image: v1.Config{},
},
provider: "aws",
image: secretsInitImage,
Expand Down Expand Up @@ -153,7 +154,7 @@ func Test_mutatingWebhook_mutateContainers(t *testing.T) {
fields: fields{
k8sClient: fake.NewSimpleClientset(),
registry: &MockRegistry{
Image: imagev1.ImageConfig{},
Image: v1.Config{},
},
provider: "google",
image: secretsInitImage,
Expand Down Expand Up @@ -199,7 +200,7 @@ func Test_mutatingWebhook_mutateContainers(t *testing.T) {
fields: fields{
k8sClient: fake.NewSimpleClientset(),
registry: &MockRegistry{
Image: imagev1.ImageConfig{
Image: v1.Config{
Entrypoint: []string{"/bin/zsh"},
},
},
Expand Down Expand Up @@ -246,7 +247,7 @@ func Test_mutatingWebhook_mutateContainers(t *testing.T) {
fields: fields{
k8sClient: fake.NewSimpleClientset(),
registry: &MockRegistry{
Image: imagev1.ImageConfig{
Image: v1.Config{
Cmd: []string{"test-cmd"},
},
},
Expand Down Expand Up @@ -292,7 +293,7 @@ func Test_mutatingWebhook_mutateContainers(t *testing.T) {
fields: fields{
k8sClient: fake.NewSimpleClientset(),
registry: &MockRegistry{
Image: imagev1.ImageConfig{},
Image: v1.Config{},
},
image: secretsInitImage,
volumeName: binVolumeName,
Expand Down
14 changes: 7 additions & 7 deletions cmd/secrets-init-webhook/registry/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,28 @@ package registry
import (
"sync"

imagev1 "github.com/opencontainers/image-spec/specs-go/v1"
v1 "github.com/google/go-containerregistry/pkg/v1"
)

// ImageCache interface
type ImageCache interface {
Get(image string) *imagev1.ImageConfig
Put(image string, imageConfig *imagev1.ImageConfig)
Get(image string) *v1.Config
Put(image string, imageConfig *v1.Config)
}

// InMemoryImageCache Concrete mutex-guarded cache
type InMemoryImageCache struct {
mutex sync.Mutex
cache map[string]imagev1.ImageConfig
cache map[string]v1.Config
}

// NewInMemoryImageCache return new mutex guarded cache
func NewInMemoryImageCache() ImageCache {
return &InMemoryImageCache{cache: map[string]imagev1.ImageConfig{}}
return &InMemoryImageCache{cache: map[string]v1.Config{}}
}

// Get image from cache
func (c *InMemoryImageCache) Get(image string) *imagev1.ImageConfig {
func (c *InMemoryImageCache) Get(image string) *v1.Config {
c.mutex.Lock()
defer c.mutex.Unlock()
if imageConfig, ok := c.cache[image]; ok {
Expand All @@ -34,7 +34,7 @@ func (c *InMemoryImageCache) Get(image string) *imagev1.ImageConfig {
}

// Put image into cache
func (c *InMemoryImageCache) Put(image string, imageConfig *imagev1.ImageConfig) {
func (c *InMemoryImageCache) Put(image string, imageConfig *v1.Config) {
c.mutex.Lock()
defer c.mutex.Unlock()
c.cache[image] = *imageConfig
Expand Down
Loading