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 <michael@weave.works>
pull/538/head
Michael Bridgen 4 years ago
parent d55d185044
commit 22a5ac7f0f

@ -28,6 +28,12 @@ import (
// it's used in at least a couple of commands. // it's used in at least a couple of commands.
// imagev1.ImageRepository // imagev1.ImageRepository
var imageRepositoryNames = names{
kind: imagev1.ImageRepositoryKind,
humanKind: "image repository",
}
type imageRepositoryAdapter struct { type imageRepositoryAdapter struct {
*imagev1.ImageRepository *imagev1.ImageRepository
} }
@ -52,6 +58,11 @@ func (a imageRepositoryListAdapter) len() int {
// imagev1.ImagePolicy // imagev1.ImagePolicy
var imagePolicyNames = names{
kind: imagev1.ImagePolicyKind,
humanKind: "image policy",
}
type imagePolicyAdapter struct { type imagePolicyAdapter struct {
*imagev1.ImagePolicy *imagev1.ImagePolicy
} }
@ -76,6 +87,11 @@ func (a imagePolicyListAdapter) len() int {
// autov1.ImageUpdateAutomation // autov1.ImageUpdateAutomation
var imageUpdateAutomationNames = names{
kind: autov1.ImageUpdateAutomationKind,
humanKind: "image update automation",
}
type imageUpdateAutomationAdapter struct { type imageUpdateAutomationAdapter struct {
*autov1.ImageUpdateAutomation *autov1.ImageUpdateAutomation
} }

@ -45,8 +45,8 @@ func init() {
} }
type deleteCommand struct { type deleteCommand struct {
humanKind string // the kind being deleted, lowercase and spaced e.g., "image policy" names
adapter adapter // for getting the value, and later deleting it object adapter // for getting the value, and later deleting it
} }
func (del deleteCommand) run(cmd *cobra.Command, args []string) error { 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, Name: name,
} }
err = kubeClient.Get(ctx, namespacedName, del.adapter.asRuntimeObject()) err = kubeClient.Get(ctx, namespacedName, del.object.asRuntimeObject())
if err != nil { if err != nil {
return err 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) 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 { if err != nil {
return err return err
} }

@ -30,8 +30,8 @@ var deleteImagePolicyCmd = &cobra.Command{
flux delete auto image-policy alpine3.x flux delete auto image-policy alpine3.x
`, `,
RunE: deleteCommand{ RunE: deleteCommand{
humanKind: "image policy", names: imagePolicyNames,
adapter: universalAdapter{&imagev1.ImagePolicy{}}, object: universalAdapter{&imagev1.ImagePolicy{}},
}.run, }.run,
} }

@ -30,8 +30,8 @@ var deleteImageRepositoryCmd = &cobra.Command{
flux delete auto image-repository alpine flux delete auto image-repository alpine
`, `,
RunE: deleteCommand{ RunE: deleteCommand{
humanKind: "image repository", names: imageRepositoryNames,
adapter: universalAdapter{&imagev1.ImageRepository{}}, object: universalAdapter{&imagev1.ImageRepository{}},
}.run, }.run,
} }

@ -30,8 +30,8 @@ var deleteImageUpdateCmd = &cobra.Command{
flux delete auto image-update latest-images flux delete auto image-update latest-images
`, `,
RunE: deleteCommand{ RunE: deleteCommand{
humanKind: "image update automation", names: imageUpdateAutomationNames,
adapter: universalAdapter{&autov1.ImageUpdateAutomation{}}, object: universalAdapter{&autov1.ImageUpdateAutomation{}},
}.run, }.run,
} }

