-
Notifications
You must be signed in to change notification settings - Fork 303
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
1,131 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,185 @@ | ||
package metrics | ||
|
||
import ( | ||
"strconv" | ||
|
||
"k8s.io/api/networking/v1beta1" | ||
"k8s.io/ingress-gce/pkg/utils" | ||
"k8s.io/klog" | ||
) | ||
|
||
type Feature string | ||
|
||
func (f Feature) String() string { | ||
return string(f) | ||
} | ||
|
||
const ( | ||
// allowHTTPKey tells the Ingress controller to allow/block HTTP access. | ||
allowHTTPKey = "kubernetes.io/ingress.allow-http" | ||
ingressClassKey = "kubernetes.io/ingress.class" | ||
gceIngressClass = "gce" | ||
gceMultiIngressClass = "gce-multi-cluster" | ||
gceL7ILBIngressClass = "gce-internal" | ||
// preSharedCertKey represents the specific pre-shared SSL | ||
// certificate for the Ingress controller to use. | ||
preSharedCertKey = "ingress.gcp.kubernetes.io/pre-shared-cert" | ||
managedCertKey = "networking.gke.io/managed-certificates" | ||
// staticIPKey is the annotation key used by controller to record GCP static ip. | ||
staticIPKey = "ingress.kubernetes.io/static-ip" | ||
|
||
ingress = Feature("Ingress") | ||
externalIngress = Feature("ExternalIngress") | ||
internalIngress = Feature("InternalIngress") | ||
httpEnabled = Feature("HTTPEnabled") | ||
hostBasedRouting = Feature("HostBasedRouting") | ||
pathBasedRouting = Feature("PathBasedRouting") | ||
tlsTermination = Feature("TLSTermination") | ||
secretBasedCertsForTLS = Feature("SecretBasedCertsForTLS") | ||
preSharedCertsForTLS = Feature("PreSharedCertsForTLS") | ||
managedCertsForTLS = Feature("ManagedCertsForTLS") | ||
staticGlobalIP = Feature("StaticGlobalIP") | ||
|
||
servicePort = Feature("L7LBServicePort") | ||
externalServicePort = Feature("L7XLBServicePort") | ||
internalServicePort = Feature("L7ILBServicePort") | ||
neg = Feature("NEG") | ||
cloudCDN = Feature("CloudCDN") | ||
cloudArmor = Feature("CloudArmor") | ||
cloudIAP = Feature("CloudIAP") | ||
backendTimeout = Feature("BackendTimeout") | ||
backendConnectionDraining = Feature("BackendConnectionDraining") | ||
clientIPAffinity = Feature("ClientIPAffinity") | ||
cookieAffinity = Feature("CookieAffinity") | ||
customRequestHeaders = Feature("CustomRequestHeaders") | ||
) | ||
|
||
// FeaturesForIngress returns the list of features for given ingress. | ||
func FeaturesForIngress(ing *v1beta1.Ingress) []Feature { | ||
features := []Feature{ingress} | ||
|
||
klog.V(4).Infof("Listing features for Ingress %s/%s", ing.Namespace, ing.Name) | ||
ingAnnotations := ing.Annotations | ||
|
||
// Determine the type of ingress based on ingress class. | ||
switch ingAnnotations[ingressClassKey] { | ||
case "", gceIngressClass, gceMultiIngressClass: | ||
features = append(features, externalIngress) | ||
case gceL7ILBIngressClass: | ||
features = append(features, internalIngress) | ||
} | ||
|
||
// Determine if http is enabled. | ||
if val, ok := ingAnnotations[allowHTTPKey]; !ok { | ||
features = append(features, httpEnabled) | ||
} else { | ||
v, err := strconv.ParseBool(val) | ||
if err == nil && v { | ||
features = append(features, httpEnabled) | ||
} | ||
} | ||
|
||
// An ingress without a host or http-path is ignored. | ||
hostBased, pathBased := false, false | ||
for _, rule := range ing.Spec.Rules { | ||
if rule.HTTP != nil && len(rule.HTTP.Paths) > 0 { | ||
pathBased = true | ||
} | ||
if rule.Host != "" { | ||
hostBased = true | ||
} | ||
if pathBased && hostBased { | ||
break | ||
} | ||
} | ||
if hostBased { | ||
features = append(features, hostBasedRouting) | ||
} | ||
if pathBased { | ||
features = append(features, pathBasedRouting) | ||
} | ||
|
||
// SSL certificate based features. | ||
sslConfigured := false | ||
if _, ok := ingAnnotations[preSharedCertKey]; ok { | ||
sslConfigured = true | ||
features = append(features, preSharedCertsForTLS) | ||
} | ||
if _, ok := ingAnnotations[managedCertKey]; ok { | ||
sslConfigured = true | ||
features = append(features, managedCertsForTLS) | ||
} | ||
if hasSecretBasedCerts(ing) { | ||
sslConfigured = true | ||
features = append(features, secretBasedCertsForTLS) | ||
} | ||
if sslConfigured { | ||
features = append(features, tlsTermination) | ||
} | ||
|
||
// Both user specified and ingress controller managed global static ips are reported. | ||
if val, ok := ingAnnotations[staticIPKey]; ok && val != "" { | ||
features = append(features, staticGlobalIP) | ||
} | ||
klog.V(4).Infof("Features for ingress %s/%s are %v", ing.Namespace, ing.Name, features) | ||
return features | ||
} | ||
|
||
func hasSecretBasedCerts(ing *v1beta1.Ingress) bool { | ||
for _, tlsSecret := range ing.Spec.TLS { | ||
if tlsSecret.SecretName == "" { | ||
continue | ||
} | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// FeaturesForServicePort returns the list of features for given service port. | ||
func FeaturesForServicePort(sp utils.ServicePort) []Feature { | ||
features := []Feature{servicePort} | ||
klog.V(4).Infof("Listing features for service port %#v", sp) | ||
if sp.L7ILBEnabled { | ||
features = append(features, internalServicePort) | ||
} else { | ||
features = append(features, externalServicePort) | ||
} | ||
if sp.NEGEnabled { | ||
features = append(features, neg) | ||
} | ||
if sp.BackendConfig == nil { | ||
klog.V(4).Infof("Features for Service port %v are %v", sp.ID, features) | ||
return features | ||
} | ||
|
||
if sp.BackendConfig.Spec.Cdn != nil && sp.BackendConfig.Spec.Cdn.Enabled { | ||
features = append(features, cloudCDN) | ||
} | ||
if sp.BackendConfig.Spec.Iap != nil && sp.BackendConfig.Spec.Iap.Enabled { | ||
features = append(features, cloudIAP) | ||
} | ||
// Possible list of Affinity types: | ||
// NONE, CLIENT_IP, GENERATED_COOKIE, CLIENT_IP_PROTO, or CLIENT_IP_PORT_PROTO. | ||
if sp.BackendConfig.Spec.SessionAffinity != nil { | ||
switch sp.BackendConfig.Spec.SessionAffinity.AffinityType { | ||
case "GENERATED_COOKIE": | ||
features = append(features, cookieAffinity) | ||
case "CLIENT_IP", "CLIENT_IP_PROTO", "CLIENT_IP_PORT_PROTO": | ||
features = append(features, clientIPAffinity) | ||
} | ||
} | ||
if sp.BackendConfig.Spec.SecurityPolicy != nil { | ||
features = append(features, cloudArmor) | ||
} | ||
if sp.BackendConfig.Spec.TimeoutSec != nil { | ||
features = append(features, backendTimeout) | ||
} | ||
if sp.BackendConfig.Spec.ConnectionDraining != nil { | ||
features = append(features, backendConnectionDraining) | ||
} | ||
if sp.BackendConfig.Spec.CustomRequestHeaders != nil { | ||
features = append(features, customRequestHeaders) | ||
} | ||
klog.V(4).Infof("Features for Service port %v are %v", sp.ID, features) | ||
return features | ||
} |
Oops, something went wrong.