...

Source file src/sigs.k8s.io/controller-runtime/pkg/client/patch_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/client

     1  /*
     2  Copyright 2021 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package client
    18  
    19  import (
    20  	"testing"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	appsv1 "k8s.io/api/apps/v1"
    25  	batchv1 "k8s.io/api/batch/v1"
    26  	corev1 "k8s.io/api/core/v1"
    27  	"k8s.io/apimachinery/pkg/api/resource"
    28  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    29  )
    30  
    31  func BenchmarkMergeFrom(b *testing.B) {
    32  	cm1 := &corev1.ConfigMap{}
    33  	cm1.SetGroupVersionKind(corev1.SchemeGroupVersion.WithKind("ConfigMap"))
    34  	cm1.ResourceVersion = "anything"
    35  
    36  	cm2 := cm1.DeepCopy()
    37  	cm2.Data = map[string]string{"key": "value"}
    38  
    39  	sts1 := &appsv1.StatefulSet{}
    40  	sts1.SetGroupVersionKind(appsv1.SchemeGroupVersion.WithKind("StatefulSet"))
    41  	sts1.ResourceVersion = "somesuch"
    42  
    43  	sts2 := sts1.DeepCopy()
    44  	sts2.Spec.Template.Spec.Containers = []corev1.Container{{
    45  		Resources: corev1.ResourceRequirements{
    46  			Requests: map[corev1.ResourceName]resource.Quantity{
    47  				corev1.ResourceCPU:    resource.MustParse("1m"),
    48  				corev1.ResourceMemory: resource.MustParse("1M"),
    49  			},
    50  		},
    51  		ReadinessProbe: &corev1.Probe{
    52  			ProbeHandler: corev1.ProbeHandler{
    53  				HTTPGet: &corev1.HTTPGetAction{},
    54  			},
    55  		},
    56  		Lifecycle: &corev1.Lifecycle{
    57  			PreStop: &corev1.LifecycleHandler{
    58  				HTTPGet: &corev1.HTTPGetAction{},
    59  			},
    60  		},
    61  		SecurityContext: &corev1.SecurityContext{},
    62  	}}
    63  
    64  	b.Run("NoOptions", func(b *testing.B) {
    65  		cmPatch := MergeFrom(cm1)
    66  		if _, err := cmPatch.Data(cm2); err != nil {
    67  			b.Fatalf("expected no error, got %v", err)
    68  		}
    69  
    70  		stsPatch := MergeFrom(sts1)
    71  		if _, err := stsPatch.Data(sts2); err != nil {
    72  			b.Fatalf("expected no error, got %v", err)
    73  		}
    74  
    75  		b.ResetTimer()
    76  		for i := 0; i < b.N; i++ {
    77  			_, _ = cmPatch.Data(cm2)
    78  			_, _ = stsPatch.Data(sts2)
    79  		}
    80  	})
    81  
    82  	b.Run("WithOptimisticLock", func(b *testing.B) {
    83  		cmPatch := MergeFromWithOptions(cm1, MergeFromWithOptimisticLock{})
    84  		if _, err := cmPatch.Data(cm2); err != nil {
    85  			b.Fatalf("expected no error, got %v", err)
    86  		}
    87  
    88  		stsPatch := MergeFromWithOptions(sts1, MergeFromWithOptimisticLock{})
    89  		if _, err := stsPatch.Data(sts2); err != nil {
    90  			b.Fatalf("expected no error, got %v", err)
    91  		}
    92  
    93  		b.ResetTimer()
    94  		for i := 0; i < b.N; i++ {
    95  			_, _ = cmPatch.Data(cm2)
    96  			_, _ = stsPatch.Data(sts2)
    97  		}
    98  	})
    99  }
   100  
   101  var _ = Describe("MergeFrom", func() {
   102  	It("should successfully create a patch for two large and similar in64s", func() {
   103  		var largeInt64 int64 = 9223372036854775807
   104  		var similarLargeInt64 int64 = 9223372036854775800
   105  		j := batchv1.Job{
   106  			ObjectMeta: metav1.ObjectMeta{
   107  				Namespace: "test",
   108  				Name:      "test",
   109  			},
   110  			Spec: batchv1.JobSpec{
   111  				ActiveDeadlineSeconds: &largeInt64,
   112  			},
   113  		}
   114  		patch := MergeFrom(j.DeepCopy())
   115  
   116  		j.Spec.ActiveDeadlineSeconds = &similarLargeInt64
   117  
   118  		data, err := patch.Data(&j)
   119  		Expect(err).NotTo(HaveOccurred())
   120  		Expect(data).To(Equal([]byte(`{"spec":{"activeDeadlineSeconds":9223372036854775800}}`)))
   121  	})
   122  })
   123  

View as plain text