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.
// 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
}

@ -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
}

@ -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,
}

@ -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,
}

@ -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,
}

@ -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
}

@ -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,
}

@ -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,
}

@ -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,
}

@ -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

@ -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
}

@ -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,
}

@ -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,
}

@ -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 {

@ -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,
}

@ -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,
}

@ -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 {

@ -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,
}

@ -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,
}

Loading…
Cancel
Save