Skip to content

Commit

Permalink
dep inject client set for custom metrics handler
Browse files Browse the repository at this point in the history
  • Loading branch information
Siva Manivannan committed Jun 20, 2024
1 parent 243194e commit 5c18c2f
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 9 deletions.
11 changes: 10 additions & 1 deletion pact/custom_metrics_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/apiserver/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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)

Expand All @@ -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")

Expand Down
21 changes: 14 additions & 7 deletions pkg/handlers/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand Down Expand Up @@ -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"))
Expand All @@ -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 {
Expand Down

0 comments on commit 5c18c2f

Please sign in to comment.