-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathkubernetes_cluster.R
269 lines (245 loc) · 9.67 KB
/
kubernetes_cluster.R
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#' Kubernetes cluster class
#'
#' Class representing a [Kubernetes](https://kubernetes.io/docs/home/) cluster. Note that this class can be used to interface with any Docker registry that supports the HTTP V2 API, not just those created via the Azure Container Registry service. Use the [kubernetes_cluster] function to instantiate new objects of this class.
#'
#' @docType class
#' @section Methods:
#' The following methods are available, in addition to those provided by the [AzureRMR::az_resource] class:
#' - `new(...)`: Initialize a new registry object. See 'Initialization' below.
#' - `create_registry_secret(registry, secret_name, email)`: Provide authentication secret for a Docker registry. See 'Secrets' below.
#' - `delete_registry_secret(secret_name)`: Delete a registry authentication secret.
#' - `create(file, ...)`: Creates a deployment or service from a file, using `kubectl create -f`.
#' - `get(type, ...)`: Get information about resources, using `kubectl get`.
#' - `run(name, image, ...)`: Run an image using `kubectl run --image`.
#' - `expose(name, type, file, ...)`: Expose a service using `kubectl expose`. If the `file` argument is provided, read service information from there.
#' - `delete(type, name, file, ...)`: Delete a resource (deployment or service) using `kubectl delete`. If the `file` argument is provided, read resource information from there.
#' - `apply(file, ...)`: Apply a configuration file, using `kubectl apply -f`.
#' - `show_dashboard(port, ...)`: Display the cluster dashboard. By default, use local port 30000.
#' - `kubectl(cmd, ...)`: Run an arbitrary `kubectl` command on this cluster. Called by the other methods above.
#' - `helm(cmd, ...)`: Run a `helm` command on this cluster.
#'
#' @section Initialization:
#' The `new()` method takes one argument: `config`, the name of the file containing the configuration details for the cluster. This should be a YAML or JSON file in the standard Kubernetes configuration format. Set this to NULL to use the default `~/.kube/config` file.
#'
#' @section Secrets:
#' The recommended way to allow a cluster to authenticate with a Docker registry is to give its service principal the appropriate role-based access. However, you can also authenticate with a username and password. To do this, call the `create_registry_secret` method with the following arguments:
#' - `registry`: An object of class either [acr] representing an Azure Container Registry service, or [DockerRegistry] representing the registry itself.
#' - `secret_name`: The name to give the secret. Defaults to the name of the registry server.
#' - `email`: The email address for the Docker registry.
#'
#' @section Kubectl and helm:
#' The methods for this class call the `kubectl` and `helm` commandline tools, passing the `--config` option to specify the configuration information for the cluster. This allows all the features supported by Kubernetes to be available immediately and with a minimum of effort, although it does require that the tools be installed. The returned object from a call to `kubectl` or `helm` will contain the following components:
#' - `status`: The exit status. If this is `NA`, then the process was killed and had no exit status.
#' - `stdout`: The standard output of the command, in a character scalar.
#' - `stderr`: The standard error of the command, in a character scalar.
#' - `timeout`: Whether the process was killed because of a timeout.
#' - `cmdline`: The command line.
#'
#' The first four components are from `processx::run`; AzureContainers adds the last to make it easier to construct scripts that can be run outside R.
#'
#' @seealso
#' [aks], [call_kubectl], [call_helm]
#'
#' [Kubectl commandline reference](https://kubernetes.io/docs/reference/generated/kubectl/kubectl-commands)
#'
#' @examples
#' \dontrun{
#'
#' rg <- AzureRMR::get_azure_login()$
#' get_subscription("subscription_id")$
#' get_resource_group("rgname")
#'
#' # get the cluster endpoint
#' kubclus <- rg$get_aks("mycluster")$get_cluster()
#'
#' # get registry authentication secret
#' kubclus$create_registry_secret(rg$get_acr("myregistry"))
#'
#' # deploy a service
#' kubclus$create("deployment.yaml")
#'
#' # deploy a service from an Internet URL
#' kubclus$create("https://example.com/deployment.yaml")
#'
#' # can also supply the deployment parameters inline
#' kubclus$create("
#' apiVersion: extensions/v1beta1
#' kind: Deployment
#' metadata:
#' name: model1
#' spec:
#' replicas: 1
#' template:
#' metadata:
#' labels:
#' app: model1
#' spec:
#' containers:
#' - name: model1
#' image: myregistry.azurecr.io/model1
#' ports:
#' - containerPort: 8000
#' imagePullSecrets:
#' - name: myregistry.azurecr.io
#' ---
#' apiVersion: v1
#' kind: Service
#' metadata:
#' name: model1-svc
#' spec:
#' selector:
#' app: model1
#' type: LoadBalancer
#' ports:
#' - protocol: TCP
#' port: 8000")
#'
#' # track status
#' kubclus$get("deployment")
#' kubclus$get("service")
#'
#' }
#' @export
KubernetesCluster <- R6::R6Class("KubernetesCluster",
public=list(
initialize=function(config=NULL)
{
private$config <- config
},
create_registry_secret=function(registry, secret_name=registry$server$hostname, email, ...)
{
if(is_acr(registry))
registry <- registry$get_docker_registry(as_admin=TRUE)
if(!is_docker_registry(registry))
stop("Must supply a Docker registry object", call.=FALSE)
if(is.null(registry$username) || is.null(registry$password))
stop("Docker registry object does not contain a username/password", call.=FALSE)
cmd <- paste0("create secret docker-registry ", secret_name,
" --docker-server=", registry$server$hostname,
" --docker-username=", registry$username,
" --docker-password=", registry$password,
" --docker-email=", email)
self$kubectl(cmd, ...)
},
delete_registry_secret=function(secret_name, ...)
{
cmd <- paste0("delete secret ", secret_name)
self$kubectl(cmd, ...)
},
run=function(name, image, options="", ...)
{
cmd <- paste0("run ", name,
" --image ", image,
" ", options)
self$kubectl(cmd, ...)
},
expose=function(name, type=c("pod", "service", "replicationcontroller", "deployment", "replicaset"),
file=NULL, options="", ...)
{
if(is.null(file))
{
type <- match.arg(type)
cmd <- paste0("expose ", type,
" ", name,
" ", options)
}
else
{
cmd <- paste0("expose -f ", make_file(file, ".yaml"),
" ", options)
}
self$kubectl(cmd, ...)
},
create=function(file, options="", ...)
{
cmd <- paste0("create -f ", make_file(file, ".yaml"),
" ", options)
self$kubectl(cmd, ...)
},
apply=function(file, options="", ...)
{
cmd <- paste0("apply -f ", make_file(file, ".yaml"),
" ", options)
self$kubectl(cmd, ...)
},
delete=function(type, name, file=NULL, options="", ...)
{
if(is.null(file))
{
cmd <- paste0("delete ", type,
" ", name,
" ", options)
}
else
{
cmd <- paste0("delete -f ", make_file(file, ".yaml"),
" ", options)
}
self$kubectl(cmd, ...)
},
get=function(type, options="", ...)
{
cmd <- paste0("get ", type,
" ", options)
self$kubectl(cmd, ...)
},
show_dashboard=function(port=30000, options="", ...)
{
cmd <- paste0("proxy --port=", port,
" ", options)
config <- if(!is.null(private$config))
paste0("--kubeconfig=", private$config)
else NULL
processx::process$new(.AzureContainers$kubectl, c(strsplit(cmd, " ", fixed=TRUE)[[1]], config), ...)
url <- paste0("http://localhost:",
port,
"/api/v1/namespaces/kube-system/services/kubernetes-dashboard/proxy/#!/overview")
message("If the dashboard does not appear, enter the URL '", url, "' in your browser")
browseURL(url)
},
kubectl=function(cmd="", ...)
{
call_kubectl(cmd, config=private$config, ...)
},
helm=function(cmd="", ...)
{
call_helm(cmd, config=private$config, ...)
}
),
private=list(
config=NULL
))
#' Create a new Kubernetes cluster object
#'
#' @param config The name of the file containing the configuration details for the cluster. This should be a YAML or JSON file in the standard Kubernetes configuration format. Set this to NULL to use the default `~/.kube/config` file.
#' @details
#' Use this function to instantiate a new object of the `KubernetesCluster` class, for interacting with a Kubernetes cluster.
#' @return
#' An R6 object of class `KubernetesCluster`.
#'
#' @seealso
#' [KubernetesCluster] for methods for working with the cluster, [call_kubectl], [call_helm]
#'
#' [docker_registry] for the corresponding function to create a Docker registry object
#'
#' @examples
#' \dontrun{
#'
#' kubernetes_cluster()
#' kubernetes_cluster("myconfig.yaml")
#'
#' }
#' @export
kubernetes_cluster <- function(config=NULL)
{
KubernetesCluster$new(config)
}
# generate a file from a character vector to be passed to kubectl
make_file <- function(file, ext="")
{
if(length(file) == 1 && (file.exists(file) || is_url(file)))
return(file)
out <- tempfile(fileext=ext)
writeLines(file, out)
out
}