create a custom resource in kubernetes using generateName field

11/25/2019

I have a sample crd defined as

crd.yaml

kind: CustomResourceDefinition
metadata:
  name: testconfig.demo.k8s.com
  namespace: testns
spec:
  group: demo.k8s.com
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: testconfigs
    singular: testconfig
    kind: TestConfig

I want to create a custom resource based on above crd but i dont want to assign a fixed name to the resource rather use the generateName field. So i generated the below cr.yaml. But when i apply it gives error that name field is mandatory

kind: TestConfig
metadata:
  generateName: test-name-
  namespace: testns
spec:
  image: testimage

Any help is highly appreciated.

-- PREETI BANSAL
kubernetes
kubernetes-custom-resources

1 Answer

11/25/2019

You should use kubectl create to create your CR with generateName.

"kubectl apply will verify the existence of the resources before take action. If the resources do not exist, it will firstly create them. If use generateName, the resource name is not yet generated when verify the existence of the resource." source

-- gnvk
Source: StackOverflow