TryParseName() public method

Attempts to parse the given resource name against this template, returning null on failure.
Although this method returns null if a name is passed in which doesn't match the template, it still throws ArgumentNullException if name is null, as this would usually indicate a programming error rather than a data error.
public TryParseName ( string name, ResourceName &result ) : bool
name string The resource name to parse against this template. Must not be null.
result ResourceName When this method returns, the parsed resource name or null if parsing fails.
return bool
Example #1
0
        /// <summary>
        /// Builds a <see cref="GkePlatformDetails"/> from the given metadata.
        /// This metadata is normally retrieved from the GCE metadata server.
        /// </summary>
        /// <param name="metadataJson">JSON metadata, normally retrieved from the GCE metadata server.</param>
        /// <returns>A populated <see cref="GkePlatformDetails"/> if the metadata represents and GKE instance;
        /// <c>null</c> otherwise.</returns>
        public static GkePlatformDetails TryLoad(string metadataJson)
        {
            JObject metadata;

            try
            {
                metadata = JObject.Parse(metadataJson);
            }
            catch
            {
                return(null);
            }
            var projectId   = metadata["project"]?["projectId"]?.ToString();
            var clusterName = metadata["instance"]?["attributes"]?["cluster-name"]?.ToString();
            var zone        = metadata["instance"]?["zone"]?.ToString();
            var hostName    = metadata["instance"]?["hostname"]?.ToString();

            if (projectId != null && clusterName != null && zone != null)
            {
                TemplatedResourceName zoneResourceName;
                if (s_zoneTemplate.TryParseName(zone, out zoneResourceName))
                {
                    return(new GkePlatformDetails(metadataJson, projectId, clusterName, zoneResourceName[1], hostName));
                }
            }
            return(null);
        }
Example #2
0
        /// <summary>
        /// Builds a <see cref="GkePlatformDetails"/> from the given metadata and kubernetes data.
        /// The metadata is normally retrieved from the GCE metadata server.
        /// The kubernetes data is normally retrieved using the kubernetes API.
        /// </summary>
        /// <param name="metadataJson">JSON metadata, normally retrieved from the GCE metadata server.
        /// Must not be <c>null</c>.</param>
        /// <param name="kubernetesData">Kubernetes data, normally retrieved using the kubernetes API.
        /// Must not be <c>null</c>.</param>
        /// <returns>A populated <see cref="GkePlatformDetails"/> if the metadata represents and GKE instance;
        /// <c>null</c> otherwise.</returns>
        public static GkePlatformDetails TryLoad(string metadataJson, KubernetesData kubernetesData)
        {
            GaxPreconditions.CheckNotNull(metadataJson, nameof(metadataJson));
            GaxPreconditions.CheckNotNull(kubernetesData, nameof(kubernetesData));
            JObject metadata      = null;
            JObject namespaceData = null;
            JObject podData       = null;

            // Parse JSON, ignoring all errors; partially available data is supported
            try { metadata = JObject.Parse(metadataJson); } catch { }
            try { namespaceData = JObject.Parse(kubernetesData.NamespaceJson); } catch { }
            try { podData = JObject.Parse(kubernetesData.PodJson); } catch { }
            if (metadata == null)
            {
                // Metadata is required. If it's not present, or the JSON cannot be parsed, return null.
                return(null);
            }
            if (namespaceData?["kind"]?.Value <string>() != "Namespace")
            {
                // If namespaceData looks corrupt/incomplete, ignore it.
                namespaceData = null;
            }
            if (podData?["kind"]?.Value <string>() != "Pod")
            {
                // If podData looks corrupt/incomplete, ignore it.
                podData = null;
            }
            var hostName    = kubernetesData.PodName ?? podData?["metadata"]?["name"]?.Value <string>() ?? ""; // Pod name is the hostname
            var projectId   = metadata["project"]?["projectId"]?.Value <string>();
            var clusterName = metadata["instance"]?["attributes"]?["cluster-name"]?.Value <string>();
            var instanceId  = metadata["instance"]?["id"]?.Value <string>();
            var zone        = metadata["instance"]?["zone"]?.Value <string>();
            var namespaceId = namespaceData?["metadata"]?["uid"]?.Value <string>() ?? "";
            var podId       = podData?["metadata"]?["uid"]?.Value <string>() ?? "";
            // A hack to find the container name. There appears to be no official way to do this.
            var regex          = new Regex($"/var/lib/kubelet/pods/{podId}/containers/([^/]+)/.*/dev/termination-log");
            var containerNames = kubernetesData.MountInfo?.Select(x =>
            {
                var match = regex.Match(x);
                if (match.Success)
                {
                    return(match.Groups[1].Value);
                }
                return(null);
            }).Where(x => x != null).ToList();
            var containerName = containerNames?.Count == 1 ? containerNames[0] : "";

            if (hostName != null && projectId != null && clusterName != null && instanceId != null &&
                zone != null && namespaceId != null && podId != null && containerName != null)
            {
                if (s_zoneTemplate.TryParseName(zone, out var zoneResourceName))
                {
                    return(new GkePlatformDetails(metadataJson, projectId, clusterName, zoneResourceName[1], hostName,
                                                  instanceId, zone, namespaceId, podId, containerName));
                }
            }
            return(null);
        }