From a87bda54cad0d635dbec1883f24579aac11c364f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Sierant?= Date: Tue, 5 Mar 2024 13:30:19 +0100 Subject: [PATCH] Removed unused code (#1501) --- pkg/kube/client/client_test.go | 32 -------------- pkg/kube/service/service.go | 81 ---------------------------------- 2 files changed, 113 deletions(-) diff --git a/pkg/kube/client/client_test.go b/pkg/kube/client/client_test.go index 0a8b5c726..fcaeccd31 100644 --- a/pkg/kube/client/client_test.go +++ b/pkg/kube/client/client_test.go @@ -2,8 +2,6 @@ package client import ( "context" - "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/annotations" - "github.com/mongodb/mongodb-kubernetes-operator/pkg/kube/service" "testing" "k8s.io/apimachinery/pkg/types" @@ -83,33 +81,3 @@ func TestDeleteConfigMap(t *testing.T) { _, err = client.GetConfigMap(types.NamespacedName{Name: "config-map", Namespace: "default"}) assert.Equal(t, err, notFoundError()) } - -// TestSetAnnotationsDoesNotChangeSuppliedObject verifies that the supplied object for annotations.SetAnnotations is not overridden due being a shallow copy. -// the function lies here, otherwise it will lead to import cycles. -func TestSetAnnotationsDoesNotChangeSuppliedObject(t *testing.T) { - c := NewClient(NewMockedClient()) - backedService := service.Builder(). - SetName("some-name"). - SetNamespace("some-namespace"). - SetAnnotations(map[string]string{"one": "annotation"}). - SetClusterIP("123"). - Build() - err := service.CreateOrUpdateService(c, backedService) - assert.NoError(t, err) - - serviceWithoutAnnotation := service.Builder(). - SetName("some-name"). - SetNamespace("some-namespace"). - Build() - - // make sure this method only changes the annotations locally and in kube - err = annotations.SetAnnotations(&serviceWithoutAnnotation, map[string]string{"new": "something"}, c) - assert.NoError(t, err) - assert.Len(t, serviceWithoutAnnotation.Annotations, 2) - assert.Equal(t, "", serviceWithoutAnnotation.Spec.ClusterIP) - - err = c.Get(context.TODO(), types.NamespacedName{Name: serviceWithoutAnnotation.GetName(), Namespace: serviceWithoutAnnotation.GetNamespace()}, &serviceWithoutAnnotation) - assert.NoError(t, err) - assert.Len(t, serviceWithoutAnnotation.Annotations, 2) - assert.Equal(t, "123", serviceWithoutAnnotation.Spec.ClusterIP) -} diff --git a/pkg/kube/service/service.go b/pkg/kube/service/service.go index 887a65d92..a2792065e 100644 --- a/pkg/kube/service/service.go +++ b/pkg/kube/service/service.go @@ -1,11 +1,7 @@ package service import ( - "fmt" - corev1 "k8s.io/api/core/v1" - apiErrors "k8s.io/apimachinery/pkg/api/errors" - "k8s.io/apimachinery/pkg/types" "sigs.k8s.io/controller-runtime/pkg/client" ) @@ -47,80 +43,3 @@ type GetUpdateCreateDeleter interface { Creator Deleter } - -func DeleteServiceIfItExists(getterDeleter GetDeleter, serviceName types.NamespacedName) error { - _, err := getterDeleter.GetService(serviceName) - if err != nil { - // If it is not found return - if apiErrors.IsNotFound(err) { - return nil - } - // Otherwise we got an error when trying to get it - return fmt.Errorf("can't get service %s: %s", serviceName, err) - } - return getterDeleter.DeleteService(serviceName) -} - -// Merge merges `source` into `dest`. Both arguments will remain unchanged -// a new service will be created and returned. -// The "merging" process is arbitrary and it only handle specific attributes -func Merge(dest corev1.Service, source corev1.Service) corev1.Service { - for k, v := range source.ObjectMeta.Annotations { - dest.ObjectMeta.Annotations[k] = v - } - - for k, v := range source.ObjectMeta.Labels { - dest.ObjectMeta.Labels[k] = v - } - - for k, v := range source.Spec.Selector { - dest.Spec.Selector[k] = v - } - - cachedNodePorts := map[int32]int32{} - for _, port := range dest.Spec.Ports { - cachedNodePorts[port.Port] = port.NodePort - } - - if len(source.Spec.Ports) > 0 { - portCopy := make([]corev1.ServicePort, len(source.Spec.Ports)) - copy(portCopy, source.Spec.Ports) - dest.Spec.Ports = portCopy - - for i := range dest.Spec.Ports { - // Source might not specify NodePort and we shouldn't override existing NodePort value - if dest.Spec.Ports[i].NodePort == 0 { - dest.Spec.Ports[i].NodePort = cachedNodePorts[dest.Spec.Ports[i].Port] - } - } - } - - dest.Spec.Type = source.Spec.Type - dest.Spec.LoadBalancerIP = source.Spec.LoadBalancerIP - dest.Spec.ExternalTrafficPolicy = source.Spec.ExternalTrafficPolicy - return dest -} - -// CreateOrUpdateService will create or update a service in Kubernetes. -func CreateOrUpdateService(getUpdateCreator GetUpdateCreator, desiredService corev1.Service) error { - namespacedName := types.NamespacedName{Namespace: desiredService.ObjectMeta.Namespace, Name: desiredService.ObjectMeta.Name} - existingService, err := getUpdateCreator.GetService(namespacedName) - - if err != nil { - if apiErrors.IsNotFound(err) { - err = getUpdateCreator.CreateService(desiredService) - if err != nil { - return err - } - } else { - return err - } - } else { - mergedService := Merge(existingService, desiredService) - err = getUpdateCreator.UpdateService(mergedService) - if err != nil { - return err - } - } - return nil -}