Factor out upsert and upsertAndWait

It's a common pattern in the create commands to construct a value,
then (if not exporting it) upsert it and wait for it to
reconcile. This commit factors `upsert`, which does the update/insert
bit, and `upsertAndWait`, which does the whole thing.

Since these output messages, they are methods of `apiType` (previously
`names`), so that they have access to the name of the kind they are
operating on.

Signed-off-by: Michael Bridgen <michael@weave.works>
pull/538/head
Michael Bridgen 4 years ago
parent 3b9b2cbe9f
commit 0e35c209d9

@ -17,13 +17,19 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"strings" "strings"
"time" "time"
"github.com/spf13/cobra"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/validation" "k8s.io/apimachinery/pkg/util/validation"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/spf13/cobra" "github.com/fluxcd/flux2/internal/utils"
) )
var createCmd = &cobra.Command{ var createCmd = &cobra.Command{
@ -46,6 +52,78 @@ func init() {
rootCmd.AddCommand(createCmd) rootCmd.AddCommand(createCmd)
} }
// upsertable is an interface for values that can be used in `upsert`.
type upsertable interface {
adapter
named
}
// upsert updates or inserts an object. Instead of providing the
// object itself, you provide a named (as in Name and Namespace)
// template value, and a mutate function which sets the values you
// want to update. The mutate function is nullary -- you mutate a
// value in the closure, e.g., by doing this:
//
// var existing Value
// existing.Name = name
// existing.Namespace = ns
// upsert(ctx, client, valueAdapter{&value}, func() error {
// value.Spec = onePreparedEarlier
// })
func (names apiType) upsert(ctx context.Context, kubeClient client.Client, object upsertable, mutate func() error) (types.NamespacedName, error) {
nsname := types.NamespacedName{
Namespace: object.GetNamespace(),
Name: object.GetName(),
}
op, err := controllerutil.CreateOrUpdate(ctx, kubeClient, object.asRuntimeObject(), mutate)
if err != nil {
return nsname, err
}
switch op {
case controllerutil.OperationResultCreated:
logger.Successf("%s created", names.kind)
case controllerutil.OperationResultUpdated:
logger.Successf("%s updated", names.kind)
}
return nsname, nil
}
type upsertWaitable interface {
upsertable
statusable
}
// upsertAndWait encodes the pattern of creating or updating a
// resource, then waiting for it to reconcile. See the note on
// `upsert` for how to work with the `mutate` argument.
func (names apiType) upsertAndWait(object upsertWaitable, mutate func() error) error {
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
kubeClient, err := utils.KubeClient(kubeconfig, kubecontext) // NB globals
if err != nil {
return err
}
logger.Generatef("generating %s", names.kind)
logger.Actionf("applying %s", names.kind)
namespacedName, err := imageRepositoryType.upsert(ctx, kubeClient, object, mutate)
if err != nil {
return err
}
logger.Waitingf("waiting for %s reconciliation", names.kind)
if err := wait.PollImmediate(pollInterval, timeout,
isReady(ctx, kubeClient, namespacedName, object)); err != nil {
return err
}
logger.Successf("%s reconciliation completed", names.kind)
return nil
}
func parseLabels() (map[string]string, error) { func parseLabels() (map[string]string, error) {
result := make(map[string]string) result := make(map[string]string)
for _, label := range labels { for _, label := range labels {

@ -17,18 +17,12 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/fluxcd/flux2/internal/utils"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1" imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
) )
@ -105,57 +99,12 @@ func createImagePolicyRun(cmd *cobra.Command, args []string) error {
return printExport(exportImagePolicy(&policy)) return printExport(exportImagePolicy(&policy))
} }
// I don't need these until attempting to upsert the object, but
// for consistency with other create commands, the following are
// given a chance to error out before reporting any progress.
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
kubeClient, err := utils.KubeClient(kubeconfig, kubecontext)
if err != nil {
return err
}
logger.Generatef("generating ImagePolicy")
logger.Actionf("applying ImagePolicy")
namespacedName, err := upsertImagePolicy(ctx, kubeClient, &policy)
if err != nil {
return err
}
logger.Waitingf("waiting for ImagePolicy reconciliation")
if err := wait.PollImmediate(pollInterval, timeout,
isReady(ctx, kubeClient, namespacedName, imagePolicyAdapter{&policy})); err != nil {
return err
}
logger.Successf("ImagePolicy reconciliation completed")
return nil
}
func upsertImagePolicy(ctx context.Context, kubeClient client.Client, policy *imagev1.ImagePolicy) (types.NamespacedName, error) {
nsname := types.NamespacedName{
Namespace: policy.GetNamespace(),
Name: policy.GetName(),
}
var existing imagev1.ImagePolicy var existing imagev1.ImagePolicy
existing.SetName(nsname.Name) copyName(&existing, &policy)
existing.SetNamespace(nsname.Namespace) err = imagePolicyType.upsertAndWait(imagePolicyAdapter{&existing}, func() error {
op, err := controllerutil.CreateOrUpdate(ctx, kubeClient, &existing, func() error {
existing.Spec = policy.Spec existing.Spec = policy.Spec
existing.SetLabels(policy.Labels) existing.SetLabels(policy.Labels)
return nil return nil
}) })
if err != nil { return err
return nsname, err
}
switch op {
case controllerutil.OperationResultCreated:
logger.Successf("ImagePolicy created")
case controllerutil.OperationResultUpdated:
logger.Successf("ImagePolicy updated")
}
return nsname, nil
} }

