Skip to content

Conversation

@sniok
Copy link
Contributor

@sniok sniok commented Dec 8, 2025

Currently we naively use kind property to differentiate between different resource types.
This is problematic since custom resource instances can have same kind values.

To make sure we have unique IDs for sources this PR adds API group, so the new format for IDs is now apiGroup/kind, for example 'apps/Deployment'. This guarantees unique IDs for each resource.

Also for custom resources a crd- prefix was added to avoid collisions with existing resource definitions we have (for example Gateway API). Ideally we wouldn't have to do this, gateway resources should just be displayed once in the Custom Resources list and extended with information instead of defining separate list/details/kubeobject instances. But that is out of scope for this PR and should be handled in #4240

Related Issue

Fixes #4245 #4237


How to test

  1. Create CRD with names like Pod or Service
  2. Create instances of those CRDs
  3. Go to map and enable CRD in the source dropdown (top left)
  4. Make sure Map doesn't crash.
YAMLs I've used to test

CRDs

apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: services.example.com
spec:
  group: example.com
  names:
    kind: Service
    plural: services
    singular: service
  scope: Namespaced
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                size:
                  type: string
                color:
                  type: string
---
apiVersion: apiextensions.k8s.io/v1
kind: CustomResourceDefinition
metadata:
  name: pods.example.com
spec:
  group: example.com
  names:
    kind: Pod
    plural: pods
    singular: pod
  scope: Namespaced
  versions:
    - name: v1
      served: true
      storage: true
      schema:
        openAPIV3Schema:
          type: object
          properties:
            spec:
              type: object
              properties:
                size:
                  type: string
                color:
                  type: string

Instances

apiVersion: example.com/v1
kind: Pod
metadata:
  name: my-pod
spec:
  size: medium
  color: blue
---
apiVersion: example.com/v1
kind: Service
metadata:
  name: my-service
spec:
  size: medium
  color: blue

@k8s-ci-robot k8s-ci-robot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 8, 2025
@k8s-ci-robot k8s-ci-robot added approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 8, 2025
@sniok sniok force-pushed the service-missing-spec branch from 476369b to 30e8432 Compare December 8, 2025 19:20
@sniok sniok marked this pull request as ready for review December 8, 2025 20:08
@k8s-ci-robot k8s-ci-robot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Dec 8, 2025
@k8s-ci-robot k8s-ci-robot requested a review from skoeva December 8, 2025 20:08
@sniok sniok changed the title frontend: Map: Refactor sources and relations IDs frontend: Map: Refactor source IDs to make them unique Dec 8, 2025
@kahirokunn
Copy link
Member

This is the PR I was looking for! Thank you! 🙏

@sniok sniok force-pushed the service-missing-spec branch from 30e8432 to 4fddb9c Compare December 10, 2025 15:40
@k8s-ci-robot k8s-ci-robot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Dec 10, 2025
@illume illume requested a review from Copilot December 10, 2025 20:26
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR refactors resource source IDs in the frontend Map feature to ensure uniqueness when handling custom resources. Previously, sources were identified solely by the kind property, which caused collisions when custom resources used the same kind names as built-in resources (e.g., a CRD with kind "Pod" or "Service"). The new approach uses a combination of API group and kind (apiGroup/kind) to create unique identifiers, with a crd- prefix added to custom resources for additional collision prevention.

  • Adds apiGroupName static getter and isClassOf type guard to KubeObject class
  • Introduces makeKubeSourceId utility function to generate unique source IDs
  • Refactors type checking from string-based kind comparisons to type-safe isClassOf guards

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
frontend/src/lib/k8s/KubeObject.ts Adds apiGroupName getter to extract API group from apiVersion and isClassOf type guard method for type-safe instance checking
frontend/src/components/resourceMap/sources/definitions/graphDefinitionUtils.tsx New utility file with makeKubeSourceId function that creates unique IDs in apiGroup/kind format
frontend/src/components/resourceMap/sources/definitions/sources.tsx Updates source creation to use makeKubeSourceId and adds crd- prefix for custom resource sources
frontend/src/components/resourceMap/sources/definitions/relations.tsx Updates relation definitions to use makeKubeSourceId for consistent source ID generation
frontend/src/components/resourceMap/nodes/KubeObjectStatus.tsx Refactors Pod type checking from string comparison to isClassOf type guard
frontend/src/components/resourceMap/KubeObjectGlance/KubeObjectGlance.tsx Refactors all resource type checks to use isClassOf instead of string-based kind comparisons
frontend/src/components/resourceMap/KubeObjectGlance/ReplicaSetGlance.tsx Updates component signature to accept `ReplicaSet
frontend/src/components/resourceMap/graph/graphFiltering.test.ts Updates test to use proper Pod instances instead of mock objects, adds App import to resolve circular dependency

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@illume illume added the kind/bug Categorizes issue or PR as related to a bug. label Dec 15, 2025
@illume
Copy link
Contributor

illume commented Dec 15, 2025

@sniok Wanted to check with you first before merging if you think this is ready to go? I'd like this in 0.39.0.
(There's only nit comments open. I assume the test changes cover the cases for the bugs this fixes?)

@illume illume added this to the v0.39.0 milestone Dec 15, 2025
@sniok sniok force-pushed the service-missing-spec branch from 4fddb9c to 7d559fd Compare December 15, 2025 15:14
@kahirokunn
Copy link
Member

This PR is the initial implementation that will now use apiGroup and apiVersion for stricter code evaluation, which was previously solely reliant on 'kind'. This holds significant meaning for Headlamp's plugin system and will serve as a guideline. Thank you for the implementation!

Copy link
Contributor

@illume illume left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉 thanks!

@k8s-ci-robot
Copy link
Contributor

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: illume, sniok

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@illume illume merged commit 9b25624 into kubernetes-sigs:main Dec 15, 2025
11 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. cncf-cla: yes Indicates the PR's author has signed the CNCF CLA. kind/bug Categorizes issue or PR as related to a bug. resourceMap size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash Report: Cannot read properties of undefined (reading 'selector')

4 participants