Skip to content

Commit

Permalink
move disableAppGitOps mutation to go (#882)
Browse files Browse the repository at this point in the history
* move disableAppGitOps mutation to go
  • Loading branch information
sgalsaleh committed Jul 29, 2020
1 parent 8d59daa commit 95500dc
Show file tree
Hide file tree
Showing 5 changed files with 98 additions and 17 deletions.
2 changes: 1 addition & 1 deletion kotsadm/pkg/apiserver/server.go
Expand Up @@ -155,7 +155,7 @@ func Start() {
r.Path("/api/v1/prometheus").Methods("OPTIONS", "POST").HandlerFunc(handlers.SetPrometheusAddress)

// GitOps
r.HandleFunc("/api/v1/gitops", handlers.NotImplemented)
r.Path("/api/v1/gitops/app/{appId}/cluster/{clusterId}/disable").Methods("OPTIONS", "POST").HandlerFunc(handlers.DisableAppGitOps)

// to avoid confusion, we don't serve this in the dev env...
if os.Getenv("DISABLE_SPA_SERVING") != "1" {
Expand Down
30 changes: 30 additions & 0 deletions kotsadm/pkg/gitops/gitops.go
Expand Up @@ -166,6 +166,36 @@ func GetDownstreamGitOps(appID string, clusterID string) (*GitOpsConfig, error)
return nil, nil
}

func DisableDownstreamGitOps(appID string, clusterID string) 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")
}

configMap, err := clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Get(context.TODO(), "kotsadm-gitops", metav1.GetOptions{})
if kuberneteserrors.IsNotFound(err) {
return errors.Wrap(err, "gitops config map not found")
}

configMapDataKey := fmt.Sprintf("%s-%s", appID, clusterID)
_, ok := configMap.Data[configMapDataKey]
if ok {
delete(configMap.Data, configMapDataKey)
}

_, err = clientset.CoreV1().ConfigMaps(os.Getenv("POD_NAMESPACE")).Update(context.TODO(), configMap, metav1.UpdateOptions{})
if err != nil {
return errors.Wrap(err, "failed to update config map")
}

return nil
}

func gitOpsConfigFromSecretData(idx int64, secretData map[string][]byte) (string, string, string, string, error) {
provider := ""
publicKey := ""
Expand Down
53 changes: 53 additions & 0 deletions kotsadm/pkg/handlers/gitops.go
@@ -0,0 +1,53 @@
package handlers

import (
"net/http"

"github.com/gorilla/mux"
"github.com/replicatedhq/kots/kotsadm/pkg/app"
"github.com/replicatedhq/kots/kotsadm/pkg/gitops"
"github.com/replicatedhq/kots/kotsadm/pkg/logger"
)

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

appID := mux.Vars(r)["appId"]
clusterID := mux.Vars(r)["clusterId"]

a, err := app.Get(appID)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

downstreamGitOps, err := gitops.GetDownstreamGitOps(a.ID, clusterID)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}

if downstreamGitOps != nil {
err := gitops.DisableDownstreamGitOps(a.ID, clusterID)
if err != nil {
logger.Error(err)
w.WriteHeader(http.StatusInternalServerError)
return
}
}

JSON(w, http.StatusNoContent, "")
}
6 changes: 0 additions & 6 deletions kotsadm/web/src/components/apps/AppConfig.jsx
Expand Up @@ -10,7 +10,6 @@ import map from "lodash/map";
import Modal from "react-modal";
import Loader from "../shared/Loader";
import { getAppConfigGroups, getKotsApp, templateConfigGroups } from "../../queries/AppsQueries";
import { updateDownstreamsStatus } from "../../mutations/AppsMutations";

import "../../scss/components/watches/WatchConfig.scss";
import { Utilities } from "../../utilities/utilities";
Expand Down Expand Up @@ -327,9 +326,4 @@ export default withRouter(compose(
}
}
}),
graphql(updateDownstreamsStatus, {
props: ({ mutate }) => ({
updateDownstreamsStatus: (slug, sequence, status) => mutate({ variables: { slug, sequence, status } })
})
}),
)(AppConfig));
24 changes: 14 additions & 10 deletions kotsadm/web/src/components/apps/AppGitops.jsx
Expand Up @@ -5,8 +5,8 @@ import Helmet from "react-helmet";
import url from "url";
import GitOpsRepoDetails from "../gitops/GitOpsRepoDetails";
import CodeSnippet from "@src/components/shared/CodeSnippet";
import { testGitOpsConnection, disableAppGitops, updateAppGitOps, createGitOpsRepo } from "../../mutations/AppsMutations";
import { getServiceSite, getAddKeyUri, requiresHostname } from "../../utilities/utilities";
import { testGitOpsConnection, updateAppGitOps, createGitOpsRepo } from "../../mutations/AppsMutations";
import { getServiceSite, getAddKeyUri, requiresHostname, Utilities } from "../../utilities/utilities";
import Modal from "react-modal";

import "../../scss/components/gitops/GitOpsSettings.scss";
Expand Down Expand Up @@ -173,16 +173,25 @@ class AppGitops extends Component {

disableGitOps = async () => {
this.setState({ disablingGitOps: true });

const appId = this.props.app?.id;
let clusterId;
if (this.props.app?.downstreams?.length) {
clusterId = this.props.app.downstreams[0].cluster.id;
}

try {
await this.props.disableAppGitops(appId, clusterId);
this.props.history.push(`/app/${this.props.app?.slug}`);
this.props.refetch();
const res = await fetch(`${window.env.API_ENDPOINT}/gitops/app/${appId}/cluster/${clusterId}/disable`, {
headers: {
"Authorization": Utilities.getToken(),
"Content-Type": "application/json",
},
method: "POST",
});
if (res.ok && res.status === 204) {
this.props.history.push(`/app/${this.props.app?.slug}`);
this.props.refetch();
}
} catch (err) {
console.log(err);
} finally {
Expand Down Expand Up @@ -381,11 +390,6 @@ export default compose(
testGitOpsConnection: (appId, clusterId) => mutate({ variables: { appId, clusterId } })
})
}),
graphql(disableAppGitops, {
props: ({ mutate }) => ({
disableAppGitops: (appId, clusterId) => mutate({ variables: { appId, clusterId } })
})
}),
graphql(createGitOpsRepo, {
props: ({ mutate }) => ({
createGitOpsRepo: (gitOpsInput) => mutate({ variables: { gitOpsInput } })
Expand Down

0 comments on commit 95500dc

Please sign in to comment.