Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add log for update #355

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions pkg/controllers/sync/dispatch/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ import (
"github.com/kubewharf/kubeadmiral/pkg/stats/metrics"
"github.com/kubewharf/kubeadmiral/pkg/util/adoption"
"github.com/kubewharf/kubeadmiral/pkg/util/managedlabel"
"github.com/kubewharf/kubeadmiral/pkg/util/yaml"
)

const (
Expand Down Expand Up @@ -381,6 +382,20 @@ func (d *managedDispatcherImpl) Update(ctx context.Context, clusterName string,
d.recordEvent(clusterName, op, "Updating")

keyedLogger.V(1).Info("Updating target object in cluster")
clusterObjYaml, err := yaml.MarshalYAML(clusterObj)
if err != nil {
return false
}
currentObjYaml, err := yaml.MarshalYAML(obj)
if err != nil {
return false
}
fedObjYaml, err := yaml.MarshalYAML(d.fedResource.Object())
if err != nil {
return false
}
keyedLogger.V(1).Info("Print the updating obj", "originalObj", string(clusterObjYaml), "currentObj", string(currentObjYaml),
"federatedObj", string(fedObjYaml))
obj, err = client.Resource(d.fedResource.TargetGVR()).Namespace(obj.GetNamespace()).Update(
ctx, obj, metav1.UpdateOptions{},
)
Expand Down
70 changes: 70 additions & 0 deletions pkg/util/yaml/yaml.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
Copyright 2025 The KubeAdmiral 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 yaml

import (
"fmt"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/runtime/schema"
"k8s.io/klog/v2"
"sigs.k8s.io/yaml"

genericschema "github.com/kubewharf/kubeadmiral/pkg/client/generic/scheme"
)

type kubernetesResource interface {
runtime.Object
schema.ObjectKind
}

// MarshalYAML marshals the given object into bytes
func MarshalYAML(obj interface{}) ([]byte, error) {
SetObjectGVK(obj)
bs, err := yaml.Marshal(obj)
if err != nil {
if o, ok := obj.(metav1.Object); ok {
klog.Error(fmt.Sprintf("failed to marshal metav1.Object %s/%s: %v", o.GetNamespace(), o.GetName(), err))
} else {
klog.Error(fmt.Sprintf("failed to marshal unknown object: %v", err))
}
return nil, err
}
return bs, nil
}

// GetObjectGVK get kubernetes object type meta
func GetObjectGVK(obj runtime.Object) schema.GroupVersionKind {
gvks, _, err := genericschema.Scheme.ObjectKinds(obj)
if err != nil {
return schema.GroupVersionKind{}
}
if len(gvks) == 0 {
return schema.GroupVersionKind{}
}
return gvks[0]
}

// SetObjectGVK set kubernetes object type meta
func SetObjectGVK(obj interface{}) {
if o, ok := obj.(kubernetesResource); ok {
if gvk := GetObjectGVK(o); !gvk.Empty() {
o.SetGroupVersionKind(gvk)
}
}
}
Loading