@ -75,8 +75,8 @@ func nameColumns(item named, includeNamespace bool) []string {
var namespaceHeader = []string{"Namespace"} var namespaceHeader = []string{"Namespace"}
type getCommand struct { type getCommand struct {
headers []string names
list summarisable list summarisable
} }
func (get getCommand) run(cmd *cobra.Command, args []string) error { 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 { 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 return nil
} }

@ -33,7 +33,8 @@ var getImagePolicyCmd = &cobra.Command{
flux get auto image-policy --all-namespaces flux get auto image-policy --all-namespaces
`, `,
RunE: getCommand{ RunE: getCommand{
list: &imagePolicyListAdapter{&imagev1.ImagePolicyList{}}, names: imagePolicyNames,
list: &imagePolicyListAdapter{&imagev1.ImagePolicyList{}},
}.run, }.run,
} }

@ -37,7 +37,8 @@ var getImageRepositoryCmd = &cobra.Command{
flux get auto image-repository --all-namespaces flux get auto image-repository --all-namespaces
`, `,
RunE: getCommand{ RunE: getCommand{
list: imageRepositoryListAdapter{&imagev1.ImageRepositoryList{}}, names: imageRepositoryNames,
list: imageRepositoryListAdapter{&imagev1.ImageRepositoryList{}},
}.run, }.run,
} }

@ -37,7 +37,8 @@ var getImageUpdateCmd = &cobra.Command{
flux get auto image-update --all-namespaces flux get auto image-update --all-namespaces
`, `,
RunE: getCommand{ RunE: getCommand{
list: &imageUpdateAutomationListAdapter{&autov1.ImageUpdateAutomationList{}}, names: imageUpdateAutomationNames,
list: &imageUpdateAutomationListAdapter{&autov1.ImageUpdateAutomationList{}},
}.run, }.run,
} }

@ -20,6 +20,15 @@ import (
"k8s.io/apimachinery/pkg/runtime" "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 // adapter is an interface for a wrapper or alias from which we can
// get a controller-runtime deserialisable value. This is used so that // 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 // you can wrap an API type to give it other useful methods, but still

@ -44,8 +44,8 @@ func init() {
} }
type reconcileCommand struct { type reconcileCommand struct {
humanKind string names
adapter reconcilable object reconcilable
} }
type reconcilable interface { type reconcilable interface {
@ -65,7 +65,7 @@ type reconcilable interface {
func (reconcile reconcileCommand) run(cmd *cobra.Command, args []string) error { func (reconcile reconcileCommand) run(cmd *cobra.Command, args []string) error {
if len(args) < 1 { 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] name := args[0]
@ -82,33 +82,33 @@ func (reconcile reconcileCommand) run(cmd *cobra.Command, args []string) error {
Name: name, Name: name,
} }
err = kubeClient.Get(ctx, namespacedName, reconcile.adapter.asRuntimeObject()) err = kubeClient.Get(ctx, namespacedName, reconcile.object.asRuntimeObject())
if err != nil { if err != nil {
return err return err
} }
if reconcile.adapter.isSuspended() { if reconcile.object.isSuspended() {
return fmt.Errorf("resource is suspended") return fmt.Errorf("resource is suspended")
} }
logger.Actionf("annotating %s %s in %s namespace", reconcile.humanKind, name, namespace) logger.Actionf("annotating %s %s in %s namespace", reconcile.kind, name, namespace)
if err := requestReconciliation(ctx, kubeClient, namespacedName, reconcile.adapter); err != nil { if err := requestReconciliation(ctx, kubeClient, namespacedName, reconcile.object); err != nil {
return err return err
} }
logger.Successf("%s annotated", reconcile.humanKind) logger.Successf("%s annotated", reconcile.kind)
lastHandledReconcileAt := reconcile.adapter.lastHandledReconcileRequest() lastHandledReconcileAt := reconcile.object.lastHandledReconcileRequest()
logger.Waitingf("waiting for %s reconciliation", reconcile.humanKind) logger.Waitingf("waiting for %s reconciliation", reconcile.kind)
if err := wait.PollImmediate(pollInterval, timeout, 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 return err
} }
logger.Successf("%s reconciliation completed", reconcile.humanKind) logger.Successf("%s reconciliation completed", reconcile.kind)
if apimeta.IsStatusConditionFalse(*reconcile.adapter.GetStatusConditions(), meta.ReadyCondition) { if apimeta.IsStatusConditionFalse(*reconcile.object.GetStatusConditions(), meta.ReadyCondition) {
return fmt.Errorf("%s reconciliation failed", reconcile.humanKind) return fmt.Errorf("%s reconciliation failed", reconcile.kind)
} }
logger.Successf(reconcile.adapter.successMessage()) logger.Successf(reconcile.object.successMessage())
return nil return nil
} }

@ -32,8 +32,8 @@ var reconcileImageRepositoryCmd = &cobra.Command{
flux reconcile auto image-repository alpine flux reconcile auto image-repository alpine
`, `,
RunE: reconcileCommand{ RunE: reconcileCommand{
humanKind: imagev1.ImageRepositoryKind, names: imageRepositoryNames,
adapter: imageRepositoryAdapter{&imagev1.ImageRepository{}}, object: imageRepositoryAdapter{&imagev1.ImageRepository{}},
}.run, }.run,
} }