@ -17,7 +17,6 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"time" "time"
@ -25,12 +24,7 @@ import (
"github.com/spf13/cobra" "github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/fluxcd/flux2/internal/utils"
imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1" imagev1 "github.com/fluxcd/image-reflector-controller/api/v1alpha1"
) )
@ -104,57 +98,13 @@ func createImageRepositoryRun(cmd *cobra.Command, args []string) error {
return printExport(exportImageRepository(&repo)) return printExport(exportImageRepository(&repo))
} }
// I don't need these until attempting to upsert the object, but // a temp value for use with the rest
// for consistency with other create commands, the following are
// given a chance to error out before reporting any progress.
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
kubeClient, err := utils.KubeClient(kubeconfig, kubecontext)
if err != nil {
return err
}
logger.Generatef("generating ImageRepository")
logger.Actionf("applying ImageRepository")
namespacedName, err := upsertImageRepository(ctx, kubeClient, &repo)
if err != nil {
return err
}
logger.Waitingf("waiting for ImageRepository reconciliation")
if err := wait.PollImmediate(pollInterval, timeout,
isReady(ctx, kubeClient, namespacedName, imageRepositoryAdapter{&repo})); err != nil {
return err
}
logger.Successf("ImageRepository reconciliation completed")
return nil
}
func upsertImageRepository(ctx context.Context, kubeClient client.Client, repo *imagev1.ImageRepository) (types.NamespacedName, error) {
nsname := types.NamespacedName{
Namespace: repo.GetNamespace(),
Name: repo.GetName(),
}
var existing imagev1.ImageRepository var existing imagev1.ImageRepository
existing.SetName(nsname.Name) copyName(&existing, &repo)
existing.SetNamespace(nsname.Namespace) err = imageRepositoryType.upsertAndWait(imageRepositoryAdapter{&existing}, func() error {
op, err := controllerutil.CreateOrUpdate(ctx, kubeClient, &existing, func() error {
existing.Spec = repo.Spec existing.Spec = repo.Spec
existing.Labels = repo.Labels existing.Labels = repo.Labels
return nil return nil
}) })
if err != nil { return err
return nsname, err
}
switch op {
case controllerutil.OperationResultCreated:
logger.Successf("ImageRepository created")
case controllerutil.OperationResultUpdated:
logger.Successf("ImageRepository updated")
}
return nsname, nil
} }

