Skip to content

Commit

Permalink
Read from tekton hub config to enable/disable the hub tasks in pipeli…
Browse files Browse the repository at this point in the history
…ne builder
  • Loading branch information
karthikjeeyar committed Jan 4, 2022
1 parent b1a553e commit 3cd71c7
Show file tree
Hide file tree
Showing 7 changed files with 238 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,8 @@
"Pod not found": "Pod not found",
"TaskRun log": "TaskRun log",
"{{taskLabel}} details": "{{taskLabel}} details",
"TektonConfig": "TektonConfig",
"TektonConfigs": "TektonConfigs",
"Pipeline {{status}}": "Pipeline {{status}}",
"Pipeline status is {{status}}. View logs.": "Pipeline status is {{status}}. View logs.",
"Pipeline not started. Start pipeline.": "Pipeline not started. Start pipeline.",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,15 @@
import { useK8sGet } from '@console/internal/components/utils/k8s-get-hook';
import { testHook } from '../../../../../../__tests__/utils/hooks-utils';
import { tekonHubPlatformTasks } from '../../../test-data/catalog-item-data';
import { filterBySupportedPlatforms } from '../catalog-utils';
import {
IntegrationTypes,
tektonHubIntegrationConfigs,
} from '../../../test-data/tekon-config-data';
import { filterBySupportedPlatforms, useTektonHubIntegration } from '../catalog-utils';

jest.mock('@console/internal/components/utils/k8s-get-hook', () => ({
useK8sGet: jest.fn(),
}));

describe('catalog-utils', () => {
const sampleTekonhubTasks = Object.values(tekonHubPlatformTasks);
Expand All @@ -26,4 +36,59 @@ describe('catalog-utils', () => {
expect(sampleTekonhubTasks.filter(filterBySupportedPlatforms)).toHaveLength(1);
});
});

describe('useTektonHubIntegration', () => {
it('Integration should be enabled if the config does not contain hub object', () => {
testHook(() => {
(useK8sGet as jest.Mock).mockReturnValue([{ spec: {} }, true, null]);
const tektonHubTasksEnabled = useTektonHubIntegration();
expect(tektonHubTasksEnabled).toBe(true);
});
});

it('Integration should be enabled if the config does not contain devconsole integration key', () => {
testHook(() => {
(useK8sGet as jest.Mock).mockReturnValue([
tektonHubIntegrationConfigs[IntegrationTypes.MISSING_INTEGRATION_KEY],
true,
]);
const tektonHubTasksEnabled = useTektonHubIntegration();
expect(tektonHubTasksEnabled).toBe(true);
});
});

it('Integration should be enabled if the config for devconsole integration key is available and set to true', () => {
testHook(() => {
(useK8sGet as jest.Mock).mockReturnValue([
tektonHubIntegrationConfigs[IntegrationTypes.ENABLED],
true,
]);
const tektonHubTasksEnabled = useTektonHubIntegration();
expect(tektonHubTasksEnabled).toBe(true);
});
});

it('Integration should be enabled by default if the fetch call errors out', () => {
testHook(() => {
(useK8sGet as jest.Mock).mockReturnValue([
tektonHubIntegrationConfigs[IntegrationTypes.ENABLED],
true,
{ error: 'cannot be fetched' },
]);
const tektonHubTasksEnabled = useTektonHubIntegration();
expect(tektonHubTasksEnabled).toBe(true);
});
});

it('Integration should be disabled if the config for devconsole integration key is available and set to false', () => {
testHook(() => {
(useK8sGet as jest.Mock).mockReturnValue([
tektonHubIntegrationConfigs[IntegrationTypes.DISABLED],
true,
]);
const tektonHubTasksEnabled = useTektonHubIntegration();
expect(tektonHubTasksEnabled).toBe(false);
});
});
});
});
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { useK8sGet } from '@console/internal/components/utils/k8s-get-hook';
import { K8sResourceKind } from '@console/internal/module/k8s';
import { TektonConfigModel } from '../../models';
import { TektonHubTask } from '../../types/tektonHub';
import { TEKTON_HUB_INTEGRATION_KEY } from './const';

export const getClusterPlatform = (): string =>
`${window.SERVER_FLAGS.GOOS}/${window.SERVER_FLAGS.GOARCH}`;
Expand All @@ -7,3 +11,17 @@ export const filterBySupportedPlatforms = (task: TektonHubTask): boolean => {
const supportedPlatforms = task?.platforms.map((p) => p.name) ?? [];
return supportedPlatforms.includes(getClusterPlatform());
};

export const useTektonHubIntegration = () => {
const [config, configLoaded, configLoadErr] = useK8sGet<K8sResourceKind>(
TektonConfigModel,
'config',
);
if (config && configLoaded && !configLoadErr) {
const devconsoleIntegrationEnabled = config.spec?.hub?.params.find(
(p) => p.name === TEKTON_HUB_INTEGRATION_KEY,
);
return devconsoleIntegrationEnabled ? devconsoleIntegrationEnabled.value : true;
}
return true;
};
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export const TEKTON_HUB_API_ENDPOINT = 'https://api.hub.tekton.dev';
export const TEKTON_HUB_ENDPOINT = `https://hub.tekton.dev`;
export const TEKTON_HUB_INTEGRATION_KEY = 'enable-devconsole-integration';
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { referenceForModel } from '@console/internal/module/k8s';
import { TaskModel } from '../../../models/pipelines';
import { TektonHubTask } from '../../../types/tektonHub';
import { TektonTaskProviders } from '../../pipelines/const';
import { filterBySupportedPlatforms } from '../catalog-utils';
import { filterBySupportedPlatforms, useTektonHubIntegration } from '../catalog-utils';
import { TEKTON_HUB_API_ENDPOINT } from '../const';
import useApiResponse from '../hooks/useApiResponse';

