From 22a5ac7f0f4973673c2c35798d5ee2a2d1fedcc4 Mon Sep 17 00:00:00 2001 From: Michael Bridgen Date: Tue, 8 Dec 2020 15:20:41 +0000 Subject: [PATCH] Standardise the names of types Most commands use either a kind, or a more readable spelling of a kind, in their output. To make this easier, this centralises the definition of those names in one place, and lets the command implementations choose whichever they need. Signed-off-by: Michael Bridgen --- cmd/flux/auto.go | 16 ++++++++++++ cmd/flux/delete.go | 8 +++--- cmd/flux/delete_auto_imagepolicy.go | 4 +-- cmd/flux/delete_auto_imagerepository.go | 4 +-- cmd/flux/delete_auto_imageupdateauto.go | 4 +-- cmd/flux/get.go | 6 ++--- cmd/flux/get_auto_imagepolicy.go | 3 ++- cmd/flux/get_auto_imagerepository.go | 3 ++- cmd/flux/get_auto_imageupdateauto.go | 3 ++- cmd/flux/object.go | 9 +++++++ cmd/flux/reconcile.go | 30 +++++++++++----------- cmd/flux/reconcile_auto_imagerepository.go | 4 +-- cmd/flux/reconcile_auto_imageupdateauto.go | 4 +-- cmd/flux/resume.go | 5 ++-- cmd/flux/resume_auto_imagerepository.go | 5 ++-- cmd/flux/resume_auto_imageupdateauto.go | 5 ++-- cmd/flux/suspend.go | 4 +-- cmd/flux/suspend_auto_imagerepository.go | 4 +-- cmd/flux/suspend_auto_imageupdateauto.go | 4 +-- 19 files changed, 75 insertions(+), 50 deletions(-) diff --git a/cmd/flux/auto.go b/cmd/flux/auto.go index 280b7d57..310642e3 100644 --- a/cmd/flux/auto.go +++ b/cmd/flux/auto.go @@ -28,6 +28,12 @@ import ( // it's used in at least a couple of commands. // imagev1.ImageRepository + +var imageRepositoryNames = names{ + kind: imagev1.ImageRepositoryKind, + humanKind: "image repository", +} + type imageRepositoryAdapter struct { *imagev1.ImageRepository } @@ -52,6 +58,11 @@ func (a imageRepositoryListAdapter) len() int { // imagev1.ImagePolicy +var imagePolicyNames = names{ + kind: imagev1.ImagePolicyKind, + humanKind: "image policy", +} + type imagePolicyAdapter struct { *imagev1.ImagePolicy } @@ -76,6 +87,11 @@ func (a imagePolicyListAdapter) len() int { // autov1.ImageUpdateAutomation +var imageUpdateAutomationNames = names{ + kind: autov1.ImageUpdateAutomationKind, + humanKind: "image update automation", +} + type imageUpdateAutomationAdapter struct { *autov1.ImageUpdateAutomation } diff --git a/cmd/flux/delete.go b/cmd/flux/delete.go index a70ca2e2..7471d67f 100644 --- a/cmd/flux/delete.go +++ b/cmd/flux/delete.go @@ -45,8 +45,8 @@ func init() { } type deleteCommand struct { - humanKind string // the kind being deleted, lowercase and spaced e.g., "image policy" - adapter adapter // for getting the value, and later deleting it + names + object adapter // for getting the value, and later deleting it } func (del deleteCommand) run(cmd *cobra.Command, args []string) error { @@ -68,7 +68,7 @@ func (del deleteCommand) run(cmd *cobra.Command, args []string) error { Name: name, } - err = kubeClient.Get(ctx, namespacedName, del.adapter.asRuntimeObject()) + err = kubeClient.Get(ctx, namespacedName, del.object.asRuntimeObject()) if err != nil { return err } @@ -84,7 +84,7 @@ func (del deleteCommand) run(cmd *cobra.Command, args []string) error { } logger.Actionf("deleting %s %s in %s namespace", del.humanKind, name, namespace) - err = kubeClient.Delete(ctx, del.adapter.asRuntimeObject()) + err = kubeClient.Delete(ctx, del.object.asRuntimeObject()) if err != nil { return err } diff --git a/cmd/flux/delete_auto_imagepolicy.go b/cmd/flux/delete_auto_imagepolicy.go index f0172835..d92b146c 100644 --- a/cmd/flux/delete_auto_imagepolicy.go +++ b/cmd/flux/delete_auto_imagepolicy.go @@ -30,8 +30,8 @@ var deleteImagePolicyCmd = &cobra.Command{ flux delete auto image-policy alpine3.x `, RunE: deleteCommand{ - humanKind: "image policy", - adapter: universalAdapter{&imagev1.ImagePolicy{}}, + names: imagePolicyNames, + object: universalAdapter{&imagev1.ImagePolicy{}}, }.run, } diff --git a/cmd/flux/delete_auto_imagerepository.go b/cmd/flux/delete_auto_imagerepository.go index 20fcdb0b..2b32b0de 100644 --- a/cmd/flux/delete_auto_imagerepository.go +++ b/cmd/flux/delete_auto_imagerepository.go @@ -30,8 +30,8 @@ var deleteImageRepositoryCmd = &cobra.Command{ flux delete auto image-repository alpine `, RunE: deleteCommand{ - humanKind: "image repository", - adapter: universalAdapter{&imagev1.ImageRepository{}}, + names: imageRepositoryNames, + object: universalAdapter{&imagev1.ImageRepository{}}, }.run, } diff --git a/cmd/flux/delete_auto_imageupdateauto.go b/cmd/flux/delete_auto_imageupdateauto.go index 69caa233..ed1470b6 100644 --- a/cmd/flux/delete_auto_imageupdateauto.go +++ b/cmd/flux/delete_auto_imageupdateauto.go @@ -30,8 +30,8 @@ var deleteImageUpdateCmd = &cobra.Command{ flux delete auto image-update latest-images `, RunE: deleteCommand{ - humanKind: "image update automation", - adapter: universalAdapter{&autov1.ImageUpdateAutomation{}}, + names: imageUpdateAutomationNames, + object: universalAdapter{&autov1.ImageUpdateAutomation{}}, }.run, } diff --git a/cmd/flux/get.go b/cmd/flux/get.go index 631d5bde..9e1ef750 100644 --- a/cmd/flux/get.go +++ b/cmd/flux/get.go @@ -75,8 +75,8 @@ func nameColumns(item named, includeNamespace bool) []string { var namespaceHeader = []string{"Namespace"} type getCommand struct { - headers []string - list summarisable + names + list summarisable } func (get getCommand) run(cmd *cobra.Command, args []string) error { @@ -98,7 +98,7 @@ func (get getCommand) run(cmd *cobra.Command, args []string) error { } if get.list.len() == 0 { - logger.Failuref("no imagerepository objects found in %s namespace", namespace) + logger.Failuref("no %s objects found in %s namespace", get.kind, namespace) return nil } diff --git a/cmd/flux/get_auto_imagepolicy.go b/cmd/flux/get_auto_imagepolicy.go index cc459e03..5737a77e 100644 --- a/cmd/flux/get_auto_imagepolicy.go +++ b/cmd/flux/get_auto_imagepolicy.go @@ -33,7 +33,8 @@ var getImagePolicyCmd = &cobra.Command{ flux get auto image-policy --all-namespaces `, RunE: getCommand{ - list: &imagePolicyListAdapter{&imagev1.ImagePolicyList{}}, + names: imagePolicyNames, + list: &imagePolicyListAdapter{&imagev1.ImagePolicyList{}}, }.run, } diff --git a/cmd/flux/get_auto_imagerepository.go b/cmd/flux/get_auto_imagerepository.go index 37a88760..ef36b2e4 100644 --- a/cmd/flux/get_auto_imagerepository.go +++ b/cmd/flux/get_auto_imagerepository.go @@ -37,7 +37,8 @@ var getImageRepositoryCmd = &cobra.Command{ flux get auto image-repository --all-namespaces `, RunE: getCommand{ - list: imageRepositoryListAdapter{&imagev1.ImageRepositoryList{}}, + names: imageRepositoryNames, + list: imageRepositoryListAdapter{&imagev1.ImageRepositoryList{}}, }.run, } diff --git a/cmd/flux/get_auto_imageupdateauto.go b/cmd/flux/get_auto_imageupdateauto.go index 2a8e27a1..7e6fd826 100644 --- a/cmd/flux/get_auto_imageupdateauto.go +++ b/cmd/flux/get_auto_imageupdateauto.go @@ -37,7 +37,8 @@ var getImageUpdateCmd = &cobra.Command{ flux get auto image-update --all-namespaces `, RunE: getCommand{ - list: &imageUpdateAutomationListAdapter{&autov1.ImageUpdateAutomationList{}}, + names: imageUpdateAutomationNames, + list: &imageUpdateAutomationListAdapter{&autov1.ImageUpdateAutomationList{}}, }.run, } diff --git a/cmd/flux/object.go b/cmd/flux/object.go index 49796b86..7a0f4b52 100644 --- a/cmd/flux/object.go +++ b/cmd/flux/object.go @@ -20,6 +20,15 @@ import ( "k8s.io/apimachinery/pkg/runtime" ) +// Most commands need one or both of the kind (e.g., +// `"ImageRepository"`) and a human-palatable name for the kind (e.g., +// `"image repository"`), to be interpolated into output. It's +// convenient to package these up ahead of time, then the command +// implementation can pick whichever it wants to use. +type names struct { + kind, humanKind string +} + // adapter is an interface for a wrapper or alias from which we can // get a controller-runtime deserialisable value. This is used so that // you can wrap an API type to give it other useful methods, but still diff --git a/cmd/flux/reconcile.go b/cmd/flux/reconcile.go index e103cbcb..7355469a 100644 --- a/cmd/flux/reconcile.go +++ b/cmd/flux/reconcile.go @@ -44,8 +44,8 @@ func init() { } type reconcileCommand struct { - humanKind string - adapter reconcilable + names + object reconcilable } type reconcilable interface { @@ -65,7 +65,7 @@ type reconcilable interface { func (reconcile reconcileCommand) run(cmd *cobra.Command, args []string) error { if len(args) < 1 { - return fmt.Errorf("%s name is required", reconcile.humanKind) + return fmt.Errorf("%s name is required", reconcile.kind) } name := args[0] @@ -82,33 +82,33 @@ func (reconcile reconcileCommand) run(cmd *cobra.Command, args []string) error { Name: name, } - err = kubeClient.Get(ctx, namespacedName, reconcile.adapter.asRuntimeObject()) + err = kubeClient.Get(ctx, namespacedName, reconcile.object.asRuntimeObject()) if err != nil { return err } - if reconcile.adapter.isSuspended() { + if reconcile.object.isSuspended() { return fmt.Errorf("resource is suspended") } - logger.Actionf("annotating %s %s in %s namespace", reconcile.humanKind, name, namespace) - if err := requestReconciliation(ctx, kubeClient, namespacedName, reconcile.adapter); err != nil { + logger.Actionf("annotating %s %s in %s namespace", reconcile.kind, name, namespace) + if err := requestReconciliation(ctx, kubeClient, namespacedName, reconcile.object); err != nil { return err } - logger.Successf("%s annotated", reconcile.humanKind) + logger.Successf("%s annotated", reconcile.kind) - lastHandledReconcileAt := reconcile.adapter.lastHandledReconcileRequest() - logger.Waitingf("waiting for %s reconciliation", reconcile.humanKind) + lastHandledReconcileAt := reconcile.object.lastHandledReconcileRequest() + logger.Waitingf("waiting for %s reconciliation", reconcile.kind) if err := wait.PollImmediate(pollInterval, timeout, - reconciliationHandled(ctx, kubeClient, namespacedName, reconcile.adapter, lastHandledReconcileAt)); err != nil { + reconciliationHandled(ctx, kubeClient, namespacedName, reconcile.object, lastHandledReconcileAt)); err != nil { return err } - logger.Successf("%s reconciliation completed", reconcile.humanKind) + logger.Successf("%s reconciliation completed", reconcile.kind) - if apimeta.IsStatusConditionFalse(*reconcile.adapter.GetStatusConditions(), meta.ReadyCondition) { - return fmt.Errorf("%s reconciliation failed", reconcile.humanKind) + if apimeta.IsStatusConditionFalse(*reconcile.object.GetStatusConditions(), meta.ReadyCondition) { + return fmt.Errorf("%s reconciliation failed", reconcile.kind) } - logger.Successf(reconcile.adapter.successMessage()) + logger.Successf(reconcile.object.successMessage()) return nil } diff --git a/cmd/flux/reconcile_auto_imagerepository.go b/cmd/flux/reconcile_auto_imagerepository.go index b487e68a..d23aaacb 100644 --- a/cmd/flux/reconcile_auto_imagerepository.go +++ b/cmd/flux/reconcile_auto_imagerepository.go @@ -32,8 +32,8 @@ var reconcileImageRepositoryCmd = &cobra.Command{ flux reconcile auto image-repository alpine `, RunE: reconcileCommand{ - humanKind: imagev1.ImageRepositoryKind, - adapter: imageRepositoryAdapter{&imagev1.ImageRepository{}}, + names: imageRepositoryNames, + object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, }.run, } diff --git a/cmd/flux/reconcile_auto_imageupdateauto.go b/cmd/flux/reconcile_auto_imageupdateauto.go index 4cc3ab62..a441b673 100644 --- a/cmd/flux/reconcile_auto_imageupdateauto.go +++ b/cmd/flux/reconcile_auto_imageupdateauto.go @@ -34,8 +34,8 @@ var reconcileImageUpdateCmd = &cobra.Command{ flux reconcile auto image-update latest-images `, RunE: reconcileCommand{ - humanKind: autov1.ImageUpdateAutomationKind, - adapter: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, + names: imageUpdateAutomationNames, + object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, }.run, } diff --git a/cmd/flux/resume.go b/cmd/flux/resume.go index 8fd53674..0796b692 100644 --- a/cmd/flux/resume.go +++ b/cmd/flux/resume.go @@ -44,9 +44,8 @@ type resumable interface { } type resumeCommand struct { - kind string - humanKind string - object resumable + names + object resumable } func (resume resumeCommand) run(cmd *cobra.Command, args []string) error { diff --git a/cmd/flux/resume_auto_imagerepository.go b/cmd/flux/resume_auto_imagerepository.go index c8be1271..ff50c230 100644 --- a/cmd/flux/resume_auto_imagerepository.go +++ b/cmd/flux/resume_auto_imagerepository.go @@ -30,9 +30,8 @@ var resumeImageRepositoryCmd = &cobra.Command{ flux resume auto image-repository alpine `, RunE: resumeCommand{ - kind: imagev1.ImageRepositoryKind, - humanKind: "image repository", - object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, + names: imageRepositoryNames, + object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, }.run, } diff --git a/cmd/flux/resume_auto_imageupdateauto.go b/cmd/flux/resume_auto_imageupdateauto.go index 8836d74f..e056caba 100644 --- a/cmd/flux/resume_auto_imageupdateauto.go +++ b/cmd/flux/resume_auto_imageupdateauto.go @@ -30,9 +30,8 @@ var resumeImageUpdateCmd = &cobra.Command{ flux resume auto image-update latest-images `, RunE: resumeCommand{ - kind: autov1.ImageUpdateAutomationKind, - humanKind: "image update automation", - object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, + names: imageUpdateAutomationNames, + object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, }.run, } diff --git a/cmd/flux/suspend.go b/cmd/flux/suspend.go index e9d46b24..96bfe701 100644 --- a/cmd/flux/suspend.go +++ b/cmd/flux/suspend.go @@ -43,8 +43,8 @@ type suspendable interface { } type suspendCommand struct { - object suspendable - humanKind string + names + object suspendable } func (suspend suspendCommand) run(cmd *cobra.Command, args []string) error { diff --git a/cmd/flux/suspend_auto_imagerepository.go b/cmd/flux/suspend_auto_imagerepository.go index c5a45830..879308ea 100644 --- a/cmd/flux/suspend_auto_imagerepository.go +++ b/cmd/flux/suspend_auto_imagerepository.go @@ -30,8 +30,8 @@ var suspendImageRepositoryCmd = &cobra.Command{ flux suspend auto image-repository alpine `, RunE: suspendCommand{ - humanKind: "image repository", - object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, + names: imageRepositoryNames, + object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, }.run, } diff --git a/cmd/flux/suspend_auto_imageupdateauto.go b/cmd/flux/suspend_auto_imageupdateauto.go index 474227d7..49a2a17e 100644 --- a/cmd/flux/suspend_auto_imageupdateauto.go +++ b/cmd/flux/suspend_auto_imageupdateauto.go @@ -30,8 +30,8 @@ var suspendImageUpdateCmd = &cobra.Command{ flux suspend auto image-update latest-images `, RunE: suspendCommand{ - humanKind: "image update automation", - object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, + names: imageUpdateAutomationNames, + object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, }.run, }