func()

in pkg/handlers/eks_credential_handler.go [70:104]


func (h *EksCredentialHandler) HandleRequest(resp http.ResponseWriter, req *http.Request) {
	ctx := logger.ContextWithField(req.Context(), "cluster-name", h.ClusterName)
	log := logger.FromContext(ctx)

	log.Infof("handling new request request from %s", req.RemoteAddr)

	eksCredentialsRequest := &credentials.EksCredentialsRequest{
		ClusterName:         h.ClusterName,
		ServiceAccountToken: req.Header.Get("Authorization"),
		RequestTargetHost:   req.Host,
	}

	creds, err := h.GetEksCredentials(ctx, eksCredentialsRequest)
	if err != nil {
		msg, code := errors.HandleCredentialFetchingError(ctx, err)
		promHttpStatus.WithLabelValues(strconv.Itoa(code)).Inc()
		http.Error(resp, msg, code)
		return
	}

	jsonOutput, err := json.Marshal(creds)
	if err != nil {
		promHttpStatus.WithLabelValues(strconv.Itoa(http.StatusInternalServerError)).Inc()
		http.Error(resp, "Unable to serialize credentials", http.StatusInternalServerError)
		return
	}

	// send the response
	resp.Header().Add("Content-Type", "application/json")
	promHttpStatus.WithLabelValues("200").Inc()
	_, err = resp.Write(jsonOutput)
	if err != nil {
		log.Errorf("failed to write response: %v", err)
	}
}