|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package gateway |
| 22 | + |
| 23 | +import ( |
| 24 | + "time" |
| 25 | + |
| 26 | + coreAPI "github.com/envoyproxy/go-control-plane/envoy/config/core/v3" |
| 27 | + upstreamHttpApi "github.com/envoyproxy/go-control-plane/envoy/extensions/upstreams/http/v3" |
| 28 | + "google.golang.org/protobuf/types/known/durationpb" |
| 29 | + |
| 30 | + "github.com/arangodb/kube-arangodb/pkg/util/errors" |
| 31 | +) |
| 32 | + |
| 33 | +type ConfigDestinationProtocol int |
| 34 | + |
| 35 | +const ( |
| 36 | + ConfigDestinationProtocolHTTP1 ConfigDestinationProtocol = iota |
| 37 | + ConfigDestinationProtocolHTTP2 |
| 38 | +) |
| 39 | + |
| 40 | +func (c *ConfigDestinationProtocol) Get() ConfigDestinationProtocol { |
| 41 | + if c == nil { |
| 42 | + return ConfigDestinationProtocolHTTP1 |
| 43 | + } |
| 44 | + |
| 45 | + switch v := *c; v { |
| 46 | + case ConfigDestinationProtocolHTTP1, ConfigDestinationProtocolHTTP2: |
| 47 | + return v |
| 48 | + default: |
| 49 | + return ConfigDestinationProtocolHTTP1 |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +func (c *ConfigDestinationProtocol) Options() *upstreamHttpApi.HttpProtocolOptions { |
| 54 | + switch c.Get() { |
| 55 | + case ConfigDestinationProtocolHTTP1: |
| 56 | + return &upstreamHttpApi.HttpProtocolOptions{ |
| 57 | + UpstreamProtocolOptions: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig_{ |
| 58 | + ExplicitHttpConfig: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig{ |
| 59 | + ProtocolConfig: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig_HttpProtocolOptions{ |
| 60 | + HttpProtocolOptions: &coreAPI.Http1ProtocolOptions{}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + }, |
| 64 | + } |
| 65 | + case ConfigDestinationProtocolHTTP2: |
| 66 | + return &upstreamHttpApi.HttpProtocolOptions{ |
| 67 | + UpstreamProtocolOptions: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig_{ |
| 68 | + ExplicitHttpConfig: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig{ |
| 69 | + ProtocolConfig: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig_Http2ProtocolOptions{ |
| 70 | + Http2ProtocolOptions: &coreAPI.Http2ProtocolOptions{ |
| 71 | + ConnectionKeepalive: &coreAPI.KeepaliveSettings{ |
| 72 | + Interval: durationpb.New(15 * time.Second), |
| 73 | + Timeout: durationpb.New(30 * time.Second), |
| 74 | + ConnectionIdleInterval: durationpb.New(60 * time.Second), |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + default: |
| 82 | + return &upstreamHttpApi.HttpProtocolOptions{ |
| 83 | + UpstreamProtocolOptions: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig_{ |
| 84 | + ExplicitHttpConfig: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig{ |
| 85 | + ProtocolConfig: &upstreamHttpApi.HttpProtocolOptions_ExplicitHttpConfig_HttpProtocolOptions{ |
| 86 | + HttpProtocolOptions: &coreAPI.Http1ProtocolOptions{}, |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +func (c *ConfigDestinationProtocol) Validate() error { |
| 95 | + switch c.Get() { |
| 96 | + case ConfigDestinationProtocolHTTP1, ConfigDestinationProtocolHTTP2: |
| 97 | + return nil |
| 98 | + default: |
| 99 | + return errors.Errorf("Invalid destination protocol") |
| 100 | + } |
| 101 | +} |
0 commit comments