Skip to content

Commit

Permalink
fix: removes missing 404 bug
Browse files Browse the repository at this point in the history
  • Loading branch information
RealAnna committed Mar 8, 2023
1 parent fa7b15e commit b22ede2
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 3 deletions.
1 change: 0 additions & 1 deletion .github/scripts/generate-helm-docs.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#!/bin/bash

# Readme generator for Keptn Helm Chart
#
Expand Down
14 changes: 12 additions & 2 deletions metrics-operator/pkg/metrics/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,12 @@ import (
"github.com/gorilla/mux"
metricsapi "github.com/keptn/lifecycle-toolkit/metrics-operator/api/v1alpha2"
"github.com/open-feature/go-sdk/pkg/openfeature"
"github.com/pkg/errors"

//"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
k8serrors "k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"
Expand Down Expand Up @@ -142,10 +146,14 @@ func (m *serverManager) returnMetric(w http.ResponseWriter, r *http.Request) {
namespace := vars["namespace"]
metric := vars["metric"]

metricObj := metricsapi.KeptnMetric{}
err := m.k8sClient.Get(context.Background(), types.NamespacedName{Name: metric, Namespace: namespace}, &metricObj)
metricObj := &metricsapi.KeptnMetric{}
err := m.k8sClient.Get(context.Background(), types.NamespacedName{Name: metric, Namespace: namespace}, metricObj)
if err != nil {
fmt.Println("failed to list keptn-metrics: " + err.Error())
if status, ok := err.(k8serrors.APIStatus); ok || errors.As(err, &status) {
w.WriteHeader(int(status.Status().Code))
}
return
}

data := map[string]string{
Expand All @@ -157,8 +165,10 @@ func (m *serverManager) returnMetric(w http.ResponseWriter, r *http.Request) {
err = json.NewEncoder(w).Encode(data)
if err != nil {
fmt.Println("failed to encode data")
w.WriteHeader(422)
os.Exit(1)
}

}

func (m *serverManager) recordMetrics() {
Expand Down
32 changes: 32 additions & 0 deletions metrics-operator/pkg/metrics/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,38 @@ func TestMetricServer_happyPath(t *testing.T) {
}, 10*time.Second, time.Second)
}

func TestMetricServer_noMetric(t *testing.T) {

err := metricsapi.AddToScheme(scheme.Scheme)
require.Nil(t, err)
k8sClient := fake.NewClientBuilder().WithScheme(scheme.Scheme).WithObjects().Build()

ctx, cancel := context.WithCancel(context.Background())

StartServerManager(ctx, k8sClient, openfeature.NewClient("klt-test2"), true, 3*time.Second)

require.Eventually(t, func() bool {
return instance.server != nil
}, 10*time.Second, time.Second)

var resp *http.Response

cli := &http.Client{}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "http://localhost:9999/api/v1/metrics/default/sample", nil)
require.Nil(t, err)
resp, err = cli.Do(req)
defer resp.Body.Close()
require.Nil(t, err)
stat := resp.StatusCode
require.Equal(t, 404, stat)

cancel()

require.Eventually(t, func() bool {
return instance.server == nil
}, 10*time.Second, time.Second)
}

func TestMetricServer_disabledServer(t *testing.T) {
err2 := metricsapi.AddToScheme(scheme.Scheme)
require.Nil(t, err2)
Expand Down

0 comments on commit b22ede2

Please sign in to comment.