Ejemplo n.º 1
0
        /// <summary>
        /// Returns the owner identification for a pod.
        /// </summary>
        /// <param name="k8s">The Kubernetes client.</param>
        /// <param name="pod">The pod.</param>
        /// <returns>The owner ID.</returns>
        private static async Task <string> GetOwnerIdAsync(IKubernetes k8s, V1Pod pod)
        {
            Covenant.Requires <ArgumentNullException>(k8s != null, nameof(k8s));
            Covenant.Requires <ArgumentNullException>(pod != null, nameof(pod));

            // We're going to favor standard top-level owners, when present.

            string ownerName = null;
            string ownerKind = null;

            foreach (var owner in pod.OwnerReferences())
            {
                switch (owner.Kind)
                {
                case "DaemonSet":
                case "Deployment":
                case "StatefulSet":

                    ownerName = owner.Name;
                    ownerKind = owner.Kind;
                    break;

                case "Node":

                    // We'll see this for static pods.  [owner.Name] is the node name which isn't terribly useful,
                    // so we'll used the pod name instead, removing the node name part, which will look something
                    // like:
                    //
                    //      kube-scheduler-master-0

                    var nodeNamePos = pod.Metadata.Name.IndexOf(owner.Name) - 1;

                    ownerName = pod.Metadata.Name.Substring(0, nodeNamePos);
                    ownerKind = owner.Kind;
                    break;

                case "ReplicaSet":

                    // Use the replica set's owner when present.

                    var replicaSet = await k8s.ReadNamespacedReplicaSetAsync(owner.Name, pod.Namespace());

                    var replicaSetOwner = replicaSet.OwnerReferences().FirstOrDefault();

                    if (replicaSetOwner != null)
                    {
                        ownerName = replicaSetOwner.Name;
                        ownerKind = replicaSetOwner.Kind;
                    }
                    else
                    {
                        ownerName = owner.Name;
                        ownerKind = owner.Kind;
                    }

                    break;
                }
            }

            var podNamespace = pod.Namespace();

            if (!string.IsNullOrEmpty(ownerName))
            {
                return($"{podNamespace}/{ownerName} ({ownerKind})");
            }

            // Default to using the pod name or kind for standalone pods.

            return($"{podNamespace}/{pod.Name} ({pod.Kind})");
        }
Ejemplo n.º 2
0
        public void TestMetadata()
        {
            // test getters on null metadata
            var pod = new V1Pod();

            Assert.Null(pod.Annotations());
            Assert.Null(pod.ApiGroup());
            var(g, v) = pod.ApiGroupAndVersion();
            Assert.Null(g);
            Assert.Null(v);
            Assert.Null(pod.ApiGroupVersion());
            Assert.Null(pod.CreationTimestamp());
            Assert.Null(pod.DeletionTimestamp());
            Assert.Null(pod.Finalizers());
            Assert.Equal(-1, pod.FindOwnerReference(r => true));
            Assert.Null(pod.Generation());
            Assert.Null(pod.GetAnnotation("x"));
            Assert.Null(pod.GetController());
            Assert.Null(pod.GetLabel("x"));
            Assert.Null(pod.GetOwnerReference(r => true));
            Assert.False(pod.HasFinalizer("x"));
            Assert.Null(pod.Labels());
            Assert.Null(pod.Name());
            Assert.Null(pod.Namespace());
            Assert.Null(pod.OwnerReferences());
            Assert.Null(pod.ResourceVersion());
            Assert.Null(pod.Uid());
            Assert.Null(pod.Metadata);

            // test API version stuff
            pod = new V1Pod()
            {
                ApiVersion = "v1"
            };
            Assert.Equal("", pod.ApiGroup());
            (g, v) = pod.ApiGroupAndVersion();
            Assert.Equal("", g);
            Assert.Equal("v1", v);
            Assert.Equal("v1", pod.ApiGroupVersion());
            pod.ApiVersion = "abc/v2";
            Assert.Equal("abc", pod.ApiGroup());
            (g, v) = pod.ApiGroupAndVersion();
            Assert.Equal("abc", g);
            Assert.Equal("v2", v);
            Assert.Equal("v2", pod.ApiGroupVersion());

            // test the Ensure*() functions
            Assert.NotNull(pod.EnsureMetadata());
            Assert.NotNull(pod.Metadata);
            Assert.NotNull(pod.Metadata.EnsureAnnotations());
            Assert.NotNull(pod.Metadata.Annotations);
            Assert.NotNull(pod.Metadata.EnsureFinalizers());
            Assert.NotNull(pod.Metadata.Finalizers);
            Assert.NotNull(pod.Metadata.EnsureLabels());
            Assert.NotNull(pod.Metadata.Labels);

            // test getters with non-null values
            DateTime ts = DateTime.UtcNow, ts2 = DateTime.Now;

            pod.Metadata = new V1ObjectMeta()
            {
                CreationTimestamp = ts,
                DeletionTimestamp = ts2,
                Generation        = 1,
                Name = "name",
                NamespaceProperty = "ns",
                ResourceVersion   = "42",
                Uid = "id"
            };
            Assert.Equal(ts, pod.CreationTimestamp().Value);
            Assert.Equal(ts2, pod.DeletionTimestamp().Value);
            Assert.Equal(1, pod.Generation().Value);
            Assert.Equal("name", pod.Name());
            Assert.Equal("ns", pod.Namespace());
            Assert.Equal("42", pod.ResourceVersion());
            Assert.Equal("id", pod.Uid());

            // test annotations and labels
            pod.SetAnnotation("x", "y");
            pod.SetLabel("a", "b");
            Assert.Equal(1, pod.Annotations().Count);
            Assert.Equal(1, pod.Labels().Count);
            Assert.Equal("y", pod.GetAnnotation("x"));
            Assert.Equal("y", pod.Metadata.Annotations["x"]);
            Assert.Null(pod.GetAnnotation("a"));
            Assert.Equal("b", pod.GetLabel("a"));
            Assert.Equal("b", pod.Metadata.Labels["a"]);
            Assert.Null(pod.GetLabel("x"));
            pod.SetAnnotation("x", null);
            Assert.Equal(0, pod.Annotations().Count);
            pod.SetLabel("a", null);
            Assert.Equal(0, pod.Labels().Count);

            // test finalizers
            Assert.False(pod.HasFinalizer("abc"));
            Assert.True(pod.AddFinalizer("abc"));
            Assert.True(pod.HasFinalizer("abc"));
            Assert.False(pod.AddFinalizer("abc"));
            Assert.False(pod.HasFinalizer("xyz"));
            Assert.False(pod.RemoveFinalizer("xyz"));
            Assert.True(pod.RemoveFinalizer("abc"));
            Assert.False(pod.HasFinalizer("abc"));
            Assert.False(pod.RemoveFinalizer("abc"));
        }