Skip to content

Defaulting to the latest tag if no tag was specified. #826

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

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
3 changes: 3 additions & 0 deletions api/v1beta1/module_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type KanikoParams struct {

type Build struct {
// +optional
// +nullable
// BuildArgs is an array of build variables that are provided to the image building backend.
BuildArgs []BuildArg `json:"buildArgs"`

Expand Down Expand Up @@ -125,6 +126,7 @@ type KernelMapping struct {
InTreeModuleToRemove string `json:"inTreeModuleToRemove"`

// +optional
// +nullable
// InTreeModulesToRemove specifies any number of in-tree kernel modules that should be removed (if present)
// before loading the kernel module from the ContainerImage
InTreeModulesToRemove []string `json:"inTreeModulesToRemove"`
Expand Down Expand Up @@ -315,6 +317,7 @@ type ModuleSpec struct {
// DevicePlugin allows overriding some properties of the container that deploys the device plugin on the node.
// Name is ignored and is set automatically by the KMM Operator.
// +optional
// +nullable
DevicePlugin *DevicePluginSpec `json:"devicePlugin"`

// ModuleLoader allows overriding some properties of the container that loads the kernel module on the node.
Expand Down
8 changes: 8 additions & 0 deletions cmd/webhook-server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,10 @@ func main() {
if err = webhook.NewModuleValidator(logger).SetupWebhookWithManager(mgr); err != nil {
cmd.FatalError(setupLogger, err, "unable to create webhook", "webhook", "ModuleValidator")
}

if err = webhook.NewModuleDefaulter(logger).SetupWebhookWithManager(mgr); err != nil {
cmd.FatalError(setupLogger, err, "unable to create mutating webhook", "webhook", "ModuleDefaulter")
}
}

if enableManagedClusterModule {
Expand All @@ -90,6 +94,10 @@ func main() {
if err = hub.NewManagedClusterModuleValidator(logger).SetupWebhookWithManager(mgr); err != nil {
cmd.FatalError(setupLogger, err, "unable to create webhook", "webhook", "ManagedClusterModuleValidator")
}

if err = hub.NewManagedClusterModuleDefaulter(logger).SetupWebhookWithManager(mgr); err != nil {
cmd.FatalError(setupLogger, err, "unable to create mutating webhook", "webhook", "ManagedClusterModuleDefaulter")
}
}

if enableNamespaceDeletion {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ spec:
description: |-
DevicePlugin allows overriding some properties of the container that deploys the device plugin on the node.
Name is ignored and is set automatically by the KMM Operator.
nullable: true
properties:
container:
properties:
Expand Down Expand Up @@ -2188,6 +2189,7 @@ spec:
- name
- value
type: object
nullable: true
type: array
dockerfileConfigMap:
description: ConfigMap that holds Dockerfile contents
Expand Down Expand Up @@ -2318,6 +2320,7 @@ spec:
- name
- value
type: object
nullable: true
type: array
dockerfileConfigMap:
description: ConfigMap that holds Dockerfile
Expand Down Expand Up @@ -2394,6 +2397,7 @@ spec:
before loading the kernel module from the ContainerImage
items:
type: string
nullable: true
type: array
literal:
description: Literal defines a literal target kernel
Expand Down
4 changes: 4 additions & 0 deletions config/crd/bases/kmm.sigs.x-k8s.io_modules.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ spec:
description: |-
DevicePlugin allows overriding some properties of the container that deploys the device plugin on the node.
Name is ignored and is set automatically by the KMM Operator.
nullable: true
properties:
container:
properties:
Expand Down Expand Up @@ -2171,6 +2172,7 @@ spec:
- name
- value
type: object
nullable: true
type: array
dockerfileConfigMap:
description: ConfigMap that holds Dockerfile contents
Expand Down Expand Up @@ -2299,6 +2301,7 @@ spec:
- name
- value
type: object
nullable: true
type: array
dockerfileConfigMap:
description: ConfigMap that holds Dockerfile contents
Expand Down Expand Up @@ -2374,6 +2377,7 @@ spec:
before loading the kernel module from the ContainerImage
items:
type: string
nullable: true
type: array
literal:
description: Literal defines a literal target kernel
Expand Down
15 changes: 15 additions & 0 deletions config/default-hub/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,13 @@ replacements:
options:
create: true
delimiter: /
- select:
kind: MutatingWebhookConfiguration
fieldPaths:
- metadata.annotations.[cert-manager.io/inject-ca-from]
options:
create: true
delimiter: /
- source:
kind: Certificate
fieldPath: metadata.name
Expand All @@ -58,6 +65,14 @@ replacements:
create: true
delimiter: /
index: 1
- select:
kind: MutatingWebhookConfiguration
fieldPaths:
- metadata.annotations.[cert-manager.io/inject-ca-from]
options:
create: true
delimiter: /
index: 1
# Replacements below adjust the DNS names of the webhook certificate based on the service name.
- source:
kind: Service
Expand Down
15 changes: 15 additions & 0 deletions config/default/kustomization.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ replacements:
options:
create: true
delimiter: /
- select:
kind: MutatingWebhookConfiguration
fieldPaths:
- metadata.annotations.[cert-manager.io/inject-ca-from]
options:
create: true
delimiter: /
- source:
kind: Certificate
fieldPath: metadata.name
Expand All @@ -60,6 +67,14 @@ replacements:
create: true
delimiter: /
index: 1
- select:
kind: MutatingWebhookConfiguration
fieldPaths:
- metadata.annotations.[cert-manager.io/inject-ca-from]
options:
create: true
delimiter: /
index: 1
# Replacements below adjust the DNS names of the webhook certificate based on the service name.
- source:
kind: Service
Expand Down
26 changes: 26 additions & 0 deletions config/webhook-hub/manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-hub-kmm-sigs-x-k8s-io-v1beta1-managedclustermodule
failurePolicy: Fail
name: vmanageclustermodule.kb.io
rules:
- apiGroups:
- hub.kmm.sigs.x-k8s.io
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- managedclustermodules
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
Expand Down
26 changes: 26 additions & 0 deletions config/webhook/manifests.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
---
apiVersion: admissionregistration.k8s.io/v1
kind: MutatingWebhookConfiguration
metadata:
name: mutating-webhook-configuration
webhooks:
- admissionReviewVersions:
- v1
clientConfig:
service:
name: webhook-service
namespace: system
path: /mutate-kmm-sigs-x-k8s-io-v1beta1-module
failurePolicy: Fail
name: vmodule.kb.io
rules:
- apiGroups:
- kmm.sigs.x-k8s.io
apiVersions:
- v1beta1
operations:
- CREATE
- UPDATE
resources:
- modules
sideEffects: None
---
apiVersion: admissionregistration.k8s.io/v1
kind: ValidatingWebhookConfiguration
metadata:
name: validating-webhook-configuration
Expand Down
4 changes: 0 additions & 4 deletions internal/registry/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ func (r *registry) getPullOptions(ctx context.Context, image string, tlsOptions
repo = tag[0]
}

if repo == "" {
return nil, fmt.Errorf("image url %s is not valid, does not contain hash or tag", image)
}

options := []crane.Option{
crane.WithContext(ctx),
}
Expand Down
23 changes: 0 additions & 23 deletions internal/registry/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,6 @@ var _ = Describe("ImageExists", func() {
Expect(err.Error()).To(ContainSubstring("failed to get pull options for image"))
})

It("should fail if the image name isn't valid", func() {

_, err = reg.ImageExists(ctx, invalidImage, &kmmv1beta1.TLSOptions{}, nil)

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not contain hash or tag"))
})

It("should fail if it cannot get key chain from secret", func() {

mockRegistryAuthGetter.EXPECT().GetKeyChain(ctx).Return(nil, errors.New("some error"))
Expand Down Expand Up @@ -222,14 +214,6 @@ var _ = Describe("GetLayersDigests", func() {
Expect(err.Error()).To(ContainSubstring("failed to get pull options for image"))
})

It("should fail if the image name isn't valid", func() {

_, err = reg.ImageExists(ctx, invalidImage, &kmmv1beta1.TLSOptions{}, nil)

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not contain hash or tag"))
})

It("should fail if it cannot get key chain from secret", func() {

mockRegistryAuthGetter.EXPECT().GetKeyChain(ctx).Return(nil, errors.New("some error"))
Expand Down Expand Up @@ -393,13 +377,6 @@ var _ = Describe("GetDigest", func() {
Expect(err.Error()).To(ContainSubstring("failed to get pull options for image"))
})

It("should fail if the image name isn't valid", func() {
_, err = reg.GetDigest(ctx, invalidImage, &kmmv1beta1.TLSOptions{}, nil)

Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("does not contain hash or tag"))
})

It("should fail if it cannot get key chain from secret", func() {
mockRegistryAuthGetter.EXPECT().GetKeyChain(ctx).Return(nil, errors.New("some error"))

Expand Down
68 changes: 68 additions & 0 deletions internal/webhook/hub/managedclustermodule_mutator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
Copyright 2022.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package hub

import (
"context"
"fmt"

"github.com/go-logr/logr"
"github.com/kubernetes-sigs/kernel-module-management/api-hub/v1beta1"
"github.com/kubernetes-sigs/kernel-module-management/internal/webhook"
"k8s.io/apimachinery/pkg/runtime"
ctrl "sigs.k8s.io/controller-runtime"
)

// FIXME: remove the code that returned an error until now
type ManagedClusterModuleDefaulter struct {
logger logr.Logger
moduleDefaulter *webhook.ModuleDefaulter
}

func NewManagedClusterModuleDefaulter(logger logr.Logger) *ManagedClusterModuleDefaulter {
return &ManagedClusterModuleDefaulter{
logger: logger,
moduleDefaulter: webhook.NewModuleDefaulter(logger),
}
}

func (mcmd *ManagedClusterModuleDefaulter) SetupWebhookWithManager(mgr ctrl.Manager) error {
// controller-runtime will set the path to `mutate-<group>-<version>-<resource> so we
// need to make sure it is set correctly in the +kubebuilder annotation below.
return ctrl.NewWebhookManagedBy(mgr).
For(&v1beta1.ManagedClusterModule{}).
WithDefaulter(mcmd).
Complete()
}

//+kubebuilder:webhook:path=/mutate-hub-kmm-sigs-x-k8s-io-v1beta1-managedclustermodule,mutating=true,failurePolicy=fail,sideEffects=None,groups=hub.kmm.sigs.x-k8s.io,resources=managedclustermodules,verbs=create;update,versions=v1beta1,name=vmanageclustermodule.kb.io,admissionReviewVersions=v1

// Default implements webhook.Default so a webhook will be registered for the type
func (mcmd *ManagedClusterModuleDefaulter) Default(ctx context.Context, obj runtime.Object) error {

mcm, ok := obj.(*v1beta1.ManagedClusterModule)
if !ok {
return fmt.Errorf("bad type for the object; expected %T, got %T", mcm, obj)
}

mcmd.logger.Info("Mutating ManagedClusterModule creation", "name", mcm.Name, "namespace", mcm.Namespace)

webhook.SetDefaultContainerImageTagIfNeeded(&mcm.Spec.ModuleSpec)
webhook.SetDefaultKernelMappingTagsIfNeeded(&mcm.Spec.ModuleSpec)

return nil
}
Loading
Loading