Skip to content

Commit

Permalink
Merge pull request #10 from fuchunlan/change-extension
Browse files Browse the repository at this point in the history
fix: Update frontend loading  mode and api path
  • Loading branch information
wansir authored Mar 5, 2024
2 parents b653b5f + 8d66475 commit e812926
Show file tree
Hide file tree
Showing 11 changed files with 41 additions and 30 deletions.
2 changes: 1 addition & 1 deletion charts/gatekeeper/charts/extension/dist/index.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions modules/web/extensions/gatekeeper/dist/index.js

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ const CreateConstraintForm = ({
className,
store,
initialValues,
cluster,
onChange = () => {},
}) => {
const [kindOptions, setKindOptions] = useState([])
Expand All @@ -44,7 +45,7 @@ const CreateConstraintForm = ({
}, [])

const handleGetKind = async () => {
const result = await store.fetchConstraintKind()
const result = await store.fetchConstraintKind({cluster})

const options =
result?.data.map(item => {
Expand Down Expand Up @@ -88,7 +89,7 @@ const CreateConstraintForm = ({
}
const { kind } = form.getFieldsValue()

const res = await store.checkName({ name: value, kind: kind.toLowerCase() })
const res = await store.checkName({ name: value, kind: kind.toLowerCase(), cluster })
if (res.exist) {
return Promise.reject({ message: t('NAME_EXIST_DESC') })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const CreateConstraintModal = ({
initialValues,
onOk,
store,
cluster,
}) => {
const [isCodeMode, setIsCodeMode] = useState(false)
const [formData, setFormData] = useState(initialValues)
Expand Down Expand Up @@ -72,6 +73,7 @@ const CreateConstraintModal = ({
initialValues={initialValues}
onChange={handleChange}
store={store}
cluster={cluster}
/>
)}
</Modal>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function ConstraintTemplateConstraints() {
};
}

const url = constraintStore.getDetailUrl({ name });
const url = constraintStore.getDetailUrl(params);
const columns = [
{
title: t('name'),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ const ConstraintList = () => {
const constraintRef = useRef();
const [form] = useForm();
const [createVisible, setCreateVisible] = useState(false);
const module = constraintStore.module;
const {module, getResourceUrl} = constraintStore;
const formTemplate = FORM_TEMPLATES[module]();

const url = getResourceUrl(params,true);

const { editYaml, del } = useCommonActions({
store: constraintStore,
params: { cluster },
Expand Down Expand Up @@ -127,7 +129,7 @@ const ConstraintList = () => {

const handleCreate = data => {
constraintStore
.post({ kind: data.kind.toLowerCase() }, data)
.post({ kind: data.kind.toLowerCase(), cluster }, data)
.then(res => {
if (res) {
callback();
Expand Down Expand Up @@ -155,7 +157,7 @@ const ConstraintList = () => {
}}
serverDataFormat={formatServerData}
placeholder={t('SEARCH_BY_NAME')}
url={`/kapis/constraints.gatekeeper.sh/v1beta1/constraints`}
url={url}
useStorageState={false}
disableRowSelect={false}
selectType={false}
Expand All @@ -171,6 +173,7 @@ const ConstraintList = () => {
setCreateVisible(false);
}}
store={constraintStore}
cluster={cluster}
/>
)}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,9 @@ const ConstraintTemplateList = () => {
const { cluster } = useParams();
const params = useParams();
const templateRef = useRef();
const requestTemplatePrefix = '/apis/templates.gatekeeper.sh/v1';
const [form] = useForm();
const [createVisible, setCreateVisible] = useState(false);
const module = constraintTemplateStore.module;
const {module, getResourceUrl} = constraintTemplateStore;
const formTemplate = FORM_TEMPLATES[module]();
const formatFn = data => {
return {
Expand Down Expand Up @@ -156,7 +155,7 @@ const ConstraintTemplateList = () => {
format={formatFn}
serverDataFormat={formatServerData}
placeholder={t('SEARCH_BY_NAME')}
url={`${requestTemplatePrefix}/constrainttemplates`}
url={getResourceUrl(params)}
useStorageState={false}
disableRowSelect={false}
selectType={false}
Expand Down
7 changes: 3 additions & 4 deletions modules/web/extensions/gatekeeper/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,19 @@ const menus = [{
icon: 'key',
assets:'https://open-policy-agent.github.io/gatekeeper/website/img/logo.svg',
order: 99,
skipAuth: true,
},{
parent: 'cluster.gatekeeper',
name: 'gatekeeper.constrainttemplates',
title: 'Constraint Templates',
order: 0,
skipAuth: true,
clusterModule: 'gatekeeper',
},
{
parent: 'cluster.gatekeeper',
name: 'gatekeeper.constraints',
title: 'Constraints',
order: 1,
skipAuth: true,
clusterModule: 'gatekeeper',
}]


Expand All @@ -31,4 +30,4 @@ const extensionConfig = {
locales,
};

globals.context.registerExtension(extensionConfig);
export default extensionConfig;
28 changes: 14 additions & 14 deletions modules/web/extensions/gatekeeper/src/store/constraint.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { BaseStore, request } from '@ks-console/shared'
import { BaseStore, getPath, request } from '@ks-console/shared'
import { find, get, isEmpty } from 'lodash'
import { API_VERSIONS } from '../utils/constants'

const module = 'constraints'

Expand Down Expand Up @@ -31,16 +32,19 @@ spec:
parameters: {}
`

const getResourceUrl = params => {
if(params.kind){
return `apis/constraints.gatekeeper.sh/v1beta1/${params.kind.toLowerCase()}`

const getResourceUrl = (params, ksVersion) => {
if (ksVersion) {
return `kapis/constraints.gatekeeper.sh/v1beta1${getPath(params)}/${module}`;
} else {
if (params.kind) {
return `${API_VERSIONS[module]}${getPath(params)}/${params.kind.toLowerCase()}`;
}
return `${API_VERSIONS[module]}${getPath(params)}`;
}
return 'apis/constraints.gatekeeper.sh/v1beta1'
}

const fetchConstraintKind = async () => {
const url = `apis/templates.gatekeeper.sh/v1/constrainttemplates`
const fetchConstraintKind = async (params) => {
const url = `${API_VERSIONS['constrainttemplates']}${getPath(params)}/constrainttemplates`
const result = await request.get(url)
const items = get(result, 'items', [])
return {
Expand All @@ -49,19 +53,16 @@ const fetchConstraintKind = async () => {
}
}

const checkNameFn = async ({ name, kind }) => {
const checkNameFn = async ({ name, kind,cluster }) => {
if (kind) {
const url = `kapis/constraints.gatekeeper.sh/v1beta1/constraints`
const url = `${API_VERSIONS[module]}${getPath({cluster})}/${module}`
const result = await request.get(url)
const items = get(result, 'items', [])
return isEmpty(find(items, { 'metadata.name': name, kind }))
}
return false
}

const create = async data => {
return request.post(`apis/constraints.gatekeeper.sh/v1beta1/${module}`, data)
}

const { ...baseStore } = BaseStore({
module,
Expand All @@ -76,7 +77,6 @@ const constraintStore = {
mapper,
fetchConstraintKind,
yamlRawData,
create,
checkNameFn,
}

Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions modules/web/extensions/gatekeeper/src/utils/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const API_VERSIONS = {
constraints: 'apis/constraints.gatekeeper.sh/v1beta1',
constrainttemplates: 'apis/templates.gatekeeper.sh/v1'
}

0 comments on commit e812926

Please sign in to comment.