Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

move disableAppGitOps mutation to go #882

Merged
merged 3 commits into from
Jul 29, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion kotsadm/pkg/apiserver/server.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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