@ -17,18 +17,12 @@ limitations under the License.
package main package main
import ( import (
"context"
"fmt" "fmt"
"github.com/spf13/cobra" "github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1" corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
"k8s.io/apimachinery/pkg/util/wait"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"github.com/fluxcd/flux2/internal/utils"
autov1 "github.com/fluxcd/image-automation-controller/api/v1alpha1" autov1 "github.com/fluxcd/image-automation-controller/api/v1alpha1"
) )
@ -108,57 +102,12 @@ func createImageUpdateRun(cmd *cobra.Command, args []string) error {
return printExport(exportImageUpdate(&update)) return printExport(exportImageUpdate(&update))
} }
// I don't need these until attempting to upsert the object, but
// for consistency with other create commands, the following are
// given a chance to error out before reporting any progress.
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
kubeClient, err := utils.KubeClient(kubeconfig, kubecontext)
if err != nil {
return err
}
logger.Generatef("generating ImageUpdateAutomation")
logger.Actionf("applying ImageUpdateAutomation")
namespacedName, err := upsertImageUpdateAutomation(ctx, kubeClient, &update)
if err != nil {
return err
}
logger.Waitingf("waiting for ImageUpdateAutomation reconciliation")
if err := wait.PollImmediate(pollInterval, timeout,
isReady(ctx, kubeClient, namespacedName, imageUpdateAutomationAdapter{&update})); err != nil {
return err
}
logger.Successf("ImageUpdateAutomation reconciliation completed")
return nil
}
func upsertImageUpdateAutomation(ctx context.Context, kubeClient client.Client, update *autov1.ImageUpdateAutomation) (types.NamespacedName, error) {
nsname := types.NamespacedName{
Namespace: update.GetNamespace(),
Name: update.GetName(),
}
var existing autov1.ImageUpdateAutomation var existing autov1.ImageUpdateAutomation
existing.SetName(nsname.Name) copyName(&existing, &update)
existing.SetNamespace(nsname.Namespace) err = imageUpdateAutomationType.upsertAndWait(imageUpdateAutomationAdapter{&existing}, func() error {
op, err := controllerutil.CreateOrUpdate(ctx, kubeClient, &existing, func() error {
existing.Spec = update.Spec existing.Spec = update.Spec
existing.Labels = update.Labels existing.Labels = update.Labels
return nil return nil
}) })
if err != nil { return err
return nsname, err
}
switch op {
case controllerutil.OperationResultCreated:
logger.Successf("ImageUpdateAutomation created")
case controllerutil.OperationResultUpdated:
logger.Successf("ImageUpdateAutomation updated")
}
return nsname, nil
} }

@ -45,7 +45,7 @@ func init() {
} }
type deleteCommand struct { type deleteCommand struct {
names apiType
object adapter // for getting the value, and later deleting it object adapter // for getting the value, and later deleting it
} }

@ -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{
names: imagePolicyNames, apiType: imagePolicyType,
object: 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{
names: imageRepositoryNames, apiType: imageRepositoryType,
object: 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{
names: imageUpdateAutomationNames, apiType: imageUpdateAutomationType,
object: universalAdapter{&autov1.ImageUpdateAutomation{}}, object: universalAdapter{&autov1.ImageUpdateAutomation{}},
}.run, }.run,
} }

