-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[deckhouse-controller] Separate deckhouse release reconcile process f…
…rom generic updater (#11129) Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com> Signed-off-by: Mikhail Scherba <mikhail.scherba@flant.com> Signed-off-by: Mikhail Scherba <41360396+miklezzzz@users.noreply.github.com> Co-authored-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com> Co-authored-by: Mikhail Scherba <mikhail.scherba@flant.com> Co-authored-by: Mikhail Scherba <41360396+miklezzzz@users.noreply.github.com>
- Loading branch information
1 parent
bef10bc
commit b8e8409
Showing
85 changed files
with
3,159 additions
and
671 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 0 additions & 22 deletions
22
deckhouse-controller/pkg/apis/deckhouse.io/v1alpha1/annotation.go
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2024 Flant JSC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ctrlutils | ||
|
||
import ( | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
) | ||
|
||
type UpdateOption func(optionsApplier UpdateOptionApplier) | ||
|
||
func (opt UpdateOption) Apply(o UpdateOptionApplier) { | ||
opt(o) | ||
} | ||
|
||
type UpdateOptionApplier interface { | ||
WithOnErrorBackoff(b *wait.Backoff) | ||
WithRetryOnConflictBackoff(b *wait.Backoff) | ||
|
||
withStatusUpdate() | ||
} | ||
|
||
func WithOnErrorBackoff(b *wait.Backoff) UpdateOption { | ||
return func(optionsApplier UpdateOptionApplier) { | ||
optionsApplier.WithOnErrorBackoff(b) | ||
} | ||
} | ||
|
||
func WithRetryOnConflictBackoff(b *wait.Backoff) UpdateOption { | ||
return func(optionsApplier UpdateOptionApplier) { | ||
optionsApplier.WithRetryOnConflictBackoff(b) | ||
} | ||
} | ||
|
||
func withStatusUpdate() UpdateOption { | ||
return func(optionsApplier UpdateOptionApplier) { | ||
optionsApplier.withStatusUpdate() | ||
} | ||
} |
112 changes: 112 additions & 0 deletions
112
deckhouse-controller/pkg/controller/ctrlutils/update.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright 2024 Flant JSC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package ctrlutils | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
|
||
"k8s.io/apimachinery/pkg/api/equality" | ||
apierrors "k8s.io/apimachinery/pkg/api/errors" | ||
"k8s.io/apimachinery/pkg/util/wait" | ||
"k8s.io/client-go/util/retry" | ||
"sigs.k8s.io/controller-runtime/pkg/client" | ||
) | ||
|
||
type MutateFn func() error | ||
|
||
type UpdateOptions struct { | ||
OnErrorBackoff wait.Backoff | ||
RetryOnConflictBackoff wait.Backoff | ||
|
||
statusUpdate bool | ||
} | ||
|
||
func (o *UpdateOptions) WithOnErrorBackoff(b *wait.Backoff) { | ||
if b == nil { | ||
return | ||
} | ||
|
||
o.OnErrorBackoff = *b | ||
} | ||
|
||
func (o *UpdateOptions) WithRetryOnConflictBackoff(b *wait.Backoff) { | ||
if b == nil { | ||
return | ||
} | ||
|
||
o.RetryOnConflictBackoff = *b | ||
} | ||
|
||
func (o *UpdateOptions) withStatusUpdate() { | ||
o.statusUpdate = true | ||
} | ||
|
||
var ErrCanNotMutateNameOrNamespace = errors.New("MutateFn cannot mutate object name and/or object namespace") | ||
|
||
func UpdateWithRetry(ctx context.Context, c client.Client, obj client.Object, f MutateFn, opts ...UpdateOption) error { | ||
options := &UpdateOptions{ | ||
OnErrorBackoff: retry.DefaultRetry, | ||
RetryOnConflictBackoff: retry.DefaultRetry, | ||
} | ||
|
||
for _, fn := range opts { | ||
fn.Apply(options) | ||
} | ||
|
||
key := client.ObjectKeyFromObject(obj) | ||
|
||
return retry.OnError(options.OnErrorBackoff, apierrors.IsServiceUnavailable, func() error { | ||
return retry.RetryOnConflict(options.RetryOnConflictBackoff, func() error { | ||
if err := c.Get(ctx, key, obj); err != nil { | ||
return client.IgnoreNotFound(err) | ||
} | ||
|
||
existing := obj.DeepCopyObject() | ||
if err := mutate(f, key, obj); err != nil { | ||
return err | ||
} | ||
|
||
if equality.Semantic.DeepEqual(existing, obj) { | ||
return nil | ||
} | ||
|
||
if options.statusUpdate { | ||
return c.Status().Update(ctx, obj) | ||
} | ||
|
||
return c.Update(ctx, obj) | ||
}) | ||
}) | ||
} | ||
|
||
func UpdateStatusWithRetry(ctx context.Context, c client.Client, obj client.Object, f MutateFn, opts ...UpdateOption) error { | ||
opts = append(opts, withStatusUpdate()) | ||
|
||
return UpdateWithRetry(ctx, c, obj, f, opts...) | ||
} | ||
|
||
// mutate wraps a MutateFn and applies validation to its result. | ||
func mutate(f MutateFn, key client.ObjectKey, obj client.Object) error { | ||
if err := f(); err != nil { | ||
return err | ||
} | ||
|
||
if newKey := client.ObjectKeyFromObject(obj); key != newKey { | ||
return ErrCanNotMutateNameOrNamespace | ||
} | ||
|
||
return nil | ||
} |
Oops, something went wrong.