Skip to content

Commit

Permalink
move resetGitOpsData mutation to go (#893)
Browse files Browse the repository at this point in the history
  • Loading branch information
sgalsaleh committed Jul 31, 2020
1 parent 5ed3eea commit 59c870a
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 7 deletions.
1 change: 1 addition & 0 deletions kotsadm/pkg/apiserver/server.go
Expand Up @@ -157,6 +157,7 @@ func Start() {
// GitOps
r.Path("/api/v1/gitops/app/{appId}/cluster/{clusterId}/update").Methods("OPTIONS", "PUT").HandlerFunc(handlers.UpdateAppGitOps)
r.Path("/api/v1/gitops/app/{appId}/cluster/{clusterId}/disable").Methods("OPTIONS", "POST").HandlerFunc(handlers.DisableAppGitOps)
r.Path("/api/v1/gitops/reset").Methods("OPTIONS", "POST").HandlerFunc(handlers.ResetGitOps)

// to avoid confusion, we don't serve this in the dev env...
if os.Getenv("DISABLE_SPA_SERVING") != "1" {
Expand Down
24 changes: 24 additions & 0 deletions kotsadm/pkg/gitops/gitops.go
Expand Up @@ -285,6 +285,30 @@ func UpdateDownstreamGitOps(appID, clusterID, uri, branch, path, format, action
return nil
}

func ResetGitOps() error {
cfg, err := config.GetConfig()
if err != nil {
return errors.Wrap(err, "failed to get cluster config")
}

clientset, err := kubernetes.NewForConfig(cfg)
if err != nil {
return errors.Wrap(err, "failed to create kubernetes clientset")
}

err = clientset.CoreV1().Secrets(os.Getenv("POD_NAMESPACE")).Delete(context.TODO(), "kotsadm-gitops", metav1.DeleteOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to delete secret")
}

err = clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Delete(context.TODO(), "kotsadm-gitops", metav1.DeleteOptions{})
if err != nil && !kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "failed to delete configmap")
}

return nil
}

func gitOpsConfigFromSecretData(idx int64, secretData map[string][]byte) (string, string, string, string, error) {
provider := ""
publicKey := ""
Expand Down
23 changes: 23 additions & 0 deletions kotsadm/pkg/handlers/gitops.go
Expand Up @@ -104,3 +104,26 @@ func DisableAppGitOps(w http.ResponseWriter, r *http.Request) {

JSON(w, http.StatusNoContent, "")
}

func ResetGitOps(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
}

if err := gitops.ResetGitOps(); err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

JSON(w, http.StatusNoContent, "")
}
30 changes: 23 additions & 7 deletions kotsadm/web/src/components/gitops/GitOpsDeploymentManager.jsx
Expand Up @@ -8,7 +8,7 @@ import { graphql, compose, withApollo } from "react-apollo";
import { listApps, getGitOpsRepo } from "@src/queries/AppsQueries";
import GitOpsFlowIllustration from "./GitOpsFlowIllustration";
import GitOpsRepoDetails from "./GitOpsRepoDetails";
import { createGitOpsRepo, updateGitOpsRepo, resetGitOpsData } from "@src/mutations/AppsMutations";
import { createGitOpsRepo, updateGitOpsRepo } from "@src/mutations/AppsMutations";
import { getServiceSite, requiresHostname, Utilities } from "../../utilities/utilities";

import "../../scss/components/gitops/GitOpsDeploymentManager.scss";
Expand Down Expand Up @@ -142,7 +142,10 @@ class GitOpsDeploymentManager extends React.Component {
const getGitOpsRepo = this.props.getGitOpsRepoQuery?.getGitOpsRepo;
if (getGitOpsRepo?.enabled) {
if (this.providerChanged()) {
await this.props.resetGitOpsData();
const success = await this.resetGitOps();
if (!success) {
return false;
}
await this.props.createGitOpsRepo(gitOpsInput);
} else {
const uriToUpdate = this.isSingleApp() ? getGitOpsRepo?.uri : "";
Expand Down Expand Up @@ -180,6 +183,24 @@ class GitOpsDeploymentManager extends React.Component {
}
}

resetGitOps = async () => {
try {
const res = await fetch(`${window.env.API_ENDPOINT}/gitops/reset`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
method: "POST",
});
if (res.ok && res.status === 204) {
return true;
}
} catch(err) {
console.log(err);
}
return false;
}

updateAppGitOps = async (appId, clusterId, gitOpsInput) => {
try {
const res = await fetch(`${window.env.API_ENDPOINT}/gitops/app/${appId}/cluster/${clusterId}/update`, {
Expand Down Expand Up @@ -536,9 +557,4 @@ export default compose(
updateGitOpsRepo: (gitOpsInput, uriToUpdate) => mutate({ variables: { gitOpsInput, uriToUpdate } })
})
}),
graphql(resetGitOpsData, {
props: ({ mutate }) => ({
resetGitOpsData: () => mutate()
})
}),
)(GitOpsDeploymentManager);

0 comments on commit 59c870a

Please sign in to comment.