|
| 1 | +package extender |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "crypto/tls" |
| 7 | + "crypto/x509" |
| 8 | + "encoding/json" |
| 9 | + "fmt" |
| 10 | + "io" |
| 11 | + v1 "k8s.io/api/core/v1" |
| 12 | + "k8s.io/klog/v2" |
| 13 | + "net/http" |
| 14 | + "os" |
| 15 | +) |
| 16 | + |
| 17 | +// EvictionRequest represents the request body sent to the external extender. |
| 18 | +type EvictionRequest struct { |
| 19 | + Pod *v1.Pod `json:"pod"` |
| 20 | + Node *v1.Node `json:"node"` |
| 21 | +} |
| 22 | + |
| 23 | +// EvictionResponse represents the response from the external extender. |
| 24 | +type EvictionResponse struct { |
| 25 | + Allow bool `json:"allow"` |
| 26 | + Reason string `json:"reason,omitempty"` |
| 27 | + Error string `json:"error,omitempty"` |
| 28 | +} |
| 29 | + |
| 30 | +// ExtenderClient |
| 31 | +type ExtenderClient struct { |
| 32 | + baseURL string // urlPrefix + decisionVerb |
| 33 | + httpClient *http.Client |
| 34 | + ignorable bool |
| 35 | + failPolicy string |
| 36 | + logger klog.Logger |
| 37 | +} |
| 38 | + |
| 39 | +// NewExtenderClient creates a new ExtenderClient from ExternalExtender config. |
| 40 | +func NewExtenderClient(extenderConfig *ExternalExtender, logger klog.Logger) (*ExtenderClient, error) { |
| 41 | + if extenderConfig == nil { |
| 42 | + return nil, fmt.Errorf("extender config is nil") |
| 43 | + } |
| 44 | + if extenderConfig.URLPrefix == "" { |
| 45 | + return nil, fmt.Errorf("urlPrefix is required") |
| 46 | + } |
| 47 | + if extenderConfig.DecisionVerb == "" { |
| 48 | + return nil, fmt.Errorf("decisionVerb is required") |
| 49 | + } |
| 50 | + if extenderConfig.HTTPTimeout.Duration <= 0 { |
| 51 | + return nil, fmt.Errorf("httpTimeout must be > 0") |
| 52 | + } |
| 53 | + |
| 54 | + baseURL := fmt.Sprintf("%s/%s", trimSuffix(extenderConfig.URLPrefix, "/"), extenderConfig.DecisionVerb) |
| 55 | + |
| 56 | + var tlsConfig *tls.Config |
| 57 | + if extenderConfig.EnableHTTPS { |
| 58 | + var err error |
| 59 | + tlsConfig, err = buildTLSConfig(extenderConfig.TLSConfig) |
| 60 | + if err != nil { |
| 61 | + return nil, fmt.Errorf("failed to build TLS config: %v", err) |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + httpClient := &http.Client{ |
| 66 | + Timeout: extenderConfig.HTTPTimeout.Duration, |
| 67 | + Transport: &http.Transport{ |
| 68 | + TLSClientConfig: tlsConfig, |
| 69 | + }, |
| 70 | + } |
| 71 | + |
| 72 | + return &ExtenderClient{ |
| 73 | + baseURL: baseURL, |
| 74 | + httpClient: httpClient, |
| 75 | + ignorable: extenderConfig.Ignorable, |
| 76 | + failPolicy: defaultIfEmpty(extenderConfig.FailPolicy, "Deny"), |
| 77 | + logger: logger.WithValues("extender", baseURL), |
| 78 | + }, nil |
| 79 | +} |
| 80 | + |
| 81 | +// DecideEviction calls the external service to decide whether to evict the pod. |
| 82 | +func (c *ExtenderClient) DecideEviction(ctx context.Context, pod *v1.Pod, node *v1.Node) (bool, string, error) { |
| 83 | + reqData := EvictionRequest{Pod: pod, Node: node} |
| 84 | + |
| 85 | + body, err := json.Marshal(reqData) |
| 86 | + if err != nil { |
| 87 | + return false, "", fmt.Errorf("failed to marshal request: %v", err) |
| 88 | + } |
| 89 | + |
| 90 | + httpRequest, err := http.NewRequestWithContext(ctx, "POST", c.baseURL, bytes.NewReader(body)) |
| 91 | + if err != nil { |
| 92 | + return false, "", fmt.Errorf("failed to create request: %v", err) |
| 93 | + } |
| 94 | + httpRequest.Header.Set("Content-Type", "application/json") |
| 95 | + |
| 96 | + c.logger.V(4).Info("Calling external decision service", "url", c.baseURL, "pod", klog.KObj(pod)) |
| 97 | + |
| 98 | + resp, err := c.httpClient.Do(httpRequest) |
| 99 | + if err != nil { |
| 100 | + errMsg := fmt.Sprintf("HTTP request failed: %v", err) |
| 101 | + c.logger.Error(err, "External decision service unreachable") |
| 102 | + if c.ignorable { |
| 103 | + c.logger.V(2).Info("ignorable=true, allowing eviction by default") |
| 104 | + return true, "extender unreachable (ignorable)", nil |
| 105 | + } |
| 106 | + return false, "", fmt.Errorf("%s", errMsg) |
| 107 | + } |
| 108 | + defer resp.Body.Close() |
| 109 | + |
| 110 | + respBody, err := io.ReadAll(resp.Body) |
| 111 | + if err != nil { |
| 112 | + return false, "", fmt.Errorf("failed to read response body: %v", err) |
| 113 | + } |
| 114 | + |
| 115 | + if resp.StatusCode != http.StatusOK { |
| 116 | + errMsg := fmt.Sprintf("non-200 status code: %d, body: %s", resp.StatusCode, string(respBody)) |
| 117 | + c.logger.Error(nil, "External decision service returned error", "statusCode", resp.StatusCode, "body", string(respBody)) |
| 118 | + if c.ignorable { |
| 119 | + c.logger.V(2).Info("ignorable=true, allowing eviction by default") |
| 120 | + return true, "extender error (ignorable)", nil |
| 121 | + } |
| 122 | + return false, "", fmt.Errorf("%s", errMsg) |
| 123 | + } |
| 124 | + |
| 125 | + var extResp EvictionResponse |
| 126 | + if err := json.Unmarshal(respBody, &extResp); err != nil { |
| 127 | + return false, "", fmt.Errorf("failed to unmarshal response: %v, body: %s", err, string(respBody)) |
| 128 | + } |
| 129 | + |
| 130 | + if extResp.Error != "" { |
| 131 | + c.logger.V(2).Info("External decision service returned logical error", "error", extResp.Error) |
| 132 | + if c.ignorable { |
| 133 | + return true, "extender logical error (ignorable)", nil |
| 134 | + } |
| 135 | + return false, extResp.Error, nil |
| 136 | + } |
| 137 | + |
| 138 | + c.logger.V(3).Info("External decision received", "allow", extResp.Allow, "reason", extResp.Reason) |
| 139 | + |
| 140 | + return extResp.Allow, extResp.Reason, nil |
| 141 | +} |
| 142 | + |
| 143 | +func trimSuffix(s, suffix string) string { |
| 144 | + if suffix == "" { |
| 145 | + return s |
| 146 | + } |
| 147 | + if s == suffix { |
| 148 | + return s |
| 149 | + } |
| 150 | + if len(s) > len(suffix) && s[len(s)-len(suffix):] == suffix { |
| 151 | + return s[:len(s)-len(suffix)] |
| 152 | + } |
| 153 | + return s |
| 154 | +} |
| 155 | + |
| 156 | +func defaultIfEmpty(s, def string) string { |
| 157 | + if s == "" { |
| 158 | + return def |
| 159 | + } |
| 160 | + return s |
| 161 | +} |
| 162 | + |
| 163 | +func buildTLSConfig(tlsConfig *ExtenderTLSConfig) (*tls.Config, error) { |
| 164 | + if tlsConfig == nil { |
| 165 | + return &tls.Config{InsecureSkipVerify: false}, nil |
| 166 | + } |
| 167 | + |
| 168 | + config := &tls.Config{InsecureSkipVerify: false} |
| 169 | + |
| 170 | + if tlsConfig.CACertFile != "" { |
| 171 | + caCert, err := os.ReadFile(tlsConfig.CACertFile) |
| 172 | + if err != nil { |
| 173 | + return nil, fmt.Errorf("failed to read CA cert file: %v", err) |
| 174 | + } |
| 175 | + caCertPool := x509.NewCertPool() |
| 176 | + if !caCertPool.AppendCertsFromPEM(caCert) { |
| 177 | + return nil, fmt.Errorf("failed to parse CA certificate") |
| 178 | + } |
| 179 | + config.RootCAs = caCertPool |
| 180 | + } |
| 181 | + |
| 182 | + return config, nil |
| 183 | +} |
0 commit comments