0

嗨,我创建了以下 CustomResourceDefinition - crd.yaml

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: test.demo.k8s.com
  namespace: testns
spec:
  group: demo.k8s.com
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: testpod
    singular: testpod
    kind: testpod

对应的资源如下——cr.yaml

kind: testpod
metadata:
  name: testpodcr
  namespace: testns
spec:
  containers:
  - name: testname
    image: test/img:v5.16
    env:
    - name: TESTING_ON
      valueFrom:
        configMapKeyRef:
          name: kubernetes-config
          key: type
    volumeMounts:
    - name: testvol
      mountPath: "/test/vol"
      readOnly: true

当我使用 client-go 程序获取 cr 对象“testpodcr”的规范值时,该值为 null。

func (c *TestConfigclient) AddNewPodForCR(obj *TestPodConfig) *v1.Pod {
    log.Println("logging obj \n", obj.Name) // Prints the name as testpodcr
    log.Println("Spec value: \n", obj.Spec) //Prints null

    dep := &v1.Pod{
        ObjectMeta: meta_v1.ObjectMeta{
            //Labels: labels,
            GenerateName: "test-pod-",
        },
        Spec: obj.Spec,
    }

    return dep
}

任何人都可以帮助弄清楚为什么规范值导致为空

4

1 回答 1

0

crd.yaml您的文件有错误。我收到以下错误:

$ kubectl apply -f crd.yaml 
The CustomResourceDefinition "test.demo.k8s.com" is invalid: metadata.name: Invalid value: "test.demo.k8s.com": must be spec.names.plural+"."+spec.group

在您的配置中找到的name: test.demo.k8s.com不匹配。plurar: testpodspec.names

我修改了你的crd.yaml,现在它可以工作了:

apiVersion: apiextensions.k8s.io/v1beta1
kind: CustomResourceDefinition
metadata:
  name: testpods.demo.k8s.com
  namespace: testns
spec:
  group: demo.k8s.com
  versions:
    - name: v1
      served: true
      storage: true
  scope: Namespaced
  names:
    plural: testpods
    singular: testpod
    kind: Testpod
$ kubectl apply -f crd.yaml 
customresourcedefinition.apiextensions.k8s.io/testpods.demo.k8s.com created

之后,您cr.yaml还必须修复:

apiVersion: "demo.k8s.com/v1"
kind: Testpod
metadata:
  name: testpodcr
  namespace: testns
spec:
  containers:
  - name: testname
    image: test/img:v5.16
    env:
    - name: TESTING_ON
      valueFrom:
        configMapKeyRef:
          name: kubernetes-config
          key: type
    volumeMounts:
    - name: testvol
      mountPath: "/test/vol"
      readOnly: true

之后我创建namespace testns并最终Testpod成功创建了对象:

$ kubectl create namespace testns
namespace/testns created
$ kubectl apply -f cr.yaml 
testpod.demo.k8s.com/testpodcr created
于 2019-11-27T17:50:42.697 回答