Skip to content

Commit

Permalink
fix: webhooks and add missing conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
rafalgalaw committed Oct 7, 2024
1 parent 805004b commit 69cc4f7
Show file tree
Hide file tree
Showing 14 changed files with 186 additions and 36 deletions.
16 changes: 16 additions & 0 deletions PROJECT
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ resources:
kind: Enterprise
path: github.com/mercedes-benz/garm-operator/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand All @@ -26,6 +29,11 @@ resources:
kind: Pool
path: github.com/mercedes-benz/garm-operator/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
defaulting: true
validation: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand All @@ -35,6 +43,9 @@ resources:
kind: Organization
path: github.com/mercedes-benz/garm-operator/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand All @@ -44,6 +55,7 @@ resources:
path: github.com/mercedes-benz/garm-operator/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
defaulting: true
validation: true
webhookVersion: v1
Expand All @@ -57,6 +69,7 @@ resources:
path: github.com/mercedes-benz/garm-operator/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
defaulting: true
validation: true
webhookVersion: v1
Expand All @@ -69,6 +82,9 @@ resources:
kind: Runner
path: github.com/mercedes-benz/garm-operator/api/v1alpha1
version: v1alpha1
webhooks:
conversion: true
webhookVersion: v1
- api:
crdVersion: v1
namespaced: true
Expand Down
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ allow_k8s_contexts('kind-garm-operator')
# as the plugin has already well written readiness checks we can use it to wait for
deploy_cert_manager(
kind_cluster_name='garm-operator', # just for security reasons ;-)
version='v1.12.0' # the version of cert-manager to deploy
version='v1.15.3' # the version of cert-manager to deploy
)

