Skip to content

Commit

Permalink
幾つかリファクタし、例を追加しました
Browse files Browse the repository at this point in the history
  • Loading branch information
kahirokunn committed Mar 21, 2024
1 parent da877f9 commit 832a703
Show file tree
Hide file tree
Showing 106 changed files with 11,343 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.log
node_modules
dist*/
lib
packages/kubekit-codegen/lib
es

.yalc
Expand Down Expand Up @@ -35,3 +35,4 @@ eks-blueprints-add-ons

examples/generate-vercel-client/swagger.json
eks-blueprints-add-ons
openapi
File renamed without changes.
File renamed without changes.
File renamed without changes.
9 changes: 9 additions & 0 deletions examples/nextjs-usage/gen-config/core-v1-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ConfigFile } from '@kubekit/codegen'

const config: ConfigFile = {
schemaFile: '../openapi/api/v1/swagger.json',
apiFile: '@kubekit/client',
outputFile: '../src/generated/k8s-client/api-v1.ts',
}

export default config
10 changes: 10 additions & 0 deletions examples/nextjs-usage/gen-config/storage-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ConfigFile } from '@kubekit/codegen'

const config: ConfigFile = {
schemaFile:
'../openapi/apis/storage.k8s.io/v1/swagger.json',
apiFile: '@kubekit/client',
outputFile: '../src/generated/k8s-client/apis-storage-k8s-io-v1.ts',
}

export default config
File renamed without changes.
File renamed without changes.
33 changes: 33 additions & 0 deletions examples/nextjs-usage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"name": "next.js",
"version": "0.1.0",
"private": true,
"scripts": {
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint",
"import": "npx @kubekit/import ./",
"gen": "npx @kubekit/codegen gen-config/storage-config.ts && npx @kubekit/codegen gen-config/core-v1-config.ts"
},
"dependencies": {
"@kubekit/client": "^0.0.3",
"next": "^14.2.0-canary.23",
"react": "^18",
"react-dom": "^18"
},
"devDependencies": {
"@kubekit/codegen": "^0.0.2",
"@kubekit/import": "^0.0.2",
"@types/node": "^20",
"@types/react": "^18",
"@types/react-dom": "^18",
"autoprefixer": "^10.0.1",
"esbuild-runner": "^2.2.2",
"eslint": "^8",
"eslint-config-next": "14.1.3",
"postcss": "^8",
"tailwindcss": "^3.3.0",
"typescript": "^5"
}
}
File renamed without changes.
File renamed without changes
File renamed without changes
File renamed without changes.
File renamed without changes.
File renamed without changes.
12 changes: 12 additions & 0 deletions examples/nextjs-usage/src/app/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { listCoreV1PodForAllNamespaces } from "@/generated/k8s-client/api-v1";

export default async function Home() {
const pods = await listCoreV1PodForAllNamespaces({})
return (
<main>
<ul>
{pods.items.map((pod, i) => <li key={i}>{pod.metadata!.name}</li>)}
</ul>
</main>
);
}
File renamed without changes.
File renamed without changes.
3,662 changes: 3,662 additions & 0 deletions examples/nextjs-usage/yarn.lock

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions examples/nodejs-usage/gen-config/core-v1-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import type { ConfigFile } from '@kubekit/codegen'

const config: ConfigFile = {
schemaFile: '../openapi/api/v1/swagger.json',
apiFile: '@kubekit/client',
outputFile: '../generated/k8s-client/api-v1.ts',
}

export default config
10 changes: 10 additions & 0 deletions examples/nodejs-usage/gen-config/storage-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import type { ConfigFile } from '@kubekit/codegen'

const config: ConfigFile = {
schemaFile:
'../openapi/apis/storage.k8s.io/v1/swagger.json',
apiFile: '@kubekit/client',
outputFile: '../generated/k8s-client/apis-storage-k8s-io-v1.ts',
}

export default config
25 changes: 25 additions & 0 deletions examples/nodejs-usage/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"name": "basic-usage",
"version": "1.0.0",
"description": "",
"main": "index.js",
"directories": {
"test": "tests"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"import": "npx @kubekit/import ./",
"gen": "npx @kubekit/codegen gen-config/storage-config.ts && npx @kubekit/codegen gen-config/core-v1-config.ts",
"dev": "npx tsx src/main.ts"
},
"author": "",
"license": "MIT",
"dependencies": {
"@kubekit/client": "0.0.3"
},
"devDependencies": {
"@kubekit/codegen": "^0.0.2",
"@kubekit/import": "^0.0.2",
"esbuild-runner": "^2.2.2"
}
}
40 changes: 40 additions & 0 deletions examples/nodejs-usage/patch-kube-config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { parse, stringify } from 'https://deno.land/std/yaml/mod.ts'
import { existsSync } from 'https://deno.land/std/fs/mod.ts'

type Env = {
name: string
value: string
}

type User = {
exec?: {
apiVersion: string
args: string[]
command: string
env?: Env[]
}
}

type KubeConfig = {
users: { name: string; user: User }[]
}

const defaultKubeConfigPath = Deno.env.get('HOME') + '/.kube/config'

