Add service metadata update unit test

This commit is contained in:
stefanprodan
2020-04-04 17:16:49 +03:00
parent 8d37b7b20b
commit 84dd0006ca
5 changed files with 24 additions and 16 deletions
+1 -1
View File
@@ -56,7 +56,7 @@ func (c *Controller) finalize(old interface{}) error {
}
// Revert the Kubernetes service
router := c.routerFactory.KubernetesRouter(canary.Spec.TargetRef.Kind, labelSelector, map[string]string{}, ports)
router := c.routerFactory.KubernetesRouter(canary.Spec.TargetRef.Kind, labelSelector, ports)
if err := router.Finalize(canary); err != nil {
return fmt.Errorf("failed revert router: %w", err)
}
+1 -1
View File
@@ -109,7 +109,7 @@ func (c *Controller) advanceCanary(name string, namespace string) {
}
// init Kubernetes router
kubeRouter := c.routerFactory.KubernetesRouter(cd.Spec.TargetRef.Kind, labelSelector, map[string]string{}, ports)
kubeRouter := c.routerFactory.KubernetesRouter(cd.Spec.TargetRef.Kind, labelSelector, ports)
if err := kubeRouter.Initialize(cd); err != nil {
c.recordEventWarningf(cd, "%v", err)
return
+1 -2
View File
@@ -35,7 +35,7 @@ func NewFactory(kubeConfig *restclient.Config, kubeClient kubernetes.Interface,
}
// KubernetesRouter returns a KubernetesRouter interface implementation
func (factory *Factory) KubernetesRouter(kind string, labelSelector string, annotations map[string]string, ports map[string]int32) KubernetesRouter {
func (factory *Factory) KubernetesRouter(kind string, labelSelector string, ports map[string]int32) KubernetesRouter {
switch kind {
case "Service":
return &KubernetesNoopRouter{}
@@ -45,7 +45,6 @@ func (factory *Factory) KubernetesRouter(kind string, labelSelector string, anno
flaggerClient: factory.flaggerClient,
kubeClient: factory.kubeClient,
labelSelector: labelSelector,
annotations: annotations,
ports: ports,
}
}
+2 -7
View File
@@ -25,7 +25,6 @@ type KubernetesDefaultRouter struct {
flaggerClient clientset.Interface
logger *zap.SugaredLogger
labelSelector string
annotations map[string]string
ports map[string]int32
}
@@ -120,15 +119,12 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
if metadata.Labels == nil {
metadata.Labels = make(map[string]string)
}
metadata.Labels[c.labelSelector] = name
if metadata.Annotations == nil {
metadata.Annotations = make(map[string]string)
}
metadata.Labels[c.labelSelector] = name
for k, v := range c.annotations {
metadata.Annotations[k] = v
}
c.logger.With("canary", fmt.Sprintf("%s.%s", canary.Name, canary.Namespace)).
Debugw(fmt.Sprintf("Creating Service %s", name), "metadata", metadata, "service_configuration", canary.Spec.Service)
@@ -187,7 +183,6 @@ func (c *KubernetesDefaultRouter) reconcileService(canary *flaggerv1.Canary, nam
selectorsDiff := cmp.Diff(svcSpec.Selector, svc.Spec.Selector)
if portsDiff != "" || selectorsDiff != "" {
svcClone := svc.DeepCopy()
svcClone.Spec.Ports = svcSpec.Ports
svcClone.Spec.Selector = svcSpec.Selector
_, err = c.kubeClient.CoreV1().Services(canary.Namespace).Update(context.TODO(), svcClone, metav1.UpdateOptions{})
+19 -5
View File
@@ -367,13 +367,13 @@ func TestServiceRouter_InitializeMetadata(t *testing.T) {
err := router.Initialize(mocks.canary)
require.NoError(t, err)
canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo-canary", metav1.GetOptions{})
canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-canary", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, "test", canarySvc.Annotations["test"])
assert.Equal(t, "test", canarySvc.Labels["test"])
assert.Equal(t, "podinfo-canary", canarySvc.Labels["app"])
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo-primary", metav1.GetOptions{})
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, 0, len(primarySvc.Annotations))
assert.Equal(t, "podinfo-primary", primarySvc.Labels["app"])
@@ -399,19 +399,33 @@ func TestServiceRouter_ReconcileMetadata(t *testing.T) {
err = router.Reconcile(mocks.canary)
require.NoError(t, err)
apexSvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo", metav1.GetOptions{})
apexSvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, "test", apexSvc.Annotations["test"])
assert.Equal(t, "test", apexSvc.Labels["test"])
assert.Equal(t, "podinfo", apexSvc.Labels["app"])
canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo-canary", metav1.GetOptions{})
canarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-canary", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, 0, len(canarySvc.Annotations))
assert.Equal(t, "podinfo-canary", canarySvc.Labels["app"])
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get("podinfo-primary", metav1.GetOptions{})
primarySvc, err := mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo-primary", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, 0, len(primarySvc.Annotations))
assert.Equal(t, "podinfo-primary", primarySvc.Labels["app"])
mocks.canary.Spec.Service.Apex = &flaggerv1.CustomMetadata{
Labels: map[string]string{"test": "test1"},
Annotations: map[string]string{"test1": "test"},
}
err = router.Reconcile(mocks.canary)
require.NoError(t, err)
apexSvc, err = mocks.kubeClient.CoreV1().Services("default").Get(context.TODO(), "podinfo", metav1.GetOptions{})
require.NoError(t, err)
assert.Equal(t, "test", apexSvc.Annotations["test1"])
assert.Equal(t, "test1", apexSvc.Labels["test"])
assert.Equal(t, "podinfo", apexSvc.Labels["app"])
}