-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.scala
132 lines (114 loc) · 3.47 KB
/
Main.scala
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
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import io.fabric8.kubernetes.api.model.ObjectMetaBuilder
import io.fabric8.kubernetes.api.model.apiextensions.v1beta1.CustomResourceDefinitionBuilder
import io.fabric8.kubernetes.client.DefaultKubernetesClient
import io.fabric8.kubernetes.client.dsl.base.CustomResourceDefinitionContext
import io.fabric8.kubernetes.client.utils.Serialization
import scala.util.Try
object Main extends App {
final val GroupName = "test.test.com"
final val GroupVersion = "v1alpha1"
final val Kind = "TestApplication"
final val KindList = s"${Kind}List"
final val Singular = "testapplication"
final val Plural = "testapplications"
final val Short = "testapp"
final val ApiVersion = GroupName + "/" + GroupVersion
final val ResourceName = s"$Plural.$GroupName"
final val Scope = "Namespaced"
val Crd =
new CustomResourceDefinitionBuilder()
.withNewMetadata()
.withName(ResourceName)
.endMetadata()
.withNewSpec()
.withGroup(GroupName)
.withNewNames()
.withNewKind(Kind)
.withListKind(KindList)
.withSingular(Singular)
.withPlural(Plural)
.withShortNames(Short)
.endNames()
.withVersion(GroupVersion)
.withScope("Namespaced")
.withPreserveUnknownFields(true)
.withNewSubresources()
.withNewStatus()
.endStatus()
.endSubresources()
.endSpec()
.withNewStatus()
.withStoredVersions(GroupVersion)
.endStatus()
.build()
val customResourceDefinitionContext =
new CustomResourceDefinitionContext.Builder()
.withVersion(GroupVersion)
.withKind(Kind)
.withGroup(GroupName)
.withPlural(Plural)
.withScope(Scope)
.build()
Serialization.jsonMapper.registerModule(DefaultScalaModule)
val client = new DefaultKubernetesClient()
client
.apiextensions()
.v1beta1()
.customResourceDefinitions()
.withName(ResourceName)
.createOrReplace(Crd)
Try { client
.customResource(customResourceDefinitionContext)
.inNamespace("default")
.withName("mytestcr")
.delete() }
client
.customResource(customResourceDefinitionContext)
.inNamespace("default")
.withName("mytestcr")
.createOrReplace(s"""
| {
| "kind": "${Kind}",
| "apiVersion": "${ApiVersion}",
| "metadata": {
| "name": "mytestcr"
| },
| "key": "value1"
| }
|""".stripMargin)
var found: java.util.Map[String, Object] = null
while (Try {
found = client
.customResource(customResourceDefinitionContext)
.inNamespace("default")
.withName("mytestcr")
.get()
assert { found != null }
println(s"Found\n${found}")
found
}.toOption.isEmpty) {
Thread.sleep(200)
}
println(found.get("key"))
found.put("key", "value2")
client.customResource(customResourceDefinitionContext)
.inNamespace("default")
.withName("mytestcr")
.edit(found)
while (Try {
found = client
.customResource(customResourceDefinitionContext)
.inNamespace("default")
.withName("mytestcr")
.get()
assert { found != null }
println(s"Edit:\n${found}")
found
}.toOption.isEmpty) {
Thread.sleep(200)
}
println(found.get("key"))
assert{ found.get("key") == "value2" }
System.exit(0)
}