public void Update(WatchEventType eventType, V1IngressClass ingressClass) { if (ingressClass is null) { throw new ArgumentNullException(nameof(ingressClass)); } if (!string.Equals(_options.ControllerClass, ingressClass.Spec.Controller, StringComparison.OrdinalIgnoreCase)) { #pragma warning disable CA1303 // Do not pass literals as localized parameters _logger.LogInformation( "Ignoring {IngressClassNamespace}/{IngressClassName} as the spec.controller is not the same as this ingress", ingressClass.Metadata.NamespaceProperty, ingressClass.Metadata.Name); #pragma warning restore CA1303 // Do not pass literals as localized parameters return; } var ingressClassName = ingressClass.Name(); lock (_sync) { if (eventType == WatchEventType.Added || eventType == WatchEventType.Modified) { _ingressClassData[ingressClassName] = new IngressClassData(ingressClass); } else if (eventType == WatchEventType.Deleted) { _ingressClassData.Remove(ingressClassName); } _isDefaultController = _ingressClassData.Values.Any(ic => ic.IsDefault); } }
public IngressClassData(V1IngressClass ingressClass) { if (ingressClass is null) { throw new ArgumentNullException(nameof(ingressClass)); } IngressClass = ingressClass; IsDefault = GetDefaultAnnotation(ingressClass); }
private async Task <ICache> GetKubernetesInfo(string name, V1IngressClass ingressClass) { var mockLogger = new Mock <ILogger <IngressCache> >(); var mockOptions = new Mock <IOptions <YarpOptions> >(); mockOptions.SetupGet(o => o.Value).Returns(new YarpOptions { ControllerClass = "microsoft.com/ingress-yarp" }); var cache = new IngressCache(mockOptions.Object, mockLogger.Object); var typeMap = new Dictionary <string, Type>(); typeMap.Add("networking.k8s.io/v1/Ingress", typeof(V1Ingress)); typeMap.Add("v1/Service", typeof(V1Service)); typeMap.Add("v1/Endpoints", typeof(V1Endpoints)); if (ingressClass is not null) { cache.Update(WatchEventType.Added, ingressClass); } var kubeObjects = await Yaml.LoadAllFromFileAsync(Path.Combine("testassets", name, "ingress.yaml"), typeMap).ConfigureAwait(false); foreach (var obj in kubeObjects) { if (obj is V1Ingress ingress) { cache.Update(WatchEventType.Added, ingress); } else if (obj is V1Service service) { cache.Update(WatchEventType.Added, service); } else if (obj is V1Endpoints endpoints) { cache.Update(WatchEventType.Added, endpoints); } } return(cache); }
public static V1IngressClass CreateIngressClass(string name, string controller, bool?isDefaultClass) { var ingressClass = new V1IngressClass { Spec = new V1IngressClassSpec { Controller = controller, }, Metadata = new V1ObjectMeta { Name = name, Annotations = new Dictionary <string, string>(), }, }; if (isDefaultClass.HasValue && isDefaultClass.Value) { ingressClass.Metadata.Annotations.Add("ingressclass.kubernetes.io/is-default-class", isDefaultClass.Value.ToString(CultureInfo.InvariantCulture)); } return(ingressClass); }
private static bool GetDefaultAnnotation(V1IngressClass ingressClass) { var annotation = ingressClass.GetAnnotation("ingressclass.kubernetes.io/is-default-class"); return(string.Equals("true", annotation, StringComparison.OrdinalIgnoreCase)); }
/// <summary> /// Called by the informer with real-time resource updates. /// </summary> /// <param name="eventType">Indicates if the resource new, updated, or deleted.</param> /// <param name="resource">The information as provided by the Kubernetes API server.</param> private void Notification(WatchEventType eventType, V1IngressClass resource) { _cache.Update(eventType, resource); }