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

Internal registry openshift push support #100

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
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
70 changes: 50 additions & 20 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ interface InternalRegistryInfo {
host: string;
username: string;
token: string;
namespace?: string;
}

const StartedStatus: extensionApi.ProviderConnectionStatus = 'started';
Expand All @@ -49,6 +50,9 @@ async function whoami(clusterUrl: string, token: string): Promise<string> {
headers: {
Authorization: `Bearer ${token}`,
},
https: {
rejectUnauthorized: false,
},
};

const username: string = await got(`${clusterUrl}/apis/user.openshift.io/v1/users/~`, gotOptions).then(response => {
Expand All @@ -64,12 +68,16 @@ async function whoami(clusterUrl: string, token: string): Promise<string> {
async function getOpenShiftInternalRegistryPublicHost(contextName: string): Promise<InternalRegistryInfo> {
const config = kubeconfig.createOrLoadFromFile(extensionApi.kubernetes.getKubeconfig().fsPath);
const context = config.getContextObject(contextName);
const namespace = context.namespace;
const cluster = config.getCluster(context.cluster);
const user = config.getUser(context.user);
const gotOptions = {
headers: {
Authorization: `Bearer ${user.token}`,
},
https: {
rejectUnauthorized: false,
},
};
const publicRegistry: string = await got(
`${cluster.server}/apis/image.openshift.io/v1/namespaces/openshift/imagestreams`,
Expand All @@ -90,6 +98,7 @@ async function getOpenShiftInternalRegistryPublicHost(contextName: string): Prom
return {
host,
username,
namespace,
token: user.token,
};
}
Expand All @@ -101,10 +110,18 @@ export async function pushImageToOpenShiftRegistry(image: ImageInfo): Promise<vo
.filter(connection => connection.status === 'started')
.map(connection => connection.connection.name);
if (!qp.length) {
extensionApi.window.showInformationMessage(
'You have no running Developer Sandbox connections. Please create new one and try again.',
// try to use crc contexts
const config = kubeconfig.createOrLoadFromFile(extensionApi.kubernetes.getKubeconfig().fsPath);
const crcContexts = config.contexts.filter(context =>
config.getCluster(context.cluster).server.startsWith(`https://api.crc.testing`),
);
return;
if (crcContexts.length === 0) {
extensionApi.window.showInformationMessage(
'You have no running Developer Sandbox connections. Please create new one and try again.',
);
return;
}
crcContexts.forEach(ct => qp.push(ct.name));
}
let targetSb: string;
if (qp.length > 1) {
Expand All @@ -130,14 +147,25 @@ export async function pushImageToOpenShiftRegistry(image: ImageInfo): Promise<vo
const imageShortName = lastIndexOfSlash !== -1 ? image.name.substring(lastIndexOfSlash + 1) : image.name;
const imageTagSuffix = image.tag ? `:${image.tag}` : ``;
const localImageName = `${image.name}${imageTagSuffix}`;
const remoteImageName = `${registryInfo.host}/${registryInfo.username}-dev/${imageShortName}${imageTagSuffix}`;
const remoteImageName = registryInfo.namespace
? `${registryInfo.host}/${registryInfo.namespace}/${imageShortName}${imageTagSuffix}`
: `${registryInfo.host}/${registryInfo.username}-dev/${imageShortName}${imageTagSuffix}`;
if (localImageName !== remoteImageName) {
await extensionApi.containerEngine.tagImage(
image.engineId,
image.name + imageTagSuffix,
`${registryInfo.host}/${registryInfo.username}-dev/${imageShortName}`,
image.tag,
);
if (registryInfo.namespace) {
await extensionApi.containerEngine.tagImage(
image.engineId,
image.name + imageTagSuffix,
`${registryInfo.host}/${registryInfo.namespace}/${imageShortName}`,
image.tag,
);
} else {
await extensionApi.containerEngine.tagImage(
image.engineId,
image.name + imageTagSuffix,
`${registryInfo.host}/${registryInfo.username}-dev/${imageShortName}`,
image.tag,
);
}
}
progress.report({ increment: 75 });
await new Promise(async (resolve, reject) => {
Expand Down Expand Up @@ -242,15 +270,17 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):
'A free, private OpenShift environment including one project and a resource quota of 14 GB RAM, and 40 GB storage. It lasts 30 days.\n\nSign up at [https://developers.redhat.com/developer-sandbox](https://developers.redhat.com/developer-sandbox/?sc_cid=7013a000003SUmgAAG).',
};

extensionApi.commands.registerCommand('sandbox.open.login.url', () => {
extensionApi.env
.openExternal(
extensionApi.Uri.parse('https://developers.redhat.com/developer-sandbox/?sc_cid=7013a000003SUmgAAG'),
)
.then(successful => {
TelemetryLogger.logUsage('sandboxOpenLoginUrlRequest', { successful });
});
});
extensionContext.subscriptions.push(
extensionApi.commands.registerCommand('sandbox.open.login.url', () => {
extensionApi.env
.openExternal(
extensionApi.Uri.parse('https://developers.redhat.com/developer-sandbox/?sc_cid=7013a000003SUmgAAG'),
)
.then(successful => {
TelemetryLogger.logUsage('sandboxOpenLoginUrlRequest', { successful });
});
}),
);

provider = extensionApi.provider.createProvider(providerOptions);

Expand All @@ -260,7 +290,7 @@ export async function activate(extensionContext: extensionApi.ExtensionContext):

const kubeconfigUri = extensionApi.kubernetes.getKubeconfig();
const kubeconfigFile = kubeconfigUri.fsPath;
console.log('Configfile location', kubeconfigFile);
console.log('Config file location', kubeconfigFile);

const disposable = provider.setKubernetesProviderConnectionFactory({
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
Loading