-
Notifications
You must be signed in to change notification settings - Fork 1
/
fastify-kubernetes.js
146 lines (126 loc) · 3.61 KB
/
fastify-kubernetes.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict'
const kubernetes = require('@kubernetes/client-node')
const plugin = require('fastify-plugin')
function getContext (config, options) {
const namespace = options.namespace || 'default'
return config.getContexts().find(context => {
if (options.context && context.name !== options.context) {
return false
}
if (options.cluster && context.cluster !== options.cluster) {
return false
}
if (options.user && context.user !== options.user) {
return false
}
return (context.namespace || 'default') === namespace
})
}
function buildGetter (config, Client) {
return function getter () {
let client
if (!client) {
client = config.makeApiClient(Client)
}
return client
}
}
function buildApi (config) {
const api = {}
for (const key of Object.keys(kubernetes)) {
const obj = kubernetes[key]
if (obj && obj.prototype && obj.prototype.setDefaultAuthentication) {
Object.defineProperty(api, key, {
configurable: true,
enumerable: true,
get: buildGetter(config, obj)
})
}
}
return api
}
function loadFromFile (file) {
if (typeof file !== 'string') {
throw new Error('Cannot load kubeconfig: option "file" is not a string')
}
const config = new kubernetes.KubeConfig()
config.loadFromFile(file)
return config
}
function loadFromString (yaml) {
if (typeof yaml !== 'string' && !Buffer.isBuffer(yaml)) {
throw new Error('Cannot load kubeconfig: option "yaml" is not a string or buffer')
}
const config = new kubernetes.KubeConfig()
config.loadFromString(yaml.toString())
return config
}
function loadFromCluster () {
const config = new kubernetes.KubeConfig()
config.loadFromCluster()
return config
}
function loadFromDefault () {
const config = new kubernetes.KubeConfig()
config.loadFromDefault()
return config
}
function loadConfig (options) {
// Explicit target
if (typeof options.kubeconfig === 'object' && options.kubeconfig !== null) {
return options.kubeconfig
} else if (options.kubeconfig === 'file') {
return loadFromFile(options.file)
} else if (options.kubeconfig === 'yaml') {
return loadFromString(options.yaml)
} else if (options.kubeconfig === 'default') {
return loadFromDefault()
} else if (options.kubeconfig === 'in-cluster') {
return loadFromCluster()
}
// Auto mode
if (options.file) {
return loadFromFile(options.file)
} else if (options.yaml) {
return loadFromString(options.yaml)
} else if (process.env.KUBERNETES_SERVICE_HOST) {
return loadFromCluster()
} else {
return loadFromDefault()
}
}
async function fastifyKubernetesPlugin (fastify, options) {
const config = loadConfig(options)
const context = getContext(config, options)
if (!context) {
return Promise.reject(new Error('Kubernetes context not found'))
}
config.setCurrentContext(context.name)
const name = options.name
const obj = {
config,
context: context.name,
cluster: context.cluster,
user: context.user,
namespace: context.namespace || 'default',
api: buildApi(config)
}
if (!name) {
if (fastify.kubernetes) {
return Promise.reject(new Error('fastify-kubernetes has already registered'))
}
fastify.decorate('kubernetes', obj)
} else {
if (!fastify.kubernetes) {
fastify.decorate('kubernetes', obj)
}
if (fastify.kubernetes[name]) {
return Promise.reject(new Error(`Kubernetes context "${name}" already registered`))
}
fastify.kubernetes[name] = obj
}
}
module.exports = plugin(fastifyKubernetesPlugin, {
fastify: '^5.x',
name: 'fastify-kubernetes'
})