Skip to content

Commit

Permalink
Refactor responses, clean up logs
Browse files Browse the repository at this point in the history
  • Loading branch information
kberkos-public committed Apr 16, 2024
1 parent 4ea2862 commit fc656bc
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 84 deletions.
6 changes: 0 additions & 6 deletions src/extension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -659,22 +659,16 @@ function logIn(command: string, ocCmd: OcCommand, session: Session, outputChanne
} else {
args = params;
}
console.log("login args: " + args);
if (args) {
console.log("login if");
console.log("logpath: " + logPath);
ocCmd
.runOcLoginCommand(args, outputChannel, logPath)
.then(() => {
session.loggedIntoOpenShift = true;
console.log("inside then");
vscode.window.showInformationMessage("Successfully logged into OpenShift cluster");
vscode.commands.executeCommand(VSCodeCommands.refreshAll);
})
.catch(e => {
console.log("GOTCHA");
session.loggedIntoOpenShift = false;
console.log("login error:" + e);
showErrorMessage(`Failure logging into OpenShift cluster: ${e}`);
});
}
Expand Down
41 changes: 15 additions & 26 deletions src/kubernetes/kubernetes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,6 @@ export class KubernetesObj extends KubernetesContext {
* @returns - A promise containing a boolean
*/
public async isUserLoggedIntoOCP(): Promise<boolean> {
console.log("kyle2");
console.log(this.coreV1Api);
if (this.coreV1Api) {
const request: k8s.CoreV1ApiListNamespacedPodRequest = {
namespace: this.namespace,
Expand Down Expand Up @@ -247,13 +245,12 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
let customResourcesString = JSON.stringify(res.body);
let customResourcesString = JSON.stringify(res);
let customResourcesList: ObjectList = JSON.parse(customResourcesString);
return customResourcesList;
})
.catch(e => {
console.log("kubernetes 1");
if (e.response && e.response.statusCode && e.response.statusCode === 404) {
if (e.code === 404) {
// 404s are fine since there's a chance that the CRD or API Version hasn't yet been created on the cluster
return undefined;
} else {
Expand Down Expand Up @@ -286,8 +283,7 @@ export class KubernetesObj extends KubernetesContext {
return true;
})
.catch(e => {
console.log("kubernetes2");
if (e.response && e.response.statusCode && e.response.statusCode === 404) {
if (e.code === 404) {
// 404s are fine since there's a chance that the CRD or API Version hasn't yet been created on the cluster
return false;
} else {
Expand Down Expand Up @@ -342,13 +338,12 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
objsString = JSON.stringify(res.body);
objsString = JSON.stringify(res);
let objsList: ObjectList = JSON.parse(objsString);
return objsList;
})
.catch(e => {
console.log("kubernetes3");
if (e.response && e.response.statusCode && e.response.statusCode === 404) {
if (e.code === 404) {
// 404s are fine since there's a chance that the CRD or API Version hasn't yet been created on the cluster
return undefined;
} else {
Expand All @@ -368,13 +363,12 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
objsString = JSON.stringify(res.body);
objsString = JSON.stringify(res);
let objsList: ObjectList = JSON.parse(objsString);
return objsList;
})
.catch(e => {
console.log("kubernetes4");
if (e.response && e.response.statusCode && e.response.statusCode === 404) {
if (e.code === 404) {
// 404s are fine since there's a chance that the CRD or API Version hasn't yet been created on the cluster
return undefined;
} else {
Expand Down Expand Up @@ -406,7 +400,7 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.getNamespacedCustomObject(request)
.then(res => {
return res.body;
return res;
})
.catch(e => {
const msg = `Failure retrieving custom resource object. ${JSON.stringify(e)}`;
Expand All @@ -433,7 +427,7 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
let crInstacesString = JSON.stringify(res.body);
let crInstacesString = JSON.stringify(res);
let crInstanceList: ObjectList = JSON.parse(crInstacesString);

if (crInstanceList.items.length === 0) {
Expand All @@ -447,8 +441,7 @@ export class KubernetesObj extends KubernetesContext {
return crInstanceNames;
})
.catch(e => {
console.log("kubernetes5");
if (e.response && e.response.statusCode && e.response.statusCode === 404) {
if (e.code === 404) {
return undefined;
} else {
const msg = `Failure retrieving Custom Resource instance names. ${JSON.stringify(e)}`;
Expand Down Expand Up @@ -492,13 +485,12 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
console.log("kubernetes6");
if (res && res.response && res.response.statusCode) {
if (res.response.statusCode !== 200) {
return undefined;
}
else {
let csvInstacesString = JSON.stringify(res.body);
let csvInstacesString = JSON.stringify(res);
let csvInstanceList: ObjectList = JSON.parse(csvInstacesString);
if (csvInstanceList.items.length > 0) {
return csvInstanceList.items[0];
Expand Down Expand Up @@ -568,9 +560,8 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
console.log("kubernetes7");
if (res.response && res.response.statusCode && res.response.statusCode === 200) {
let zosCloudBrokerInstancesListString = JSON.stringify(res.body);
if (res && res.items && res.items.length > 0) {
let zosCloudBrokerInstancesListString = JSON.stringify(res);
let zosCloudBrokerInstanceList: ObjectList = JSON.parse(zosCloudBrokerInstancesListString);
if (zosCloudBrokerInstanceList.items.length === 0) {
return false;
Expand Down Expand Up @@ -602,7 +593,6 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.getNamespacedCustomObject(request)
.then(res => {
console.log("kubernetes8");
if (res.response && res.response.statusCode && res.response.statusCode === 200) {
return true;
}
Expand Down Expand Up @@ -656,9 +646,8 @@ export class KubernetesObj extends KubernetesContext {
}
})
.catch(e => {
console.log("kubernetes9");
// Bypass 403 and 401 error messages since these will always occur when the user is logged out
if (e.statusCode !== 403 && e.statusCode !== 401) {
if (e.code !== 403 && e.code !== 401) {
const msg = `Failure retrieving Namespace list: ${JSON.stringify(e)}`;
console.error(msg);
}
Expand Down Expand Up @@ -696,7 +685,7 @@ export class KubernetesObj extends KubernetesContext {
return this.customObjectsApi
?.listNamespacedCustomObject(request)
.then(res => {
const objsString = JSON.stringify(res.body);
const objsString = JSON.stringify(res);
const objsList: ObjectList = JSON.parse(objsString);
if (objsList.items.length === 0) {
showErrorMessage("OperatorCollection resource not detected in namespace");
Expand Down
5 changes: 0 additions & 5 deletions src/kubernetes/kubernetesContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ export class KubernetesContext {
public openshiftServerURL: string | undefined = "";
constructor(namespace?: string) {
const kc = new k8s.KubeConfig();
console.log("Namespace: " + namespace);
if (namespace === undefined) {
if (fs.existsSync("/var/run/secrets/kubernetes.io/serviceaccount")) {
kc.loadFromCluster();
Expand All @@ -37,7 +36,6 @@ export class KubernetesContext {
const homeDirPath = k8s.findHomeDir();
const kcPath = homeDirPath ? path.join(homeDirPath, ".kube", "config") : path.join(".kube", "config");
if (!fs.existsSync(kcPath) || !kc.currentContext || kc.currentContext === "loaded-context") {
console.log("inside if");
// If kc is still empty, the Kube Config file is likely invalid
vscode.window.showWarningMessage("Your KubeConfig file has not been properly configured.");

Expand All @@ -51,13 +49,10 @@ export class KubernetesContext {
}
});
} else {
console.log("inside else");
// validate kube config by trying to make Api Clients
try {
this.openshiftServerURL = kc.getCurrentCluster()?.server;
this.coreV1Api = kc.makeApiClient(k8s.CoreV1Api);
console.log("coreV1Api:");
console.log(this.coreV1Api);
this.customObjectsApi = kc.makeApiClient(k8s.CustomObjectsApi);
} catch (error) {
showErrorMessage(`Failed to validate KubeConfig file. ${error}`);
Expand Down
1 change: 0 additions & 1 deletion src/shellCommands/ocSdkCommands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ async function getRequest(apiUrl: string): Promise<string | undefined> {
resp.on("data", chunk => {
data += chunk;
});
console.log("ocSdkCommands-376");
if (resp && resp.statusCode === 200) {
resp.on("end", () => {
resolve(JSON.parse(data));
Expand Down
Loading

0 comments on commit fc656bc

Please sign in to comment.