-
Notifications
You must be signed in to change notification settings - Fork 743
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #592 from weaveworks/check-metrics-server-availabi…
…lity Check metrics server availability during canary initialization
- Loading branch information
Showing
9 changed files
with
179 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package controller | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
"k8s.io/client-go/tools/record" | ||
|
||
flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1" | ||
"github.com/weaveworks/flagger/pkg/metrics/observers" | ||
) | ||
|
||
func TestController_checkMetricProviderAvailability(t *testing.T) { | ||
t.Run("builtin", func(t *testing.T) { | ||
// ok | ||
analysis := &flaggerv1.CanaryAnalysis{Metrics: []flaggerv1.CanaryMetric{{Name: "request-success-rate"}}} | ||
canary := &flaggerv1.Canary{Spec: flaggerv1.CanarySpec{Analysis: analysis}} | ||
obs, err := observers.NewFactory(testMetricsServerURL) | ||
require.NoError(t, err) | ||
ctrl := Controller{observerFactory: obs, logger: zap.S(), eventRecorder: &record.FakeRecorder{}} | ||
require.NoError(t, ctrl.checkMetricProviderAvailability(canary)) | ||
|
||
// error | ||
ctrl.observerFactory, err = observers.NewFactory("http://non-exist") | ||
require.NoError(t, err) | ||
require.Error(t, ctrl.checkMetricProviderAvailability(canary)) | ||
|
||
// ok | ||
canary.Spec.MetricsServer = testMetricsServerURL | ||
require.NoError(t, ctrl.checkMetricProviderAvailability(canary)) | ||
}) | ||
|
||
t.Run("templateRef", func(t *testing.T) { | ||
ctrl := newDeploymentFixture(nil).ctrl | ||
|
||
// error (not found) | ||
analysis := &flaggerv1.CanaryAnalysis{Metrics: []flaggerv1.CanaryMetric{{ | ||
Name: "", TemplateRef: &flaggerv1.CrossNamespaceObjectReference{ | ||
Name: "non-exist", Namespace: "default", | ||
}, | ||
}}} | ||
canary := &flaggerv1.Canary{Spec: flaggerv1.CanarySpec{Analysis: analysis}} | ||
require.Error(t, ctrl.checkMetricProviderAvailability(canary)) | ||
|
||
// ok | ||
canary.Spec.Analysis.Metrics[0].TemplateRef = &flaggerv1.CrossNamespaceObjectReference{ | ||
Name: "envoy", | ||
Namespace: "default", | ||
} | ||
require.NoError(t, ctrl.checkMetricProviderAvailability(canary)) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package controller | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"net/http" | ||
"net/http/httptest" | ||
"os" | ||
"testing" | ||
|
||
flaggerv1 "github.com/weaveworks/flagger/pkg/apis/flagger/v1beta1" | ||
clientset "github.com/weaveworks/flagger/pkg/client/clientset/versioned" | ||
|
||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
) | ||
|
||
var testMetricsServerURL string | ||
|
||
func TestMain(m *testing.M) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
if r.URL.Query()["query"][0] == "vector(1)" { | ||
// for IsOnline invoked during canary initialization | ||
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1545905245.458,"1"]}]}}`)) | ||
return | ||
} | ||
w.Write([]byte(`{"status":"success","data":{"resultType":"vector","result":[{"metric":{},"value":[1545905245.458,"100"]}]}}`)) | ||
})) | ||
|
||
testMetricsServerURL = ts.URL | ||
defer ts.Close() | ||
os.Exit(m.Run()) | ||
} | ||
|
||
func assertPhase(flaggerClient clientset.Interface, canary string, phase flaggerv1.CanaryPhase) error { | ||
c, err := flaggerClient.FlaggerV1beta1().Canaries("default").Get(context.TODO(), canary, metav1.GetOptions{}) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if c.Status.Phase != phase { | ||
return fmt.Errorf("got canary state %s wanted %s", c.Status.Phase, phase) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func alwaysReady() bool { | ||
return true | ||
} | ||
|
||
func toFloatPtr(val int) *float64 { | ||
v := float64(val) | ||
return &v | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters