Skip to content

Commit

Permalink
add k8s
Browse files Browse the repository at this point in the history
  • Loading branch information
Flaque committed Nov 20, 2024
1 parent d38fad0 commit 921f627
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 66 deletions.
9 changes: 7 additions & 2 deletions deno.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@
"parse-duration": "^1.1.0",
"react": "^18.3.1",
"semver": "^7.6.3",
"tiny-invariant": "^1.3.3"
"tiny-invariant": "^1.3.3",
"yaml": "^2.6.1"
},
"devDependencies": {
"@types/semver": "^7.5.8"
Expand Down
28 changes: 28 additions & 0 deletions src/lib/clusters/clusters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,15 @@ export function registerClusters(program: Command) {
id,
});
});

// sf clusters users ls|list
clusters
.command("users list")
.description("List users in a cluster")
.option("--json", "Output in JSON format")
.action(async (options) => {
await listClusterUsersAction({ returnJson: options.json });
});
}

async function listClustersAction({ returnJson }: { returnJson?: boolean }) {
Expand Down Expand Up @@ -177,3 +186,22 @@ async function removeClusterUserAction({ id }: { id: string }) {

console.log(data);
}

async function listClusterUsersAction({ returnJson }: { returnJson?: boolean }) {
const api = await apiClient();

const { data, error, response } = await api.GET("/v0/credentials");

if (!response.ok) {
return logAndQuit(`Failed to list users in cluster: ${response.statusText}`);
}

if (!data) {
console.error(error);
return logAndQuit(
`Failed to list users in cluster: Unexpected response from server: ${response}`
);
}

console.log(data);
}
51 changes: 0 additions & 51 deletions src/lib/clusters/csr.ts

This file was deleted.

80 changes: 80 additions & 0 deletions src/lib/clusters/kubeconfig.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import yaml from "yaml";

interface Kubeconfig {
apiVersion: string;
clusters: {
name: string;
cluster: {
"certificate-authority-data": string;
server: string;
};
}[];
contexts: {
name: string;
context: {
cluster: string;
user: string;
namespace?: string;
};
}[];
users: {
name: string;
user: {
token?: string;
"client-certificate-data"?: string;
"client-key-data"?: string;
};
}[];
"current-context": string;
kind: string;
preferences: Record<string, unknown>;
}

export function createKubeConfigString(props: {
cluster: {
certificateAuthorityData: string;
kubernetesApiUrl: string;
name: string;
namespace?: string;
};
user: {
token: string;
name: string;
};
}) {
const kubeconfig: Kubeconfig = {
apiVersion: "v1",
clusters: [
{
name: props.cluster.name,
cluster: {
"certificate-authority-data": props.cluster.certificateAuthorityData,
server: props.cluster.kubernetesApiUrl,
},
},
],
contexts: [
{
name: props.cluster.name,
context: {
cluster: props.cluster.name,
user: props.user.name,
namespace: props.cluster.namespace,
},
},
],
users: [
{
name: props.user.name,
user: {
token: props.user.token,
},
},
],
"current-context": props.cluster.name,
kind: "Config",
preferences: {},
};

return yaml.stringify(kubeconfig);
}
87 changes: 75 additions & 12 deletions src/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1736,7 +1736,16 @@ export interface operations {
id: string;
username?: string;
label?: string;
csr: string;
pubkey: string;
cluster: {
/** @constant */
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
};
})[];
has_more: boolean;
/** @constant */
Expand All @@ -1755,7 +1764,16 @@ export interface operations {
id: string;
username?: string;
label?: string;
csr: string;
pubkey: string;
cluster: {
/** @constant */
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
};
})[];
has_more: boolean;
/** @constant */
Expand All @@ -1774,7 +1792,16 @@ export interface operations {
id: string;
username?: string;
label?: string;
csr: string;
pubkey: string;
cluster: {
/** @constant */
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
};
})[];
has_more: boolean;
/** @constant */
Expand Down Expand Up @@ -1864,38 +1891,38 @@ export interface operations {
/** @constant */
object?: "ssh_credential";
} | {
csr: string;
username?: string;
username: string;
label?: string;
cluster_id: string;
/** @constant */
object: "k8s_credential";
pubkey: string;
};
"multipart/form-data": {
pubkey: string;
username: string;
/** @constant */
object?: "ssh_credential";
} | {
csr: string;
username?: string;
username: string;
label?: string;
cluster_id: string;
/** @constant */
object: "k8s_credential";
pubkey: string;
};
"text/plain": {
pubkey: string;
username: string;
/** @constant */
object?: "ssh_credential";
} | {
csr: string;
username?: string;
username: string;
label?: string;
cluster_id: string;
/** @constant */
object: "k8s_credential";
pubkey: string;
};
};
};
Expand All @@ -1917,7 +1944,16 @@ export interface operations {
id: string;
username?: string;
label?: string;
csr: string;
pubkey: string;
cluster: {
/** @constant */
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
};
};
"multipart/form-data": {
/** @constant */
Expand All @@ -1931,7 +1967,16 @@ export interface operations {
id: string;
username?: string;
label?: string;
csr: string;
pubkey: string;
cluster: {
/** @constant */
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
};
};
"text/plain": {
/** @constant */
Expand All @@ -1945,7 +1990,16 @@ export interface operations {
id: string;
username?: string;
label?: string;
csr: string;
pubkey: string;
cluster: {
/** @constant */
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
};
};
};
};
Expand Down Expand Up @@ -2052,6 +2106,9 @@ export interface operations {
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
}[];
has_more: boolean;
/** @constant */
Expand All @@ -2063,6 +2120,9 @@ export interface operations {
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
}[];
has_more: boolean;
/** @constant */
Expand All @@ -2074,6 +2134,9 @@ export interface operations {
object: "kubernetes_cluster";
id: string;
kubernetes_api_url?: string;
name: string;
kubernetes_namespace: string;
kubernetes_ca_cert?: string;
}[];
has_more: boolean;
/** @constant */
Expand Down

0 comments on commit 921f627

Please sign in to comment.