Skip to content

Commit

Permalink
Added checks for API server errors
Browse files Browse the repository at this point in the history
  • Loading branch information
marcus-rodan-sinch committed Feb 1, 2021
1 parent 118cac5 commit aad671c
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 5 deletions.
7 changes: 3 additions & 4 deletions pkg/canary/config_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,18 +208,17 @@ func (ct *ConfigTracker) GetTargetConfigs(cd *flaggerv1.Canary) (map[string]Conf
for configMapName := range configMapNames {
config, err := ct.getRefFromConfigMap(configMapName, cd.Namespace)
if err != nil {
ct.Logger.Errorf("getRefFromConfigMap failed: %v", err)
continue
return nil, fmt.Errorf("configmap %s.%s get query error: %w", configMapName, cd.Namespace, err)
}
if config != nil {
res[config.GetName()] = *config
}
}

for secretName := range secretNames {
secret, err := ct.getRefFromSecret(secretName, cd.Namespace)
if err != nil {
ct.Logger.Errorf("getRefFromSecret failed: %v", err)
continue
return nil, fmt.Errorf("secret %s.%s get query error: %w", secretName, cd.Namespace, err)
}
if secret != nil {
res[secret.GetName()] = *secret
Expand Down
29 changes: 29 additions & 0 deletions pkg/canary/config_tracker_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,15 @@ package canary

import (
"context"
"errors"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
k8sTesting "k8s.io/client-go/testing"
)

func TestConfigIsDisabled(t *testing.T) {
Expand Down Expand Up @@ -303,3 +306,29 @@ func TestConfigTracker_Secrets(t *testing.T) {
assert.True(t, originalVolPresent, "Volume for original secret with disabled tracking should be present")
})
}

func TestConfigTracker_HasConfigChanged_ShouldReturnErrorWhenAPIServerIsDown(t *testing.T) {
t.Run("secret", func(t *testing.T) {
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
mocks, kubeClient := newCustomizableFixture(dc)

kubeClient.PrependReactor("get", "secrets", func(action k8sTesting.Action) (bool, runtime.Object, error) {
return true, nil, errors.New("server error")
})

_, err := mocks.controller.configTracker.HasConfigChanged(mocks.canary)
assert.Error(t, err)
})

t.Run("configmap", func(t *testing.T) {
dc := deploymentConfigs{name: "podinfo", label: "name", labelValue: "podinfo"}
mocks, kubeClient := newCustomizableFixture(dc)

kubeClient.PrependReactor("get", "configmaps", func(action k8sTesting.Action) (bool, runtime.Object, error) {
return true, nil, errors.New("server error")
})

_, err := mocks.controller.configTracker.HasConfigChanged(mocks.canary)
assert.Error(t, err)
})
}
7 changes: 6 additions & 1 deletion pkg/canary/deployment_fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,11 @@ func (d deploymentControllerFixture) initializeCanary(t *testing.T) {
}

func newDeploymentFixture(dc deploymentConfigs) deploymentControllerFixture {
fixture, _ := newCustomizableFixture(dc)
return fixture
}

func newCustomizableFixture(dc deploymentConfigs) (deploymentControllerFixture, *fake.Clientset) {
// init canary
cc := canaryConfigs{targetName: dc.name}
canary := newDeploymentControllerTestCanary(cc)
Expand Down Expand Up @@ -121,7 +126,7 @@ func newDeploymentFixture(dc deploymentConfigs) deploymentControllerFixture {
logger: logger,
flaggerClient: flaggerClient,
kubeClient: kubeClient,
}
}, kubeClient
}

func newDeploymentControllerTestConfigMap() *corev1.ConfigMap {
Expand Down

0 comments on commit aad671c

Please sign in to comment.