Skip to content
This repository was archived by the owner on Dec 6, 2024. It is now read-only.

Commit e7cc5c2

Browse files
author
Krish Chowdhary
committed
adds conversion methods between spec and API
1 parent 7a8f1e9 commit e7cc5c2

20 files changed

+189
-42
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
.build
44
*.swp
55
release-tools
6+
vendor
7+
.idea
8+
travis.yml

Diff for: apis/objectstorage.k8s.io/v1alpha1/azure_types.go

+18
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,25 @@ limitations under the License.
1818

1919
package v1alpha1
2020

21+
import osspec "sigs.k8s.io/container-object-storage-interface-spec"
22+
2123
type AzureProtocol struct {
2224
ContainerName string `json:"containerName,omitempty"`
2325
StorageAccount string `json:"storageAccount,omitempty"`
2426
}
27+
28+
func (azure *AzureProtocol) ConvertToExternal() *osspec.Protocol_AzureBlob {
29+
return &osspec.Protocol_AzureBlob{
30+
AzureBlob: &osspec.AzureBlobParameters{
31+
ContainerName: azure.ContainerName,
32+
StorageAccount: azure.StorageAccount,
33+
},
34+
}
35+
}
36+
37+
func ConvertFromAzureExternal(ext *osspec.AzureBlobParameters) *AzureProtocol {
38+
azure := &AzureProtocol{}
39+
azure.StorageAccount = ext.StorageAccount
40+
azure.ContainerName = ext.ContainerName
41+
return azure
42+
}

Diff for: apis/objectstorage.k8s.io/v1alpha1/gcs_types.go

+22
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,31 @@ limitations under the License.
1818

1919
package v1alpha1
2020

21+
import osspec "sigs.k8s.io/container-object-storage-interface-spec"
22+
2123
type GCSProtocol struct {
2224
BucketName string `json:"bucketName,omitempty"`
2325
PrivateKeyName string `json:"privateKeyName,omitempty"`
2426
ProjectID string `json:"projectID,omitempty"`
2527
ServiceAccount string `json:"serviceAccount,omitempty"`
2628
}
29+
30+
func (gcs *GCSProtocol) ConvertToExternal() *osspec.Protocol_Gcs {
31+
return &osspec.Protocol_Gcs{
32+
Gcs: &osspec.GCSParameters{
33+
BucketName: gcs.BucketName,
34+
PrivateKeyName: gcs.PrivateKeyName,
35+
ProjectId: gcs.ProjectID,
36+
ServiceAccount: gcs.ServiceAccount,
37+
},
38+
}
39+
}
40+
41+
func ConvertFromGCSExternal(ext *osspec.GCSParameters) *GCSProtocol {
42+
gcs := &GCSProtocol{}
43+
gcs.BucketName = ext.BucketName
44+
gcs.PrivateKeyName = ext.PrivateKeyName
45+
gcs.ProjectID = ext.ProjectId
46+
gcs.ServiceAccount = ext.ServiceAccount
47+
return gcs
48+
}

Diff for: apis/objectstorage.k8s.io/v1alpha1/protocol_types.go

+73
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,23 @@ limitations under the License.
1616

1717
package v1alpha1
1818

19+
import (
20+
"errors"
21+
22+
osspec "sigs.k8s.io/container-object-storage-interface-spec"
23+
)
24+
1925
type ProtocolName string
2026

2127
const (
2228
ProtocolNameS3 ProtocolName = "s3"
2329
ProtocolNameAzure ProtocolName = "azureBlob"
2430
ProtocolNameGCS ProtocolName = "gcs"
31+
32+
MissingS3Protocol = "missing s3 in protocol"
33+
MissingAzureProtocol = "missing azure in protocol"
34+
MissingGCSProtocol = "missing gcs in protocol"
35+
InvalidProtocolName = "invalid protocol name"
2536
)
2637

