Skip to content
This repository was archived by the owner on Nov 19, 2020. It is now read-only.

Commit b78f121

Browse files
committed
update scripts
Add new packages to register.go & add some more helper functions to the Time type
1 parent 2300d98 commit b78f121

File tree

3 files changed

+136
-23
lines changed

3 files changed

+136
-23
lines changed

scripts/generate.sh

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
#!/bin/bash -e
22

33
TEMPDIR=$(mktemp -d)
4+
echo $TEMPDIR
45
mkdir -p $TEMPDIR/src/github.com/golang
56
ln -s $PWD/_output/src/github.com/golang/protobuf $TEMPDIR/src/github.com/golang/protobuf
6-
function cleanup {
7+
8+
function cleanup() {
79
unlink $TEMPDIR/src/github.com/golang/protobuf
810
rm -rf $TEMPDIR
911
}
@@ -13,7 +15,7 @@ trap cleanup EXIT
1315
export PATH=$PWD/_output/bin:$PATH
1416

1517
# Copy all .proto files from Kubernetes into a temporary directory.
16-
REPOS=( "apimachinery" "api" "apiextensions-apiserver" "kube-aggregator" )
18+
REPOS=("apimachinery" "api" "apiextensions-apiserver" "kube-aggregator")
1719
for REPO in "${REPOS[@]}"; do
1820
SOURCE=$PWD/_output/kubernetes/staging/src/k8s.io/$REPO
1921
TARGET=$TEMPDIR/src/k8s.io
@@ -25,27 +27,30 @@ done
2527
rm -r $TEMPDIR/src/k8s.io/apimachinery/pkg/apis/testapigroup
2628

2729
cd $TEMPDIR/src
28-
for FILE in $( find . -type f ); do
30+
for FILE in $(find . -type f); do
2931
protoc --gofast_out=. $FILE
3032
done
31-
rm $( find . -type f -name '*.proto' );
33+
rm $(find . -type f -name '*.proto')
3234
cd -
3335

3436
export GOPATH=$TEMPDIR
35-
function mvpkg {
37+
function mvpkg() {
3638
FROM="k8s.io/$1"
3739
TO="github.com/ericchiang/k8s/$2"
3840
mkdir -p "$GOPATH/src/$(dirname $TO)"
3941
echo "gompvpkg -from=$FROM -to=$TO"
4042
gomvpkg -from=$FROM -to=$TO
4143
}
4244

45+
# manually import some packages
4346
mvpkg apiextensions-apiserver/pkg/apis/apiextensions/v1beta1 apis/apiextensions/v1beta1
47+
mvpkg apiextensions-apiserver/pkg/apis/apiextensions/v1 apis/apiextensions/v1
4448
mvpkg apimachinery/pkg/api/resource apis/resource
4549
mvpkg apimachinery/pkg/apis/meta apis/meta
4650
mvpkg apimachinery/pkg/runtime runtime
4751
mvpkg apimachinery/pkg/util util
48-
for DIR in $( ls ${TEMPDIR}/src/k8s.io/api/ ); do
52+
53+
for DIR in $(ls ${TEMPDIR}/src/k8s.io/api/); do
4954
mvpkg api/$DIR apis/$DIR
5055
done
5156
mvpkg kube-aggregator/pkg/apis/apiregistration apis/apiregistration

scripts/json.go.partial

Lines changed: 122 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,137 @@ import (
88
// JSON marshaling logic for the Time type so it can be used for custom
99
// resources, which serialize to JSON.
1010

11-
func (t Time) MarshalJSON() ([]byte, error) {
12-
var seconds, nanos int64
13-
if t.Seconds != nil {
14-
seconds = *t.Seconds
11+
// Copied from https://github.com/kubernetes/kubernetes/blob/d9faaca64738a50455f38dd88845e8b4b5ca37e2/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/time.go
12+
13+
// DeepCopyInto creates a deep-copy of the Time value. The underlying time.Time
14+
// type is effectively immutable in the time API, so it is safe to
15+
// copy-by-assign, despite the presence of (unexported) Pointer fields.
16+
func (t *Time) DeepCopyInto(out *Time) {
17+
*out = *t
18+
}
19+
20+
// NewTime returns a wrapped instance of the provided time
21+
func NewTime(time time.Time) Time {
22+
return Time{time}
23+
}
24+
25+
// Date returns the Time corresponding to the supplied parameters
26+
// by wrapping time.Date.
27+
func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
28+
return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)}
29+
}
30+
31+
// Now returns the current local time.
32+
func Now() Time {
33+
return Time{time.Now()}
34+
}
35+
36+
// IsZero returns true if the value is nil or time is zero.
37+
func (t *Time) IsZero() bool {
38+
if t == nil {
39+
return true
40+
}
41+
return t.Time.IsZero()
42+
}
43+
44+
// Before reports whether the time instant t is before u.
45+
func (t *Time) Before(u *Time) bool {
46+
if t != nil && u != nil {
47+
return t.Time.Before(u.Time)
48+
}
49+
return false
50+
}
51+
52+
// Equal reports whether the time instant t is equal to u.
53+
func (t *Time) Equal(u *Time) bool {
54+
if t == nil && u == nil {
55+
return true
56+
}
57+
if t != nil && u != nil {
58+
return t.Time.Equal(u.Time)
59+
}
60+
return false
61+
}
62+
63+
// Unix returns the local time corresponding to the given Unix time
64+
// by wrapping time.Unix.
65+
func Unix(sec int64, nsec int64) Time {
66+
return Time{time.Unix(sec, nsec)}
67+
}
68+
69+
// Rfc3339Copy returns a copy of the Time at second-level precision.
70+
func (t Time) Rfc3339Copy() Time {
71+
copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
72+
return Time{copied}
73+
}
74+
75+
// UnmarshalJSON implements the json.Unmarshaller interface.
76+
func (t *Time) UnmarshalJSON(b []byte) error {
77+
if len(b) == 4 && string(b) == "null" {
78+
t.Time = time.Time{}
79+
return nil
1580
}
16-
if t.Nanos != nil {
17-
nanos = int64(*t.Nanos)
81+
82+
var str string
83+
err := json.Unmarshal(b, &str)
84+
if err != nil {
85+
return err
1886
}
19-
return json.Marshal(time.Unix(seconds, nanos))
87+
88+
pt, err := time.Parse(time.RFC3339, str)
89+
if err != nil {
90+
return err
91+
}
92+
93+
t.Time = pt.Local()
94+
return nil
2095
}
2196

22-
func (t *Time) UnmarshalJSON(p []byte) error {
23-
var t1 time.Time
24-
if err := json.Unmarshal(p, &t1); err != nil {
97+
// UnmarshalQueryParameter converts from a URL query parameter value to an object
98+
func (t *Time) UnmarshalQueryParameter(str string) error {
99+
if len(str) == 0 {
100+
t.Time = time.Time{}
101+
return nil
102+
}
103+
// Tolerate requests from older clients that used JSON serialization to build query params
104+
if len(str) == 4 && str == "null" {
105+
t.Time = time.Time{}
106+
return nil
107+
}
108+
109+
pt, err := time.Parse(time.RFC3339, str)
110+
if err != nil {
25111
return err
26112
}
27-
seconds := t1.Unix()
28-
nanos := int32(t1.UnixNano())
29-
t.Seconds = &seconds
30-
t.Nanos = &nanos
113+
114+
t.Time = pt.Local()
31115
return nil
32116
}
33117

118+
// MarshalJSON implements the json.Marshaler interface.
119+
func (t Time) MarshalJSON() ([]byte, error) {
120+
if t.IsZero() {
121+
// Encode unset/nil objects as JSON's "null".
122+
return []byte("null"), nil
123+
}
124+
buf := make([]byte, 0, len(time.RFC3339)+2)
125+
buf = append(buf, '"')
126+
// time cannot contain non escapable JSON characters
127+
buf = t.UTC().AppendFormat(buf, time.RFC3339)
128+
buf = append(buf, '"')
129+
return buf, nil
130+
}
131+
132+
// ToUnstructured implements the value.UnstructuredConverter interface.
133+
func (t Time) ToUnstructured() interface{} {
134+
if t.IsZero() {
135+
return nil
136+
}
137+
buf := make([]byte, 0, len(time.RFC3339))
138+
buf = t.UTC().AppendFormat(buf, time.RFC3339)
139+
return string(buf)
140+
}
141+
34142
// Status must implement json.Unmarshaler for the codec to deserialize a JSON
35143
// payload into it.
36144
//

scripts/register.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,6 @@ var apiGroups = []APIGroup{
5454
{"MutatingWebhookConfiguration", "", NotNamespaced},
5555
{"ValidatingWebhookConfiguration", "", NotNamespaced},
5656
},
57-
"v1alpha1": []Resource{
58-
{"InitializerConfiguration", "", NotNamespaced},
59-
},
6057
},
6158
},
6259
{
@@ -66,6 +63,9 @@ var apiGroups = []APIGroup{
6663
"v1beta1": []Resource{
6764
{"CustomResourceDefinition", "", NotNamespaced},
6865
},
66+
"v1": []Resource{
67+
{"CustomResourceDefinition","", NotNamespaced},
68+
},
6969
},
7070
},
7171
{

0 commit comments

Comments
 (0)