const kubeConfigPath = Deno.env.get('KUBECONFIG') || defaultKubeConfigPath
if (existsSync(kubeConfigPath)) {
const kubeConfig = parse(
await Deno.readTextFile(kubeConfigPath)
) as KubeConfig
kubeConfig.users.forEach(({ user }) => {
const AWS_PROFILE_ENV = user.exec?.env?.find(
(e) => e.name === 'AWS_PROFILE'
)
const HAS_PROFILE_ARG = user.exec?.args?.find((arg) => arg === '--profile')
if (AWS_PROFILE_ENV && !HAS_PROFILE_ARG) {
user.exec?.args.push('--profile')
user.exec?.args.push(AWS_PROFILE_ENV.value)
}
})
await Deno.writeTextFile(kubeConfigPath, stringify(kubeConfig))
}
200 changes: 200 additions & 0 deletions examples/nodejs-usage/src/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,200 @@
import {
createCoreV1NamespacedPod,
deleteCoreV1NamespacedPod,
listCoreV1PodForAllNamespaces,
patchCoreV1NamespacedPod,
patchCoreV1NamespacedPodStatus,
readCoreV1NamespacedPod,
} from '../generated/k8s-client/api-v1'

// $ kubectl run nginx --image=nginx --restart=Never --dry-run=client -o yaml
// apiVersion: v1
// kind: Pod
// metadata:
// creationTimestamp: null
// labels:
// run: nginx
// name: nginx
// spec:
// containers:
// - image: nginx
// name: nginx
// dnsPolicy: ClusterFirst
// restartPolicy: Never

async function main() {
const controller = new AbortController()

setTimeout(() => controller.abort(), 2000)

try {
await listCoreV1PodForAllNamespaces(
{
watch: true,
},
{
watchEventHandler: console.log,
signal: controller.signal
}
)
} catch (e) {
if (e.name !== "AbortError") {
throw e
}
}
await listCoreV1PodForAllNamespaces({
watch: false,
})

await createCoreV1NamespacedPod({
namespace: 'default',
body: {
metadata: {
name: 'test',
namespace: 'default',
},
spec: {
containers: [{ name: 'nginx', image: 'nginx' }],
dnsPolicy: 'ClusterFirst',
restartPolicy: 'Never',
},
status: {
phase: 'Unknown',
},
},
})

await patchCoreV1NamespacedPod({
namespace: 'default',
name: 'test',
contentType: 'application/strategic-merge-patch+json',
body: {
metadata: {
annotations: {
patch: 'true',
},
},
},
fieldManager: 'client-side-apply',
fieldValidation: 'Strict',
})

await patchCoreV1NamespacedPod({
namespace: 'default',
name: 'test',
contentType: 'application/json-patch+json',
body: [
{ op: 'add', path: '/metadata/annotations/testpatch', value: 'success' },
],
fieldManager: 'json-patch',
})

await deleteCoreV1NamespacedPod({
name: 'test',
namespace: 'default',
body: {},
})

await patchCoreV1NamespacedPod({
namespace: 'default',
name: 'nginx',
fieldManager: 'server-side-apply',
fieldValidation: 'Strict',
contentType: 'application/apply-patch+yaml',
body: {
apiVersion: 'v1',
kind: 'Pod',
metadata: {
labels: {
run: 'nginx',
},
name: 'nginx',
namespace: 'default',
},
spec: {
containers: [
{
image: 'nginx',
name: 'nginx',
resources: {},
},
],
dnsPolicy: 'ClusterFirst',
restartPolicy: 'Never',
},
status: {},
},
})

await readCoreV1NamespacedPod({
name: 'nginx',
namespace: 'default',
})

await patchCoreV1NamespacedPodStatus({
name: 'nginx',
namespace: 'default',
fieldManager: 'kahiro',
contentType: 'application/apply-patch+yaml',
body: {
apiVersion: 'v1',
kind: 'Pod',
status: {
phase: 'Failed',
},
},
})

// await wait(async () => {
// const res = await readCoreV1NamespacedPod({
// name: 'nginx',
// namespace: 'default',
// })
// if (
// !res.status?.conditions?.find(
// (c) => c.type === 'Ready' && c.status === 'True'
// )
// ) {
// return retry
// }

// return res
// })

await deleteCoreV1NamespacedPod({
name: 'nginx',
namespace: 'default',
body: {},
})

console.info('Successfully')
}
main()

type Executor<T> = (params: ExecutorParams) => Promise<Retry | T>
type ExecutorParams = { retry: Retry }
const retry = Symbol()
type Retry = typeof retry

async function wait<T>(
executor: Executor<T>,
{ interval = 500, timeout = 5000 } = {}
): Promise<T> {
const startTime = Date.now()

while (true) {
const elapsed = Date.now() - startTime

if (elapsed > timeout) {
throw new Error('Timeout reached')
}

const result = await executor({ retry })

if (result !== retry) {
return result
}

await new Promise((r) => setTimeout(r, interval))
}
}
3 changes: 3 additions & 0 deletions examples/nodejs-usage/tests/next.js/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
Loading

0 comments on commit 832a703

Please sign in to comment.