Skip to content

Commit

Permalink
feat(controller): select which arch to cache in helm values
Browse files Browse the repository at this point in the history
  • Loading branch information
plaffitt committed Oct 24, 2023
1 parent 47d2590 commit fd2697c
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 14 deletions.
26 changes: 20 additions & 6 deletions cmd/cache/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,25 @@ func (re *regexpArrayFlags) Set(value string) error {
return nil
}

type arrayFlags []string

func (a *arrayFlags) String() string {
return strings.Join(*a, ",")
}

func (a *arrayFlags) Set(value string) error {
*a = append(*a, value)
return nil
}

func main() {
var metricsAddr string
var enableLeaderElection bool
var probeAddr string
var expiryDelay uint
var proxyPort int
var ignoreImages regexpArrayFlags
var architectures arrayFlags
var maxConcurrentCachedImageReconciles int
flag.StringVar(&metricsAddr, "metrics-bind-address", ":8080", "The address the metric endpoint binds to.")
flag.StringVar(&probeAddr, "health-probe-bind-address", ":8081", "The address the probe endpoint binds to.")
Expand All @@ -64,7 +76,8 @@ func main() {
"Enabling this will ensure there is only one active controller manager.")
flag.UintVar(&expiryDelay, "expiry-delay", 30, "The delay in days before deleting an unused CachedImage.")
flag.IntVar(&proxyPort, "proxy-port", 8082, "The port where the proxy is listening on this machine.")
flag.Var(&ignoreImages, "ignore-images", "List of regexes that represents images to be excluded.")
flag.Var(&ignoreImages, "ignore-images", "Regex that represents images to be excluded (this flag can be used multiple times).")
flag.Var(&architectures, "arch", "Architecture of image to put in cache (this flag can be used multiple times).")
flag.StringVar(&registry.Endpoint, "registry-endpoint", "kube-image-keeper-registry:5000", "The address of the registry where cached images are stored.")
flag.IntVar(&maxConcurrentCachedImageReconciles, "max-concurrent-cached-image-reconciles", 3, "Maximum number of CachedImages that can be handled and reconciled at the same time (put or removed from cache).")

Expand Down Expand Up @@ -92,11 +105,12 @@ func main() {
}

if err = (&controllers.CachedImageReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("cachedimage-controller"),
ApiReader: mgr.GetAPIReader(),
ExpiryDelay: time.Duration(expiryDelay*24) * time.Hour,
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Recorder: mgr.GetEventRecorderFor("cachedimage-controller"),
ApiReader: mgr.GetAPIReader(),
ExpiryDelay: time.Duration(expiryDelay*24) * time.Hour,
Architectures: []string(architectures),
}).SetupWithManager(mgr, maxConcurrentCachedImageReconciles); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "CachedImage")
os.Exit(1)
Expand Down
11 changes: 6 additions & 5 deletions controllers/cachedimage_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ const cachedImageFinalizerName = "cachedimage.kuik.enix.io/finalizer"
// CachedImageReconciler reconciles a CachedImage object
type CachedImageReconciler struct {
client.Client
Scheme *runtime.Scheme
Recorder record.EventRecorder
ApiReader client.Reader
ExpiryDelay time.Duration
Scheme *runtime.Scheme
Recorder record.EventRecorder
ApiReader client.Reader
ExpiryDelay time.Duration
Architectures []string
}

