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

Commit

Permalink
update scripts
Browse files Browse the repository at this point in the history
Add new packages to register.go & add some more helper functions to the Time type
  • Loading branch information
daxmc99 committed Nov 11, 2020
1 parent 2300d98 commit b78f121
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 23 deletions.
17 changes: 11 additions & 6 deletions scripts/generate.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
#!/bin/bash -e

TEMPDIR=$(mktemp -d)
echo $TEMPDIR
mkdir -p $TEMPDIR/src/github.com/golang
ln -s $PWD/_output/src/github.com/golang/protobuf $TEMPDIR/src/github.com/golang/protobuf
function cleanup {

function cleanup() {
unlink $TEMPDIR/src/github.com/golang/protobuf
rm -rf $TEMPDIR
}
Expand All @@ -13,7 +15,7 @@ trap cleanup EXIT
export PATH=$PWD/_output/bin:$PATH

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

cd $TEMPDIR/src
for FILE in $( find . -type f ); do
for FILE in $(find . -type f); do
protoc --gofast_out=. $FILE
done
rm $( find . -type f -name '*.proto' );
rm $(find . -type f -name '*.proto')
cd -

export GOPATH=$TEMPDIR
function mvpkg {
function mvpkg() {
FROM="k8s.io/$1"
TO="github.com/ericchiang/k8s/$2"
mkdir -p "$GOPATH/src/$(dirname $TO)"
echo "gompvpkg -from=$FROM -to=$TO"
gomvpkg -from=$FROM -to=$TO
}

# manually import some packages
mvpkg apiextensions-apiserver/pkg/apis/apiextensions/v1beta1 apis/apiextensions/v1beta1
mvpkg apiextensions-apiserver/pkg/apis/apiextensions/v1 apis/apiextensions/v1
mvpkg apimachinery/pkg/api/resource apis/resource
mvpkg apimachinery/pkg/apis/meta apis/meta
mvpkg apimachinery/pkg/runtime runtime
mvpkg apimachinery/pkg/util util
for DIR in $( ls ${TEMPDIR}/src/k8s.io/api/ ); do

for DIR in $(ls ${TEMPDIR}/src/k8s.io/api/); do
mvpkg api/$DIR apis/$DIR
done
mvpkg kube-aggregator/pkg/apis/apiregistration apis/apiregistration
Expand Down
136 changes: 122 additions & 14 deletions scripts/json.go.partial
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,137 @@ import (
// JSON marshaling logic for the Time type so it can be used for custom
// resources, which serialize to JSON.

func (t Time) MarshalJSON() ([]byte, error) {
var seconds, nanos int64
if t.Seconds != nil {
seconds = *t.Seconds
// Copied from https://github.com/kubernetes/kubernetes/blob/d9faaca64738a50455f38dd88845e8b4b5ca37e2/staging/src/k8s.io/apimachinery/pkg/apis/meta/v1/time.go

// DeepCopyInto creates a deep-copy of the Time value. The underlying time.Time
// type is effectively immutable in the time API, so it is safe to
// copy-by-assign, despite the presence of (unexported) Pointer fields.
func (t *Time) DeepCopyInto(out *Time) {
*out = *t
}

// NewTime returns a wrapped instance of the provided time
func NewTime(time time.Time) Time {
return Time{time}
}

// Date returns the Time corresponding to the supplied parameters
// by wrapping time.Date.
func Date(year int, month time.Month, day, hour, min, sec, nsec int, loc *time.Location) Time {
return Time{time.Date(year, month, day, hour, min, sec, nsec, loc)}
}

// Now returns the current local time.
func Now() Time {
return Time{time.Now()}
}

// IsZero returns true if the value is nil or time is zero.
func (t *Time) IsZero() bool {
if t == nil {
return true
}
return t.Time.IsZero()
}

// Before reports whether the time instant t is before u.
func (t *Time) Before(u *Time) bool {
if t != nil && u != nil {
return t.Time.Before(u.Time)
}
return false
}

// Equal reports whether the time instant t is equal to u.
func (t *Time) Equal(u *Time) bool {
if t == nil && u == nil {
return true
}
if t != nil && u != nil {
return t.Time.Equal(u.Time)
}
return false
}

// Unix returns the local time corresponding to the given Unix time
// by wrapping time.Unix.
func Unix(sec int64, nsec int64) Time {
return Time{time.Unix(sec, nsec)}
}

// Rfc3339Copy returns a copy of the Time at second-level precision.
func (t Time) Rfc3339Copy() Time {
copied, _ := time.Parse(time.RFC3339, t.Format(time.RFC3339))
return Time{copied}
}

// UnmarshalJSON implements the json.Unmarshaller interface.
func (t *Time) UnmarshalJSON(b []byte) error {
if len(b) == 4 && string(b) == "null" {
t.Time = time.Time{}
return nil
}
if t.Nanos != nil {
nanos = int64(*t.Nanos)

var str string
err := json.Unmarshal(b, &str)
if err != nil {
return err
}
return json.Marshal(time.Unix(seconds, nanos))

pt, err := time.Parse(time.RFC3339, str)
if err != nil {
return err
}

t.Time = pt.Local()
return nil
}

func (t *Time) UnmarshalJSON(p []byte) error {
var t1 time.Time
if err := json.Unmarshal(p, &t1); err != nil {
// UnmarshalQueryParameter converts from a URL query parameter value to an object
func (t *Time) UnmarshalQueryParameter(str string) error {
if len(str) == 0 {
t.Time = time.Time{}
return nil
}
// Tolerate requests from older clients that used JSON serialization to build query params
if len(str) == 4 && str == "null" {
t.Time = time.Time{}
return nil
}

pt, err := time.Parse(time.RFC3339, str)
if err != nil {
return err
}
seconds := t1.Unix()
nanos := int32(t1.UnixNano())
t.Seconds = &seconds
t.Nanos = &nanos

t.Time = pt.Local()
return nil
}

// MarshalJSON implements the json.Marshaler interface.
func (t Time) MarshalJSON() ([]byte, error) {
if t.IsZero() {
// Encode unset/nil objects as JSON's "null".
return []byte("null"), nil
}
buf := make([]byte, 0, len(time.RFC3339)+2)
buf = append(buf, '"')
// time cannot contain non escapable JSON characters
buf = t.UTC().AppendFormat(buf, time.RFC3339)
buf = append(buf, '"')
return buf, nil
}

// ToUnstructured implements the value.UnstructuredConverter interface.
func (t Time) ToUnstructured() interface{} {
if t.IsZero() {
return nil
}
buf := make([]byte, 0, len(time.RFC3339))
buf = t.UTC().AppendFormat(buf, time.RFC3339)
return string(buf)
}

// Status must implement json.Unmarshaler for the codec to deserialize a JSON
// payload into it.
//
Expand Down
6 changes: 3 additions & 3 deletions scripts/register.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,6 @@ var apiGroups = []APIGroup{
{"MutatingWebhookConfiguration", "", NotNamespaced},
{"ValidatingWebhookConfiguration", "", NotNamespaced},
},
"v1alpha1": []Resource{
{"InitializerConfiguration", "", NotNamespaced},
},
},
},
{
Expand All @@ -66,6 +63,9 @@ var apiGroups = []APIGroup{
"v1beta1": []Resource{
{"CustomResourceDefinition", "", NotNamespaced},
},
"v1": []Resource{
{"CustomResourceDefinition","", NotNamespaced},
},
},
},
{
Expand Down

0 comments on commit b78f121

Please sign in to comment.