@ -34,8 +34,8 @@ var reconcileImageUpdateCmd = &cobra.Command{
flux reconcile auto image-update latest-images flux reconcile auto image-update latest-images
`, `,
RunE: reconcileCommand{ RunE: reconcileCommand{
humanKind: autov1.ImageUpdateAutomationKind, names: imageUpdateAutomationNames,
adapter: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}},
}.run, }.run,
} }

@ -44,9 +44,8 @@ type resumable interface {
} }
type resumeCommand struct { type resumeCommand struct {
kind string names
humanKind string object resumable
object resumable
} }
func (resume resumeCommand) run(cmd *cobra.Command, args []string) error { func (resume resumeCommand) run(cmd *cobra.Command, args []string) error {

@ -30,9 +30,8 @@ var resumeImageRepositoryCmd = &cobra.Command{
flux resume auto image-repository alpine flux resume auto image-repository alpine
`, `,
RunE: resumeCommand{ RunE: resumeCommand{
kind: imagev1.ImageRepositoryKind, names: imageRepositoryNames,
humanKind: "image repository", object: imageRepositoryAdapter{&imagev1.ImageRepository{}},
object: imageRepositoryAdapter{&imagev1.ImageRepository{}},
}.run, }.run,
} }

@ -30,9 +30,8 @@ var resumeImageUpdateCmd = &cobra.Command{
flux resume auto image-update latest-images flux resume auto image-update latest-images
`, `,
RunE: resumeCommand{ RunE: resumeCommand{
kind: autov1.ImageUpdateAutomationKind, names: imageUpdateAutomationNames,
humanKind: "image update automation", object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}},
object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}},
}.run, }.run,
} }

@ -43,8 +43,8 @@ type suspendable interface {
} }
type suspendCommand struct { type suspendCommand struct {
object suspendable names
humanKind string object suspendable
} }
func (suspend suspendCommand) run(cmd *cobra.Command, args []string) error { func (suspend suspendCommand) run(cmd *cobra.Command, args []string) error {

@ -30,8 +30,8 @@ var suspendImageRepositoryCmd = &cobra.Command{
flux suspend auto image-repository alpine flux suspend auto image-repository alpine
`, `,
RunE: suspendCommand{ RunE: suspendCommand{
humanKind: "image repository", names: imageRepositoryNames,
object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, object: imageRepositoryAdapter{&imagev1.ImageRepository{}},
}.run, }.run,
} }

@ -30,8 +30,8 @@ var suspendImageUpdateCmd = &cobra.Command{
flux suspend auto image-update latest-images flux suspend auto image-update latest-images
`, `,
RunE: suspendCommand{ RunE: suspendCommand{
humanKind: "image update automation", names: imageUpdateAutomationNames,
object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}},
}.run, }.run,
} }

Loading…
Cancel
Save