Merge pull request #530 from XiShanYongYe-Chang/cleanup-todo
Replace util.CreateOrUpdateWork with helper.CreateOrUpdateWork
This commit is contained in:
commit
18681cbbe3
|
@ -19,6 +19,7 @@ import (
|
|||
|
||||
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
||||
"github.com/karmada-io/karmada/pkg/util"
|
||||
"github.com/karmada-io/karmada/pkg/util/helper"
|
||||
"github.com/karmada-io/karmada/pkg/util/names"
|
||||
"github.com/karmada-io/karmada/pkg/util/restmapper"
|
||||
)
|
||||
|
@ -100,16 +101,7 @@ func (c *HorizontalPodAutoscalerController) buildWorks(hpa *autoscalingv1.Horizo
|
|||
util.MergeLabel(hpaObj, util.WorkNamespaceLabel, workNamespace)
|
||||
util.MergeLabel(hpaObj, util.WorkNameLabel, workName)
|
||||
|
||||
hpaJSON, err := hpaObj.MarshalJSON()
|
||||
if err != nil {
|
||||
klog.Errorf("Failed to marshal hpa %s/%s. Error: %v",
|
||||
hpaObj.GetNamespace(), hpaObj.GetName(), err)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO(@XiShanYongYe-Chang): refactor util.CreateOrUpdateWork with pkg/util/helper/work.go
|
||||
err = util.CreateOrUpdateWork(c.Client, objectMeta, hpaJSON)
|
||||
if err != nil {
|
||||
if err = helper.CreateOrUpdateWork(c.Client, objectMeta, hpaObj); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -23,6 +23,7 @@ import (
|
|||
|
||||
"github.com/karmada-io/karmada/pkg/apis/cluster/v1alpha1"
|
||||
"github.com/karmada-io/karmada/pkg/util"
|
||||
"github.com/karmada-io/karmada/pkg/util/helper"
|
||||
"github.com/karmada-io/karmada/pkg/util/names"
|
||||
)
|
||||
|
||||
|
@ -119,15 +120,7 @@ func (c *Controller) buildWorks(namespace *v1.Namespace, clusters []v1alpha1.Clu
|
|||
util.MergeLabel(namespaceObj, util.WorkNamespaceLabel, workNamespace)
|
||||
util.MergeLabel(namespaceObj, util.WorkNameLabel, workName)
|
||||
|
||||
namespaceJSON, err := namespaceObj.MarshalJSON()
|
||||
if err != nil {
|
||||
klog.Errorf("Failed to marshal namespace %s. Error: %v", namespaceObj.GetName(), err)
|
||||
return err
|
||||
}
|
||||
|
||||
// TODO(@XiShanYongYe-Chang): refactor util.CreateOrUpdateWork with pkg/util/helper/work.go
|
||||
err = util.CreateOrUpdateWork(c.Client, objectMeta, namespaceJSON)
|
||||
if err != nil {
|
||||
if err = helper.CreateOrUpdateWork(c.Client, objectMeta, namespaceObj); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,14 +1,6 @@
|
|||
package util
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime"
|
||||
"k8s.io/klog/v2"
|
||||
"sigs.k8s.io/controller-runtime/pkg/client"
|
||||
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
|
||||
|
||||
workv1alpha1 "github.com/karmada-io/karmada/pkg/apis/work/v1alpha1"
|
||||
)
|
||||
|
||||
|
@ -20,40 +12,3 @@ func GetBindingClusterNames(binding *workv1alpha1.ResourceBinding) []string {
|
|||
}
|
||||
return clusterNames
|
||||
}
|
||||
|
||||
// CreateOrUpdateWork creates a Work object if not exist, or updates if it already exist.
|
||||
func CreateOrUpdateWork(client client.Client, objectMeta metav1.ObjectMeta, rawExtension []byte) error {
|
||||
work := &workv1alpha1.Work{
|
||||
ObjectMeta: objectMeta,
|
||||
Spec: workv1alpha1.WorkSpec{
|
||||
Workload: workv1alpha1.WorkloadTemplate{
|
||||
Manifests: []workv1alpha1.Manifest{
|
||||
{
|
||||
RawExtension: runtime.RawExtension{
|
||||
Raw: rawExtension,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
runtimeObject := work.DeepCopy()
|
||||
operationResult, err := controllerutil.CreateOrUpdate(context.TODO(), client, runtimeObject, func() error {
|
||||
runtimeObject.Spec = work.Spec
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
klog.Errorf("Failed to create/update work %s/%s. Error: %v", work.GetNamespace(), work.GetName(), err)
|
||||
return err
|
||||
}
|
||||
|
||||
if operationResult == controllerutil.OperationResultCreated {
|
||||
klog.Infof("Create work %s/%s successfully.", work.GetNamespace(), work.GetName())
|
||||
} else if operationResult == controllerutil.OperationResultUpdated {
|
||||
klog.Infof("Update work %s/%s successfully.", work.GetNamespace(), work.GetName())
|
||||
} else {
|
||||
klog.V(2).Infof("Work %s/%s is up to date.", work.GetNamespace(), work.GetName())
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue