70 lines
2.2 KiB
Go
Executable File
70 lines
2.2 KiB
Go
Executable File
/*
|
|
Copyright 2024 The Karmada Authors.
|
|
|
|
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 helper
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"k8s.io/apimachinery/pkg/api/equality"
|
|
"sigs.k8s.io/controller-runtime/pkg/client"
|
|
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
|
)
|
|
|
|
// UpdateStatus updates the given object's status in the Kubernetes
|
|
// cluster. The object's desired state must be reconciled with the existing
|
|
// state inside the passed in callback MutateFn.
|
|
//
|
|
// The MutateFn is called when updating an object's status.
|
|
//
|
|
// It returns the executed operation and an error.
|
|
//
|
|
// Note: changes to any sub-resource other than status will be ignored.
|
|
// Changes to the status sub-resource will only be applied if the object
|
|
// already exist.
|
|
func UpdateStatus(ctx context.Context, c client.Client, obj client.Object, f controllerutil.MutateFn) (controllerutil.OperationResult, error) {
|
|
key := client.ObjectKeyFromObject(obj)
|
|
if err := c.Get(ctx, key, obj); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
|
|
existing := obj.DeepCopyObject()
|
|
if err := mutate(f, key, obj); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
|
|
if equality.Semantic.DeepEqual(existing, obj) {
|
|
return controllerutil.OperationResultNone, nil
|
|
}
|
|
|
|
if err := c.Status().Update(ctx, obj); err != nil {
|
|
return controllerutil.OperationResultNone, err
|
|
}
|
|
return controllerutil.OperationResultUpdatedStatusOnly, nil
|
|
}
|
|
|
|
// mutate wraps a MutateFn and applies validation to its result.
|
|
func mutate(f controllerutil.MutateFn, key client.ObjectKey, obj client.Object) error {
|
|
if err := f(); err != nil {
|
|
return err
|
|
}
|
|
if newKey := client.ObjectKeyFromObject(obj); key != newKey {
|
|
return fmt.Errorf("MutateFn cannot mutate object name and/or object namespace")
|
|
}
|
|
return nil
|
|
}
|