Skip to content

Commit

Permalink
add unit test, update context for integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
raghu-nandan-bs committed Nov 14, 2023
1 parent 8a98b85 commit 08f434a
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 9 deletions.
5 changes: 4 additions & 1 deletion service/k8s/configmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package k8s

import (
"context"
"fmt"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
Expand Down Expand Up @@ -76,11 +77,13 @@ func (p *ConfigMapService) CreateOrUpdateConfigMap(namespace string, configMap *
return err
}

if hashingEnabled() {
if HashingEnabled() {
if !shouldUpdate(configMap, storedConfigMap) {
p.logger.Debugf("%v/%v configmap is upto date, no need to apply changes...", configMap.Namespace, configMap.Name)
fmt.Printf("%v/%v configmap is upto date, no need to apply changes...", configMap.Namespace, configMap.Name)
return nil
}
fmt.Printf("%v\n\n%v\n\n configmap has different resource", configMap, storedConfigMap)
p.logger.Debugf("%v/%v configmap has a different resource hash, updating the object...", configMap.Namespace, configMap.Name)
addHashAnnotation(configMap)
}
Expand Down
2 changes: 1 addition & 1 deletion service/k8s/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (d *DeploymentService) CreateOrUpdateDeployment(namespace string, deploymen
return err
}

if hashingEnabled() {
if HashingEnabled() {
if !shouldUpdate(deployment, storedDeployment) {
d.logger.Debugf("%v/%v deployment is upto date, no need to apply changes...", deployment.Namespace, deployment.Name)
return nil
Expand Down
62 changes: 62 additions & 0 deletions service/k8s/hash_annotations_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package k8s

import "testing"

// create test for addHashAnnotation

// create a dummy struct that implements Annotated interface
type dummy struct {
annotations map[string]string
name string
}

func (d *dummy) GetAnnotations() map[string]string {
return d.annotations
}

func (d *dummy) SetAnnotations(annotations map[string]string) {
d.annotations = annotations
}

func (d *dummy) GetName() string {
return d.name
}

func TestAddHashAnnotation(t *testing.T) {
originalObject := &dummy{name: "test"}
copyOfOriginalObject := &dummy{name: "test"}
differentObject := &dummy{name: "test2"}

addHashAnnotation(originalObject)

tests := []struct {
name string
object Annotated
errorMessage string
expected bool
}{
{
name: "Hashes of same object should be equal",
object: copyOfOriginalObject,
errorMessage: "Hashes of same object should be equal",
expected: true,
},
{
name: "Hashes of different objects should not be equal",
object: differentObject,
errorMessage: "Hashes of different objects should not be equal",
expected: false,
},
}
for _, test := range tests {
addHashAnnotation(test.object)
hash := test.object.GetAnnotations()[resourceHashAnnotationKey]
if hash == "" {
t.Errorf("Hash not created")
}
equal := hash == originalObject.GetAnnotations()[resourceHashAnnotationKey]
if equal != test.expected {
t.Error(test.errorMessage)
}
}
}
8 changes: 6 additions & 2 deletions service/k8s/k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ var (
objectHashingEnabled bool
)

func hashingEnabled() bool {
func HashingEnabled() bool {
return objectHashingEnabled
}

func SetHashingEnabled(enabled bool) {
objectHashingEnabled = enabled
}

type services struct {
ConfigMap
Secret
Expand All @@ -44,7 +48,7 @@ type services struct {

// New returns a new Kubernetes service.
func New(kubecli kubernetes.Interface, crdcli redisfailoverclientset.Interface, apiextcli apiextensionscli.Interface, logger log.Logger, metricsRecorder metrics.Recorder, enableHashing bool) Services {
objectHashingEnabled = enableHashing
SetHashingEnabled(enableHashing)
return &services{
ConfigMap: NewConfigMapService(kubecli, logger, metricsRecorder),
Secret: NewSecretService(kubecli, logger, metricsRecorder),
Expand Down
2 changes: 1 addition & 1 deletion service/k8s/poddisruptionbudget.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ func (p *PodDisruptionBudgetService) CreateOrUpdatePodDisruptionBudget(namespace
return err
}

if hashingEnabled() {
if HashingEnabled() {
if !shouldUpdate(podDisruptionBudget, storedPodDisruptionBudget) {
p.logger.Debugf("%v/%v pdb is upto date, no need to apply changes...", podDisruptionBudget.Namespace, podDisruptionBudget.Name)
return nil
Expand Down
4 changes: 2 additions & 2 deletions service/k8s/rbac.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ func (r *RBACService) CreateOrUpdateRole(namespace string, role *rbacv1.Role) er
return err
}

if hashingEnabled() {
if HashingEnabled() {
if !shouldUpdate(role, storedRole) {
r.logger.Debugf("%v/%v role is upto date, no need to apply changes...", role.Namespace, role.Name)
return nil
Expand Down Expand Up @@ -156,7 +156,7 @@ func (r *RBACService) CreateOrUpdateRoleBinding(namespace string, binding *rbacv
return err
}

if hashingEnabled() {
if HashingEnabled() {
if !shouldUpdate(binding, storedBinding) {
r.logger.Debugf("%v/%v rolebinding is upto date, no need to apply changes...", binding.Namespace, binding.Name)
return nil
Expand Down
2 changes: 1 addition & 1 deletion service/k8s/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ func (s *ServiceService) CreateOrUpdateService(namespace string, service *corev1
return err
}

if hashingEnabled() {
if HashingEnabled() {
if !shouldUpdate(service, storedService) {
s.logger.Debugf("%v/%v service is upto date, no need to apply changes...", service.Namespace, service.Name)
return nil
Expand Down
2 changes: 1 addition & 1 deletion service/k8s/statefulset.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (s *StatefulSetService) CreateOrUpdateStatefulSet(namespace string, statefu
statefulSet.Spec.VolumeClaimTemplates = storedStatefulSet.Spec.VolumeClaimTemplates
statefulSet.Annotations = util.MergeAnnotations(storedStatefulSet.Annotations, statefulSet.Annotations)

if hashingEnabled() {
if HashingEnabled() {
delete(statefulSet.Annotations, resourceHashAnnotationKey) // this will be regenerated if changes are required.
if !shouldUpdate(statefulSet, storedStatefulSet) {
s.logger.Debugf("%v/%v statefulset is upto date, no need to apply changes...", statefulSet.Namespace, statefulSet.Name)
Expand Down

0 comments on commit 08f434a

Please sign in to comment.