Expand Down Expand Up @@ -82,9 +82,11 @@ const useTektonHubTasksProvider: ExtensionHook<CatalogItem[]> = ({
verb: 'update',
});

const integrationEnabled = useTektonHubIntegration();

const [tektonHubTasks, tasksLoaded, tasksError] = useApiResponse<TektonHubTask>(
`${TEKTON_HUB_API_ENDPOINT}/resources`,
canCreateTask && canUpdateTask,
canCreateTask && canUpdateTask && integrationEnabled,
);

React.useMemo(
Expand Down
17 changes: 17 additions & 0 deletions frontend/packages/pipelines-plugin/src/models/pipelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,3 +220,20 @@ export const RepositoryModel: K8sKind = {
badge: BadgeType.DEV,
color,
};

export const TektonConfigModel: K8sKind = {
apiGroup: 'operator.tekton.dev',
apiVersion: 'v1alpha1',
label: 'TektonConfig',
// t('pipelines-plugin~TektonConfig')
labelKey: 'pipelines-plugin~TektonConfig',
// t('pipelines-plugin~TektonConfigs')
labelPluralKey: 'pipelines-plugin~TektonConfigs',
plural: 'tektonconfigs',
abbr: 'TC',
namespaced: false,
kind: 'TektonConfig',
id: 'tektonconfig',
labelPlural: 'TektonConfigs',
crd: true,
};
130 changes: 130 additions & 0 deletions frontend/packages/pipelines-plugin/src/test-data/tekon-config-data.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
import { K8sResourceKind } from 'public/module/k8s';
import { TEKTON_HUB_INTEGRATION_KEY } from '../components/catalog/const';

export enum IntegrationTypes {
ENABLED = 'enabled',
DISABLED = 'disabled',
MISSING_INTEGRATION_KEY = 'missing-integration-key',
}

type TekonHubIntegrationConfigs = { [key in IntegrationTypes]?: K8sResourceKind };

const sampleTektonConfig = {
apiVersion: 'operator.tekton.dev/v1alpha1',
kind: 'TektonConfig',
metadata: {
creationTimestamp: '2022-01-04T05:13:42Z',
finalizers: ['tektonconfigs.operator.tekton.dev'],
name: 'config',
resourceVersion: '97919',
uid: '3f5045e0-44b5-4a50-8ba8-1e55e9f953d0',
},
spec: {
addon: {
params: [
{
name: 'clusterTasks',
value: 'true',
},
{
name: 'pipelineTemplates',
value: 'true',
},
],
},
pipeline: {
'running-in-environment-with-injected-sidecars': true,
'metrics.taskrun.duration-type': 'histogram',
'disable-home-env-overwrite': true,
'metrics.pipelinerun.duration-type': 'histogram',
params: [
{
name: 'enableMetrics',
value: 'true',
},
],
'default-service-account': 'pipeline',
'disable-working-directory-overwrite': true,
'scope-when-expressions-to-task': false,
'require-git-ssh-secret-known-hosts': false,
'enable-tekton-oci-bundles': false,
'metrics.taskrun.level': 'task',
'metrics.pipelinerun.level': 'pipeline',
'enable-api-fields': 'stable',
'enable-custom-tasks': false,
'disable-creds-init': false,
'disable-affinity-assistant': true,
},
config: {},
params: [
{
name: 'createRbacResource',
value: 'true',
},
],
pruner: {
keep: 100,
resources: ['pipelinerun'],
schedule: '0 8 * * *',
},
profile: 'all',
targetNamespace: 'openshift-pipelines',
dashboard: {
readonly: false,
},
trigger: {
'default-service-account': 'pipeline',
'enable-api-fields': 'stable',
},
},
status: {
conditions: [
{
lastTransitionTime: '2022-01-04T05:15:04Z',
status: 'True',
type: 'ComponentsReady',
},
{
lastTransitionTime: '2022-01-04T05:15:54Z',
status: 'True',
type: 'PostInstall',
},
{
lastTransitionTime: '2022-01-04T05:13:54Z',
status: 'True',
type: 'PreInstall',
},
{
lastTransitionTime: '2022-01-04T05:15:54Z',
status: 'True',
type: 'Ready',
},
],
tektonInstallerSets: {
'rhosp-rbac': 'rbac-resources',
},
version: 'v1.6.1',
},
};

export const tektonHubIntegrationConfigs: TekonHubIntegrationConfigs = {
[IntegrationTypes.MISSING_INTEGRATION_KEY]: sampleTektonConfig,
[IntegrationTypes.ENABLED]: {
...sampleTektonConfig,
spec: {
...sampleTektonConfig.spec,
hub: {
params: [{ name: TEKTON_HUB_INTEGRATION_KEY, value: true }],
},
},
},
[IntegrationTypes.DISABLED]: {
...sampleTektonConfig,
spec: {
...sampleTektonConfig.spec,
hub: {
params: [{ name: TEKTON_HUB_INTEGRATION_KEY, value: false }],
},
},
},
};

0 comments on commit 3cd71c7

Please sign in to comment.