@ -60,11 +60,6 @@ func statusAndMessage(conditions []metav1.Condition) (string, string) {
return string(metav1.ConditionFalse), "waiting to be reconciled" return string(metav1.ConditionFalse), "waiting to be reconciled"
} }
type named interface {
GetName() string
GetNamespace() string
}
func nameColumns(item named, includeNamespace bool) []string { func nameColumns(item named, includeNamespace bool) []string {
if includeNamespace { if includeNamespace {
return []string{item.GetNamespace(), item.GetName()} return []string{item.GetNamespace(), item.GetName()}
@ -75,7 +70,7 @@ func nameColumns(item named, includeNamespace bool) []string {
var namespaceHeader = []string{"Namespace"} var namespaceHeader = []string{"Namespace"}
type getCommand struct { type getCommand struct {
names apiType
list summarisable list summarisable
} }

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

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

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

@ -29,7 +29,7 @@ import (
// imagev1.ImageRepository // imagev1.ImageRepository
var imageRepositoryNames = names{ var imageRepositoryType = apiType{
kind: imagev1.ImageRepositoryKind, kind: imagev1.ImageRepositoryKind,
humanKind: "image repository", humanKind: "image repository",
} }
@ -58,7 +58,7 @@ func (a imageRepositoryListAdapter) len() int {
// imagev1.ImagePolicy // imagev1.ImagePolicy
var imagePolicyNames = names{ var imagePolicyType = apiType{
kind: imagev1.ImagePolicyKind, kind: imagev1.ImagePolicyKind,
humanKind: "image policy", humanKind: "image policy",
} }
@ -87,7 +87,7 @@ func (a imagePolicyListAdapter) len() int {
// autov1.ImageUpdateAutomation // autov1.ImageUpdateAutomation
var imageUpdateAutomationNames = names{ var imageUpdateAutomationType = apiType{
kind: autov1.ImageUpdateAutomationKind, kind: autov1.ImageUpdateAutomationKind,
humanKind: "image update automation", humanKind: "image update automation",
} }

@ -25,7 +25,7 @@ import (
// `"image repository"`), to be interpolated into output. It's // `"image repository"`), to be interpolated into output. It's
// convenient to package these up ahead of time, then the command // convenient to package these up ahead of time, then the command
// implementation can pick whichever it wants to use. // implementation can pick whichever it wants to use.
type names struct { type apiType struct {
kind, humanKind string kind, humanKind string
} }
@ -47,3 +47,18 @@ type universalAdapter struct {
func (c universalAdapter) asRuntimeObject() runtime.Object { func (c universalAdapter) asRuntimeObject() runtime.Object {
return c.obj return c.obj
} }
// named is for adapters that have Name and Namespace fields, which
// are sometimes handy to get hold of. ObjectMeta implements these, so
// they shouldn't need any extra work.
type named interface {
GetName() string
GetNamespace() string
SetName(string)
SetNamespace(string)
}
func copyName(target, source named) {
target.SetName(source.GetName())
target.SetNamespace(source.GetNamespace())
}

@ -44,7 +44,7 @@ func init() {
} }
type reconcileCommand struct { type reconcileCommand struct {
names apiType
object reconcilable object reconcilable
} }

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

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

@ -45,7 +45,7 @@ type resumable interface {
} }
type resumeCommand struct { type resumeCommand struct {
names apiType
object resumable object resumable
} }

@ -30,8 +30,8 @@ var resumeImageRepositoryCmd = &cobra.Command{
flux resume image repository alpine flux resume image repository alpine
`, `,
RunE: resumeCommand{ RunE: resumeCommand{
names: imageRepositoryNames, apiType: imageRepositoryType,
object: imageRepositoryAdapter{&imagev1.ImageRepository{}}, object: imageRepositoryAdapter{&imagev1.ImageRepository{}},
}.run, }.run,
} }

@ -30,8 +30,8 @@ var resumeImageUpdateCmd = &cobra.Command{
flux resume image update latest-images flux resume image update latest-images
`, `,
RunE: resumeCommand{ RunE: resumeCommand{
names: imageUpdateAutomationNames, apiType: imageUpdateAutomationType,
object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}}, object: imageUpdateAutomationAdapter{&autov1.ImageUpdateAutomation{}},
}.run, }.run,
} }

@ -43,7 +43,7 @@ type suspendable interface {
} }
type suspendCommand struct { type suspendCommand struct {
names apiType
object suspendable object suspendable
} }

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

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

Loading…
Cancel
Save