From c61d8c13fcb110c6f8aaf3346cf0fbdf6c7b3d94 Mon Sep 17 00:00:00 2001 From: Noah Fontes Date: Fri, 19 Mar 2021 14:07:21 -0700 Subject: [PATCH] Update k8sutil, move validation and API objects into a more clear location, release images with tags --- CHANGELOG.md | 8 ++ Makefile | 2 +- go.mod | 2 +- go.sum | 4 +- .../v1alpha1}/obj/checkout.go | 39 ++++------ .../pvpool.puppet.com/v1alpha1/obj/pool.go | 54 +++++++++++++ .../{ => v1alpha1}/validation/validation.go | 0 pkg/controller/app/checkout.go | 4 +- pkg/controller/app/checkout_state.go | 10 +-- pkg/controller/app/pool.go | 4 +- pkg/controller/app/pool_replica.go | 24 +++--- pkg/controller/app/pool_state.go | 6 +- pkg/controller/reconciler/checkout.go | 4 +- pkg/controller/reconciler/pool.go | 4 +- pkg/obj/pool.go | 78 ------------------- pkg/webhook/checkout.go | 4 +- pkg/webhook/pool.go | 6 +- tests/e2e/checkout_helper_test.go | 16 ++-- tests/e2e/pool_helper_test.go | 24 +++--- 19 files changed, 133 insertions(+), 160 deletions(-) rename pkg/{ => apis/pvpool.puppet.com/v1alpha1}/obj/checkout.go (50%) create mode 100644 pkg/apis/pvpool.puppet.com/v1alpha1/obj/pool.go rename pkg/apis/pvpool.puppet.com/{ => v1alpha1}/validation/validation.go (100%) delete mode 100644 pkg/obj/pool.go diff --git a/CHANGELOG.md b/CHANGELOG.md index 311e7b3..3053fda 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,14 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] +### Changed + +* The location of the `validation` and `obj` packages has been moved to better represent the specific APIs they work with. + +### Build + +* Images are now uploaded with the tagged version instead of "latest". + ## [0.2.0] - 2021-03-18 ### Added diff --git a/Makefile b/Makefile index 6deaf1f..c23f2ce 100644 --- a/Makefile +++ b/Makefile @@ -87,7 +87,7 @@ define build_artifact_manifest_yaml_rule $(call build_artifact_manifest_yaml,$(1)): export CGO_ENABLED := 0 $(call build_artifact_manifest_yaml,$(1)): generate $(call build_artifact_dir,$(1)) $(call versioned_artifact_kustomization_yaml,$(1)) .FORCE $(GO) run sigs.k8s.io/kustomize/kustomize/v3 build $(call versioned_artifact_dir,$(1)) \$(eval) - | $(KO) resolve -f - >$$@ + | $(KO) resolve --tags "$(PVPOOL_VERSION)" -f - >$$@ # The Kustomization file to allow other users to also leverage Kustomize with # the built target. diff --git a/go.mod b/go.mod index 916b6dd..955e36d 100644 --- a/go.mod +++ b/go.mod @@ -6,7 +6,7 @@ require ( github.com/golangci/golangci-lint v1.36.0 github.com/google/uuid v1.1.2 github.com/puppetlabs/leg/errmap v0.1.0 - github.com/puppetlabs/leg/k8sutil v0.3.2 + github.com/puppetlabs/leg/k8sutil v0.4.0 github.com/puppetlabs/leg/mainutil v0.1.2 github.com/puppetlabs/leg/mathutil v0.1.0 github.com/puppetlabs/leg/timeutil v0.3.0 diff --git a/go.sum b/go.sum index 52ecd15..a470b7d 100644 --- a/go.sum +++ b/go.sum @@ -659,8 +659,8 @@ github.com/puppetlabs/leg/datastructure v0.1.0 h1:0703wQJ71etqsPOr+vfiTBHkq0+tVV github.com/puppetlabs/leg/datastructure v0.1.0/go.mod h1:4Kwk/83hkiR1smN1gRsi0LJDgVDbD672JpWjRPBVka8= github.com/puppetlabs/leg/errmap v0.1.0 h1:1oH50d/sch1kB5JuIRrLf0hg9gSr5pfAmTUc6o8CtZQ= github.com/puppetlabs/leg/errmap v0.1.0/go.mod h1:8oVNaeaaprDjbMYWHj5lLHsD1nsnKZbv0Jw+SjoJ6hY= -github.com/puppetlabs/leg/k8sutil v0.3.2 h1:U0EWaKqieC4tEclMa4Jc9+2ZPKnwiUx1I9B0ijmdP5U= -github.com/puppetlabs/leg/k8sutil v0.3.2/go.mod h1:5LN5B926v3QNyCQa8IYYIoFS+4f2ak2Mg0LHdIPFQZU= +github.com/puppetlabs/leg/k8sutil v0.4.0 h1:DGGOOrJoiUte4V0o9HJu51+Zb2bzN0lHaDBIfIq9Ri8= +github.com/puppetlabs/leg/k8sutil v0.4.0/go.mod h1:5LN5B926v3QNyCQa8IYYIoFS+4f2ak2Mg0LHdIPFQZU= github.com/puppetlabs/leg/lifecycle v0.2.0 h1:WYaQF+mdW8Wy+tRHkEE9175Bhkd3zJ7i0qnOQkb+BmY= github.com/puppetlabs/leg/lifecycle v0.2.0/go.mod h1:QtYNNukWpkcLWZAWcM9tVxcWfqn9mULH5J3dCkMqzGk= github.com/puppetlabs/leg/logging v0.1.0 h1:G8M2w3izYEtoaH+d3rIJZ9iLX2oW2T/jO+J4l+T0Ieo= diff --git a/pkg/obj/checkout.go b/pkg/apis/pvpool.puppet.com/v1alpha1/obj/checkout.go similarity index 50% rename from pkg/obj/checkout.go rename to pkg/apis/pvpool.puppet.com/v1alpha1/obj/checkout.go index 25e6e96..19c5dd6 100644 --- a/pkg/obj/checkout.go +++ b/pkg/apis/pvpool.puppet.com/v1alpha1/obj/checkout.go @@ -12,29 +12,20 @@ import ( var CheckoutKind = pvpoolv1alpha1.CheckoutKind type Checkout struct { + *helper.NamespaceScopedAPIObject + Key client.ObjectKey Object *pvpoolv1alpha1.Checkout } -var _ lifecycle.Deleter = &Checkout{} -var _ lifecycle.Loader = &Checkout{} -var _ lifecycle.Owner = &Checkout{} -var _ lifecycle.Persister = &Checkout{} - -func (c *Checkout) Delete(ctx context.Context, cl client.Client, opts ...lifecycle.DeleteOption) (bool, error) { - return helper.DeleteIgnoreNotFound(ctx, cl, c.Object, opts...) -} - -func (c *Checkout) Own(ctx context.Context, other lifecycle.Ownable) error { - return other.Owned(ctx, lifecycle.TypedObject{GVK: CheckoutKind, Object: c.Object}) -} - -func (c *Checkout) Load(ctx context.Context, cl client.Client) (bool, error) { - return helper.GetIgnoreNotFound(ctx, cl, c.Key, c.Object) +func makeCheckout(key client.ObjectKey, obj *pvpoolv1alpha1.Checkout) *Checkout { + c := &Checkout{Key: key, Object: obj} + c.NamespaceScopedAPIObject = helper.ForNamespaceScopedAPIObject(&c.Key, lifecycle.TypedObject{GVK: CheckoutKind, Object: c.Object}) + return c } -func (c *Checkout) Persist(ctx context.Context, cl client.Client) error { - return helper.CreateOrUpdate(ctx, cl, c.Object, helper.WithObjectKey(c.Key)) +func (c *Checkout) Copy() *Checkout { + return makeCheckout(c.Key, c.Object.DeepCopy()) } func (c *Checkout) PersistStatus(ctx context.Context, cl client.Client) error { @@ -51,15 +42,13 @@ func (c *Checkout) Condition(typ pvpoolv1alpha1.CheckoutConditionType) (pvpoolv1 } func NewCheckout(key client.ObjectKey) *Checkout { - return &Checkout{ - Key: key, - Object: &pvpoolv1alpha1.Checkout{}, - } + return makeCheckout(key, &pvpoolv1alpha1.Checkout{}) } func NewCheckoutFromObject(obj *pvpoolv1alpha1.Checkout) *Checkout { - return &Checkout{ - Key: client.ObjectKeyFromObject(obj), - Object: obj, - } + return makeCheckout(client.ObjectKeyFromObject(obj), obj) +} + +func NewCheckoutPatcher(upd, orig *Pool) lifecycle.Persister { + return helper.NewPatcher(upd.Object, orig.Object, helper.WithObjectKey(upd.Key)) } diff --git a/pkg/apis/pvpool.puppet.com/v1alpha1/obj/pool.go b/pkg/apis/pvpool.puppet.com/v1alpha1/obj/pool.go new file mode 100644 index 0000000..49ff82f --- /dev/null +++ b/pkg/apis/pvpool.puppet.com/v1alpha1/obj/pool.go @@ -0,0 +1,54 @@ +package obj + +import ( + "context" + + "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/helper" + "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" + pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +var PoolKind = pvpoolv1alpha1.PoolKind + +type Pool struct { + *helper.NamespaceScopedAPIObject + + Key client.ObjectKey + Object *pvpoolv1alpha1.Pool +} + +func makePool(key client.ObjectKey, obj *pvpoolv1alpha1.Pool) *Pool { + p := &Pool{Key: key, Object: obj} + p.NamespaceScopedAPIObject = helper.ForNamespaceScopedAPIObject(&p.Key, lifecycle.TypedObject{GVK: PoolKind, Object: p.Object}) + return p +} + +func (p *Pool) Copy() *Pool { + return makePool(p.Key, p.Object.DeepCopy()) +} + +func (p *Pool) PersistStatus(ctx context.Context, cl client.Client) error { + return cl.Status().Update(ctx, p.Object) +} + +func (p *Pool) Condition(typ pvpoolv1alpha1.PoolConditionType) (pvpoolv1alpha1.PoolCondition, bool) { + for _, cond := range p.Object.Status.Conditions { + if cond.Type == typ { + return cond, true + } + } + return pvpoolv1alpha1.PoolCondition{Type: typ}, false +} + +func NewPool(key client.ObjectKey) *Pool { + return makePool(key, &pvpoolv1alpha1.Pool{}) +} + +func NewPoolFromObject(obj *pvpoolv1alpha1.Pool) *Pool { + return makePool(client.ObjectKeyFromObject(obj), obj) +} + +func NewPoolPatcher(upd, orig *Pool) lifecycle.Persister { + return helper.NewPatcher(upd.Object, orig.Object, helper.WithObjectKey(upd.Key)) +} diff --git a/pkg/apis/pvpool.puppet.com/validation/validation.go b/pkg/apis/pvpool.puppet.com/v1alpha1/validation/validation.go similarity index 100% rename from pkg/apis/pvpool.puppet.com/validation/validation.go rename to pkg/apis/pvpool.puppet.com/v1alpha1/validation/validation.go diff --git a/pkg/controller/app/checkout.go b/pkg/controller/app/checkout.go index 0af45f8..f8580eb 100644 --- a/pkg/controller/app/checkout.go +++ b/pkg/controller/app/checkout.go @@ -2,11 +2,11 @@ package app import ( pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" corev1 "k8s.io/api/core/v1" ) -func ConfigureCheckout(cs *CheckoutState) *obj.Checkout { +func ConfigureCheckout(cs *CheckoutState) *pvpoolv1alpha1obj.Checkout { if cs.PersistentVolume == nil { // Something didn't go right when loading probably. Clear our state. cs.Checkout.Object.Status.VolumeName = "" diff --git a/pkg/controller/app/checkout_state.go b/pkg/controller/app/checkout_state.go index 150c913..f620323 100644 --- a/pkg/controller/app/checkout_state.go +++ b/pkg/controller/app/checkout_state.go @@ -11,7 +11,7 @@ import ( "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/helper" "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -21,7 +21,7 @@ import ( ) type CheckoutState struct { - Checkout *obj.Checkout + Checkout *pvpoolv1alpha1obj.Checkout PersistentVolumeClaim *corev1obj.PersistentVolumeClaim // PersistentVolume is the PV corresponding to the PVC owned by this @@ -85,7 +85,7 @@ func (cs *CheckoutState) loadFromVolumeName(ctx context.Context, cl client.Clien switch { case ctrl.UID == cs.Checkout.Object.GetUID(): klog.V(4).InfoS("checkout state: load: persistent volume is used by this checkout", "checkout", cs.Checkout.Key, "pvc", claim.Key, "pv", volume.Name) - case schema.FromAPIVersionAndKind(ctrl.APIVersion, ctrl.Kind) == obj.CheckoutKind: + case schema.FromAPIVersionAndKind(ctrl.APIVersion, ctrl.Kind) == pvpoolv1alpha1obj.CheckoutKind: // Race condition where the claim has been reassigned from under us. klog.InfoS("checkout state: load: persistent volume stolen (not using)", "checkout", cs.Checkout.Key, "pvc", claim.Key, "pv", volume.Name) return nil @@ -108,7 +108,7 @@ func (cs *CheckoutState) loadFromPool(ctx context.Context, cl client.Client) (bo namespace = cs.Checkout.Key.Namespace } - pool := obj.NewPool(client.ObjectKey{ + pool := pvpoolv1alpha1obj.NewPool(client.ObjectKey{ Namespace: namespace, Name: cs.Checkout.Object.Spec.PoolRef.Name, }) @@ -231,7 +231,7 @@ func (cs *CheckoutState) Persist(ctx context.Context, cl client.Client) error { return nil } -func NewCheckoutState(c *obj.Checkout) *CheckoutState { +func NewCheckoutState(c *pvpoolv1alpha1obj.Checkout) *CheckoutState { claimName := c.Object.Spec.ClaimName if claimName == "" { claimName = c.Key.Name diff --git a/pkg/controller/app/pool.go b/pkg/controller/app/pool.go index 41884dd..971fc4e 100644 --- a/pkg/controller/app/pool.go +++ b/pkg/controller/app/pool.go @@ -2,10 +2,10 @@ package app import ( pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" ) -func ConfigurePool(ps *PoolState) *obj.Pool { +func ConfigurePool(ps *PoolState) *pvpoolv1alpha1obj.Pool { ps.Pool.Object.Status.ObservedGeneration = ps.Pool.Object.GetGeneration() ps.Pool.Object.Status.Replicas = int32(len(ps.Available) + len(ps.Initializing) + len(ps.Stale)) ps.Pool.Object.Status.AvailableReplicas = int32(len(ps.Available)) diff --git a/pkg/controller/app/pool_replica.go b/pkg/controller/app/pool_replica.go index 0f430ce..626791b 100644 --- a/pkg/controller/app/pool_replica.go +++ b/pkg/controller/app/pool_replica.go @@ -11,8 +11,8 @@ import ( "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" "github.com/puppetlabs/leg/k8sutil/pkg/norm" "github.com/puppetlabs/leg/mathutil/pkg/rand" - "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/validation" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" + pvpoolv1alpha1validation "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/validation" batchv1 "k8s.io/api/batch/v1" corev1 "k8s.io/api/core/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -40,12 +40,12 @@ var ( }, }, }, - BackoffLimit: pointer.Int32Ptr(validation.MountJobMaxBackoffLimit), + BackoffLimit: pointer.Int32Ptr(pvpoolv1alpha1validation.MountJobMaxBackoffLimit), } ) type PoolReplica struct { - Pool *obj.Pool + Pool *pvpoolv1alpha1obj.Pool PersistentVolumeClaim *corev1obj.PersistentVolumeClaim PersistentVolume *corev1obj.PersistentVolume InitJob *batchv1obj.Job @@ -122,7 +122,7 @@ func (pr *PoolReplica) Persist(ctx context.Context, cl client.Client) error { // Don't use OwnUncontrolled here because it will block deletion of the job. // Just use SetDependencyOf, our external tracking mechanism. if err := DependencyManager.SetDependencyOf(pr.InitJob.Object, lifecycle.TypedObject{ - GVK: obj.PoolKind, + GVK: pvpoolv1alpha1obj.PoolKind, Object: pr.Pool.Object, }); err != nil { return err @@ -145,7 +145,7 @@ func (pr *PoolReplica) Available() bool { return pr.PersistentVolume != nil && pr.PersistentVolumeClaim.Object.GetAnnotations()[PoolReplicaPhaseAnnotationKey] == PoolReplicaPhaseAnnotationValueAvailable } -func NewPoolReplica(p *obj.Pool, key client.ObjectKey) *PoolReplica { +func NewPoolReplica(p *pvpoolv1alpha1obj.Pool, key client.ObjectKey) *PoolReplica { return &PoolReplica{ Pool: p, PersistentVolumeClaim: corev1obj.NewPersistentVolumeClaim(key), @@ -202,15 +202,15 @@ func ConfigurePoolReplica(pr *PoolReplica) *PoolReplica { // Configure some of the required fields. if pr.InitJob.Object.Spec.Template.Spec.RestartPolicy == "" { - pr.InitJob.Object.Spec.Template.Spec.RestartPolicy = validation.MountJobSpecBackoffPolicy + pr.InitJob.Object.Spec.Template.Spec.RestartPolicy = pvpoolv1alpha1validation.MountJobSpecBackoffPolicy } - if pr.InitJob.Object.Spec.ActiveDeadlineSeconds == nil || *pr.InitJob.Object.Spec.ActiveDeadlineSeconds > validation.MountJobMaxActiveDeadlineSeconds { - pr.InitJob.Object.Spec.ActiveDeadlineSeconds = pointer.Int64Ptr(validation.MountJobMaxActiveDeadlineSeconds) + if pr.InitJob.Object.Spec.ActiveDeadlineSeconds == nil || *pr.InitJob.Object.Spec.ActiveDeadlineSeconds > pvpoolv1alpha1validation.MountJobMaxActiveDeadlineSeconds { + pr.InitJob.Object.Spec.ActiveDeadlineSeconds = pointer.Int64Ptr(pvpoolv1alpha1validation.MountJobMaxActiveDeadlineSeconds) } - if pr.InitJob.Object.Spec.BackoffLimit != nil && *pr.InitJob.Object.Spec.BackoffLimit > validation.MountJobMaxBackoffLimit { - pr.InitJob.Object.Spec.BackoffLimit = pointer.Int32Ptr(validation.MountJobMaxBackoffLimit) + if pr.InitJob.Object.Spec.BackoffLimit != nil && *pr.InitJob.Object.Spec.BackoffLimit > pvpoolv1alpha1validation.MountJobMaxBackoffLimit { + pr.InitJob.Object.Spec.BackoffLimit = pointer.Int32Ptr(pvpoolv1alpha1validation.MountJobMaxBackoffLimit) } // Set up volume. @@ -233,7 +233,7 @@ func ConfigurePoolReplica(pr *PoolReplica) *PoolReplica { return pr } -func ApplyPoolReplica(ctx context.Context, cl client.Client, p *obj.Pool, id string) (*PoolReplica, error) { +func ApplyPoolReplica(ctx context.Context, cl client.Client, p *pvpoolv1alpha1obj.Pool, id string) (*PoolReplica, error) { key := client.ObjectKey{ Namespace: p.Key.Namespace, Name: norm.MetaNameSuffixed(p.Key.Name, fmt.Sprintf("-%s", id)), diff --git a/pkg/controller/app/pool_state.go b/pkg/controller/app/pool_state.go index c4fbdeb..9c19458 100644 --- a/pkg/controller/app/pool_state.go +++ b/pkg/controller/app/pool_state.go @@ -11,7 +11,7 @@ import ( "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" "github.com/puppetlabs/leg/mathutil/pkg/rand" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" @@ -20,7 +20,7 @@ import ( ) type PoolState struct { - Pool *obj.Pool + Pool *pvpoolv1alpha1obj.Pool Initializing PoolReplicas Available PoolReplicas Stale PoolReplicas @@ -284,7 +284,7 @@ func (ps *PoolState) Persist(ctx context.Context, cl client.Client) error { return nil } -func NewPoolState(p *obj.Pool) *PoolState { +func NewPoolState(p *pvpoolv1alpha1obj.Pool) *PoolState { return &PoolState{ Pool: p, Conds: make(map[pvpoolv1alpha1.PoolConditionType]pvpoolv1alpha1.Condition), diff --git a/pkg/controller/reconciler/checkout.go b/pkg/controller/reconciler/checkout.go index c5636df..1f42726 100644 --- a/pkg/controller/reconciler/checkout.go +++ b/pkg/controller/reconciler/checkout.go @@ -5,8 +5,8 @@ import ( "time" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" "github.com/puppetlabs/pvpool/pkg/controller/app" - "github.com/puppetlabs/pvpool/pkg/obj" "github.com/puppetlabs/pvpool/pkg/opt" "golang.org/x/time/rate" corev1 "k8s.io/api/core/v1" @@ -40,7 +40,7 @@ func (pr *CheckoutReconciler) Reconcile(ctx context.Context, req reconcile.Reque } }() - checkout := obj.NewCheckout(req.NamespacedName) + checkout := pvpoolv1alpha1obj.NewCheckout(req.NamespacedName) if ok, err := checkout.Load(ctx, pr.cl); err != nil || !ok { return reconcile.Result{}, err } diff --git a/pkg/controller/reconciler/pool.go b/pkg/controller/reconciler/pool.go index 9898714..8e2f974 100644 --- a/pkg/controller/reconciler/pool.go +++ b/pkg/controller/reconciler/pool.go @@ -6,8 +6,8 @@ import ( "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" "github.com/puppetlabs/pvpool/pkg/controller/app" - "github.com/puppetlabs/pvpool/pkg/obj" "github.com/puppetlabs/pvpool/pkg/opt" "golang.org/x/time/rate" batchv1 "k8s.io/api/batch/v1" @@ -46,7 +46,7 @@ func (pr *PoolReconciler) Reconcile(ctx context.Context, req reconcile.Request) } }() - pool := obj.NewPool(req.NamespacedName) + pool := pvpoolv1alpha1obj.NewPool(req.NamespacedName) if ok, err := pool.Load(ctx, pr.cl); err != nil || !ok { return reconcile.Result{}, err } diff --git a/pkg/obj/pool.go b/pkg/obj/pool.go deleted file mode 100644 index 2dff685..0000000 --- a/pkg/obj/pool.go +++ /dev/null @@ -1,78 +0,0 @@ -package obj - -import ( - "context" - - "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/helper" - "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" - pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "sigs.k8s.io/controller-runtime/pkg/client" -) - -var PoolKind = pvpoolv1alpha1.PoolKind - -type Pool struct { - Key client.ObjectKey - Object *pvpoolv1alpha1.Pool -} - -var _ lifecycle.Deleter = &Pool{} -var _ lifecycle.Finalizable = &Pool{} -var _ lifecycle.Loader = &Pool{} -var _ lifecycle.Owner = &Pool{} -var _ lifecycle.Persister = &Pool{} - -func (p *Pool) Delete(ctx context.Context, cl client.Client, opts ...lifecycle.DeleteOption) (bool, error) { - return helper.DeleteIgnoreNotFound(ctx, cl, p.Object, opts...) -} - -func (p *Pool) Finalizing() bool { - return !p.Object.GetDeletionTimestamp().IsZero() -} - -func (p *Pool) AddFinalizer(ctx context.Context, name string) bool { - return helper.AddFinalizer(p.Object, name) -} - -func (p *Pool) RemoveFinalizer(ctx context.Context, name string) bool { - return helper.RemoveFinalizer(p.Object, name) -} - -func (p *Pool) Load(ctx context.Context, cl client.Client) (bool, error) { - return helper.GetIgnoreNotFound(ctx, cl, p.Key, p.Object) -} - -func (p *Pool) Own(ctx context.Context, other lifecycle.Ownable) error { - return other.Owned(ctx, lifecycle.TypedObject{GVK: PoolKind, Object: p.Object}) -} - -func (p *Pool) Persist(ctx context.Context, cl client.Client) error { - return helper.CreateOrUpdate(ctx, cl, p.Object, helper.WithObjectKey(p.Key)) -} - -func (p *Pool) PersistStatus(ctx context.Context, cl client.Client) error { - return cl.Status().Update(ctx, p.Object) -} - -func (p *Pool) Condition(typ pvpoolv1alpha1.PoolConditionType) (pvpoolv1alpha1.PoolCondition, bool) { - for _, cond := range p.Object.Status.Conditions { - if cond.Type == typ { - return cond, true - } - } - return pvpoolv1alpha1.PoolCondition{Type: typ}, false -} - -func NewPool(key client.ObjectKey) *Pool { - return &Pool{ - Key: key, - Object: &pvpoolv1alpha1.Pool{}, - } -} - -func NewPoolFromObject(obj *pvpoolv1alpha1.Pool) *Pool { - return &Pool{ - Key: client.ObjectKeyFromObject(obj), - Object: obj, - } -} diff --git a/pkg/webhook/checkout.go b/pkg/webhook/checkout.go index f0ee698..2140d20 100644 --- a/pkg/webhook/checkout.go +++ b/pkg/webhook/checkout.go @@ -7,7 +7,7 @@ import ( "net/http" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/validation" + pvpoolv1alpha1validation "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/validation" admissionv1 "k8s.io/api/admission/v1" authorizationv1 "k8s.io/api/authorization/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" @@ -44,7 +44,7 @@ func (cv *CheckoutValidator) ValidateUpdate(old runtime.Object) error { } var errs field.ErrorList - errs = append(errs, validation.ValidateCheckoutUpdate(cv.Checkout, oldCV.Checkout)...) + errs = append(errs, pvpoolv1alpha1validation.ValidateCheckoutUpdate(cv.Checkout, oldCV.Checkout)...) if len(errs) != 0 { return k8serrors.NewInvalid(pvpoolv1alpha1.CheckoutKind.GroupKind(), cv.GetName(), errs) diff --git a/pkg/webhook/pool.go b/pkg/webhook/pool.go index e2e31f1..06fcd2a 100644 --- a/pkg/webhook/pool.go +++ b/pkg/webhook/pool.go @@ -5,7 +5,7 @@ import ( "net/http" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/validation" + pvpoolv1alpha1validation "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/validation" "k8s.io/apimachinery/pkg/api/errors" runtime "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/validation/field" @@ -27,7 +27,7 @@ var _ webhook.Validator = &PoolValidator{} func (pv *PoolValidator) ValidateCreate() error { var errs field.ErrorList - errs = append(errs, validation.ValidatePoolSpec(&pv.Spec, field.NewPath("spec"))...) + errs = append(errs, pvpoolv1alpha1validation.ValidatePoolSpec(&pv.Spec, field.NewPath("spec"))...) if len(errs) != 0 { return errors.NewInvalid(pvpoolv1alpha1.PoolKind.GroupKind(), pv.GetName(), errs) @@ -43,7 +43,7 @@ func (pv *PoolValidator) ValidateUpdate(old runtime.Object) error { } var errs field.ErrorList - errs = append(errs, validation.ValidatePoolSpecUpdate(&pv.Spec, &oldPV.Spec, field.NewPath("spec"))...) + errs = append(errs, pvpoolv1alpha1validation.ValidatePoolSpecUpdate(&pv.Spec, &oldPV.Spec, field.NewPath("spec"))...) if len(errs) != 0 { return errors.NewInvalid(pvpoolv1alpha1.PoolKind.GroupKind(), pv.GetName(), errs) diff --git a/tests/e2e/checkout_helper_test.go b/tests/e2e/checkout_helper_test.go index 531998a..7147188 100644 --- a/tests/e2e/checkout_helper_test.go +++ b/tests/e2e/checkout_helper_test.go @@ -6,7 +6,7 @@ import ( "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" "sigs.k8s.io/controller-runtime/pkg/client" @@ -16,7 +16,7 @@ type CheckoutHelpers struct { eit *EnvironmentInTest } -func (ch *CheckoutHelpers) WaitCheckedOut(ctx context.Context, co *obj.Checkout) (*obj.Checkout, error) { +func (ch *CheckoutHelpers) WaitCheckedOut(ctx context.Context, co *pvpoolv1alpha1obj.Checkout) (*pvpoolv1alpha1obj.Checkout, error) { err := Wait(ctx, func(ctx context.Context) (bool, error) { if _, err := (lifecycle.RequiredLoader{Loader: co}).Load(ctx, ch.eit.ControllerClient); err != nil { return true, err @@ -35,7 +35,7 @@ func (ch *CheckoutHelpers) WaitCheckedOut(ctx context.Context, co *obj.Checkout) return co, nil } -func (ch *CheckoutHelpers) RequireWaitCheckedOut(ctx context.Context, co *obj.Checkout) *obj.Checkout { +func (ch *CheckoutHelpers) RequireWaitCheckedOut(ctx context.Context, co *pvpoolv1alpha1obj.Checkout) *pvpoolv1alpha1obj.Checkout { co, err := ch.WaitCheckedOut(ctx, co) require.NoError(ch.eit.t, err) return co @@ -56,11 +56,11 @@ func (o *CreateCheckoutOptions) ApplyOptions(opts []CreateCheckoutOption) { } } -func (ch *CheckoutHelpers) CreateCheckout(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) (*obj.Checkout, error) { +func (ch *CheckoutHelpers) CreateCheckout(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) (*pvpoolv1alpha1obj.Checkout, error) { o := &CreateCheckoutOptions{} o.ApplyOptions(opts) - co := obj.NewCheckout(key) + co := pvpoolv1alpha1obj.NewCheckout(key) co.Object.Spec = pvpoolv1alpha1.CheckoutSpec{ PoolRef: pvpoolv1alpha1.PoolReference{ Namespace: poolKey.Namespace, @@ -76,13 +76,13 @@ func (ch *CheckoutHelpers) CreateCheckout(ctx context.Context, key, poolKey clie return co, nil } -func (ch *CheckoutHelpers) RequireCreateCheckout(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) *obj.Checkout { +func (ch *CheckoutHelpers) RequireCreateCheckout(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) *pvpoolv1alpha1obj.Checkout { co, err := ch.CreateCheckout(ctx, key, poolKey, opts...) require.NoError(ch.eit.t, err) return co } -func (ch *CheckoutHelpers) CreateCheckoutThenWaitCheckedOut(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) (*obj.Checkout, error) { +func (ch *CheckoutHelpers) CreateCheckoutThenWaitCheckedOut(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) (*pvpoolv1alpha1obj.Checkout, error) { co, err := ch.CreateCheckout(ctx, key, poolKey, opts...) if err != nil { return nil, err @@ -91,6 +91,6 @@ func (ch *CheckoutHelpers) CreateCheckoutThenWaitCheckedOut(ctx context.Context, return ch.WaitCheckedOut(ctx, co) } -func (ch *CheckoutHelpers) RequireCreateCheckoutThenWaitCheckedOut(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) *obj.Checkout { +func (ch *CheckoutHelpers) RequireCreateCheckoutThenWaitCheckedOut(ctx context.Context, key, poolKey client.ObjectKey, opts ...CreateCheckoutOption) *pvpoolv1alpha1obj.Checkout { return ch.RequireWaitCheckedOut(ctx, ch.RequireCreateCheckout(ctx, key, poolKey, opts...)) } diff --git a/tests/e2e/pool_helper_test.go b/tests/e2e/pool_helper_test.go index f3b2162..4b2616f 100644 --- a/tests/e2e/pool_helper_test.go +++ b/tests/e2e/pool_helper_test.go @@ -6,7 +6,7 @@ import ( "github.com/puppetlabs/leg/k8sutil/pkg/controller/obj/lifecycle" pvpoolv1alpha1 "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1" - "github.com/puppetlabs/pvpool/pkg/obj" + pvpoolv1alpha1obj "github.com/puppetlabs/pvpool/pkg/apis/pvpool.puppet.com/v1alpha1/obj" "github.com/stretchr/testify/require" corev1 "k8s.io/api/core/v1" "k8s.io/apimachinery/pkg/api/resource" @@ -19,7 +19,7 @@ type PoolHelpers struct { eit *EnvironmentInTest } -func (ph *PoolHelpers) WaitSettled(ctx context.Context, p *obj.Pool) (*obj.Pool, error) { +func (ph *PoolHelpers) WaitSettled(ctx context.Context, p *pvpoolv1alpha1obj.Pool) (*pvpoolv1alpha1obj.Pool, error) { err := Wait(ctx, func(ctx context.Context) (bool, error) { if _, err := (lifecycle.RequiredLoader{Loader: p}).Load(ctx, ph.eit.ControllerClient); err != nil { return true, err @@ -42,7 +42,7 @@ func (ph *PoolHelpers) WaitSettled(ctx context.Context, p *obj.Pool) (*obj.Pool, return p, nil } -func (ph *PoolHelpers) RequireWaitSettled(ctx context.Context, p *obj.Pool) *obj.Pool { +func (ph *PoolHelpers) RequireWaitSettled(ctx context.Context, p *pvpoolv1alpha1obj.Pool) *pvpoolv1alpha1obj.Pool { p, err := ph.WaitSettled(ctx, p) require.NoError(ph.eit.t, err) return p @@ -64,11 +64,11 @@ func (o *CreatePoolOptions) ApplyOptions(opts []CreatePoolOption) { } } -func (ph *PoolHelpers) CreatePool(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) (*obj.Pool, error) { +func (ph *PoolHelpers) CreatePool(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) (*pvpoolv1alpha1obj.Pool, error) { o := &CreatePoolOptions{} o.ApplyOptions(opts) - p := obj.NewPool(key) + p := pvpoolv1alpha1obj.NewPool(key) p.Object.Spec = pvpoolv1alpha1.PoolSpec{ Replicas: o.Replicas, Selector: metav1.LabelSelector{ @@ -101,13 +101,13 @@ func (ph *PoolHelpers) CreatePool(ctx context.Context, key client.ObjectKey, opt return p, nil } -func (ph *PoolHelpers) RequireCreatePool(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) *obj.Pool { +func (ph *PoolHelpers) RequireCreatePool(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) *pvpoolv1alpha1obj.Pool { p, err := ph.CreatePool(ctx, key, opts...) require.NoError(ph.eit.t, err) return p } -func (ph *PoolHelpers) CreatePoolThenWaitSettled(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) (*obj.Pool, error) { +func (ph *PoolHelpers) CreatePoolThenWaitSettled(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) (*pvpoolv1alpha1obj.Pool, error) { p, err := ph.CreatePool(ctx, key, opts...) if err != nil { return nil, err @@ -116,11 +116,11 @@ func (ph *PoolHelpers) CreatePoolThenWaitSettled(ctx context.Context, key client return ph.WaitSettled(ctx, p) } -func (ph *PoolHelpers) RequireCreatePoolThenWaitSettled(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) *obj.Pool { +func (ph *PoolHelpers) RequireCreatePoolThenWaitSettled(ctx context.Context, key client.ObjectKey, opts ...CreatePoolOption) *pvpoolv1alpha1obj.Pool { return ph.RequireWaitSettled(ctx, ph.RequireCreatePool(ctx, key, opts...)) } -func (ph *PoolHelpers) ScalePool(ctx context.Context, p *obj.Pool, replicas int32) (*obj.Pool, error) { +func (ph *PoolHelpers) ScalePool(ctx context.Context, p *pvpoolv1alpha1obj.Pool, replicas int32) (*pvpoolv1alpha1obj.Pool, error) { p.Object.Spec.Replicas = &replicas if err := p.Persist(ctx, ph.eit.ControllerClient); err != nil { return nil, err @@ -129,13 +129,13 @@ func (ph *PoolHelpers) ScalePool(ctx context.Context, p *obj.Pool, replicas int3 return p, nil } -func (ph *PoolHelpers) RequireScalePool(ctx context.Context, p *obj.Pool, replicas int32) *obj.Pool { +func (ph *PoolHelpers) RequireScalePool(ctx context.Context, p *pvpoolv1alpha1obj.Pool, replicas int32) *pvpoolv1alpha1obj.Pool { p, err := ph.ScalePool(ctx, p, replicas) require.NoError(ph.eit.t, err) return p } -func (ph *PoolHelpers) ScalePoolThenWaitSettled(ctx context.Context, p *obj.Pool, replicas int32) (*obj.Pool, error) { +func (ph *PoolHelpers) ScalePoolThenWaitSettled(ctx context.Context, p *pvpoolv1alpha1obj.Pool, replicas int32) (*pvpoolv1alpha1obj.Pool, error) { p, err := ph.ScalePool(ctx, p, replicas) if err != nil { return nil, err @@ -144,6 +144,6 @@ func (ph *PoolHelpers) ScalePoolThenWaitSettled(ctx context.Context, p *obj.Pool return ph.WaitSettled(ctx, p) } -func (ph *PoolHelpers) RequireScalePoolThenWaitSettled(ctx context.Context, p *obj.Pool, replicas int32) *obj.Pool { +func (ph *PoolHelpers) RequireScalePoolThenWaitSettled(ctx context.Context, p *pvpoolv1alpha1obj.Pool, replicas int32) *pvpoolv1alpha1obj.Pool { return ph.RequireWaitSettled(ctx, ph.RequireScalePool(ctx, p, replicas)) }