|
| 1 | +/* |
| 2 | +Copyright 2025 The Kubernetes Authors. |
| 3 | +
|
| 4 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +you may not use this file except in compliance with the License. |
| 6 | +You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | +Unless required by applicable law or agreed to in writing, software |
| 11 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +See the License for the specific language governing permissions and |
| 14 | +limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +// Package protocol contains definitions and functions for transforming COSI gRPC spec definitions |
| 18 | +// into COSI Kubernetes definitions. |
| 19 | +package protocol |
| 20 | + |
| 21 | +import ( |
| 22 | + "fmt" |
| 23 | + |
| 24 | + cosiapi "sigs.k8s.io/container-object-storage-interface/client/apis/objectstorage/v1alpha2" |
| 25 | + cosiproto "sigs.k8s.io/container-object-storage-interface/proto" |
| 26 | +) |
| 27 | + |
| 28 | +// ObjectProtocolTranslator translates object protocol types between the RPC driver-domain and |
| 29 | +// Kubernetes API user-domain. |
| 30 | +type ObjectProtocolTranslator struct{} |
| 31 | + |
| 32 | +var ( |
| 33 | + objectProtocolProtoApiMapping = map[cosiproto.ObjectProtocol_Type]cosiapi.ObjectProtocol{ |
| 34 | + cosiproto.ObjectProtocol_S3: cosiapi.ObjectProtocolS3, |
| 35 | + cosiproto.ObjectProtocol_AZURE: cosiapi.ObjectProtocolAzure, |
| 36 | + cosiproto.ObjectProtocol_GCS: cosiapi.ObjectProtocolGcs, |
| 37 | + } |
| 38 | +) |
| 39 | + |
| 40 | +// RpcToApi translates object protocols from RPC to API. |
| 41 | +func (ObjectProtocolTranslator) RpcToApi(in cosiproto.ObjectProtocol_Type) (cosiapi.ObjectProtocol, error) { |
| 42 | + a, ok := objectProtocolProtoApiMapping[in] |
| 43 | + if !ok { |
| 44 | + return cosiapi.ObjectProtocol(""), fmt.Errorf("unknown driver protocol %q", string(in)) |
| 45 | + } |
| 46 | + return a, nil |
| 47 | +} |
| 48 | + |
| 49 | +// ApiToRpc translates object protocols from API to RPC. |
| 50 | +func (ObjectProtocolTranslator) ApiToRpc(in cosiapi.ObjectProtocol) (cosiproto.ObjectProtocol_Type, error) { |
| 51 | + for p, a := range objectProtocolProtoApiMapping { |
| 52 | + if a == in { |
| 53 | + return p, nil |
| 54 | + } |
| 55 | + } |
| 56 | + return cosiproto.ObjectProtocol_UNKNOWN, fmt.Errorf("unknown api protocol %q", string(in)) |
| 57 | +} |
| 58 | + |
| 59 | +// An RpcApiTranslator translates types between the RPC driver-domain and Kubernetes API user-domain |
| 60 | +// for a particular protocol. |
| 61 | +type RpcApiTranslator[RpcType any, ApiType comparable] interface { |
| 62 | + // RpcToApi translates bucket info from RPC to API with no validation. |
| 63 | + // If the input is nil, the result map MUST be nil. |
| 64 | + // All possible API info fields SHOULD be present in the result, even if the corresponding RPC |
| 65 | + // info is omitted; an empty string value should be used for keys with no explicit config. |
| 66 | + RpcToApi(RpcType) map[ApiType]string |
| 67 | + |
| 68 | + // ApiToRpc translates bucket info from API to RPC with no validation. |
| 69 | + // If the input map is nil or empty, this is assumed to mean the protocol is not supported, and |
| 70 | + // the result MUST be nil. |
| 71 | + ApiToRpc(map[ApiType]string) RpcType |
| 72 | + |
| 73 | + // Validate checks that user-domain API fields meet requirements and expectations. |
| 74 | + Validate(map[ApiType]string, cosiapi.BucketAccessAuthenticationType) error |
| 75 | +} |
| 76 | + |
| 77 | +// contains is a helper that returns true if the given `list` contains the item `key`. |
| 78 | +// Useful for a variety of Validate() implementations. |
| 79 | +func contains[T comparable](list []T, key T) bool { |
| 80 | + for _, i := range list { |
| 81 | + if i == key { |
| 82 | + return true |
| 83 | + } |
| 84 | + } |
| 85 | + return false |
| 86 | +} |
0 commit comments