//+kubebuilder:rbac:groups=kuik.enix.io,resources=cachedimages,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -157,7 +158,7 @@ func (r *CachedImageReconciler) Reconcile(ctx context.Context, req ctrl.Request)
if !isCached {
r.Recorder.Eventf(&cachedImage, "Normal", "Caching", "Start caching image %s", cachedImage.Spec.SourceImage)
keychain := registry.NewKubernetesKeychain(r.ApiReader, cachedImage.Spec.PullSecretsNamespace, cachedImage.Spec.PullSecretNames)
if err := registry.CacheImage(cachedImage.Spec.SourceImage, keychain); err != nil {
if err := registry.CacheImage(cachedImage.Spec.SourceImage, keychain, r.Architectures); err != nil {
log.Error(err, "failed to cache image")
r.Recorder.Eventf(&cachedImage, "Warning", "CacheFailed", "Failed to cache image %s, reason: %s", cachedImage.Spec.SourceImage, err)
return ctrl.Result{}, err
Expand Down
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ require (
github.com/bytedance/sonic v1.9.1 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/chenzhuoyu/base64x v0.0.0-20221115062448-fe3a3abad311 // indirect
github.com/containerd/stargz-snapshotter/estargz v0.12.1 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/docker/docker-credential-helpers v0.7.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
Expand All @@ -74,6 +75,7 @@ require (
github.com/imdario/mergo v0.3.12 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/json-iterator/go v1.1.12 // indirect
github.com/klauspost/compress v1.15.11 // indirect
github.com/klauspost/cpuid/v2 v2.2.4 // indirect
github.com/leodido/go-urn v1.2.4 // indirect
github.com/mattn/go-isatty v0.0.19 // indirect
Expand All @@ -94,6 +96,7 @@ require (
github.com/spf13/pflag v1.0.5 // indirect
github.com/twitchyliquid64/golang-asm v0.15.1 // indirect
github.com/ugorji/go/codec v1.2.11 // indirect
github.com/vbatts/tar-split v0.11.2 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/arch v0.3.0 // indirect
golang.org/x/crypto v0.11.0 // indirect
Expand Down
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -143,13 +143,15 @@ github.com/cockroachdb/datadriven v0.0.0-20200714090401-bf6692d28da5/go.mod h1:h
github.com/cockroachdb/errors v1.2.4/go.mod h1:rQD95gz6FARkaKkQXUksEje/d9a6wBJoCr5oaCLELYA=
github.com/cockroachdb/logtags v0.0.0-20190617123548-eb05cc24525f/go.mod h1:i/u985jwjWRlyHXQbwatDASoW0RMlZ/3i9yJHE2xLkI=
github.com/containerd/stargz-snapshotter/estargz v0.12.1 h1:+7nYmHJb0tEkcRaAW+MHqoKaJYZmkikupxCqVtmPuY0=
github.com/containerd/stargz-snapshotter/estargz v0.12.1/go.mod h1:12VUuCq3qPq4y8yUW+l5w3+oXV3cx2Po3KSe/SmPGqw=
github.com/coreos/bbolt v1.3.2/go.mod h1:iRUV2dpdMOn7Bo10OQBFzIJO9kkE559Wcmn+qkEiiKk=
github.com/coreos/etcd v3.3.13+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-oidc v2.1.0+incompatible/go.mod h1:CgnwVTmzoESiwO9qyAFEMiHoZ1nMCKZlZ9V6mm3/LKc=
github.com/coreos/go-semver v0.3.0/go.mod h1:nnelYz7RCh+5ahJtPPxZlU+153eP4D4r3EedlOD2RNk=
github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7vjVVG0kc13fIWeqUViNPyEJxv/OmvnBo0Yme4=
github.com/coreos/go-systemd/v22 v22.3.2/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSVTIJ3seZv2GcEnc=
github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA=
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
Expand Down Expand Up @@ -388,6 +390,7 @@ github.com/kisielk/errcheck v1.2.0/go.mod h1:/BMXB+zMLi60iA8Vv6Ksmxu/1UDYcXs4uQL
github.com/kisielk/errcheck v1.5.0/go.mod h1:pFxgyoBC7bSaBwPgfKdkLd5X25qrDl4LWUI2bnpBCr8=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/klauspost/compress v1.15.11 h1:Lcadnb3RKGin4FYM/orgq0qde+nc15E5Cbqg4B9Sx9c=
github.com/klauspost/compress v1.15.11/go.mod h1:QPwzmACJjUTFsnSHH934V6woptycfrDDJnH7hvFVbGM=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/cpuid/v2 v2.2.4 h1:acbojRNwl3o09bUq+yDCtZFc1aiwaAAxtcn8YkZXnvk=
github.com/klauspost/cpuid/v2 v2.2.4/go.mod h1:RVVoqg1df56z8g3pUjL/3lE5UfnlrJX8tyFgg4nqhuY=
Expand Down Expand Up @@ -570,7 +573,9 @@ github.com/twitchyliquid64/golang-asm v0.15.1 h1:SU5vSMR7hnwNxj24w34ZyCi/FmDZTkS
github.com/twitchyliquid64/golang-asm v0.15.1/go.mod h1:a1lVb/DtPvCB8fslRZhAngC2+aY1QWCk3Cedj/Gdt08=
github.com/ugorji/go/codec v1.2.11 h1:BMaWp1Bb6fHwEtbplGBGJ498wD+LKlNSl25MjdZY4dU=
github.com/ugorji/go/codec v1.2.11/go.mod h1:UNopzCgEMSXjBc6AOMqYvWC1ktqTAfzJZUZgYf6w6lg=
github.com/urfave/cli v1.22.4/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/vbatts/tar-split v0.11.2 h1:Via6XqJr0hceW4wff3QRzD5gAk/tatMw/4ZA7cTlIME=
github.com/vbatts/tar-split v0.11.2/go.mod h1:vV3ZuO2yWSVsz+pfFzDG/upWH1JhjOiEaWq6kXyQ3VI=
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
3 changes: 3 additions & 0 deletions helm/kube-image-keeper/templates/controller-deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ spec:
{{- range .Values.controllers.webhook.ignoredImages }}
- -ignore-images={{- . }}
{{- end }}
{{- range .Values.architectures }}
- -arch={{- . }}
{{- end }}
ports:
- containerPort: 9443
name: webhook-server
Expand Down
2 changes: 2 additions & 0 deletions helm/kube-image-keeper/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
cachedImagesExpiryDelay: 30
# -- If true, install the CRD
installCRD: true
# -- List of architectures to put in cache
architectures: [amd64]

controllers:
# Maximum number of CachedImages that can be handled and reconciled at the same time (put or remove from cache)
Expand Down
17 changes: 15 additions & 2 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (

"github.com/google/go-containerregistry/pkg/authn"
"github.com/google/go-containerregistry/pkg/name"
v1 "github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/remote/transport"
"github.com/google/go-containerregistry/pkg/v1/types"
Expand Down Expand Up @@ -95,7 +97,7 @@ func DeleteImage(imageName string) error {
return remote.Delete(digest)
}

func CacheImage(imageName string, keychain authn.Keychain) error {
func CacheImage(imageName string, keychain authn.Keychain, architectures []string) error {
destRef, err := parseLocalReference(imageName)
if err != nil {
return err
Expand All @@ -121,7 +123,17 @@ func CacheImage(imageName string, keychain authn.Keychain) error {
if err != nil {
return err
}
if err := remote.WriteIndex(destRef, index); err != nil {

filteredIndex := mutate.RemoveManifests(index, func(desc v1.Descriptor) bool {
for _, arch := range architectures {
if arch == desc.Platform.Architecture {
return false
}
}
return true
})

if err := remote.WriteIndex(destRef, filteredIndex); err != nil {
return err
}
default:
Expand All @@ -133,6 +145,7 @@ func CacheImage(imageName string, keychain authn.Keychain) error {
return err
}
}

return nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ func Test_CacheImage(t *testing.T) {

Endpoint = cacheRegistry.Addr()
keychain := NewKubernetesKeychain(nil, "default", []string{})
err := CacheImage(originRegistry.Addr()+"/"+tt.image, keychain)
err := CacheImage(originRegistry.Addr()+"/"+tt.image, keychain, []string{"amd64"})
if tt.wantErr != "" {
g.Expect(err).To(BeAssignableToTypeOf(tt.errType))
g.Expect(err).To(MatchError(ContainSubstring(tt.wantErr)))
Expand Down

0 comments on commit fd2697c

Please sign in to comment.