kubernetes 0.2.0

A Kubernetes client library for Rust
Documentation
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
use api::meta::v1::{ItemList, LabelSelector, ObjectMeta};
use api::{IntOrString, Integer, Quantity, Time, TypeMeta, TypeMetaStruct};
use serde_json::{self, Map, Value};
use std::borrow::Cow;
use std::default::Default;
use {GroupVersion, Metadata};

const API_GROUP: &str = "v1";
pub const GROUP_VERSION: GroupVersion = GroupVersion {
    group: "",
    version: "v1",
};

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Pod {
    #[serde(flatten)]
    typemeta: TypeMetaStruct<Pod>,
    #[serde(default)]
    pub metadata: ObjectMeta,
    #[serde(default)]
    pub spec: PodSpec,
    #[serde(default)]
    pub status: PodStatus,
}

pub type PodList = ItemList<Pod>;

impl TypeMeta for Pod {
    fn api_version() -> &'static str {
        API_GROUP
    }
    fn kind() -> &'static str {
        "Pod"
    }
}

impl Metadata for Pod {
    fn api_version(&self) -> &str {
        <Pod as TypeMeta>::api_version()
    }
    fn kind(&self) -> &str {
        <Pod as TypeMeta>::kind()
    }
    fn metadata(&self) -> Cow<ObjectMeta> {
        Cow::Borrowed(&self.metadata)
    }
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodTemplateSpec {
    pub metadata: ObjectMeta,
    pub spec: PodSpec,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodSpec {
    pub active_deadline_seconds: Option<Integer>,
    pub affinity: Option<Affinity>,
    pub automount_service_account_token: Option<bool>,
    #[serde(default)]
    pub containers: Vec<Container>,
    #[serde(default = "clusterfirst")]
    pub dns_policy: DNSPolicy,
    #[serde(default)]
    pub host_aliases: Vec<HostAlias>,
    #[serde(default)]
    pub host_ipc: bool,
    #[serde(default)]
    pub host_network: bool,
    #[serde(default, rename = "hostPID")]
    pub host_pid: bool,
    pub hostname: Option<String>,
    #[serde(default)]
    pub image_pull_secrets: Vec<LocalObjectReference>,
    #[serde(default)]
    pub init_containers: Vec<Container>,
    pub node_name: Option<String>,
    #[serde(default)]
    pub node_selector: Map<String, Value>,
    pub priority: Option<Integer>,
    pub priority_class_name: Option<String>,
    #[serde(default = "always")]
    pub restart_policy: RestartPolicy,
    pub scheduler_name: Option<String>,
    pub security_context: Option<PodSecurityContext>,
    pub service_account: Option<String>,
    pub service_account_name: Option<String>,
    pub subdomain: Option<String>,
    #[serde(default = "int30")]
    pub termination_grace_period_seconds: Integer,
    #[serde(default)]
    pub tolerations: Vec<Toleration>,
    #[serde(default)]
    pub volumes: Vec<Volume>,
}

impl Default for PodSpec {
    fn default() -> Self {
        serde_json::from_value(Value::Object(Default::default())).unwrap()
    }
}

#[test]
fn podspec_default() {
    let _: PodSpec = Default::default();
}

fn clusterfirst() -> DNSPolicy {
    DNSPolicy::ClusterFirst
}
fn always() -> RestartPolicy {
    RestartPolicy::Always
}
fn int30() -> Integer {
    30
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum DNSPolicy {
    ClusterFirst,
    ClusterFirstWithHostNet,
    Default,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum RestartPolicy {
    Always,
    OnFailure,
    Never,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Affinity {
    pub node_affinity: Option<NodeAffinity>,
    pub pod_affinity: Option<PodAffinity>,
    pub pod_anti_affinity: Option<PodAntiAffinity>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NodeAffinity {
    #[serde(default)]
    pub preferred_during_scheduling_ignored_during_execution: Vec<PreferredSchedulingTerm>,
    pub required_during_scheduling_ignored_during_execution: Option<NodeSelector>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PreferredSchedulingTerm {
    pub preference: Option<NodeSelectorTerm>,
    pub weight: Option<Integer>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NodeSelector {
    pub node_selector_terms: Vec<NodeSelectorTerm>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NodeSelectorTerm {
    pub match_expressions: Vec<NodeSelectorRequirement>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NodeSelectorRequirement {
    pub key: String,
    pub operator: NodeSelectorOperator,
    #[serde(default)]
    pub values: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum NodeSelectorOperator {
    In,
    NotIn,
    Exists,
    DoesNotExist,
    Gt,
    Lt,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodAffinity {
    pub preferred_during_scheduling_ignored_during_execution: Option<WeightedPodAffinityTerm>,
    #[serde(default)]
    pub required_during_scheduling_ignored_during_execution: Vec<PodAffinityTerm>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct WeightedPodAffinityTerm {
    pub pod_affinity_term: PodAffinityTerm,
    pub weight: Integer,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodAffinityTerm {
    pub label_selector: LabelSelector,
    #[serde(default)]
    pub namespaces: Vec<String>,
    pub topology_key: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodAntiAffinity {
    #[serde(default)]
    pub preferred_during_scheduling_ignored_during_execution: Vec<WeightedPodAffinityTerm>,
    #[serde(default)]
    pub required_during_scheduling_ignored_during_execution: Vec<PodAffinityTerm>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Container {
    #[serde(default)]
    pub args: Vec<String>,
    #[serde(default)]
    pub command: Vec<String>,
    #[serde(default)]
    pub env: Vec<EnvVar>,
    #[serde(default)]
    pub env_from: Vec<EnvFromSource>,
    pub image: Option<String>,
    pub image_pull_policy: Option<PullPolicy>,
    pub lifecycle: Option<Lifecycle>,
    pub liveness_probe: Option<Probe>,
    pub readiness_probe: Option<Probe>,
    #[serde(default)]
    pub name: String,
    #[serde(default)]
    pub ports: Vec<ContainerPort>,
    pub resources: Option<ResourceRequirements>,
    pub security_context: Option<SecurityContext>,
    #[serde(default)]
    pub stdin: bool,
    #[serde(default)]
    pub stdin_once: bool,
    #[serde(default)]
    pub tty: bool,
    #[serde(default = "devterminationlog")]
    pub termination_message_path: String,
    #[serde(default = "file")]
    pub termination_message_policy: TerminationMessagePolicy,
    #[serde(default)]
    pub volume_mounts: Vec<VolumeMount>,
    pub working_dir: Option<String>,
}

impl Default for Container {
    fn default() -> Self {
        serde_json::from_value(Value::Object(Default::default())).unwrap()
    }
}

#[test]
fn container_default() {
    let _: Container = Default::default();
}

fn devterminationlog() -> String {
    "/dev/termination-log".into()
}
fn file() -> TerminationMessagePolicy {
    TerminationMessagePolicy::File
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum PullPolicy {
    Always,
    Never,
    IfNotPresent,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum TerminationMessagePolicy {
    File,
    FallbackToLogsOnError,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct EnvVar {
    pub name: String,
    #[serde(default)]
    pub value: String,
    pub value_from: Option<EnvVarSource>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum EnvVarSource {
    ConfigMapKeyRef(ConfigMapKeySelector),
    FieldRef(ObjectFieldSelector),
    ResourceFieldRef(ResourceFieldSelector),
    SecretKeyRef(SecretKeySelector),
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ConfigMapKeySelector {
    pub key: String,
    pub name: String,
    #[serde(default)]
    pub optional: bool,
}

fn v1() -> String {
    "v1".into()
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ObjectFieldSelector {
    #[serde(default = "v1")]
    pub api_version: String,
    pub field_path: String,
}

fn quant1() -> Quantity {
    "1".into()
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ResourceFieldSelector {
    pub container_name: Option<String>,
    #[serde(default = "quant1")]
    pub divisor: Quantity,
    pub resource: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SecretKeySelector {
    pub key: String,
    pub name: String,
    #[serde(default)]
    pub optional: bool,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct EnvFromSource {
    pub prefix: Option<String>,
    pub config_map_ref: Option<ConfigMapEnvSource>,
    pub secret_ref: Option<SecretEnvSource>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ConfigMapEnvSource {
    pub name: String,
    #[serde(default)]
    pub optional: bool,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SecretEnvSource {
    pub name: String,
    #[serde(default)]
    pub optional: bool,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Lifecycle {
    pub post_start: Option<Handler>,
    pub pre_stop: Option<Handler>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum Handler {
    Exec(ExecAction),
    HttpGet(HTTPGetAction),
    TcpSocket(TCPSocketAction),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ExecAction {
    pub command: Vec<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct HTTPGetAction {
    pub host: Option<String>,
    #[serde(default)]
    pub http_headers: Vec<HTTPHeader>,
    pub path: String,
    pub port: IntOrString,
    #[serde(default = "http")]
    pub scheme: String,
}

fn http() -> String {
    "HTTP".into()
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct HTTPHeader {
    pub name: String,
    pub value: String,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct TCPSocketAction {
    pub host: Option<String>,
    pub port: IntOrString,
}

fn int1() -> Integer {
    1
}
fn int3() -> Integer {
    3
}
fn int10() -> Integer {
    10
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Probe {
    pub exec: Option<ExecAction>,
    pub http_get: Option<HTTPGetAction>,
    pub tcp_socket: Option<TCPSocketAction>,
    #[serde(default = "int3")]
    pub failure_threshold: Integer,
    #[serde(default)]
    pub initial_delay_seconds: Integer,
    #[serde(default = "int10")]
    pub period_seconds: Integer,
    #[serde(default = "int1")]
    pub success_threshold: Integer,
    #[serde(default = "int1")]
    pub timeout_seconds: Integer,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ContainerPort {
    pub container_port: Integer,
    #[serde(rename = "hostIP")]
    pub host_ip: Option<String>,
    pub host_port: Option<Integer>,
    pub name: Option<String>,
    #[serde(default = "tcp")]
    pub protocol: Protocol,
}

fn tcp() -> Protocol {
    Protocol::TCP
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum Protocol {
    TCP,
    UDP,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ResourceRequirements {
    #[serde(default)]
    pub limits: Map<String, Value>,
    #[serde(default)]
    pub requests: Map<String, Value>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SecurityContext {
    pub allow_privilege_escalation: Option<bool>,
    pub capabilities: Option<Capabilities>,
    #[serde(default)]
    pub privileged: bool,
    #[serde(default)]
    pub read_only_root_filesystem: bool,
    #[serde(default)]
    pub run_as_non_root: bool,
    pub run_as_user: Option<Integer>,
    #[serde(rename = "seLinuxOptions")]
    pub selinux_options: Option<SELinuxOptions>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Capabilities {
    #[serde(default)]
    pub add: Vec<String>,
    #[serde(default)]
    pub drop: Vec<String>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SELinuxOptions {
    pub level: String,
    pub role: String,
    #[serde(rename = "type")]
    pub typ: String,
    pub user: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct VolumeMount {
    pub mount_path: String,
    pub mount_propagation: Option<String>,
    pub name: String,
    #[serde(default)]
    pub read_only: bool,
    #[serde(default)]
    pub sub_path: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct HostAlias {
    pub hostnames: Vec<String>,
    pub ip: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct LocalObjectReference {
    pub name: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodSecurityContext {
    pub fs_group: Option<Integer>,
    #[serde(default)]
    pub run_as_non_root: bool,
    pub run_as_user: Option<Integer>,
    #[serde(default)]
    pub supplemental_groups: Vec<Integer>,
    #[serde(rename = "seLinuxOptions")]
    pub selinux_options: Option<SELinuxOptions>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum TaintEffect {
    NoSchedule,
    PreferNoSchedule,
    NoExecute,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum TolerationOperator {
    Exists,
    Equal,
}

fn equal() -> TolerationOperator {
    TolerationOperator::Equal
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Toleration {
    pub effect: Option<TaintEffect>,
    pub key: Option<String>,
    #[serde(default = "equal")]
    pub operator: TolerationOperator,
    pub toleration_seconds: Option<Integer>,
    pub value: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct Volume {
    pub name: String,
    #[serde(flatten)]
    pub source: VolumeSource,
}

// This is not a real k8s type
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum VolumeSource {
    //#[serde(rename="awsElasticBlockStore")]
    //AwsElasticBlockStore(AWSElasticBlockStoreVolumeSource),
    //AzureDisk(AzureDiskVolumeSource),
    //AzureFile(AzureFileVolumeSource),
    //#[serde(rename="cephfs")]
    //CephFS(CephFSVolumeSource),
    //Cinder(CinderVolumeSource),
    ConfigMap(ConfigMapVolumeSource),
    #[serde(rename = "downwardAPI")]
    DownwardAPI(DownwardAPIVolumeSource),
    EmptyDir(EmptyDirVolumeSource),
    //#[serde(rename="fc")]
    //FC(FCVolumeSource)
    //FlexVolume(FlexVolumeSource)
    //Flocker(FlockerVolumeSource)
    //#[serde(rename="gcePersistentDisk")]
    //GCEPersistentDisk(GCEPersistentDiskVolumeSource)
    //GitRepo(GitRepoVolumeSource)
    //Glusterfs(GlusterfsVolumeSource)
    HostPath(HostPathVolumeSource),
    //#[serde(rename="iscsi")]
    //ISCSI(ISCSIVolumeSource),
    #[serde(rename = "nfs")]
    NFS(NFSVolumeSource),
    PersistentVolumeClaim(PersistentVolumeClaimVolumeSource),
    //photonPersistentDisk
    //portworxVolume
    //projected
    //quobyte
    //rbd
    //scaleIO
    Secret(SecretVolumeSource),
    //storageos
    //vsphereVolume
}

fn int0644() -> Integer {
    0o644
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ConfigMapVolumeSource {
    #[serde(default = "int0644")]
    pub default_mode: Integer,
    #[serde(default)]
    pub items: Vec<KeyToPath>,
    pub name: String,
    #[serde(default)]
    pub optional: bool,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct KeyToPath {
    pub key: String,
    pub mode: Option<Integer>,
    pub path: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DownwardAPIVolumeSource {
    #[serde(default = "int0644")]
    pub default_mode: Integer,
    #[serde(default)]
    pub items: Vec<DownwardAPIVolumeFile>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct DownwardAPIVolumeFile {
    pub field_ref: Option<ObjectFieldSelector>,
    pub mode: Option<Integer>,
    pub path: String,
    pub resource_field_ref: Option<ResourceFieldSelector>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct EmptyDirVolumeSource {
    #[serde(default)]
    pub medium: String,
    pub size_limit: Option<Quantity>,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct HostPathVolumeSource {
    pub path: String,
    #[serde(default, rename = "type")]
    pub typ: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct NFSVolumeSource {
    pub path: String,
    #[serde(default)]
    pub read_only: bool,
    pub server: String,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PersistentVolumeClaimVolumeSource {
    pub claim_name: String,
    #[serde(default)]
    pub read_only: bool,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct SecretVolumeSource {
    #[serde(default = "int0644")]
    pub default_mode: Integer,
    #[serde(default)]
    pub items: Vec<KeyToPath>,
    pub secret_name: String,
    #[serde(default)]
    pub optional: bool,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum ConditionStatus {
    True,
    False,
    Unknown,
}

#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodStatus {
    pub phase: Option<PodPhase>,
    #[serde(default)]
    pub conditions: Vec<PodCondition>,
    pub message: Option<String>,
    pub reason: Option<String>,
    #[serde(rename = "hostIP")]
    pub host_ip: Option<String>,
    #[serde(rename = "podIP")]
    pub pod_ip: Option<String>,
    pub start_time: Option<Time>,
    #[serde(default)]
    pub init_container_statuses: Vec<ContainerStatus>,
    #[serde(default)]
    pub container_statuses: Vec<ContainerStatus>,
    pub qos_class: Option<PodQOSClass>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum PodConditionType {
    PodScheduled,
    Ready,
    Initialized,
    Unschedulable,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct PodCondition {
    #[serde(rename = "type")]
    pub typ: PodConditionType,
    pub status: ConditionStatus,
    pub last_probe_time: Option<Time>,
    pub last_transition_time: Option<Time>,
    pub reason: Option<String>,
    pub message: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ContainerStatus {
    pub name: String,
    pub state: Option<ContainerState>,
    pub last_termination_state: Option<ContainerState>,
    pub ready: bool,
    pub restart_count: Integer,
    pub image: String,
    #[serde(rename = "imageID")]
    pub image_id: String,
    #[serde(rename = "containerID")]
    pub container_id: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum PodPhase {
    Pending,
    Running,
    Succeeded,
    Failed,
    Unknown,
}

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq)]
pub enum PodQOSClass {
    Guaranteed,
    Burstable,
    BestEffort,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub enum ContainerState {
    Waiting(ContainerStateWaiting),
    Running(ContainerStateRunning),
    Terminated(ContainerStateTerminated),
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ContainerStateWaiting {
    pub reason: Option<String>,
    pub message: Option<String>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ContainerStateRunning {
    pub started_at: Option<Time>,
}

#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
#[serde(rename_all = "camelCase")]
pub struct ContainerStateTerminated {
    pub exit_code: Integer,
    pub signal: Option<Integer>,
    pub reason: Option<String>,
    pub message: Option<String>,
    pub started_at: Option<Time>,
    pub finished_at: Option<Time>,
    #[serde(rename = "container_ID")]
    pub container_id: Option<String>,
}

#[test]
fn deser_pod() {
    let yaml = r#"
      apiVersion: v1
      kind: Pod
      metadata:
        annotations:
          kubernetes.io/config.hash: 9d80efa9dfda66b126ec4f9b5f7a004f
          kubernetes.io/config.mirror: 9d80efa9dfda66b126ec4f9b5f7a004f
          kubernetes.io/config.seen: 2018-02-17T23:17:05.703130559Z
          kubernetes.io/config.source: file
          scheduler.alpha.kubernetes.io/critical-pod: ""
        creationTimestamp: 2018-02-17T23:21:30Z
        labels:
          component: etcd
          tier: control-plane
        name: etcd-minikube
        namespace: kube-system
        resourceVersion: "82834"
        selfLink: /api/v1/namespaces/kube-system/pods/etcd-minikube
        uid: 491e0972-1439-11e8-bdc8-525400cf4e41
      spec:
        containers:
        - command:
          - etcd
          - --listen-client-urls=http://127.0.0.1:2379
          - --advertise-client-urls=http://127.0.0.1:2379
          - --data-dir=/data
          image: gcr.io/google_containers/etcd-amd64:3.1.10
          imagePullPolicy: IfNotPresent
          livenessProbe:
            failureThreshold: 8
            httpGet:
              host: 127.0.0.1
              path: /health
              port: 2379
              scheme: HTTP
            initialDelaySeconds: 15
            periodSeconds: 10
            successThreshold: 1
            timeoutSeconds: 15
          name: etcd
          resources: {}
          terminationMessagePath: /dev/termination-log
          terminationMessagePolicy: File
          volumeMounts:
          - mountPath: /data
            name: etcd
        dnsPolicy: ClusterFirst
        hostNetwork: true
        nodeName: minikube
        restartPolicy: Always
        schedulerName: default-scheduler
        securityContext: {}
        terminationGracePeriodSeconds: 30
        tolerations:
        - effect: NoExecute
          operator: Exists
        volumes:
        - hostPath:
            path: /data
            type: DirectoryOrCreate
          name: etcd
      status:
        conditions:
        - lastProbeTime: null
          lastTransitionTime: 2018-02-20T18:00:06Z
          status: "True"
          type: Initialized
        - lastProbeTime: null
          lastTransitionTime: 2018-02-20T18:00:09Z
          status: "True"
          type: Ready
        - lastProbeTime: null
          lastTransitionTime: 2018-02-20T18:00:05Z
          status: "True"
          type: PodScheduled
        containerStatuses:
        - containerID: docker://f16f6af6da79cab1ea508dae8cf1b03dd19d8d1a724e20e0c9fff74c88081d78
          image: gcr.io/google_containers/etcd-amd64:3.1.10
          imageID: docker-pullable://gcr.io/google_containers/etcd-amd64@sha256:28cf78933de29fd26d7a879e51ebd39784cd98109568fd3da61b141257fb85a6
          lastState:
            terminated:
              containerID: docker://1eb66b2babd510dc4010b96c8503d8c267a787f824c79826c1ea00e3e09d87a4
              exitCode: 0
              finishedAt: 2018-02-20T07:03:14Z
              reason: Completed
              startedAt: 2018-02-19T23:51:16Z
          name: etcd
          ready: true
          restartCount: 3
          state:
            running:
              startedAt: 2018-02-20T18:00:07Z
        hostIP: 192.168.122.187
        phase: Running
        podIP: 192.168.122.187
        qosClass: BestEffort
        startTime: 2018-02-20T18:00:06Z
        "#;

    let pod: Pod = ::serde_yaml::from_str(yaml).unwrap();
    assert_eq!(pod.status.phase, Some(PodPhase::Running));
    assert_eq!(pod.spec.volumes.len(), 1);
    assert_eq!(pod.spec.volumes[0].name, "etcd");
    assert_eq!(
        pod.spec.volumes[0].source,
        VolumeSource::HostPath(HostPathVolumeSource {
            path: String::from("/data"),
            typ: String::from("DirectoryOrCreate"),
        })
    );

    // roundtrip
    let rt_json = ::serde_json::to_value(&pod).unwrap();
    assert_eq!(rt_json["apiVersion"], "v1");
    assert_eq!(rt_json["kind"], "Pod");
    let pod2: Pod = ::serde_json::from_value(rt_json).unwrap();
    assert_eq!(pod, pod2);
}