Skip to content

Commit

Permalink
Merge pull request #27 from rmb938/depupdates
Browse files Browse the repository at this point in the history
dep updates + all code changes needed for them
  • Loading branch information
rmb938 committed Nov 10, 2023
2 parents 1a57eb7 + 852d2e2 commit 1a02698
Show file tree
Hide file tree
Showing 19 changed files with 384 additions and 1,527 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pr_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.19.1'
go-version: "^1.21.4"

- name: Build Manager
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push_master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.19.1'
go-version: "^1.21.4"

- name: Build Manager
run: |
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/push_tag.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-go@v2
with:
go-version: '^1.19.1'
go-version: "^1.21.4"

- name: Build Manager
run: |
Expand Down
80 changes: 70 additions & 10 deletions api/v1alpha1/hostport_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ limitations under the License.
package v1alpha1

import (
"fmt"

"k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -42,7 +50,11 @@ var _ webhook.Defaulter = &HostPort{}
func (r *HostPort) Default() {
hostportlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
if r.DeletionTimestamp.IsZero() {
controllerutil.AddFinalizer(r, HostPortFinalizer)
}

return
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
Expand All @@ -51,25 +63,73 @@ func (r *HostPort) Default() {
var _ webhook.Validator = &HostPort{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *HostPort) ValidateCreate() error {
func (r *HostPort) ValidateCreate() (admission.Warnings, error) {
hostportlog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
var allErrs field.ErrorList

if len(allErrs) == 0 {
return nil, nil
}

return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: r.Kind},
r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *HostPort) ValidateUpdate(old runtime.Object) error {
hostportlog.Info("validate update", "name", r.Name)
func (r *HostPort) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {

// TODO(user): fill in your validation logic upon object update.
return nil
hostportlog.Info("validate update", "name", r.Name)
oldHP := old.(*HostPort)

var allErrs field.ErrorList

// don't allow changing class
if r.Spec.HostPortClassName != oldHP.Spec.HostPortClassName {
allErrs = append(allErrs,
field.Forbidden(field.NewPath("spec").Child("hostPortClassName"),
"cannot change hostPortClassName"),
)
}

// don't allow changing claim
if equality.Semantic.DeepEqual(oldHP.Spec.ClaimRef, r.Spec.ClaimRef) == false {
allErrs = append(allErrs,
field.Forbidden(field.NewPath("spec").Child("claimRef"),
"cannot change claimRef"),
)
}

// don't allow changing port once set
if oldHP.Status.Port > 0 && r.Status.Port != oldHP.Status.Port {
allErrs = append(allErrs,
field.Forbidden(field.NewPath("status").Child("port"),
"cannot change port"),
)
}

// TODO: only allow setting port when also setting as allocated
if oldHP.Status.Port == 0 && r.Status.Port > 0 && r.Status.Phase != HostPortPhaseAllocated {
allErrs = append(allErrs,
field.Invalid(field.NewPath("status").Child("port"), r.Status.Port,
fmt.Sprintf("port can only be set when also setting the phase to %s", HostPortPhaseAllocated)),
)
}

if len(allErrs) == 0 {
return nil, nil
}

return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: r.Kind},
r.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *HostPort) ValidateDelete() error {
func (r *HostPort) ValidateDelete() (admission.Warnings, error) {
hostportlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
58 changes: 48 additions & 10 deletions api/v1alpha1/hostportclaim_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ limitations under the License.
package v1alpha1

import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -40,9 +45,12 @@ var _ webhook.Defaulter = &HostPortClaim{}

// Default implements webhook.Defaulter so a webhook will be registered for the type
func (r *HostPortClaim) Default() {

hostportclaimlog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
if r.DeletionTimestamp.IsZero() {
controllerutil.AddFinalizer(r, HostPortFinalizer)
}
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
Expand All @@ -51,25 +59,55 @@ func (r *HostPortClaim) Default() {
var _ webhook.Validator = &HostPortClaim{}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *HostPortClaim) ValidateCreate() error {
func (r *HostPortClaim) ValidateCreate() (admission.Warnings, error) {

hostportclaimlog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
var allErrs field.ErrorList

if len(allErrs) == 0 {
return nil, nil
}

return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: r.Kind},
r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *HostPortClaim) ValidateUpdate(old runtime.Object) error {
func (r *HostPortClaim) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
hostportclaimlog.Info("validate update", "name", r.Name)

// TODO(user): fill in your validation logic upon object update.
return nil
oldHPC := old.(*HostPortClaim)

var allErrs field.ErrorList

if r.Spec.HostPortClassName != oldHPC.Spec.HostPortClassName {
allErrs = append(allErrs,
field.Forbidden(field.NewPath("spec").Child("hostPortClassName"),
"cannot change hostPortClassName"),
)
}

if len(oldHPC.Spec.HostPortName) > 0 && oldHPC.Spec.HostPortName != r.Spec.HostPortName {
allErrs = append(allErrs,
field.Forbidden(field.NewPath("spec").Child("hostPortName"),
"cannot change hostPortName once set"),
)
}

if len(allErrs) == 0 {
return nil, nil
}

return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: r.Kind},
r.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *HostPortClaim) ValidateDelete() error {
func (r *HostPortClaim) ValidateDelete() (admission.Warnings, error) {
hostportclaimlog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
55 changes: 46 additions & 9 deletions api/v1alpha1/hostportclass_webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,15 @@ limitations under the License.
package v1alpha1

import (
apierrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/apimachinery/pkg/util/validation/field"
ctrl "sigs.k8s.io/controller-runtime"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/webhook"
"sigs.k8s.io/controller-runtime/pkg/webhook/admission"
)

// log is for logging in this package.
Expand All @@ -42,34 +47,66 @@ var _ webhook.Defaulter = &HostPortClass{}
func (r *HostPortClass) Default() {
hostportclasslog.Info("default", "name", r.Name)

// TODO(user): fill in your defaulting logic.
if r.DeletionTimestamp.IsZero() {
controllerutil.AddFinalizer(r, HostPortFinalizer)
}
}

// TODO(user): change verbs to "verbs=create;update;delete" if you want to enable deletion validation.
// +kubebuilder:webhook:verbs=create;update,path=/validate-hostport-rmb938-com-v1alpha1-hostportclass,mutating=false,failurePolicy=fail,groups=hostport.rmb938.com,resources=hostportclasses,versions=v1alpha1,sideEffects=None,admissionReviewVersions=v1,name=vhostportclass.kb.io

var _ webhook.Validator = &HostPortClass{}

func (r *HostPortClass) validatePools() field.ErrorList {
var allErrs field.ErrorList

for index, pool := range r.Spec.Pools {
if pool.Start > pool.End {
allErrs = append(allErrs, field.Invalid(field.NewPath("spec").Child("pools").Index(index).Child("end"), pool.End,
"End must be greater or equal to start"))
}
}

// TODO: make sure there are no overlapping pools

return allErrs
}

// ValidateCreate implements webhook.Validator so a webhook will be registered for the type
func (r *HostPortClass) ValidateCreate() error {
func (r *HostPortClass) ValidateCreate() (admission.Warnings, error) {
hostportclasslog.Info("validate create", "name", r.Name)

// TODO(user): fill in your validation logic upon object creation.
return nil
allErrs := r.validatePools()

if len(allErrs) == 0 {
return nil, nil
}

return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: r.Kind},
r.Name, allErrs)
}

// ValidateUpdate implements webhook.Validator so a webhook will be registered for the type
func (r *HostPortClass) ValidateUpdate(old runtime.Object) error {
func (r *HostPortClass) ValidateUpdate(old runtime.Object) (admission.Warnings, error) {
hostportclasslog.Info("validate update", "name", r.Name)
_ = old.(*HostPortClass)

allErrs := r.validatePools()

if len(allErrs) == 0 {
return nil, nil
}

// TODO(user): fill in your validation logic upon object update.
return nil
return nil, apierrors.NewInvalid(
schema.GroupKind{Group: GroupVersion.Group, Kind: r.Kind},
r.Name, allErrs)
}

// ValidateDelete implements webhook.Validator so a webhook will be registered for the type
func (r *HostPortClass) ValidateDelete() error {
func (r *HostPortClass) ValidateDelete() (admission.Warnings, error) {
hostportclasslog.Info("validate delete", "name", r.Name)

// TODO(user): fill in your validation logic upon object deletion.
return nil
return nil, nil
}
3 changes: 1 addition & 2 deletions controllers/hostport_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

hostportv1alpha1 "github.com/rmb938/hostport-allocator/api/v1alpha1"
)
Expand Down Expand Up @@ -208,7 +207,7 @@ func (r *HostPortReconciler) SetupWithManager(mgr ctrl.Manager) error {

return ctrl.NewControllerManagedBy(mgr).
For(&hostportv1alpha1.HostPort{}).
Watches(&source.Kind{Type: &hostportv1alpha1.HostPortClaim{}}, handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
Watches(&hostportv1alpha1.HostPortClaim{}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
hpc := object.(*hostportv1alpha1.HostPortClaim)
var req []reconcile.Request

Expand Down
5 changes: 2 additions & 3 deletions controllers/hostportclaim_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/handler"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
"sigs.k8s.io/controller-runtime/pkg/source"

hostportv1alpha1 "github.com/rmb938/hostport-allocator/api/v1alpha1"
)
Expand Down Expand Up @@ -171,7 +170,7 @@ func (r *HostPortClaimReconciler) Reconcile(ctx context.Context, req ctrl.Reques
func (r *HostPortClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {
return ctrl.NewControllerManagedBy(mgr).
For(&hostportv1alpha1.HostPortClaim{}).
Watches(&source.Kind{Type: &corev1.Pod{}}, handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
Watches(&corev1.Pod{}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
pod := object.(*corev1.Pod)
var req []reconcile.Request

Expand All @@ -188,7 +187,7 @@ func (r *HostPortClaimReconciler) SetupWithManager(mgr ctrl.Manager) error {

return req
})).
Watches(&source.Kind{Type: &hostportv1alpha1.HostPort{}}, handler.EnqueueRequestsFromMapFunc(func(object client.Object) []reconcile.Request {
Watches(&hostportv1alpha1.HostPort{}, handler.EnqueueRequestsFromMapFunc(func(ctx context.Context, object client.Object) []reconcile.Request {
hp := object.(*hostportv1alpha1.HostPort)
var req []reconcile.Request

Expand Down
3 changes: 1 addition & 2 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"k8s.io/client-go/rest"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/envtest"
"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
logf "sigs.k8s.io/controller-runtime/pkg/log"
"sigs.k8s.io/controller-runtime/pkg/log/zap"

Expand All @@ -46,7 +45,7 @@ func TestAPIs(t *testing.T) {

RunSpecsWithDefaultAndCustomReporters(t,
"Controller Suite",
[]Reporter{printer.NewlineReporter{}})
[]Reporter{})
}

var _ = BeforeSuite(func(done Done) {
Expand Down
Loading

0 comments on commit 1a02698

Please sign in to comment.