|
| 1 | +/* |
| 2 | + * Copyright (c) 2020-2024. Devtron Inc. |
| 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 k8s |
| 18 | + |
| 19 | +import ( |
| 20 | + "github.com/caarlos0/env" |
| 21 | + "go.uber.org/zap" |
| 22 | + utilnet "k8s.io/apimachinery/pkg/util/net" |
| 23 | + "k8s.io/client-go/rest" |
| 24 | + "net" |
| 25 | + "net/http" |
| 26 | + "time" |
| 27 | +) |
| 28 | + |
| 29 | +type DefaultK8sHttpTransportConfig struct{} |
| 30 | + |
| 31 | +type CustomK8sHttpTransportConfig struct { |
| 32 | + UseCustomTransport bool `env:"USE_CUSTOM_HTTP_TRANSPORT" envDefault:"false"` |
| 33 | + TimeOut int `env:"K8s_TCP_TIMEOUT" envDefault:"30"` |
| 34 | + KeepAlive int `env:"K8s_TCP_KEEPALIVE" envDefault:"30"` |
| 35 | + TLSHandshakeTimeout int `env:"K8s_TLS_HANDSHAKE_TIMEOUT" envDefault:"10"` |
| 36 | + MaxIdleConnsPerHost int `env:"K8s_CLIENT_MAX_IDLE_CONNS_PER_HOST" envDefault:"25"` |
| 37 | + IdleConnTimeout int `env:"K8s_TCP_IDLE_CONN_TIMEOUT" envDefault:"300"` |
| 38 | +} |
| 39 | + |
| 40 | +func NewDefaultK8sHttpTransportConfig() *DefaultK8sHttpTransportConfig { |
| 41 | + return &DefaultK8sHttpTransportConfig{} |
| 42 | +} |
| 43 | + |
| 44 | +func NewCustomK8sHttpTransportConfig(logger *zap.SugaredLogger) *CustomK8sHttpTransportConfig { |
| 45 | + customK8sHttpTransportConfig := &CustomK8sHttpTransportConfig{} |
| 46 | + err := env.Parse(customK8sHttpTransportConfig) |
| 47 | + if err != nil { |
| 48 | + logger.Errorw("error in parsing custom k8s http configurations from env", "err", err) |
| 49 | + } |
| 50 | + return customK8sHttpTransportConfig |
| 51 | +} |
| 52 | + |
| 53 | +type TransportType string |
| 54 | + |
| 55 | +const ( |
| 56 | + TransportTypeDefault TransportType = "default" |
| 57 | + TransportTypeOverridden TransportType = "overridden" |
| 58 | +) |
| 59 | + |
| 60 | +type HttpTransportConfig struct { |
| 61 | + customHttpClientConfig HttpTransportInterface |
| 62 | + defaultHttpClientConfig HttpTransportInterface |
| 63 | +} |
| 64 | + |
| 65 | +func NewHttpTransportConfig(logger *zap.SugaredLogger) *HttpTransportConfig { |
| 66 | + return &HttpTransportConfig{ |
| 67 | + customHttpClientConfig: NewCustomK8sHttpTransportConfig(logger), |
| 68 | + defaultHttpClientConfig: NewDefaultK8sHttpTransportConfig(), |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +type HttpTransportInterface interface { |
| 73 | + OverrideConfigWithCustomTransport(config *rest.Config) (*rest.Config, error) |
| 74 | +} |
| 75 | + |
| 76 | +// OverrideConfigWithCustomTransport |
| 77 | +// sets returns the given rest config without any modifications even if UseCustomTransport is enabled. |
| 78 | +// This is used when we want to use the default rest.Config provided by the client-go library. |
| 79 | +func (impl *DefaultK8sHttpTransportConfig) OverrideConfigWithCustomTransport(config *rest.Config) (*rest.Config, error) { |
| 80 | + return config, nil |
| 81 | +} |
| 82 | + |
| 83 | +// OverrideConfigWithCustomTransport |
| 84 | +// overrides the given rest config with custom transport if UseCustomTransport is enabled. |
| 85 | +// if the config already has a defined transport, we don't override it. |
| 86 | +func (impl *CustomK8sHttpTransportConfig) OverrideConfigWithCustomTransport(config *rest.Config) (*rest.Config, error) { |
| 87 | + if !impl.UseCustomTransport || config.Transport != nil { |
| 88 | + return config, nil |
| 89 | + } |
| 90 | + |
| 91 | + dial := (&net.Dialer{ |
| 92 | + Timeout: time.Duration(impl.TimeOut) * time.Second, |
| 93 | + KeepAlive: time.Duration(impl.KeepAlive) * time.Second, |
| 94 | + }).DialContext |
| 95 | + |
| 96 | + // Get the TLS options for this client config |
| 97 | + tlsConfig, err := rest.TLSConfigFor(config) |
| 98 | + if err != nil { |
| 99 | + return nil, err |
| 100 | + } |
| 101 | + |
| 102 | + transport := utilnet.SetTransportDefaults(&http.Transport{ |
| 103 | + Proxy: config.Proxy, |
| 104 | + TLSHandshakeTimeout: time.Duration(impl.TLSHandshakeTimeout) * time.Second, |
| 105 | + TLSClientConfig: tlsConfig, |
| 106 | + MaxIdleConns: impl.MaxIdleConnsPerHost, |
| 107 | + MaxConnsPerHost: impl.MaxIdleConnsPerHost, |
| 108 | + MaxIdleConnsPerHost: impl.MaxIdleConnsPerHost, |
| 109 | + DialContext: dial, |
| 110 | + DisableCompression: config.DisableCompression, |
| 111 | + IdleConnTimeout: time.Duration(impl.IdleConnTimeout) * time.Second, |
| 112 | + }) |
| 113 | + |
| 114 | + rt, err := rest.HTTPWrappersForConfig(config, transport) |
| 115 | + if err != nil { |
| 116 | + return nil, err |
| 117 | + } |
| 118 | + |
| 119 | + config.Transport = rt |
| 120 | + config.Timeout = time.Duration(impl.TimeOut) * time.Second |
| 121 | + |
| 122 | + // set default tls config and remove auth/exec provides since we use it in a custom transport. |
| 123 | + // we already set tls config in the transport |
| 124 | + config.TLSClientConfig = rest.TLSClientConfig{} |
| 125 | + config.AuthProvider = nil |
| 126 | + config.ExecProvider = nil |
| 127 | + |
| 128 | + return config, nil |
| 129 | +} |
0 commit comments