Skip to content
This repository has been archived by the owner on Mar 9, 2022. It is now read-only.

Commit

Permalink
Merge pull request #14 from puppetlabs/tasks/api-adjustments
Browse files Browse the repository at this point in the history
Update k8sutil, move validation and API objects into a more clear location, release images with tags
  • Loading branch information
impl authored Mar 19, 2021
2 parents aaa2ecf + c61d8c1 commit 0ad86f1
Show file tree
Hide file tree
Showing 19 changed files with 133 additions and 160 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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))
}
54 changes: 54 additions & 0 deletions pkg/apis/pvpool.puppet.com/v1alpha1/obj/pool.go
Original file line number Diff line number Diff line change
@@ -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))
}
4 changes: 2 additions & 2 deletions pkg/controller/app/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ""
Expand Down
10 changes: 5 additions & 5 deletions pkg/controller/app/checkout_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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,
})
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/app/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
24 changes: 12 additions & 12 deletions pkg/controller/app/pool_replica.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand All @@ -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),
Expand Down Expand Up @@ -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.
Expand All @@ -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)),
Expand Down
6 changes: 3 additions & 3 deletions pkg/controller/app/pool_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -20,7 +20,7 @@ import (
)

type PoolState struct {
Pool *obj.Pool
Pool *pvpoolv1alpha1obj.Pool
Initializing PoolReplicas
Available PoolReplicas
Stale PoolReplicas
Expand Down Expand Up @@ -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),
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/reconciler/checkout.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/controller/reconciler/pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}
Expand Down
Loading

0 comments on commit 0ad86f1

Please sign in to comment.