# mode could be either 'local' or 'debug'
Expand Down
11 changes: 11 additions & 0 deletions api/v1alpha1/enterprise_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ import (
"github.com/mercedes-benz/garm-operator/api/v1beta1"
v1 "k8s.io/api/core/v1"
apiconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var _ conversion.Convertible = &Enterprise{}

func (e *Enterprise) ConvertTo(dstRaw conversion.Hub) error {
return Convert_v1alpha1_Enterprise_To_v1beta1_Enterprise(e, dstRaw.(*v1beta1.Enterprise), nil)
}

func (e *Enterprise) ConvertFrom(dstRaw conversion.Hub) error {
return Convert_v1beta1_Enterprise_To_v1alpha1_Enterprise(dstRaw.(*v1beta1.Enterprise), e, nil)
}

func Convert_v1alpha1_EnterpriseSpec_To_v1beta1_EnterpriseSpec(in *EnterpriseSpec, out *v1beta1.EnterpriseSpec, s apiconversion.Scope) error {
out.CredentialsRef = v1.TypedLocalObjectReference{
Name: in.CredentialsName,
Expand Down
3 changes: 1 addition & 2 deletions api/v1alpha1/enterprise_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@
package v1alpha1

import (
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/mercedes-benz/garm-operator/pkg/conditions"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

// EnterpriseSpec defines the desired state of Enterprise
Expand Down
17 changes: 17 additions & 0 deletions api/v1alpha1/enterprise_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT

package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var enterpriselog = logf.Log.WithName("enterprise-resource")

func (r *Enterprise) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
16 changes: 16 additions & 0 deletions api/v1alpha1/image_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package v1alpha1

import (
"github.com/mercedes-benz/garm-operator/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var _ conversion.Convertible = &Image{}

func (i *Image) ConvertTo(dstRaw conversion.Hub) error {
return Convert_v1alpha1_Image_To_v1beta1_Image(i, dstRaw.(*v1beta1.Image), nil)
}

func (i *Image) ConvertFrom(dstRaw conversion.Hub) error {
return Convert_v1beta1_Image_To_v1alpha1_Image(dstRaw.(*v1beta1.Image), i, nil)
}
9 changes: 9 additions & 0 deletions api/v1alpha1/organization_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,17 @@ import (
v1beta1 "github.com/mercedes-benz/garm-operator/api/v1beta1"
v1 "k8s.io/api/core/v1"
apiconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

func (o *Organization) ConvertTo(dstRaw conversion.Hub) error {
return Convert_v1alpha1_Organization_To_v1beta1_Organization(o, dstRaw.(*v1beta1.Organization), nil)
}

func (o *Organization) ConvertFrom(dstRaw conversion.Hub) error {
return Convert_v1beta1_Organization_To_v1alpha1_Organization(dstRaw.(*v1beta1.Organization), o, nil)
}

func Convert_v1alpha1_OrganizationSpec_To_v1beta1_OrganizationSpec(in *OrganizationSpec, out *v1beta1.OrganizationSpec, s apiconversion.Scope) error {
out.CredentialsRef = v1.TypedLocalObjectReference{
Name: in.CredentialsName,
Expand Down
17 changes: 17 additions & 0 deletions api/v1alpha1/organization_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT

package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var organizationlog = logf.Log.WithName("organization-resource")

func (r *Organization) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
16 changes: 16 additions & 0 deletions api/v1alpha1/pool_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package v1alpha1

import (
"github.com/mercedes-benz/garm-operator/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var _ conversion.Convertible = &Pool{}

func (p *Pool) ConvertTo(dstRaw conversion.Hub) error {
return Convert_v1alpha1_Pool_To_v1beta1_Pool(p, dstRaw.(*v1beta1.Pool), nil)
}

func (p *Pool) ConvertFrom(dstRaw conversion.Hub) error {
return Convert_v1beta1_Pool_To_v1alpha1_Pool(dstRaw.(*v1beta1.Pool), p, nil)
}
19 changes: 0 additions & 19 deletions api/v1alpha1/pool_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package v1alpha1

import (
"context"
"encoding/json"
"fmt"
"reflect"
Expand Down Expand Up @@ -39,7 +38,6 @@ var _ webhook.Validator = &Pool{}
// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (p *Pool) ValidateCreate() (admission.Warnings, error) {
poollog.Info("validate create", "name", p.Name)
ctx := context.TODO()

if err := p.validateExtraSpec(); err != nil {
return nil, apierrors.NewInvalid(schema.GroupKind{Group: GroupVersion.Group, Kind: "Pool"},
Expand All @@ -48,23 +46,6 @@ func (p *Pool) ValidateCreate() (admission.Warnings, error) {
)
}

poolImage, err := p.GetImageCR(ctx, c)
if err != nil {
poollog.Error(err, "cannot fetch Image", "error", err)
return nil, nil
}

duplicate, duplicateName, err := p.CheckDuplicate(ctx, c, poolImage)
if err != nil {
poollog.Error(err, "error checking for duplicate", "error", err)
return nil, nil
}

if duplicate {
err := fmt.Sprintf("pool with same image, flavor, provider and github scope already exists: %s", duplicateName)
return nil, apierrors.NewBadRequest(err)
}

return nil, nil
}

Expand Down
11 changes: 11 additions & 0 deletions api/v1alpha1/repository_conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,19 @@ import (
"github.com/mercedes-benz/garm-operator/api/v1beta1"
v1 "k8s.io/api/core/v1"
apiconversion "k8s.io/apimachinery/pkg/conversion"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var _ conversion.Convertible = &Repository{}

func (r *Repository) ConvertTo(dstRaw conversion.Hub) error {
return Convert_v1alpha1_Repository_To_v1beta1_Repository(r, dstRaw.(*v1beta1.Repository), nil)
}

func (r *Repository) ConvertFrom(dstRaw conversion.Hub) error {
return Convert_v1beta1_Repository_To_v1alpha1_Repository(dstRaw.(*v1beta1.Repository), r, nil)
}

func Convert_v1alpha1_RepositorySpec_To_v1beta1_RepositorySpec(in *RepositorySpec, out *v1beta1.RepositorySpec, s apiconversion.Scope) error {
out.CredentialsRef = v1.TypedLocalObjectReference{
Name: in.CredentialsName,
Expand Down
16 changes: 16 additions & 0 deletions api/v1alpha1/runner_conversion.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package v1alpha1

import (
"github.com/mercedes-benz/garm-operator/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/conversion"
)

var _ conversion.Convertible = &Runner{}

func (r *Runner) ConvertTo(dstRaw conversion.Hub) error {
return Convert_v1alpha1_Runner_To_v1beta1_Runner(r, dstRaw.(*v1beta1.Runner), nil)
}

func (r *Runner) ConvertFrom(dstRaw conversion.Hub) error {
return Convert_v1beta1_Runner_To_v1alpha1_Runner(dstRaw.(*v1beta1.Runner), r, nil)
}
17 changes: 17 additions & 0 deletions api/v1alpha1/runner_webhook.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// SPDX-License-Identifier: MIT

package v1alpha1

import (
ctrl "sigs.k8s.io/controller-runtime"
logf "sigs.k8s.io/controller-runtime/pkg/log"
)

// log is for logging in this package.
var runnerlog = logf.Log.WithName("runner-resource")

func (r *Runner) SetupWebhookWithManager(mgr ctrl.Manager) error {
return ctrl.NewWebhookManagedBy(mgr).
For(r).
Complete()
}
52 changes: 38 additions & 14 deletions cmd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,13 @@ package main
import (
"context"
"fmt"
"log"

"gopkg.in/yaml.v2"
"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
_ "k8s.io/client-go/plugin/pkg/client/auth"
"k8s.io/klog/v2/textlogger"
"log"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/cache"
"sigs.k8s.io/controller-runtime/pkg/controller"
Expand Down Expand Up @@ -153,18 +152,6 @@ func run() error {
return fmt.Errorf("unable to create controller Pool: %w", err)
}

if err = (&garmoperatorv1alpha1.Pool{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Pool: %w", err)
}

if err = (&garmoperatorv1alpha1.Image{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Image: %w", err)
}

if err = (&garmoperatorv1alpha1.Repository{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Repository: %w", err)
}

if err = (&garmcontroller.OrganizationReconciler{
Client: mgr.GetClient(),
Scheme: mgr.GetScheme(),
Expand Down Expand Up @@ -234,6 +221,43 @@ func run() error {
}).SetupWithManager(mgr); err != nil {
return fmt.Errorf("unable to create controller GitHubCredentials: %w", err)
}

// webhooks
if err = (&garmoperatorv1alpha1.Enterprise{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Enterprise: %w", err)
}

if err = (&garmoperatorv1alpha1.Organization{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Organization: %w", err)
}

if err = (&garmoperatorv1alpha1.Pool{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Pool: %w", err)
}

if err = (&garmoperatorv1beta1.Pool{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Pool: %w", err)
}

if err = (&garmoperatorv1alpha1.Image{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Image: %w", err)
}

if err = (&garmoperatorv1beta1.Image{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Image: %w", err)
}

if err = (&garmoperatorv1alpha1.Repository{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Repository: %w", err)
}

if err = (&garmoperatorv1beta1.Repository{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Repository: %w", err)
}

if err = (&garmoperatorv1alpha1.Runner{}).SetupWebhookWithManager(mgr); err != nil {
return fmt.Errorf("unable to create webhook Runner: %w", err)
}
//+kubebuilder:scaffold:builder

if err := mgr.AddHealthzCheck("healthz", healthz.Ping); err != nil {
Expand Down

0 comments on commit 69cc4f7

Please sign in to comment.