/// <summary> /// Returns the <see cref="V1OwnerReference"/> for the host node. /// </summary> /// <param name="k8s">The Kubernetes client to be used to query for the node information.</param> /// <returns> /// The <see cref="V1OwnerReference"/> for the node or <c>null</c> when this couldn't /// be determined. /// </returns> public static async Task <V1OwnerReference> GetOwnerReferenceAsync(IKubernetes k8s) { Covenant.Requires <ArgumentNullException>(k8s != null, nameof(k8s)); if (NeonHelper.IsLinux) { using (await mutex.AcquireAsync()) { // Return any cached information. if (cachedOwnerReference != null) { return(cachedOwnerReference); } // Query Kubernetes for the node information based on the the node's hostname. cachedNode = await k8s.ReadNodeAsync(Name); cachedOwnerReference = new V1OwnerReference(apiVersion: cachedNode.ApiVersion, name: cachedNode.Name(), kind: cachedNode.Kind, uid: cachedNode.Uid()); return(cachedOwnerReference); } } else { // Emulate without an owner reference. return(null); } }
/// <summary> /// Gets key values from the specified metadata. /// </summary> /// <param name="metadata">The metadata.</param> /// <param name="ownerReference">The owner reference.</param> /// <param name="clusterScoped">if set to <c>true</c> [cluster scoped].</param> /// <returns>NamespacedName.</returns> public static NamespacedName From(V1ObjectMeta metadata, [NotNull] V1OwnerReference ownerReference, bool?clusterScoped = null) { _ = metadata ?? throw new ArgumentNullException(nameof(metadata)); _ = ownerReference ?? throw new ArgumentNullException(nameof(ownerReference)); return(new NamespacedName( clusterScoped ?? false ? null : metadata.NamespaceProperty, ownerReference.Name)); }
/// <summary>Adds an owner reference to the object. No attempt is made to ensure the reference is correct or fits with the /// other references. /// </summary> /// <param name="obj">the object meta<see cref="V1ObjectMeta"/></param> /// <param name="ownerRef">the owner reference to the object</param> public static void AddOwnerReference(this IMetadata <V1ObjectMeta> obj, V1OwnerReference ownerRef) { if (ownerRef == null) { throw new ArgumentNullException(nameof(ownerRef)); } if (EnsureMetadata(obj).OwnerReferences == null) { obj.Metadata.OwnerReferences = new List <V1OwnerReference>(); } obj.Metadata.OwnerReferences.Add(ownerRef); }
/// <summary>Determines whether an owner reference references the given object.</summary> /// <param name="owner">the object reference<see cref="V1ObjectReference"/></param> /// <param name="obj">the object meta<see cref="V1ObjectMeta"/></param> /// <returns>true if the owner reference references the given object</returns> public static bool Matches(this V1OwnerReference owner, IKubernetesObject <V1ObjectMeta> obj) { if (owner == null) { throw new ArgumentNullException(nameof(owner)); } if (obj == null) { throw new ArgumentNullException(nameof(obj)); } return(owner.ApiVersion == obj.ApiVersion && owner.Kind == obj.Kind && owner.Name == obj.Name() && owner.Uid == obj.Uid()); }
public void TestReferences() { // test object references var pod = new V1Pod() { ApiVersion = "v1", Kind = "Pod" }; pod.Metadata = new V1ObjectMeta() { Name = "name", NamespaceProperty = "ns", ResourceVersion = "ver", Uid = "id" }; var objr = new V1ObjectReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), NamespaceProperty = pod.Namespace(), Uid = pod.Uid() }; Assert.True(objr.Matches(pod)); (pod.ApiVersion, pod.Kind) = (null, null); Assert.False(objr.Matches(pod)); (pod.ApiVersion, pod.Kind) = ("v1", "Pod"); Assert.True(objr.Matches(pod)); pod.Metadata.Name = "nome"; Assert.False(objr.Matches(pod)); // test owner references (pod.ApiVersion, pod.Kind) = ("abc/xyz", "sometimes"); var ownr = new V1OwnerReference() { ApiVersion = "abc/xyz", Kind = "sometimes", Name = pod.Name(), Uid = pod.Uid() }; Assert.True(ownr.Matches(pod)); (pod.ApiVersion, pod.Kind) = (null, null); Assert.False(ownr.Matches(pod)); (ownr.ApiVersion, ownr.Kind) = ("v1", "Pod"); Assert.False(ownr.Matches(pod)); (pod.ApiVersion, pod.Kind) = (ownr.ApiVersion, ownr.Kind); Assert.True(ownr.Matches(pod)); ownr.Name = "nim"; Assert.False(ownr.Matches(pod)); ownr.Name = pod.Name(); var svc = new V1Service(); svc.AddOwnerReference(ownr); Assert.Equal(0, svc.FindOwnerReference(pod)); Assert.Equal(-1, svc.FindOwnerReference(svc)); Assert.Same(ownr, svc.GetOwnerReference(pod)); Assert.Null(svc.GetOwnerReference(svc)); Assert.Null(svc.GetController()); svc.OwnerReferences()[0].Controller = true; Assert.Same(ownr, svc.GetController()); Assert.Same(ownr, svc.RemoveOwnerReference(pod)); Assert.Equal(0, svc.OwnerReferences().Count); svc.AddOwnerReference(new V1OwnerReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), Uid = pod.Uid(), Controller = true }); svc.AddOwnerReference(new V1OwnerReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), Uid = pod.Uid(), Controller = false }); svc.AddOwnerReference(new V1OwnerReference() { ApiVersion = pod.ApiVersion, Kind = pod.Kind, Name = pod.Name(), Uid = pod.Uid() }); Assert.Equal(3, svc.OwnerReferences().Count); Assert.NotNull(svc.RemoveOwnerReference(pod)); Assert.Equal(2, svc.OwnerReferences().Count); Assert.True(svc.RemoveOwnerReferences(pod)); Assert.Equal(0, svc.OwnerReferences().Count); }