Skip to content

Commit

Permalink
use k8s native encoder
Browse files Browse the repository at this point in the history
  • Loading branch information
fgksgf committed Jan 15, 2025
1 parent 46f076a commit 604feb0
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 7 deletions.
2 changes: 1 addition & 1 deletion pkg/controllers/pd/tasks/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ import (

const (
fakeVersion = "v1.2.3"
expectedPodSpecHash = "765648cb46"
expectedPodSpecHash = "7d4f6bf985"
)

func TestTaskPod(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/tidb/tasks/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

const (
fakeVersion = "v1.2.3"
podSpecHash = "6cfdc7d895"
podSpecHash = "fc445b4c6"
)

func TestTaskPod(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/tiflash/tasks/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import (

const (
fakeVersion = "v1.2.3"
fakePodHash = "76b9ffc44"
fakePodHash = "5bf69bf8f9"
)

func TestTaskPod(t *testing.T) {
Expand Down
2 changes: 1 addition & 1 deletion pkg/controllers/tikv/tasks/pod_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ import (

const (
fakeVersion = "v1.2.3"
podSpecHash = "6b85d6945f"
podSpecHash = "5f7b88ffbd"
)

func TestTaskPod(t *testing.T) {
Expand Down
22 changes: 19 additions & 3 deletions pkg/utils/k8s/pod.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,34 @@
package k8s

import (
"encoding/json"
"bytes"
"fmt"
"hash/fnv"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
serializerjson "k8s.io/apimachinery/pkg/runtime/serializer/json"
"k8s.io/apimachinery/pkg/util/rand"
"k8s.io/client-go/kubernetes/scheme"

"github.com/pingcap/tidb-operator/apis/core/v1alpha1"
maputil "github.com/pingcap/tidb-operator/pkg/utils/map"
hashutil "github.com/pingcap/tidb-operator/third_party/kubernetes/pkg/util/hash"
)

var podEncoder = scheme.Codecs.EncoderForVersion(
serializerjson.NewSerializerWithOptions(
serializerjson.DefaultMetaFactory,
scheme.Scheme,
scheme.Scheme,
serializerjson.SerializerOptions{
Yaml: false,
Pretty: false,
Strict: true,
}),
corev1.SchemeGroupVersion,
)

// CalculateHashAndSetLabels calculate the hash of pod spec and set it to the pod labels.
func CalculateHashAndSetLabels(pod *corev1.Pod) {
spec := pod.Spec.DeepCopy()
Expand All @@ -38,9 +53,10 @@ func CalculateHashAndSetLabels(pod *corev1.Pod) {
}

// This prevents the hash from being changed when new fields are added to the `PodSpec` due to K8s version upgrades.
data, _ := json.Marshal(spec)
buf := bytes.Buffer{}
_ = podEncoder.Encode(&corev1.Pod{Spec: *spec}, &buf)
hasher := fnv.New32a()
hashutil.DeepHashObject(hasher, data)
hashutil.DeepHashObject(hasher, buf.Bytes())

if pod.Labels == nil {
pod.Labels = map[string]string{}
Expand Down
1 change: 1 addition & 0 deletions pkg/utils/k8s/revision/controller_revision.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ func getPatch(obj client.Object, gvk schema.GroupVersionKind) ([]byte, error) {
}),
gvk.GroupVersion(),
)
encoderMap[gvk.GroupVersion()] = encoder
}

buf := bytes.Buffer{}
Expand Down

0 comments on commit 604feb0

Please sign in to comment.