Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/kubernetes/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kubernetes CustomResource规范值返回null_Kubernetes_Kubernetes Custom Resources - Fatal编程技术网

Kubernetes CustomResource规范值返回null

Kubernetes CustomResource规范值返回null,kubernetes,kubernetes-custom-resources,Kubernetes,Kubernetes Custom Resources,您好,我创建了以下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

您好,我创建了以下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
当我使用clientgo程序获取cr对象“testpodcr”的spec值时,该值为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
}

任何人都可以帮我弄清楚为什么spec值会变成null

您的
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
spec.names
中的
plurar:testpod
不匹配

我修改了你的
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
之后,您的
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
之后,我创建了
名称空间
testns
,并最终成功创建了
Testpod
对象:

$ kubectl create namespace testns
namespace/testns created
$ kubectl apply -f cr.yaml 
testpod.demo.k8s.com/testpodcr created

您是否使用kubectl在群集中看到您的对象?
$ kubectl create namespace testns
namespace/testns created
$ kubectl apply -f cr.yaml 
testpod.demo.k8s.com/testpodcr created