forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
createManagedMasterK8s.groovy
191 lines (175 loc) · 7.97 KB
/
createManagedMasterK8s.groovy
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
/*** BEGIN META {
"name" : "Create a Managed Controller in CloudBees Core on Modern Cloud Platform",
"comment" : "This script creates a Kubernetes Managed Controller programmatically similarly to what can be done through the UI.
It has been tested with version 2.289.1.2 of CloudBees Core",
"parameters" : [],
"core": "2.289.1.2",
"authors" : [
{ name : "Allan Burdajewicz" }
]
} END META**/
import com.cloudbees.masterprovisioning.kubernetes.KubernetesImagePullSecret
import com.cloudbees.masterprovisioning.kubernetes.KubernetesMasterProvisioning
import com.cloudbees.opscenter.server.model.ManagedMaster
import com.cloudbees.opscenter.server.properties.ConnectedMasterLicenseServerProperty
import com.cloudbees.opscenter.server.properties.ConnectedMasterOwnerProperty
/*****************
* INPUTS *
*****************/
/**
* The controller name is mandatory
*/
String controllerName = "mm-from-groovy"
/**
* Location of the controller
* Leave empty to add to the top level
* Provide full name of the folder to place controller in the folder, e.g.
* String controllerParent = "Controllers"
* String controllerParent = "Division/Sub-division"
*/
String controllerParent = ""
/**
* Following attributes may be specified. The values proposed are the default from version 2.2.9 of Controller Provisioning
*
* Note: If not setting properties explicitly, the defaults will be used.
*/
/* Controller */
String controllerDisplayName = ""
String controllerDescription = ""
String controllerPropertyOwners = ""
Integer controllerPropertyOwnersDelay = 5
/* Controller Provisioning */
Integer k8sDisk = 50
Integer k8sMemory = 3072
/**
* Since version 2.235.4.1, we recommend not using the Heap Ratio. Instead add `-XX:MinRAMPercentage` and
* `-XX:MaxRAMPercentage` to the Java options. For example, a ratio of 0.5d translate to a percentage of 50:
* `-XX:MinRAMPercentage=50.0 -XX:MaxRAMPercentage=50.0`
*
* See https://support.cloudbees.com/hc/en-us/articles/204859670-Java-Heap-settings-best-practice and
* https://docs.cloudbees.com/docs/release-notes/latest/cloudbees-ci/modern-cloud-platforms/2.235.4.1.
*/
Double k8sMemoryRatio = null
Double k8sCpus = 1
String k8sFsGroup = "1000"
Boolean k8sAllowExternalAgents = false
String k8sClusterEndpointId = "default"
String k8sEnvVars = ""
String k8sJavaOptions = "-XX:MinRAMPercentage=50.0 -XX:MaxRAMPercentage=50.0"
String k8sJenkinsOptions = ""
String k8sImage = 'CloudBees CI - Managed Master - 2.289.1.2'
List<KubernetesImagePullSecret> k8sImagePullSecrets = Collections.emptyList()
// Example:
// def k8sImagePullSecret1 = new KubernetesImagePullSecret(); k8sImagePullSecret1.setValue("useast-reg")
// List<KubernetesImagePullSecret> k8sImagePullSecrets = Arrays.asList(k8sImagePullSecret1)
Integer k8sLivenessInitialDelaySeconds = 300
Integer k8sLivenessPeriodSeconds = 10
Integer k8sLivenessTimeoutSeconds = 10
Integer k8sReadinessInitialDelaySeconds = 30
Integer k8sReadinessFailureThreshold = 100
Integer k8sReadinessTimeoutSeconds = 5
String k8sStorageClassName = ""
String k8sSystemProperties = ""
String k8sNamespace = ""
String k8sNodeSelectors = ""
Long k8sTerminationGracePeriodSeconds = 1200L
String k8sYaml = ""
/**
* cascBundle (optional). Configuration as Code Configuration Bundle
*/
String cascBundle = ""
/*****************
* CREATE CONTROLLER *
*****************/
def jenkins = Jenkins.get()
ModifiableTopLevelItemGroup parent = controllerParent?.trim() ? jenkins.getItemByFullName(controllerParent) : jenkins
if (!parent) {
println("Cannot find parent '${controllerParent}'.")
return
}
/**
* Create a Managed Controllers with just a name (this will automatically fill required values for id, idName, grantId, etc...)
* Similar to creating an iten in the UI
*/
ManagedMaster newInstance = parent.createProject(jenkins.getDescriptorByType(ManagedMaster.DescriptorImpl.class), controllerName, true)
newInstance.setDescription(controllerDescription)
newInstance.setDisplayName(controllerDisplayName)
/************************
* CONFIGURATION BUNDLE *
************************/
if(cascBundle?.trim()) {
// Properties must follow this order
// Note: ConnectedMasterTokenProperty supported since 2.289.1.2. For earlier version, comment out the following.
newInstance.getProperties().replace(new com.cloudbees.opscenter.server.casc.config.ConnectedMasterTokenProperty(hudson.util.Secret.fromString(UUID.randomUUID().toString())))
// Note: ConnectedMasterCascProperty supported since 2.277.4.x. For earlier version, comment out the following.
newInstance.getProperties().replace(new com.cloudbees.opscenter.server.casc.config.ConnectedMasterCascProperty(cascBundle))
}
/********************
* CONFIGURE CONTROLLER *
********************/
/**
* Configure the Controller provisioning details. Refer to the `config.xml` for more details.
* Similar to configuring a Managed Controller from the UI
*/
KubernetesMasterProvisioning controllerProvisioning = new KubernetesMasterProvisioning()
controllerProvisioning.setDomain(controllerName.toLowerCase())
/**
* Apply Managed Controller provisioning configuration (similar to what is configured through the Managed Controller UI)
* Note: If not setting properties explicitly, the defaults will be used.
*/
controllerProvisioning.setDisk(k8sDisk)
controllerProvisioning.setMemory(k8sMemory)
if(k8sMemoryRatio) {
controllerProvisioning.setHeapRatio(new com.cloudbees.jce.masterprovisioning.Ratio(k8sMemoryRatio))
/**
* For versions earlier than 2.235.4.1 (Master Provisioning plugin 2.5.6), use setRatio
* controllerProvisioning.setRatio(k8sMemoryRatio)
*/
}
controllerProvisioning.setCpus(k8sCpus)
controllerProvisioning.setFsGroup(k8sFsGroup)
controllerProvisioning.setAllowExternalAgents(k8sAllowExternalAgents)
controllerProvisioning.setClusterEndpointId(k8sClusterEndpointId)
controllerProvisioning.setEnvVars(k8sEnvVars)
controllerProvisioning.setJavaOptions(k8sJavaOptions)
controllerProvisioning.setJenkinsOptions(k8sJenkinsOptions)
controllerProvisioning.setImage(k8sImage)
controllerProvisioning.setImagePullSecrets(k8sImagePullSecrets)
controllerProvisioning.setLivenessInitialDelaySeconds(k8sLivenessInitialDelaySeconds)
controllerProvisioning.setLivenessPeriodSeconds(k8sLivenessPeriodSeconds)
controllerProvisioning.setLivenessTimeoutSeconds(k8sLivenessTimeoutSeconds)
controllerProvisioning.setReadinessInitialDelaySeconds(k8sReadinessInitialDelaySeconds)
controllerProvisioning.setReadinessFailureThreshold(k8sReadinessFailureThreshold)
controllerProvisioning.setReadinessTimeoutSeconds(k8sReadinessTimeoutSeconds)
controllerProvisioning.setStorageClassName(k8sStorageClassName)
controllerProvisioning.setSystemProperties(k8sSystemProperties)
controllerProvisioning.setNamespace(k8sNamespace)
controllerProvisioning.setNodeSelectors(k8sNodeSelectors)
controllerProvisioning.setTerminationGracePeriodSeconds(k8sTerminationGracePeriodSeconds)
controllerProvisioning.setYaml(k8sYaml)
/**
* Provide Controller item general configuration (similar to what is configured through the Controller UI)
* Note: If not setting properties explicitly, the defaults will be used.
*/
if (controllerPropertyOwners != null && !controllerPropertyOwners.isEmpty()) {
newInstance.getProperties().replace(new ConnectedMasterOwnerProperty(controllerPropertyOwners, controllerPropertyOwnersDelay))
}
newInstance.getProperties().replace(new ConnectedMasterLicenseServerProperty(new ConnectedMasterLicenseServerProperty.DescriptorImpl().defaultStrategy()))
/**
* Save the configuration
*/
newInstance.setConfiguration(controllerProvisioning)
newInstance.save()
/**
* Retrieve the controller from the API and print the details of the created Managed Controller
*/
def instance = jenkins.getItemByFullName(newInstance.fullName, ManagedMaster.class)
println "${instance.fullName}"
println " id: ${instance.id}"
println " idName: ${instance.idName}"
/******************************
* PROVISION AND START CONTROLLER *
******************************/
instance.provisionAndStartAction()
println "Started the controller..."
return