/// <summary> /// Extract ownership data from metadata JSON. /// </summary> /// <param name="metadata">Kubernetes scan metadata.</param> /// <param name="token">A signal to stop processing.</param> /// <returns>list of new ownerships.</returns> private List <OwnershipInfo> ExtractOwnershipFromK8sAudit(MetadataKube metadata, CancellationToken token) { var ownershipList = new List <OwnershipInfo>(); dynamic json = JsonConvert.DeserializeObject(metadata.JSON, this.jsonSerializerSettings); var clusterId = json["audit"]["cluster-id"]; // we're interested in Namespaces, Deployments, StatefulSets, DaemonSets, Jobs, CronJobs foreach (var nsobj in json["cluster"]["Namespaces"]) { metadata meta = JsonConvert.DeserializeObject <metadata>(nsobj.metadata.ToString()); ownershipList.Add(new OwnershipInfo { Owner = meta.Owner, ComponentId = $"/k8s/{clusterId}/ns/{meta.name}", }); } foreach (var controller in json["cluster"]["Controllers"]) { var kind = controller.Kind.ToString().ToLowerInvariant(); metadata meta = JsonConvert.DeserializeObject <metadata>(controller.ObjectMeta.metadata.ToString()); ownershipList.Add(new OwnershipInfo { Owner = meta.Owner, ComponentId = $"/k8s/{clusterId}/ns/{meta._namespace}/${kind}/{meta.name}", }); } return(ownershipList); }
/// <summary> /// Creates Kubernetes Metadata entity from internal model. /// </summary> /// <param name="metadata">Internal metadata model.</param> /// <returns>Database compatible entity.</returns> public static MetadataKubeEntity ToEntity(this MetadataKube metadata) { return(new MetadataKubeEntity { Date = metadata.Date, JSON = metadata.JSON, }); }