Skip to content

Commit

Permalink
allow extensions to customize icons (containers#1899)
Browse files Browse the repository at this point in the history
Signed-off-by: lstocchi <lstocchi@redhat.com>
  • Loading branch information
lstocchi committed Jul 6, 2023
1 parent 762d35f commit 455ec94
Show file tree
Hide file tree
Showing 17 changed files with 1,730 additions and 3 deletions.
Binary file added extensions/kind/kind-icon.woff2
Binary file not shown.
17 changes: 17 additions & 0 deletions extensions/kind/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,30 @@
}
}
},
"icons": {
"kind-icon": {
"description": "Kind icon",
"default": {
"fontPath": "kind-icon.woff2",
"fontCharacter": "\\EA01"
}
}
},
"menus": {
"dashboard/image": [
{
"command": "kind.image.move",
"title": "Push image to Kind cluster"
}
]
},
"views": {
"icons/containersList": [
{
"when": "io.x-k8s.kind.cluster in containerLabelKeys",
"icon": "${kind-icon}"
}
]
}
},
"scripts": {
Expand Down
1 change: 1 addition & 0 deletions packages/main/src/plugin/api/container-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export interface ContainerInfo extends Dockerode.ContainerInfo {
status: string;
engineId: string;
};
icon?: string;
}

export interface SimpleContainerInfo extends Dockerode.ContainerInfo {
Expand Down
21 changes: 21 additions & 0 deletions packages/main/src/plugin/api/view-info.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**********************************************************************
* Copyright (C) 2023 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
***********************************************************************/
export interface ViewContribution {
when: string | null | undefined;
icon: string;
}
23 changes: 23 additions & 0 deletions packages/main/src/plugin/container-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ import { Emitter } from './events/emitter.js';
import fs from 'node:fs';
import { pipeline } from 'node:stream/promises';
import type { ApiSenderType } from './api.js';
import { Context, ContextKeyService } from './contextkey/contextKeyService.js';
import { ContextKeyExpr, IContext } from './contextkey/contextKey.js';
import { ViewRegistry } from './view-registry.js';
export interface InternalContainerProvider {
name: string;
id: string;
Expand Down Expand Up @@ -79,6 +82,7 @@ export class ContainerProviderRegistry {
private apiSender: ApiSenderType,
private imageRegistry: ImageRegistry,
private telemetryService: Telemetry,
private viewRegistry: ViewRegistry,
) {
const libPodDockerode = new LibpodDockerode();
libPodDockerode.enhancePrototypeWithLibPod();
Expand Down Expand Up @@ -266,6 +270,8 @@ export class ContainerProviderRegistry {

async listContainers(): Promise<ContainerInfo[]> {
let telemetryOptions = {};
const viewContribution = this.viewRegistry.getViewContribution('icons/containersList');
const containerContext = new ContextKeyService();
const containers = await Promise.all(
Array.from(this.internalProviders.values()).map(async provider => {
try {
Expand Down Expand Up @@ -312,6 +318,22 @@ export class ContainerProviderRegistry {
StartedAt = '';
}

// it prepares the context for the icon contributions
const contextId = containerContext.createChildContext();
const context = containerContext.getContextValuesContainer(contextId);
context.setValue('containerLabelKeys', Object.keys(container.Labels));
let icon;

// it checks if someone contributed to the containerlist view
viewContribution.every((contribution) => {
const contextExprDeserialized = ContextKeyExpr.deserialize(contribution.when);
if (contextExprDeserialized?.evaluate(context)) {
icon = contribution.icon;
return false;
}
return true;
});

// do we have a matching pod for this container ?
let pod;
const matchingPod = pods.find(pod =>
Expand All @@ -332,6 +354,7 @@ export class ContainerProviderRegistry {
engineId: provider.id,
engineType: provider.connection.type,
StartedAt,
icon,
};
return containerInfo;
}),
Expand Down

0 comments on commit 455ec94

Please sign in to comment.