Skip to content
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
12 changes: 11 additions & 1 deletion frontend/packages/console-demo-plugin/src/plugin.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@ import {
ResourceListPage,
ResourceDetailPage,
Perspective,
YAMLTemplate,
} from '@console/plugin-sdk';

// TODO(vojtech): internal code needed by plugins should be moved to console-shared package
import { PodModel } from '@console/internal/models';
import { FLAGS } from '@console/internal/const';

import * as models from './models';
import { yamlTemplates } from './yaml-templates';

type ConsumedExtensions =
| ModelDefinition
Expand All @@ -27,7 +29,8 @@ type ConsumedExtensions =
| ResourceClusterNavItem
| ResourceListPage
| ResourceDetailPage
| Perspective;
| Perspective
| YAMLTemplate;

const plugin: Plugin<ConsumedExtensions> = [
{
Expand Down Expand Up @@ -110,6 +113,13 @@ const plugin: Plugin<ConsumedExtensions> = [
landingPageURL: '/search',
},
},
{
type: 'YAMLTemplate',
properties: {
model: models.FooBarModel,
template: yamlTemplates.getIn([models.FooBarModel, 'default']),
},
},
];

export default plugin;
12 changes: 12 additions & 0 deletions frontend/packages/console-demo-plugin/src/yaml-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Map as ImmutableMap } from 'immutable';

import { FooBarModel } from './models';

export const yamlTemplates = ImmutableMap()
.setIn([FooBarModel, 'default'], `
apiVersion: ${FooBarModel.apiVersion}
kind: ${FooBarModel.kind}
metadata:
name: example
namespace: default
`);
5 changes: 5 additions & 0 deletions frontend/packages/console-plugin-sdk/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isNavItem,
isResourcePage,
isPerspective,
isYAMLTemplate,
} from './typings';

/**
Expand Down Expand Up @@ -39,4 +40,8 @@ export class ExtensionRegistry {
public getPerspectives() {
return this.extensions.filter(isPerspective);
}

public getYAMLTemplates() {
return this.extensions.filter(isYAMLTemplate);
}
}
1 change: 1 addition & 0 deletions frontend/packages/console-plugin-sdk/src/typings/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,4 @@ export * from './models';
export * from './nav';
export * from './pages';
export * from './perspective';
export * from './yaml-templates';
18 changes: 18 additions & 0 deletions frontend/packages/console-plugin-sdk/src/typings/yaml-templates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Extension } from '.';
import { K8sKind } from '@console/internal/module/k8s';

namespace ExtensionProperties {
export interface YAMLTemplate {
model: K8sKind;
template: string;
templateName?: string;
}
}

export interface YAMLTemplate extends Extension<ExtensionProperties.YAMLTemplate> {
type: 'YAMLTemplate';
}

export function isYAMLTemplate(e: Extension<any>): e is YAMLTemplate {
return e.type === 'YAMLTemplate';
}
20 changes: 19 additions & 1 deletion frontend/public/models/yaml-templates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ import { Map as ImmutableMap } from 'immutable';

import { GroupVersionKind, referenceForModel } from '../module/k8s';
import * as k8sModels from '../models';
import * as plugins from '../plugins';

/**
* Sample YAML manifests for some of the statically-defined Kubernetes models.
*/
export const yamlTemplates = ImmutableMap<GroupVersionKind, ImmutableMap<string, string>>()
const baseTemplates = ImmutableMap<GroupVersionKind, ImmutableMap<string, string>>()
.setIn(['DEFAULT', 'default'], `
apiVersion: ''
kind: ''
Expand Down Expand Up @@ -809,3 +810,20 @@ spec:
kind: MachineSet
name: worker
`);

const pluginTemplates = ImmutableMap<GroupVersionKind, ImmutableMap<string, string>>()
.withMutations(map => {
plugins.registry.getYAMLTemplates().forEach(yt => {
const modelRef = referenceForModel(yt.properties.model);
const templateName = yt.properties.templateName || 'default';

if (!baseTemplates.hasIn([modelRef, templateName])) {
map.setIn([modelRef, templateName], yt.properties.template);
} else {
// eslint-disable-next-line no-console
console.warn(`attempt to redefine YAML template ${templateName} for model ${modelRef}`);
Copy link
Contributor

Choose a reason for hiding this comment

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

What about throwing and Error? IMO, this conflict should be avoided.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This code gets referenced & evaluated very early during Console startup.

Consequently, throwing an Error here will effectively break the whole application - just a blank page with error stack trace shown in the browser console. This is not the behavior we want.

In good browsers, console.warn and console.error provide you with a link to source code (based on source maps) if you want to investigate the issue.

Copy link
Contributor

Choose a reason for hiding this comment

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

This should be caught at build time.
@vojtechszocs and I talked about fixing this situation by creating unit tests to avoid duplicates and therefore can avoid this type of runtime validation.

}
});
});

export const yamlTemplates = baseTemplates.merge(pluginTemplates);