fix: Use server-side apply for object create during update

Signed-off-by: George Jenkins <gvjenkins@gmail.com>
(cherry picked from commit 18616e6ce9)
pull/31646/head
George Jenkins 10 months ago committed by Matt Farina
parent 861adc2f4a
commit 9dfe3b35ec
No known key found for this signature in database
GPG Key ID: 92C44A3D421FF7F9

@ -419,6 +419,7 @@ func (i *Install) RunWithContext(ctx context.Context, ch ci.Charter, vals map[st
if err != nil { if err != nil {
return nil, err return nil, err
} }
if _, err := i.cfg.KubeClient.Create( if _, err := i.cfg.KubeClient.Create(
resourceList, resourceList,
kube.ClientCreateOptionServerSideApply(i.ServerSideApply, false)); err != nil && !apierrors.IsAlreadyExists(err) { kube.ClientCreateOptionServerSideApply(i.ServerSideApply, false)); err != nil && !apierrors.IsAlreadyExists(err) {

@ -114,6 +114,9 @@ const (
FieldValidationDirectiveStrict FieldValidationDirective = "Strict" FieldValidationDirectiveStrict FieldValidationDirective = "Strict"
) )
type CreateApplyFunc func(target *resource.Info) error
type UpdateApplyFunc func(original, target *resource.Info) error
func init() { func init() {
// Add CRDs to the scheme. They are missing by default. // Add CRDs to the scheme. They are missing by default.
if err := apiextv1.AddToScheme(scheme.Scheme); err != nil { if err := apiextv1.AddToScheme(scheme.Scheme); err != nil {
@ -268,28 +271,16 @@ func ClientCreateOptionFieldValidationDirective(fieldValidationDirective FieldVa
} }
} }
// Create creates Kubernetes resources specified in the resource list. func (c *Client) makeCreateApplyFunc(serverSideApply, forceConflicts, dryRun bool, fieldValidationDirective FieldValidationDirective) CreateApplyFunc {
func (c *Client) Create(resources ResourceList, options ...ClientCreateOption) (*Result, error) { if serverSideApply {
c.Logger().Debug("creating resource(s)", "resources", len(resources)) c.Logger().Debug(
"using server-side apply for resource creation",
createOptions := clientCreateOptions{ slog.Bool("forceConflicts", forceConflicts),
serverSideApply: true, // Default to server-side apply slog.Bool("dryRun", dryRun),
fieldValidationDirective: FieldValidationDirectiveStrict, slog.String("fieldValidationDirective", string(fieldValidationDirective)))
}
errs := make([]error, 0, len(options))
for _, o := range options {
errs = append(errs, o(&createOptions))
}
if err := errors.Join(errs...); err != nil {
return nil, fmt.Errorf("invalid client create option(s): %w", err)
}
makeCreateApplyFunc := func() func(target *resource.Info) error {
if createOptions.serverSideApply {
c.Logger().Debug("using server-side apply for resource creation", slog.Bool("forceConflicts", createOptions.forceConflicts), slog.Bool("dryRun", createOptions.dryRun), slog.String("fieldValidationDirective", string(createOptions.fieldValidationDirective)))
return func(target *resource.Info) error { return func(target *resource.Info) error {
err := patchResourceServerSide(target, createOptions.dryRun, createOptions.forceConflicts, createOptions.fieldValidationDirective) err := patchResourceServerSide(target, dryRun, forceConflicts, fieldValidationDirective)
logger := c.Logger().With( logger := c.Logger().With(
slog.String("namespace", target.Namespace), slog.String("namespace", target.Namespace),
@ -308,9 +299,31 @@ func (c *Client) Create(resources ResourceList, options ...ClientCreateOption) (
c.Logger().Debug("using client-side apply for resource creation") c.Logger().Debug("using client-side apply for resource creation")
return createResource return createResource
}
// Create creates Kubernetes resources specified in the resource list.
func (c *Client) Create(resources ResourceList, options ...ClientCreateOption) (*Result, error) {
c.Logger().Debug("creating resource(s)", "resources", len(resources))
createOptions := clientCreateOptions{
serverSideApply: true, // Default to server-side apply
fieldValidationDirective: FieldValidationDirectiveStrict,
} }
if err := perform(resources, makeCreateApplyFunc()); err != nil { errs := make([]error, 0, len(options))
for _, o := range options {
errs = append(errs, o(&createOptions))
}
if err := errors.Join(errs...); err != nil {
return nil, fmt.Errorf("invalid client create option(s): %w", err)
}
createApplyFunc := c.makeCreateApplyFunc(
createOptions.serverSideApply,
createOptions.forceConflicts,
createOptions.dryRun,
createOptions.fieldValidationDirective)
if err := perform(resources, createApplyFunc); err != nil {
return nil, err return nil, err
} }
return &Result{Created: resources}, nil return &Result{Created: resources}, nil
@ -512,7 +525,7 @@ func (c *Client) BuildTable(reader io.Reader, validate bool) (ResourceList, erro
transformRequests) transformRequests)
} }
func (c *Client) update(originals, targets ResourceList, updateApplyFunc UpdateApplyFunc) (*Result, error) { func (c *Client) update(originals, targets ResourceList, createApplyFunc CreateApplyFunc, updateApplyFunc UpdateApplyFunc) (*Result, error) {
updateErrors := []error{} updateErrors := []error{}
res := &Result{} res := &Result{}
@ -686,8 +699,6 @@ func ClientUpdateOptionUpgradeClientSideFieldManager(upgradeClientSideFieldManag
} }
} }
type UpdateApplyFunc func(original, target *resource.Info) error
// Update takes the current list of objects and target list of objects and // Update takes the current list of objects and target list of objects and
// creates resources that don't already exist, updates resources that have been // creates resources that don't already exist, updates resources that have been
// modified in the target configuration, and deletes resources from the current // modified in the target configuration, and deletes resources from the current
@ -723,6 +734,12 @@ func (c *Client) Update(originals, targets ResourceList, options ...ClientUpdate
return &Result{}, fmt.Errorf("invalid operation: cannot use server-side apply and force replace together") return &Result{}, fmt.Errorf("invalid operation: cannot use server-side apply and force replace together")
} }
createApplyFunc := c.makeCreateApplyFunc(
updateOptions.serverSideApply,
updateOptions.forceConflicts,
updateOptions.dryRun,
updateOptions.fieldValidationDirective)
makeUpdateApplyFunc := func() UpdateApplyFunc { makeUpdateApplyFunc := func() UpdateApplyFunc {
if updateOptions.forceReplace { if updateOptions.forceReplace {
c.Logger().Debug( c.Logger().Debug(
@ -783,7 +800,7 @@ func (c *Client) Update(originals, targets ResourceList, options ...ClientUpdate
} }
} }
return c.update(originals, targets, makeUpdateApplyFunc()) return c.update(originals, targets, createApplyFunc, makeUpdateApplyFunc())
} }
// Delete deletes Kubernetes resources specified in the resources list with // Delete deletes Kubernetes resources specified in the resources list with

Loading…
Cancel
Save