Skip to content

Commit

Permalink
Merge pull request #95 from guymguym/guy-operator
Browse files Browse the repository at this point in the history
v2.0.3 + fix big int json parsing
  • Loading branch information
guymguym authored Oct 22, 2019
2 parents 8fc981c + 2204fd4 commit 0a9b637
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 31 deletions.
2 changes: 1 addition & 1 deletion deploy/operator.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ spec:
serviceAccountName: noobaa
containers:
- name: noobaa-operator
image: noobaa/noobaa-operator:2.0.2
image: noobaa/noobaa-operator:2.0.3
imagePullPolicy: IfNotPresent
resources:
limits:
Expand Down
6 changes: 3 additions & 3 deletions pkg/bundle/deploy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package bundle

const Version = "2.0.2"
const Version = "2.0.3"

const Sha256_deploy_cluster_role_yaml = "f719ff8e0015a73d4e6ff322d2b30efa1cc89fcb3f856c06a5910785cb9e8dd8"

Expand Down Expand Up @@ -1741,7 +1741,7 @@ spec:
sourceNamespace: marketplace
`

const Sha256_deploy_operator_yaml = "ffc307ad6c1a4f0c5c6e23d821eaf811f7fd0026eaa1815b049bb367503fabfa"
const Sha256_deploy_operator_yaml = "ee73435f1e351d4e4bf9b33bc38b2520a67e75a6815a4c2f8154dc9dd3effb7e"

const File_deploy_operator_yaml = `apiVersion: apps/v1
kind: Deployment
Expand All @@ -1761,7 +1761,7 @@ spec:
serviceAccountName: noobaa
containers:
- name: noobaa-operator
image: noobaa/noobaa-operator:2.0.2
image: noobaa/noobaa-operator:2.0.3
imagePullPolicy: IfNotPresent
resources:
limits:
Expand Down
39 changes: 30 additions & 9 deletions pkg/nb/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
// Package nb makes client API calls to noobaa servers.
package nb

import (
"encoding/json"
)

// Client is the interface providing typed noobaa API calls
type Client interface {
SetAuthToken(token string)
Expand Down Expand Up @@ -124,16 +128,33 @@ type StorageInfo struct {
Real *BigInt `json:"real,omitempty"`
}

// BigInt is an api type to handle large integers that cannot be represented by JSON
type BigInt int64
// BigInt is an api type to handle large integers that cannot be represented by JSON which is limited to 53 bits (less than 8 PB)
type BigInt struct {
N int64 `json:"n"`
Peta int64 `json:"peta"`
}

// MarshalJSON is custom marshalling because the json schema is oneOf integer or {n,peta}
func (n BigInt) MarshalJSON() ([]byte, error) {
if n.Peta == 0 {
return json.Marshal(n.N)
}
type bigint BigInt
return json.Marshal(bigint(n))
}

// TODO need to write custom marshalling because the json schema is oneOf integer or {n,peta}
// func (n BigInt) MarshalJSON() ([]byte, error) {
// return nil, nil
// }
// func (n *BigInt) UnmarshalJSON(data []byte) error {
// return nil
// }
// UnmarshalJSON is custom unmarshalling because the json schema is oneOf integer or {n,peta}
func (n *BigInt) UnmarshalJSON(data []byte) error {
var i int64
err := json.Unmarshal(data, &i)
if err == nil {
n.N = i
n.Peta = 0
return nil
}
type bigint BigInt
return json.Unmarshal(data, (*bigint)(n))
}

// PoolInfo is a struct of pool info returned by the API
type PoolInfo struct {
Expand Down
62 changes: 45 additions & 17 deletions pkg/nb/api_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,48 @@
package nb

/*func TestCreateSystemAPI(t *testing.T) {
api := CreateSystemAPI{
Name: "name.test",
Email: "[email protected]",
Password: "password!test",
import (
"encoding/json"
"fmt"
"testing"
)

func testBigIntMarshal(t *testing.T, bi BigInt) {
data, err := json.Marshal(bi)
if err != nil {
t.Fatal(err)
}
fmt.Printf("%+v => %q \n", bi, string(data))
}

func testBigIntUnmarshal(t *testing.T, bigIntJSON string) {
bi := BigInt{}
err := json.Unmarshal([]byte(bigIntJSON), &bi)
if err != nil {
t.Fatal(err)
}
expectedReq := fmt.Sprintf(`{"api":"system_api","method":"create_system","params":{"name":"%s","email":"%s","password":"%s"}}`, api.Name, api.Email, api.Password)
expectedRes := fmt.Sprintf(`{"op":"res","reqid":"1","took":11.123,"reply":{"token":"abc"}}`)
reqBody, res := api.build()
reqBytes, err := json.Marshal(reqBody)
assert.NoError(t, err)
assert.Equal(t, expectedReq, string(reqBytes))
err = json.Unmarshal([]byte(expectedRes), res)
assert.NoError(t, err)
assert.Equal(t, api.Response.Op, "res")
assert.Equal(t, api.Response.RequestID)
assert.Equal(t, api.Response.Reply.Token, "abc")
}*/
fmt.Printf("Unmarshal %q => %#v \n", bigIntJSON, bi)
}

func TestBigIntMarshalSmall(t *testing.T) {
testBigIntMarshal(t, BigInt{N: 11})
}

func TestBigIntMarshalBig(t *testing.T) {
testBigIntMarshal(t, BigInt{N: 666, Peta: 1})
}

func TestBigIntUnmarshalZero(t *testing.T) {
testBigIntUnmarshal(t, `0`)
}

func TestBigIntUnmarshalSmall(t *testing.T) {
testBigIntUnmarshal(t, `3333`)
}

func TestBigIntUnmarshalBig(t *testing.T) {
testBigIntUnmarshal(t, `{"n":1111,"peta":29}`)
}

func TestBigIntUnmarshalBigNoPeta(t *testing.T) {
testBigIntUnmarshal(t, `{"n":99}`)
}
2 changes: 1 addition & 1 deletion version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ package version

const (
// Version is the noobaa-operator version (semver)
Version = "2.0.2"
Version = "2.0.3"
)

0 comments on commit 0a9b637

Please sign in to comment.