Skip to content

Commit

Permalink
move collectoSupportBundle query to go (#875)
Browse files Browse the repository at this point in the history
* move collectoSupportBundle query to go
  • Loading branch information
sgalsaleh committed Jul 28, 2020
1 parent d3bfe55 commit 323af55
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 15 deletions.
1 change: 1 addition & 0 deletions kotsadm/pkg/apiserver/server.go
Expand Up @@ -73,6 +73,7 @@ func Start() {
r.Path("/api/v1/troubleshoot/supportbundle/{bundleId}/redactions").Methods("OPTIONS", "GET").HandlerFunc(handlers.GetSupportBundleRedactions)
r.Path("/api/v1/troubleshoot/supportbundle/{bundleId}/redactions").Methods("PUT").HandlerFunc(handlers.SetSupportBundleRedactions)
r.Path("/api/v1/troubleshoot/supportbundle/{bundleId}/download").Methods("OPTIONS", "GET").HandlerFunc(handlers.DownloadSupportBundle)
r.Path("/api/v1/troubleshoot/supportbundle/app/{appId}/cluster/{clusterId}/collect").Methods("OPTIONS", "POST").HandlerFunc(handlers.CollectSupportBundle)
r.Path("/api/v1/troubleshoot/analyzebundle/{bundleId}").Methods("POST").HandlerFunc(handlers.NodeProxy(upstream))

// redactor routes
Expand Down
30 changes: 30 additions & 0 deletions kotsadm/pkg/handlers/troubleshoot.go
Expand Up @@ -298,6 +298,36 @@ func DownloadSupportBundle(w http.ResponseWriter, r *http.Request) {
io.Copy(w, f)
}

func CollectSupportBundle(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(200)
return
}

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

a, err := app.Get(mux.Vars(r)["appId"])
if err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}

if err := supportbundle.Collect(a.ID, mux.Vars(r)["clusterId"]); err != nil {
logger.Error(err)
w.WriteHeader(500)
return
}

JSON(w, 204, "")
}

func UploadSupportBundle(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")
Expand Down
14 changes: 14 additions & 0 deletions kotsadm/pkg/supportbundle/supportbundle.go
Expand Up @@ -98,6 +98,20 @@ func Get(id string) (*types.SupportBundle, error) {
return s, nil
}

func Collect(appID string, clusterID string) error {
db := persistence.MustGetPGSession()
query := `insert into pending_supportbundle (id, app_id, cluster_id, created_at) values ($1, $2, $3, $4)`

id := ksuid.New().String()

_, err := db.Exec(query, id, appID, clusterID, time.Now())
if err != nil {
return errors.Wrap(err, "failed to insert support bundle")
}

return nil
}

func CreateBundle(requestedID string, appID string, archivePath string) (*types.SupportBundle, error) {
fi, err := os.Stat(archivePath)
if err != nil {
Expand Down
31 changes: 16 additions & 15 deletions kotsadm/web/src/components/troubleshoot/GenerateSupportBundle.jsx
@@ -1,14 +1,12 @@
import * as React from "react";
import Helmet from "react-helmet";
import { withRouter, Link } from "react-router-dom";
import { compose, withApollo, graphql } from "react-apollo";
import Modal from "react-modal";

import Loader from "../shared/Loader";
import CodeSnippet from "@src/components/shared/CodeSnippet";
import UploadSupportBundleModal from "../troubleshoot/UploadSupportBundleModal";
import ConfigureRedactorsModal from "./ConfigureRedactorsModal";
import { collectSupportBundle } from "../../mutations/TroubleshootMutations";
import { Utilities } from "../../utilities/utilities";
import { Repeater } from "../../utilities/repeater";

Expand Down Expand Up @@ -147,6 +145,7 @@ class GenerateSupportBundle extends React.Component {

collectBundle = (clusterId) => {
const { watch } = this.props;

this.setState({
isGeneratingBundle: true,
});
Expand All @@ -155,11 +154,20 @@ class GenerateSupportBundle extends React.Component {
bundle.id
});

this.props.collectSupportBundle(watch.id, clusterId);

setTimeout(() => {
this.redirectOnNewBundle(currentBundles);
}, 1000);
fetch(`${window.env.API_ENDPOINT}/troubleshoot/supportbundle/app/${watch?.id}/cluster/${clusterId}/collect`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
method: "POST",
})
.then(() => {
this.redirectOnNewBundle(currentBundles);
})
.catch((err) => {
console.log(err);
this.setState({ isGeneratingBundle: false });
});
}

redirectOnNewBundle(currentBundles) {
Expand Down Expand Up @@ -275,11 +283,4 @@ class GenerateSupportBundle extends React.Component {
}
}

export default withRouter(compose(
withApollo,
graphql(collectSupportBundle, {
props: ({ mutate }) => ({
collectSupportBundle: (appId, clusterId) => mutate({ variables: { appId, clusterId } })
})
}),
)(GenerateSupportBundle));
export default withRouter(GenerateSupportBundle);

0 comments on commit 323af55

Please sign in to comment.