Create Application services for Kubernetes ClustersBackground This KB contains an update set that allows the creation of Application services for Kubernetes clusters, using capabilities that are a part of the ServiceMapping product. The KB will explain how the services are created, what those services contain, and various limitations and disclosures about this feature. Please read the entire KB and then follow the steps below to successfully activate the feature: Remove any customizations to Tag-based services as they might interfere with the feature. If you want to keep some customizations reach out Customer Support to get approval for that.Install the attached Update SetCreate services for existing Kubernetes clusters using the script attached below How it works The update set contains a new business rule on the table Kubernetes Cluster [cmdb_ci_kubernetes_cluster]. For every new record inserted to the table, the BR will create a unique key-value for this record, and then create a new Tag-based service with the above unique key-value and a name equal to the name of the Kubernetes cluster. At this point the service will only contain the Kubernetes cluster. After that, the scheduled job "Populate Calculated Services" will run (the job runs once a day at 1am) and will populate the service based on new traversal rules for Kubernetes cluster. Updating the name of the cluster will automatically update the name of its Tag-based service. Deleting the Kubernetes cluster will automatically delete its Tag-based service. New business rules added: Create Service for Cluster [cmdb_ci_kubernetes_cluster]: For each new Kubernetes cluster, create a unique key-value with the key kubernetes.tag-based and the cluster sys_id as value. Then create a Tag-based service with the same key-value Update name of Service for Cluster [cmdb_ci_kubernetes_cluster]: Check if the name of the Kubernetes cluster changed, if so, look for a Tag-based service matching the cluster's old name, and update it accordingly Delete Service for Cluster [cmdb_ci_kubernetes_cluster] If a kubernetes cluster is deleted, look for a Tag-based service with a name matching the cluster, and delete it as well Prevent delete tag kubernetes.tag-based [cmdb_key_value]: Check if a record with the key 'kubernetes.tag-based-service' is being deleted, if so, prevent this action (this is done to ensure the Tag-based service remains correctly populated) What services this feature creates The feature will create a Tag-based service with the following tag: {key: 'kubernetes.tag-based-service', value: CLUSTER_SYS_ID} The service will be populated based on the following topology: (defined by the new traversal rules) If you wish to add new CIs to the map that don't appear in the above topology you will have to create a new record in the table Traversal Rules [svc_traversal_rules] matching the new CI in the following way: Rule definition -> Create new rule that represents the relations connecting the new CI to the Kubernetes cluster. For example, in the case of Kubernetes node: Is reverse -> falseUsed By -> Service By TagsOrder -> 1100+ (If an existing rule is required to bring the parent type of the new rule, this rule's order must be higher) How to deactivate the feature From the menu, go to: Service Mapping -> Administration -> Tag-Based Service Traversal Rules. Set the following rules to Active='false': From the menu, go to: System Definition -> Business Rules. Set the following rules to Active='false': Create Service for ClusterUpdate name of Service for ClusterDelete Service for ClusterPrevent delete tag kubernetes.tag-based Limitations The service will be created with the same name as the Kubernetes cluster, this means that if there are 2 or more clusters with same name only one of them will have a service (due to the uniqueness of a service name). Please make sure to verify there are no multiple Kubernetes clusters with the same name. * Do not change the name of the Tag-based service! For the feature to work as intended the name of the the service must match the name of the Kubernetes cluster it represents. The service created will follow current limitations defined in the Service Mapping product. Specifically the number of CIs is limited by the property svc_by_tags.max_ci_limit, with default value of 1000. * Please note that the service map will follow any restrictions of the chosen view (for example, when viewing the service through the Unified Map, if the number of CIs is more than 250, the last level of the service will be cut). Create services for existing clusters Use the script below to create Tag-based services for existing Kubernetes clusters. The script will go over the whole Kubernetes Cluster table and create a service for each one. (Please note that this process will take 5~20 sec for each cluster) To create a service for a specific cluster (instead of the whole table) assign the sys_id of the cluster to the variable CLUSTER_ID and uncomment line 4. var CLUSTER_ID = ''; var createServiceByTags = new SMServiceByTagsUtils(); var gr = new GlideRecord('cmdb_ci_kubernetes_cluster'); if (!gs.nil(CLUSTER_ID)) gr.addQuery('sys_id', CLUSTER_ID); gr.query(); // Go over all records in 'cmdb_ci_kubernetes_cluster' and for each one create a new Tag-based service while (gr.next()) { var clusterId = gr.getUniqueValue(); // Create a cmdb_key_value for the new tag-based service var tagGr = new GlideRecord('cmdb_key_value'); tagGr.setValue('configuration_item', clusterId); tagGr.setValue('key', 'kubernetes.tag-based-service'); tagGr.setValue('value', clusterId); tagGr.insert(); // This tag matches the cmdb_key_value created above and will be used for the service creation var tagList = [{ tag: 'kubernetes.tag-based-service', value: clusterId }]; var serviceFieldsMap = { name: gr.getValue('name') }; try { // Create a tag-based service from above tag with the same name as the kubernetes cluster var serviceId = createServiceByTags.createServiceFromTagsList(tagList, serviceFieldsMap, null); if (gs.nil(serviceId)) { gs.error("Failed to create service for cluster " + clusterId); } else { gs.info("Successfully created service for cluster " + clusterId + ", service ID: " + serviceId); } } catch (e) { gs.error("Failed to create service for cluster " + clusterId + " with exception: " + e.message); } }