Skip to content

Commit

Permalink
Removed unused code (#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
lsierant authored Mar 5, 2024
1 parent c81c05c commit a87bda5
Show file tree
Hide file tree
Showing 2 changed files with 0 additions and 113 deletions.
32 changes: 0 additions & 32 deletions pkg/kube/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
81 changes: 0 additions & 81 deletions pkg/kube/service/service.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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
}

0 comments on commit a87bda5

Please sign in to comment.