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

ENG-2964 GetEnvironment RTKQuery #1343

Merged
merged 8 commits into from
May 22, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
package handler
package v2

import (
"context"
"net/http"
"os"

"github.com/aqueducthq/aqueduct/cmd/server/handler"
"github.com/aqueducthq/aqueduct/lib"
)

const inK8sClusterEnvVarName = "AQUEDUCT_IN_K8S_CLUSTER"

type getServerEnvironmentResponse struct {
type getEnvironmentResponse struct {
// Whether the server is running within a k8s cluster.
InK8sCluster bool `json:"inK8sCluster"`
Version string `json:"version"`
}

// Route: /api/environment
// Route: /api/v2/environment
// This file should map directly to
// src/ui/common/src/handlers/v2/EnvironmentGet.tsx
//
// Method: GET
// Request:
//
// Headers:
// `api-key`: user's API Key
//
// Response: Aqueduct server's environment variables.
type GetServerEnvironmentHandler struct {
GetHandler
type EnvironmentHandler struct {
handler.GetHandler
}

func (*GetServerEnvironmentHandler) Name() string {
return "GetServerEnvironment"
func (*EnvironmentHandler) Name() string {
return "GetEnvironment"
}

func (*GetServerEnvironmentHandler) Prepare(r *http.Request) (interface{}, int, error) {
func (*EnvironmentHandler) Prepare(r *http.Request) (interface{}, int, error) {
return nil, http.StatusOK, nil
}

func (h *GetServerEnvironmentHandler) Perform(ctx context.Context, interfaceArgs interface{}) (interface{}, int, error) {
func (h *EnvironmentHandler) Perform(ctx context.Context, interfaceArgs interface{}) (interface{}, int, error) {
inCluster := false
if os.Getenv(inK8sClusterEnvVarName) == "1" {
inCluster = true
}

return getServerEnvironmentResponse{
return getEnvironmentResponse{
InK8sCluster: inCluster,
Version: lib.ServerVersionNumber,
}, http.StatusOK, nil
Expand Down
4 changes: 2 additions & 2 deletions src/golang/cmd/server/routes/routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
NodeDagOperatorsRoute = "/api/v2/workflow/{workflowID}/dag/{dagID}/node/operators"
NodeOperatorContentRoute = "/api/v2/workflow/{workflowID}/dag/{dagID}/node/operator/{nodeID}/content"
NodesResultsRoute = "/api/v2/workflow/{workflowID}/result/{dagResultID}/nodes/results"
EnvironmentRoute = "/api/v2/environment"

// V1 routes
GetArtifactVersionsRoute = "/api/artifact/versions"
Expand Down Expand Up @@ -73,6 +74,5 @@ const (
GetWorkflowDagResultRoute = "/api/workflow/{workflowId}/result/{workflowDagResultId}"
GetWorkflowHistoryRoute = "/api/workflow/{workflowId}/history"

GetServerVersionRoute = "/api/version"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is any SDK code still using this route? To be safe, we could keep these routes and map them to the same handler (similar to workflow trigger / delete / edit routes)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the version route does have a usage. I will keep this one in place.

GetServerEnvironmentRoute = "/api/environment"
GetServerVersionRoute = "/api/version"
)
4 changes: 2 additions & 2 deletions src/golang/cmd/server/server/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ func (s *AqServer) Handlers() map[string]handler.Handler {
DAGResultRepo: s.DAGResultRepo,
OperatorRepo: s.OperatorRepo,
},
routes.EnvironmentRoute: &v2.EnvironmentHandler{},

// V1 Handlers
// (ENG-2715) Remove deprecated ones
Expand Down Expand Up @@ -425,7 +426,6 @@ func (s *AqServer) Handlers() map[string]handler.Handler {

IntegrationRepo: s.IntegrationRepo,
},
routes.GetServerVersionRoute: &handler.GetServerVersionHandler{},
routes.GetServerEnvironmentRoute: &handler.GetServerEnvironmentHandler{},
routes.GetServerVersionRoute: &handler.GetServerVersionHandler{},
}
}
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { Checkbox, FormControlLabel } from '@mui/material';
import Box from '@mui/material/Box';
import React from 'react';
import { useEffect, useState } from 'react';
import { useEffect } from 'react';
import { useFormContext } from 'react-hook-form';
import * as Yup from 'yup';

import {
IntegrationDialogProps,
KubernetesConfig,
} from '../../../utils/integrations';
import { apiAddress } from '../../hooks/useAqueductConsts';
import { IntegrationTextInputField } from './IntegrationTextInputField';

const Placeholders: KubernetesConfig = {
Expand All @@ -18,9 +17,14 @@ const Placeholders: KubernetesConfig = {
use_same_cluster: 'false',
};

export const KubernetesDialog: React.FC<IntegrationDialogProps> = ({
interface KuberentesDialogProps extends IntegrationDialogProps {
inK8sCluster: boolean;
}

export const KubernetesDialog: React.FC<KuberentesDialogProps> = ({
editMode = false,
user,
inK8sCluster = false,
}) => {
const { register, setValue, getValues } = useFormContext();
const use_same_cluster = getValues('use_same_cluster');
Expand All @@ -31,27 +35,6 @@ export const KubernetesDialog: React.FC<IntegrationDialogProps> = ({
setValue('use_same_cluster', 'false');
}, []);

const [inK8sCluster, setInK8sCluster] = useState(false);

// TODO: https://linear.app/aqueducthq/issue/ENG-2964/move-k8s-use-same-cluster-request-to-rtkquery
useEffect(() => {
const fetchEnvironment = async () => {
const environmentResponse = await fetch(`${apiAddress}/api/environment`, {
method: 'GET',
headers: {
'api-key': user.apiKey,
},
});

const responseBody = await environmentResponse.json();
setInK8sCluster(responseBody['inK8sCluster']);
};

if (user) {
fetchEnvironment().catch(console.error);
}
}, [user]);

return (
<Box sx={{ mt: 2 }}>
{inK8sCluster && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useFormContext } from 'react-hook-form';
import { useDispatch } from 'react-redux';
import * as Yup from 'yup';

import { useEnvironmentGetQuery } from '../../../handlers/AqueductApi';
import { handleConnectToNewIntegration } from '../../../reducers/integration';
import { AppDispatch } from '../../../stores/store';
import { IntegrationDialogProps } from '../../../utils/integrations';
Expand Down Expand Up @@ -42,6 +43,11 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
loading,
onCloseDialog,
}) => {
const {
data: environment,
error,
isLoading,
} = useEnvironmentGetQuery({ apiKey: user.apiKey });
const { register, setValue } = useFormContext();

const [currentStep, setCurrentStep] = useState('INITIAL');
Expand Down Expand Up @@ -137,14 +143,19 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
);
};

interface RegularK8sStepLayoutProps extends IntegrationDialogProps {
inK8sCluster?: boolean;
}

// We're going to need to share some more info with the dialogs, as they're not all just forms that we can
// register anymore in the case of this layout.
const RegularK8sStepLayout: React.FC<IntegrationDialogProps> = ({
const RegularK8sStepLayout: React.FC<RegularK8sStepLayoutProps> = ({
user,
editMode,
onCloseDialog,
loading,
disabled,
inK8sCluster = false,
}) => {
const methods = useFormContext();
const dispatch: AppDispatch = useDispatch();
Expand All @@ -170,6 +181,7 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
onCloseDialog={onCloseDialog}
loading={loading}
disabled={disabled}
inK8sCluster={inK8sCluster}
/>
<DialogActionButtons
onCloseDialog={onCloseDialog}
Expand Down Expand Up @@ -376,6 +388,7 @@ export const OnDemandKubernetesDialog: React.FC<IntegrationDialogProps> = ({
loading={loading}
onCloseDialog={onCloseDialog}
editMode={editMode}
inK8sCluster={environment?.inK8sCluster}
/>
);
case 'ONDEMAND_K8S':
Expand Down
27 changes: 11 additions & 16 deletions src/ui/common/src/components/layouts/menuSidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import { useDispatch } from 'react-redux';
import { Link as RouterLink, useLocation } from 'react-router-dom';
import UserProfile from 'src/utils/auth';

import { useEnvironmentGetQuery } from '../../handlers/AqueductApi';
import { AppDispatch } from '../../stores/store';
import { getPathPrefix } from '../../utils/getPathPrefix';
import { apiAddress } from '../hooks/useAqueductConsts';
import {
menuSidebar,
menuSidebarContent,
Expand Down Expand Up @@ -120,26 +120,21 @@ const MenuSidebar: React.FC<{
}> = ({ onSidebarItemClicked, user }) => {
const dispatch: AppDispatch = useDispatch();
const [currentPage, setCurrentPage] = useState(undefined);
const [versionNumber, setVersionNumber] = useState('');
const location = useLocation();

const { data } = useEnvironmentGetQuery(
{ apiKey: user.apiKey },
{
skip: !user?.apiKey,
}
);

console.log('data: ', data);

useEffect(() => {
setCurrentPage(location.pathname);
}, [dispatch, location.pathname]);

useEffect(() => {
async function fetchVersionNumber() {
const res = await fetch(`${apiAddress}/api/version`, {
method: 'GET',
headers: { 'api-key': user.apiKey },
});
const versionNumberResponse = await res.json();
setVersionNumber(versionNumberResponse.version);
}

fetchVersionNumber();
}, [user.apiKey]);

const pathPrefix = getPathPrefix();
return (
<Box style={menuSidebar}>
Expand Down Expand Up @@ -270,7 +265,7 @@ const MenuSidebar: React.FC<{
</Box>
<Box marginLeft="14px" marginBottom="16px">
<Typography variant="caption" sx={{ color: 'white' }}>
{versionNumber.length > 0 ? `v${versionNumber}` : ''}
{data?.version ? `v${data.version}` : ''}
</Typography>
</Box>
</Box>
Expand Down
12 changes: 12 additions & 0 deletions src/ui/common/src/handlers/AqueductApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ import {
DagResultsGetRequest,
DagResultsGetResponse,
} from './v2/DagResultsGet';
import {
environmentGetQuery,
EnvironmentGetRequest,
} from './v2/EnvironmentGet';
import {
integrationOperatorsGetQuery,
IntegrationOperatorsGetRequest,
Expand Down Expand Up @@ -134,6 +138,13 @@ export const aqueductApi = createApi({
query: (req) => dagResultsGetQuery(req),
transformErrorResponse,
}),
environmentGet: builder.query<
EnvironmentGetResponse,
EnvironmentGetRequest
>({
query: (req) => environmentGetQuery(req),
transformErrorResponse,
}),
integrationOperatorsGet: builder.query<
IntegrationOperatorsGetResponse,
IntegrationOperatorsGetRequest
Expand Down Expand Up @@ -246,6 +257,7 @@ export const {
useDagOperatorsGetQuery,
useDagResultGetQuery,
useDagResultsGetQuery,
useEnvironmentGetQuery,
useIntegrationOperatorsGetQuery,
useIntegrationWorkflowsGetQuery,
useIntegrationsWorkflowsGetQuery,
Expand Down
13 changes: 13 additions & 0 deletions src/ui/common/src/handlers/v2/EnvironmentGet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { APIKeyParameter } from '../parameters/Header';

export type EnvironmentGetRequest = APIKeyParameter;

export type EnvironmentGetResponse = {
inK8sCluster: boolean;
version: string;
};

export const environmentGetQuery = (req: EnvironmentGetRequest) => ({
url: 'environment',
headers: { 'api-key': req.apiKey },
});