|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +// contributor license agreements. See the NOTICE file distributed with |
| 3 | +// this work for additional information regarding copyright ownership. |
| 4 | +// The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +// (the "License"); you may not use this file except in compliance with |
| 6 | +// the License. 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 | +package annotations |
| 17 | + |
| 18 | +import ( |
| 19 | + "strings" |
| 20 | +) |
| 21 | + |
| 22 | +const ( |
| 23 | + // AnnotationsPrefix is the apisix annotation prefix |
| 24 | + AnnotationsPrefix = "k8s.apisix.apache.org/" |
| 25 | + |
| 26 | + // Supported annotations |
| 27 | + AnnotationsUseRegex = AnnotationsPrefix + "use-regex" |
| 28 | + AnnotationsEnableWebSocket = AnnotationsPrefix + "enable-websocket" |
| 29 | + AnnotationsPluginConfigName = AnnotationsPrefix + "plugin-config-name" |
| 30 | + AnnotationsUpstreamScheme = AnnotationsPrefix + "upstream-scheme" |
| 31 | + |
| 32 | + // Support retries and timeouts on upstream |
| 33 | + AnnotationsUpstreamRetry = AnnotationsPrefix + "upstream-retries" |
| 34 | + AnnotationsUpstreamTimeoutConnect = AnnotationsPrefix + "upstream-connect-timeout" |
| 35 | + AnnotationsUpstreamTimeoutRead = AnnotationsPrefix + "upstream-read-timeout" |
| 36 | + AnnotationsUpstreamTimeoutSend = AnnotationsPrefix + "upstream-send-timeout" |
| 37 | +) |
| 38 | + |
| 39 | +const ( |
| 40 | + // Supported the annotations of the APISIX plugins |
| 41 | + |
| 42 | + // cors plugin |
| 43 | + AnnotationsEnableCors = AnnotationsPrefix + "enable-cors" |
| 44 | + AnnotationsCorsAllowOrigin = AnnotationsPrefix + "cors-allow-origin" |
| 45 | + AnnotationsCorsAllowHeaders = AnnotationsPrefix + "cors-allow-headers" |
| 46 | + AnnotationsCorsAllowMethods = AnnotationsPrefix + "cors-allow-methods" |
| 47 | + |
| 48 | + // csrf plugin |
| 49 | + AnnotationsEnableCsrf = AnnotationsPrefix + "enable-csrf" |
| 50 | + AnnotationsCsrfKey = AnnotationsPrefix + "csrf-key" |
| 51 | + |
| 52 | + // redirect plugin |
| 53 | + AnnotationsHttpToHttps = AnnotationsPrefix + "http-to-https" |
| 54 | + AnnotationsHttpRedirect = AnnotationsPrefix + "http-redirect" |
| 55 | + AnnotationsHttpRedirectCode = AnnotationsPrefix + "http-redirect-code" |
| 56 | + |
| 57 | + // rewrite plugin |
| 58 | + AnnotationsRewriteTarget = AnnotationsPrefix + "rewrite-target" |
| 59 | + AnnotationsRewriteTargetRegex = AnnotationsPrefix + "rewrite-target-regex" |
| 60 | + AnnotationsRewriteTargetRegexTemplate = AnnotationsPrefix + "rewrite-target-regex-template" |
| 61 | + |
| 62 | + // response-rewrite plugin |
| 63 | + AnnotationsEnableResponseRewrite = AnnotationsPrefix + "enable-response-rewrite" |
| 64 | + AnnotationsResponseRewriteStatusCode = AnnotationsPrefix + "response-rewrite-status-code" |
| 65 | + AnnotationsResponseRewriteBody = AnnotationsPrefix + "response-rewrite-body" |
| 66 | + AnnotationsResponseRewriteBodyBase64 = AnnotationsPrefix + "response-rewrite-body-base64" |
| 67 | + AnnotationsResponseRewriteHeaderAdd = AnnotationsPrefix + "response-rewrite-add-header" |
| 68 | + AnnotationsResponseRewriteHeaderSet = AnnotationsPrefix + "response-rewrite-set-header" |
| 69 | + AnnotationsResponseRewriteHeaderRemove = AnnotationsPrefix + "response-rewrite-remove-header" |
| 70 | + |
| 71 | + // forward-auth plugin |
| 72 | + AnnotationsForwardAuthURI = AnnotationsPrefix + "auth-uri" |
| 73 | + AnnotationsForwardAuthSSLVerify = AnnotationsPrefix + "auth-ssl-verify" |
| 74 | + AnnotationsForwardAuthRequestHeaders = AnnotationsPrefix + "auth-request-headers" |
| 75 | + AnnotationsForwardAuthUpstreamHeaders = AnnotationsPrefix + "auth-upstream-headers" |
| 76 | + AnnotationsForwardAuthClientHeaders = AnnotationsPrefix + "auth-client-headers" |
| 77 | + |
| 78 | + // ip-restriction plugin |
| 79 | + AnnotationsAllowlistSourceRange = AnnotationsPrefix + "allowlist-source-range" |
| 80 | + AnnotationsBlocklistSourceRange = AnnotationsPrefix + "blocklist-source-range" |
| 81 | + |
| 82 | + // http-method plugin |
| 83 | + AnnotationsHttpAllowMethods = AnnotationsPrefix + "http-allow-methods" |
| 84 | + AnnotationsHttpBlockMethods = AnnotationsPrefix + "http-block-methods" |
| 85 | + |
| 86 | + // key-auth plugin and basic-auth plugin |
| 87 | + // auth-type: keyAuth | basicAuth |
| 88 | + AnnotationsAuthType = AnnotationsPrefix + "auth-type" |
| 89 | + |
| 90 | + // support backend service cross namespace |
| 91 | + AnnotationsSvcNamespace = AnnotationsPrefix + "svc-namespace" |
| 92 | +) |
| 93 | + |
| 94 | +// Handler abstracts the behavior so that the apisix-ingress-controller knows |
| 95 | +type IngressAnnotationsParser interface { |
| 96 | + // Handle parses the target annotation and converts it to the type-agnostic structure. |
| 97 | + // The return value might be nil since some features have an explicit switch, users should |
| 98 | + // judge whether Handle is failed by the second error value. |
| 99 | + Parse(Extractor) (any, error) |
| 100 | +} |
| 101 | + |
| 102 | +// Extractor encapsulates some auxiliary methods to extract annotations. |
| 103 | +type Extractor interface { |
| 104 | + // GetStringAnnotation returns the string value of the target annotation. |
| 105 | + // When the target annoatation is missing, empty string will be given. |
| 106 | + GetStringAnnotation(string) string |
| 107 | + // GetStringsAnnotation returns a string slice which splits the value of target |
| 108 | + // annotation by the comma symbol. When the target annotation is missing, a nil |
| 109 | + // slice will be given. |
| 110 | + GetStringsAnnotation(string) []string |
| 111 | + // GetBoolAnnotation returns a boolean value from the given annotation. |
| 112 | + // When value is "true", true will be given, other values will be treated as |
| 113 | + // false. |
| 114 | + GetBoolAnnotation(string) bool |
| 115 | +} |
| 116 | + |
| 117 | +type extractor struct { |
| 118 | + annotations map[string]string |
| 119 | +} |
| 120 | + |
| 121 | +func (e *extractor) GetStringAnnotation(name string) string { |
| 122 | + return e.annotations[name] |
| 123 | +} |
| 124 | + |
| 125 | +func (e *extractor) GetStringsAnnotation(name string) []string { |
| 126 | + value := e.GetStringAnnotation(name) |
| 127 | + if value == "" { |
| 128 | + return nil |
| 129 | + } |
| 130 | + return strings.Split(value, ",") |
| 131 | +} |
| 132 | + |
| 133 | +func (e *extractor) GetBoolAnnotation(name string) bool { |
| 134 | + return e.annotations[name] == "true" |
| 135 | +} |
| 136 | + |
| 137 | +// NewExtractor creates an annotation extractor. |
| 138 | +func NewExtractor(annotations map[string]string) Extractor { |
| 139 | + return &extractor{ |
| 140 | + annotations: annotations, |
| 141 | + } |
| 142 | +} |
0 commit comments