2738
type Protocol struct {
@@ -36,3 +47,65 @@ type Protocol struct {
3647
// +optional
3748
GCS *GCSProtocol `json:"gcs,omitempty"`
3849
}
50+
51+
func (in *Protocol) ConvertToExternal() (*osspec.Protocol, error) {
52+
external := &osspec.Protocol{
53+
Version: in.Version,
54+
}
55+
56+
switch in.Name {
57+
case ProtocolNameS3:
58+
if in.S3 != nil {
59+
return nil, errors.New(MissingS3Protocol)
60+
}
61+
external.Name = osspec.ProtocolName_S3
62+
external.Type = in.S3.ConvertToExternal()
63+
case ProtocolNameAzure:
64+
if in.S3 != nil {
65+
return nil, errors.New(MissingAzureProtocol)
66+
}
67+
external.Name = osspec.ProtocolName_AZURE
68+
external.Type = in.AzureBlob.ConvertToExternal()
69+
case ProtocolNameGCS:
70+
if in.S3 != nil {
71+
return nil, errors.New(MissingGCSProtocol)
72+
}
73+
external.Name = osspec.ProtocolName_GCS
74+
external.Type = in.GCS.ConvertToExternal()
75+
default:
76+
external.Name = osspec.ProtocolName_UnknownProtocol
77+
}
78+
79+
return external, nil
80+
}
81+
82+
func ConvertFromProtocolExternal(external *osspec.Protocol) (*Protocol, error) {
83+
in := &Protocol{}
84+
in.Version = external.Version
85+
86+
switch external.Name {
87+
case osspec.ProtocolName_S3:
88+
if external.GetS3() != nil {
89+
return nil, errors.New(MissingS3Protocol)
90+
}
91+
in.Name = ProtocolNameS3
92+
in.S3 = ConvertFromS3External(external.GetS3())
93+
case osspec.ProtocolName_AZURE:
94+
if external.GetAzureBlob() != nil {
95+
return nil, errors.New(MissingAzureProtocol)
96+
}
97+
in.Name = ProtocolNameAzure
98+
in.AzureBlob = ConvertFromAzureExternal(external.GetAzureBlob())
99+
case osspec.ProtocolName_GCS:
100+
if external.GetGcs() != nil {
101+
return nil, errors.New(MissingGCSProtocol)
102+
}
103+
in.Name = ProtocolNameGCS
104+
in.GCS = ConvertFromGCSExternal(external.GetGcs())
105+
default:
106+
// TODO - Do we to set the protocol Name to specific value here?
107+
return nil, errors.New(InvalidProtocolName)
108+
}
109+
110+
return in, nil
111+
}

Diff for: apis/objectstorage.k8s.io/v1alpha1/s3_types.go

+36-3
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,50 @@ limitations under the License.
1818

1919
package v1alpha1
2020

21+
import (
22+
osspec "sigs.k8s.io/container-object-storage-interface-spec"
23+
)
24+
2125
type S3SignatureVersion string
2226

2327
const (
24-
S3SignatureVersionV2 S3SignatureVersion = "S3v2"
25-
S3SignatureVersionV4 S3SignatureVersion = "S3v4"
28+
S3SignatureVersionV2 S3SignatureVersion = "S3V2"
29+
S3SignatureVersionV4 S3SignatureVersion = "S3V4"
2630
)
2731

2832
type S3Protocol struct {
2933
Endpoint string `json:"endpoint,omitempty"`
3034
BucketName string `json:"bucketName,omitempty"`
3135
Region string `json:"region,omitempty"`
32-
// +kubebuilder:validation:Enum:={s3v2,s3v4}
36+
// +kubebuilder:validation:Enum:={S3V2,S3V4}
3337
SignatureVersion S3SignatureVersion `json:"signatureVersion,omitempty"`
3438
}
39+
40+
func (s3 *S3Protocol) ConvertToExternal() *osspec.Protocol_S3 {
41+
sigver, ok := osspec.S3SignatureVersion_value[string(s3.SignatureVersion)]
42+
if !ok {
43+
// NOTE - 0 here is equivalent to UnknownSignature
44+
sigver = 0
45+
}
46+
return &osspec.Protocol_S3{
47+
S3: &osspec.S3Parameters{
48+
Endpoint: s3.Endpoint,
49+
BucketName: s3.BucketName,
50+
Region: s3.Region,
51+
SignatureVersion: osspec.S3SignatureVersion(sigver),
52+
},
53+
}
54+
}
55+
56+
func ConvertFromS3External(ext *osspec.S3Parameters) *S3Protocol {
57+
s3 := &S3Protocol{}
58+
s3.BucketName = ext.BucketName
59+
s3.Endpoint = ext.Endpoint
60+
s3.Region = ext.Region
61+
vers, ok := osspec.S3SignatureVersion_name[int32(ext.SignatureVersion)]
62+
if !ok {
63+
vers = osspec.S3SignatureVersion_name[0]
64+
}
65+
s3.SignatureVersion = S3SignatureVersion(vers)
66+
return s3
67+
}

Diff for: clientset/fake/register.go

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: crds/objectstorage.k8s.io_bucketaccessclasses.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -101,5 +101,5 @@ status:
101101
acceptedNames:
102102
kind: ""
103103
plural: ""
104-
conditions: null
105-
storedVersions: null
104+
conditions: []
105+
storedVersions: []

Diff for: crds/objectstorage.k8s.io_bucketaccesses.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,5 +183,5 @@ status:
183183
acceptedNames:
184184
kind: ""
185185
plural: ""
186-
conditions: null
187-
storedVersions: null
186+
conditions: []
187+
storedVersions: []

Diff for: crds/objectstorage.k8s.io_bucketaccessrequests.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,5 +62,5 @@ status:
6262
acceptedNames:
6363
kind: ""
6464
plural: ""
65-
conditions: null
66-
storedVersions: null
65+
conditions: []
66+
storedVersions: []

Diff for: crds/objectstorage.k8s.io_bucketclasses.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ spec:
9191
type: string
9292
signatureVersion:
9393
enum:
94-
- s3v2
95-
- s3v4
94+
- S3V2
95+
- S3V4
9696
type: string
9797
type: object
9898
version:
@@ -114,5 +114,5 @@ status:
114114
acceptedNames:
115115
kind: ""
116116
plural: ""
117-
conditions: null
118-
storedVersions: null
117+
conditions: []
118+
storedVersions: []

Diff for: crds/objectstorage.k8s.io_bucketrequests.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,5 @@ status:
5757
acceptedNames:
5858
kind: ""
5959
plural: ""
60-
conditions: null
61-
storedVersions: null
60+
conditions: []
61+
storedVersions: []

Diff for: crds/objectstorage.k8s.io_buckets.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,8 @@ spec:
151151
type: string
152152
signatureVersion:
153153
enum:
154-
- s3v2
155-
- s3v4
154+
- S3V2
155+
- S3V4
156156
type: string
157157
type: object
158158
version:
@@ -185,5 +185,5 @@ status:
185185
acceptedNames:
186186
kind: ""
187187
plural: ""
188-
conditions: null
189-
storedVersions: null
188+
conditions: []
189+
storedVersions: []

Diff for: go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,6 @@ require (
1212
k8s.io/apimachinery v0.19.4
1313
k8s.io/client-go v0.19.4
1414
k8s.io/kube-openapi v0.0.0-20200923155610-8b5066479488
15+
sigs.k8s.io/container-object-storage-interface-spec v0.0.0-20210224211525-dfa3af562c18
1516
sigs.k8s.io/controller-runtime v0.6.3
1617
)

0 commit comments

Comments
 (0)