diff --git a/pact/custom_metrics_test.go b/pact/custom_metrics_test.go index 14bb065..f93c574 100644 --- a/pact/custom_metrics_test.go +++ b/pact/custom_metrics_test.go @@ -13,7 +13,10 @@ import ( "github.com/pact-foundation/pact-go/dsl" "github.com/replicatedhq/kotskinds/apis/kots/v1beta1" "github.com/replicatedhq/replicated-sdk/pkg/handlers" + "github.com/replicatedhq/replicated-sdk/pkg/k8sutil" "github.com/replicatedhq/replicated-sdk/pkg/store" + "github.com/replicatedhq/replicated-sdk/pkg/util" + "k8s.io/client-go/kubernetes/fake" ) func TestSendCustomAppMetrics(t *testing.T) { @@ -78,7 +81,13 @@ func TestSendCustomAppMetrics(t *testing.T) { defer store.SetStore(nil) if err := pact.Verify(func() error { - handlers.SendCustomAppMetrics(clientWriter, clientRequest) + fakeClientSet := fake.NewSimpleClientset( + k8sutil.CreateTestDeployment(util.GetReplicatedDeploymentName(), "replicated-sdk-instance-namespace", "1", map[string]string{"app": "replicated"}), + k8sutil.CreateTestReplicaSet("replicated-sdk-instance-replicaset", "replicated-sdk-instance-namespace", "1"), + k8sutil.CreateTestPod("replicated-sdk-instance-pod", "replicated-sdk-instance-namespace", "replicated-sdk-instance-replicaset", map[string]string{"app": "replicated"}), + ) + h := handlers.SendCustomAppMetrics(fakeClientSet) + h(clientWriter, clientRequest) if clientWriter.Code != http.StatusOK { return fmt.Errorf("expected status code %d but got %d", http.StatusOK, clientWriter.Code) } diff --git a/pkg/apiserver/server.go b/pkg/apiserver/server.go index 24cb3fd..fb92220 100644 --- a/pkg/apiserver/server.go +++ b/pkg/apiserver/server.go @@ -11,6 +11,7 @@ import ( appstatetypes "github.com/replicatedhq/replicated-sdk/pkg/appstate/types" "github.com/replicatedhq/replicated-sdk/pkg/buildversion" "github.com/replicatedhq/replicated-sdk/pkg/handlers" + "github.com/replicatedhq/replicated-sdk/pkg/k8sutil" sdklicensetypes "github.com/replicatedhq/replicated-sdk/pkg/license/types" ) @@ -48,6 +49,12 @@ func Start(params APIServerParams) { log.Fatalf("failed to bootstrap: %v", err) } + clientset, err := k8sutil.GetClientset() + if err != nil { + log.Fatalf("failed to get clientset: %v", err) + return + } + r := mux.NewRouter() r.Use(handlers.CorsMiddleware) @@ -66,7 +73,7 @@ func Start(params APIServerParams) { r.HandleFunc("/api/v1/app/info", handlers.GetCurrentAppInfo).Methods("GET") r.HandleFunc("/api/v1/app/updates", handlers.GetAppUpdates).Methods("GET") r.HandleFunc("/api/v1/app/history", handlers.GetAppHistory).Methods("GET") - r.HandleFunc("/api/v1/app/custom-metrics", handlers.SendCustomAppMetrics).Methods("POST", "PATCH") + r.HandleFunc("/api/v1/app/custom-metrics", handlers.SendCustomAppMetrics(clientset)).Methods("POST", "PATCH") r.HandleFunc("/api/v1/app/custom-metrics/{key}", handlers.DeleteCustomAppMetricsKey).Methods("DELETE") r.HandleFunc("/api/v1/app/instance-tags", handlers.SendAppInstanceTags).Methods("POST") diff --git a/pkg/handlers/app.go b/pkg/handlers/app.go index 62d9f58..ea67601 100644 --- a/pkg/handlers/app.go +++ b/pkg/handlers/app.go @@ -28,6 +28,7 @@ import ( "github.com/replicatedhq/replicated-sdk/pkg/util" helmrelease "helm.sh/helm/v3/pkg/release" "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" + "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/scheme" ) @@ -353,7 +354,13 @@ func mockReleaseToAppRelease(mockRelease integrationtypes.MockRelease) AppReleas return appRelease } -func SendCustomAppMetrics(w http.ResponseWriter, r *http.Request) { +func SendCustomAppMetrics(clientset kubernetes.Interface) func(w http.ResponseWriter, r *http.Request) { + return func(w http.ResponseWriter, r *http.Request) { + sendCustomAppMetrics(clientset, w, r) + } +} + +func sendCustomAppMetrics(clientset kubernetes.Interface, w http.ResponseWriter, r *http.Request) { request := SendCustomAppMetricsRequest{} if err := json.NewDecoder(r.Body).Decode(&request); err != nil { logger.Error(errors.Wrap(err, "decode request")) @@ -367,12 +374,12 @@ func SendCustomAppMetrics(w http.ResponseWriter, r *http.Request) { return } - clientset, err := k8sutil.GetClientset() - if err != nil { - logger.Error(errors.Wrap(err, "failed to get clientset")) - w.WriteHeader(http.StatusInternalServerError) - return - } + // clientset, err := k8sutil.GetClientset() + // if err != nil { + // logger.Error(errors.Wrap(err, "failed to get clientset")) + // w.WriteHeader(http.StatusInternalServerError) + // return + // } overwrite := true if r.Method == http.MethodPatch {