0

我正在编写一个通过 yaml 部署应用程序的运算符。但是这个 yaml 存储在dataConfigMap 的部分 -

apiVersion: v1
kind: ConfigMap
metadata:
  name: test-config
  namespace: default
data:
  hstr-opeartor.yaml: |
    apiVersion: apps/v1
    kind: Deployment
    metadata:
      name: 'hstr-operator'
      namespace: 'cloudian'
    spec:
      replicas: 1
      selector:
        matchLabels:
          name: 'hyperstore-operator'
      template:
        metadata:
          labels:
            name: 'hyperstore-operator'
        spec:
          containers:
          - args:
            - --enable-leader-election
            command:
            - hyperstore-operator
            env:
            - name: REBUILD_TIMER_EMM
              value: "360"
            - name: REBUILD_TIMER_NODE
              value: "360"
            - name: REBUILD_TIMER_PVC
              value: "60"
            - name: IMAGE_PULL_SECRETS
              value: 'hyperstore-regcred'
            - name: IMAGE_HSC
              value: quay.io/cldn/test-image-v2
            - name: WATCH_NAMESPACE
              valueFrom:
                fieldRef:
                  fieldPath: metadata.namespace
            - name: POD_NAME
              valueFrom:
                fieldRef:
                  fieldPath: metadata.name
            - name: OPERATOR_NAME
              value: hyperstore-operator
            - name: IMAGE_PULL_POLICY
              value: IfNotPresent
            image: quay.io/cldn/test-image
            imagePullPolicy: IfNotPresent
            name: hyperstore-operator
            resources:
              limits:
                cpu: 100m
                memory: 1Gi
              requests:
                cpu: 100m
                memory: 500Mi
          imagePullSecrets:
          - name: 'hyperstore-regcred'
          terminationGracePeriodSeconds: 30

对 yaml 进行了精简,以便对问题提供足够的了解。但实际上这是我可以kubectl apply -f <resource>使用的完整部署文件。

// newPodForCR returns a busybox pod with the same name/namespace as the cr
func newPodForCR(cr *hapv1alpha1.HscOperator) *corev1.Pod {
    labels := map[string]string{
        "app": cr.Name,
    }
    return &corev1.Pod{
        ObjectMeta: metav1.ObjectMeta{
            Name:      cr.Name + "-pod",
            Namespace: "default",
            Labels:    labels,
        },
        Spec: corev1.PodSpec{
            Containers: []corev1.Container{
                {
                    Name:  cr.Name,
                    Image: cr.Spec.Image,
                },
            },
        },
    }
}

在我的操作员的控制器代码中,我确保ConfigMap已创建。但是现在我想获取这个 configmap 的数据部分并使用 yaml 创建一个新的 pod。我如何在 go 中阅读并创建一个新的 pod?

4

0 回答 0