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

adds conversion methods between spec and API #23

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@
.build
*.swp
release-tools
vendor
.idea
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ This repository hosts the API defintion of the Custom Resource Definitions (CRD)

**NOTE**: All of the APIs are defined under the API group `objectstorage.k8s.io`.

For more information about COSI, visit our [documentation](https://github.com/kubernetes-sigs/container-object-storage-interface-api/tree/master/docs/index.md).
For more information about COSI, visit our [documentation](https://sigs.k8s.io/container-object-storage-interface-api/tree/master/docs/index.md).

## Developer Guide

All API definitions are in [`apis/objectstorage.k8s.io/`](./apis/objectstorage.k8s.io/). All API changes **_MUST_** satisfy the following requirements:

- Must be backwards compatible
- Must be in-sync with the API definitions in [sigs.k8s.io/container-object-storage-interface-spec](https://github.com/kubernetes-sigs/container-object-storage-interface-spec)
- Must be in-sync with the API definitions in [sigs.k8s.io/container-object-storage-interface-spec](https://sigs.k8s.io/container-object-storage-interface-spec)

### Build and Test

Expand Down
18 changes: 18 additions & 0 deletions apis/objectstorage.k8s.io/v1alpha1/azure_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,25 @@ limitations under the License.

package v1alpha1

import osspec "sigs.k8s.io/container-object-storage-interface-spec"

type AzureProtocol struct {
ContainerName string `json:"containerName,omitempty"`
StorageAccount string `json:"storageAccount,omitempty"`
}

func (azure *AzureProtocol) ConvertToExternal() *osspec.Protocol_AzureBlob {
return &osspec.Protocol_AzureBlob{
AzureBlob: &osspec.AzureBlobParameters{
ContainerName: azure.ContainerName,
StorageAccount: azure.StorageAccount,
},
}
}

func ConvertFromAzureExternal(ext *osspec.AzureBlobParameters) *AzureProtocol {
return &AzureProtocol{
StorageAccount: ext.StorageAccount,
ContainerName: ext.ContainerName,
}
}
22 changes: 22 additions & 0 deletions apis/objectstorage.k8s.io/v1alpha1/gcs_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,31 @@ limitations under the License.

package v1alpha1

import osspec "sigs.k8s.io/container-object-storage-interface-spec"

type GCSProtocol struct {
BucketName string `json:"bucketName,omitempty"`
PrivateKeyName string `json:"privateKeyName,omitempty"`
ProjectID string `json:"projectID,omitempty"`
ServiceAccount string `json:"serviceAccount,omitempty"`
}

func (gcs *GCSProtocol) ConvertToExternal() *osspec.Protocol_Gcs {
return &osspec.Protocol_Gcs{
Gcs: &osspec.GCSParameters{
BucketName: gcs.BucketName,
PrivateKeyName: gcs.PrivateKeyName,
ProjectId: gcs.ProjectID,
ServiceAccount: gcs.ServiceAccount,
},
}
}

func ConvertFromGCSExternal(ext *osspec.GCSParameters) *GCSProtocol {
return &GCSProtocol{
BucketName: ext.BucketName,
PrivateKeyName: ext.PrivateKeyName,
ProjectID: ext.ProjectId,
ServiceAccount: ext.ServiceAccount,
}
}
24 changes: 12 additions & 12 deletions apis/objectstorage.k8s.io/v1alpha1/openapi_generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions apis/objectstorage.k8s.io/v1alpha1/protocol_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,23 @@ limitations under the License.

package v1alpha1

import (
"errors"

osspec "sigs.k8s.io/container-object-storage-interface-spec"
)

type ProtocolName string

const (
ProtocolNameS3 ProtocolName = "s3"
ProtocolNameAzure ProtocolName = "azureBlob"
ProtocolNameGCS ProtocolName = "gcs"

MissingS3Protocol = "missing s3 in protocol"
MissingAzureProtocol = "missing azure in protocol"
MissingGCSProtocol = "missing gcs in protocol"
InvalidProtocolName = "invalid protocol name"
)

type Protocol struct {
Expand All @@ -36,3 +47,67 @@ type Protocol struct {
// +optional
GCS *GCSProtocol `json:"gcs,omitempty"`
}

func (in *Protocol) ConvertToExternal() (*osspec.Protocol, error) {
external := &osspec.Protocol{
Version: in.Version,
}

switch in.Name {
case ProtocolNameS3:
if in.S3 == nil {
return nil, errors.New(MissingS3Protocol)
}
external.Name = osspec.ProtocolName_S3
external.Type = in.S3.ConvertToExternal()
case ProtocolNameAzure:
if in.AzureBlob == nil {
return nil, errors.New(MissingAzureProtocol)
}
external.Name = osspec.ProtocolName_AZURE
external.Type = in.AzureBlob.ConvertToExternal()
case ProtocolNameGCS:
if in.GCS == nil {
return nil, errors.New(MissingGCSProtocol)
}
external.Name = osspec.ProtocolName_GCS
external.Type = in.GCS.ConvertToExternal()
default:
external.Name = osspec.ProtocolName_UnknownProtocol
return external, errors.New(InvalidProtocolName)

}

return external, nil
}

func ConvertFromProtocolExternal(external *osspec.Protocol) (*Protocol, error) {
in := &Protocol{}
in.Version = external.Version

switch external.Name {
case osspec.ProtocolName_S3:
if external.GetS3() == nil {
return nil, errors.New(MissingS3Protocol)
}
in.Name = ProtocolNameS3
in.S3 = ConvertFromS3External(external.GetS3())
case osspec.ProtocolName_AZURE:
if external.GetAzureBlob() == nil {
return nil, errors.New(MissingAzureProtocol)
}
in.Name = ProtocolNameAzure
in.AzureBlob = ConvertFromAzureExternal(external.GetAzureBlob())
case osspec.ProtocolName_GCS:
if external.GetGcs() == nil {
return nil, errors.New(MissingGCSProtocol)
}
in.Name = ProtocolNameGCS
in.GCS = ConvertFromGCSExternal(external.GetGcs())
default:
// TODO - Do we to set the protocol Name to specific value here?
return nil, errors.New(InvalidProtocolName)
}

return in, nil
}
39 changes: 36 additions & 3 deletions apis/objectstorage.k8s.io/v1alpha1/s3_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,50 @@ limitations under the License.

package v1alpha1

import (
osspec "sigs.k8s.io/container-object-storage-interface-spec"
)

type S3SignatureVersion string

const (
S3SignatureVersionV2 S3SignatureVersion = "S3v2"
S3SignatureVersionV4 S3SignatureVersion = "S3v4"
S3SignatureVersionV2 S3SignatureVersion = "S3V2"
S3SignatureVersionV4 S3SignatureVersion = "S3V4"
)

type S3Protocol struct {
Endpoint string `json:"endpoint,omitempty"`
BucketName string `json:"bucketName,omitempty"`
Region string `json:"region,omitempty"`
// +kubebuilder:validation:Enum:={s3v2,s3v4}
// +kubebuilder:validation:Enum:={S3V2,S3V4}
SignatureVersion S3SignatureVersion `json:"signatureVersion,omitempty"`
}

func (s3 *S3Protocol) ConvertToExternal() *osspec.Protocol_S3 {
sigver, ok := osspec.S3SignatureVersion_value[string(s3.SignatureVersion)]
if !ok {
// NOTE - 0 here is equivalent to UnknownSignature
sigver = 0
}
return &osspec.Protocol_S3{
S3: &osspec.S3Parameters{
Endpoint: s3.Endpoint,
BucketName: s3.BucketName,
Region: s3.Region,
SignatureVersion: osspec.S3SignatureVersion(sigver),
},
}
}

func ConvertFromS3External(ext *osspec.S3Parameters) *S3Protocol {
vers, ok := osspec.S3SignatureVersion_name[int32(ext.SignatureVersion)]
if !ok {
vers = osspec.S3SignatureVersion_name[0]
}
return &S3Protocol{
BucketName: ext.BucketName,
Endpoint: ext.Endpoint,
Region: ext.Region,
SignatureVersion: S3SignatureVersion(vers),
}
}
2 changes: 1 addition & 1 deletion clientset/fake/register.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crds/objectstorage.k8s.io_bucketaccessclasses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: (devel)
creationTimestamp: null
name: bucketaccessclasses.objectstorage.k8s.io
spec:
Expand Down
2 changes: 1 addition & 1 deletion crds/objectstorage.k8s.io_bucketaccesses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: (devel)
creationTimestamp: null
name: bucketaccesses.objectstorage.k8s.io
spec:
Expand Down
2 changes: 1 addition & 1 deletion crds/objectstorage.k8s.io_bucketaccessrequests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: (devel)
creationTimestamp: null
name: bucketaccessrequests.objectstorage.k8s.io
spec:
Expand Down
6 changes: 3 additions & 3 deletions crds/objectstorage.k8s.io_bucketclasses.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
annotations:
controller-gen.kubebuilder.io/version: v0.4.1
controller-gen.kubebuilder.io/version: (devel)
creationTimestamp: null
name: bucketclasses.objectstorage.k8s.io
spec:
Expand Down Expand Up @@ -91,8 +91,8 @@ spec:
type: string
signatureVersion:
enum:
- s3v2
- s3v4
- S3V2
- S3V4
type: string
type: object
version:
Expand Down
Loading