Skip to content

Commit

Permalink
move setPrometheusAddress mutation to go (#874)
Browse files Browse the repository at this point in the history
* move setPrometheusAddress mutation to go
  • Loading branch information
sgalsaleh committed Jul 28, 2020
1 parent 8407cb4 commit d3bfe55
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 23 deletions.
2 changes: 1 addition & 1 deletion kotsadm/pkg/apiserver/server.go
Expand Up @@ -151,7 +151,7 @@ func Start() {
r.Path("/api/v1/kurl/generate-node-join-command-master").Methods("OPTIONS", "POST").HandlerFunc(handlers.GenerateNodeJoinCommandMaster)

// Prometheus
r.HandleFunc("/api/v1/prometheus", handlers.NotImplemented)
r.Path("/api/v1/prometheus").Methods("OPTIONS", "POST").HandlerFunc(handlers.SetPrometheusAddress)

// GitOps
r.HandleFunc("/api/v1/gitops", handlers.NotImplemented)
Expand Down
43 changes: 43 additions & 0 deletions kotsadm/pkg/handlers/prometheus.go
@@ -0,0 +1,43 @@
package handlers

import (
"encoding/json"
"net/http"

"github.com/replicatedhq/kots/kotsadm/pkg/kotsadmparams"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
)

type SetPrometheusAddressRequest struct {
Value string `json:"value"`
}

func SetPrometheusAddress(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Allow-Headers", "content-type, origin, accept, authorization")

if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
return
}

if err := requireValidSession(w, r); err != nil {
logger.Error(err)
return
}

setPrometheusAddressRequest := SetPrometheusAddressRequest{}
if err := json.NewDecoder(r.Body).Decode(&setPrometheusAddressRequest); err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}

if err := kotsadmparams.Set("PROMETHEUS_ADDRESS", setPrometheusAddressRequest.Value); err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}

JSON(w, 204, "")
}
12 changes: 12 additions & 0 deletions kotsadm/pkg/kotsadmparams/kotsadmparams.go
Expand Up @@ -22,3 +22,15 @@ func Get(name string) (string, error) {

return value, nil
}

func Set(name string, value string) error {
db := persistence.MustGetPGSession()
query := `insert into kotsadm_params (key, value) values ($1, $2) on conflict (key) do update set value = $2`

_, err := db.Exec(query, name, value)
if err != nil {
return errors.Wrap(err, "failed to exec")
}

return nil
}
49 changes: 27 additions & 22 deletions kotsadm/web/src/components/apps/Dashboard.jsx
Expand Up @@ -13,7 +13,6 @@ import Modal from "react-modal";
import { Repeater } from "../../utilities/repeater";
import { Utilities } from "../../utilities/utilities";
import { getAppLicense, getUpdateDownloadStatus } from "@src/queries/AppsQueries";
import { setPrometheusAddress } from "@src/mutations/AppsMutations";

import { XYPlot, XAxis, YAxis, HorizontalGridLines, VerticalGridLines, LineSeries, DiscreteColorLegend, Crosshair } from "react-vis";

Expand Down Expand Up @@ -54,8 +53,11 @@ class Dashboard extends Component {
airgapUpdateError: "",
startSnapshotErrorMsg: "",
showUpdateCheckerModal: false,
appStatus: null,
metrics: [],
dashboard: {
appStatus: null,
metrics: [],
prometheusAddress: "",
},
getAppDashboardJob: new Repeater(),
}

Expand All @@ -68,19 +70,25 @@ class Dashboard extends Component {

updatePromValue = () => {
this.setState({ savingPromValue: true });
this.props.client.mutate({
mutation: setPrometheusAddress,
variables: {
value: this.state.promValue,

fetch(`${window.env.API_ENDPOINT}/prometheus`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
body: JSON.stringify({
value: this.state.promValue,
}),
method: "POST",
})
.then(() => {
.then(async () => {
await this.getAppDashboard();
this.setState({ savingPromValue: false });
this.getAppDashboard();
})
.catch(() => {
.catch((err) => {
console.log(err);
this.setState({ savingPromValue: false });
})
});
}

onPromValueChange = (e) => {
Expand Down Expand Up @@ -151,9 +159,11 @@ class Dashboard extends Component {
.then(async (res) => {
const response = await res.json();
this.setState({
appStatus: response.appStatus,
promValue: response.prometheusAddress,
metrics: response.metrics,
dashboard: {
appStatus: response.appStatus,
prometheusAddress: response.prometheusAddress,
metrics: response.metrics,
},
});
resolve();
})
Expand Down Expand Up @@ -542,7 +552,7 @@ class Dashboard extends Component {
cardName="Application"
application={true}
cardIcon="applicationIcon"
appStatus={this.state.appStatus?.state}
appStatus={this.state.dashboard?.appStatus?.state}
url={this.props.match.url}
links={links}
app={app}
Expand Down Expand Up @@ -607,13 +617,13 @@ class Dashboard extends Component {
}
</div>
<div className="u-marginTop--30 flex flex1">
{this.state.promValue ?
{this.state.dashboard?.prometheusAddress ?
<div>
<div className="flex flex1 justifyContent--flexEnd">
<span className="card-link" onClick={this.toggleConfigureGraphs}> Configure Prometheus Address </span>
</div>
<div className="flex-auto flex flexWrap--wrap u-width--full">
{this.state.metrics.map(this.renderGraph)}
{this.state.dashboard?.metrics.map(this.renderGraph)}
</div>
</div>
:
Expand Down Expand Up @@ -712,9 +722,4 @@ export default compose(
};
}
}),
graphql(setPrometheusAddress, {
props: ({ mutate }) => ({
setPrometheusAddress: (value) => mutate({ variables: { value } })
})
}),
)(Dashboard);

0 comments on commit d3bfe55

Please sign in to comment.