卓越飞翔博客卓越飞翔博客

卓越飞翔 - 您值得收藏的技术分享站
技术文章34523本站已运行393

如何在Client go中创建服务端口

如何在client go中创建服务端口

在Client go中创建服务端口是一项重要的技能,对于开发人员来说至关重要。通过创建服务端口,可以实现客户端与服务器之间的通信,从而实现数据的传输和交互。本文将由php小编新一为大家介绍如何在Client go中创建服务端口,帮助开发人员更好地掌握这一技能。让我们一起来了解吧!

问题内容

我在 servicespec 中添加端口字段时遇到问题。我做错了什么?

import (
    corev1 "k8s.io/api/core/v1"
    metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

    port := corev1.ServicePort{}
    port.Port = 8443
    ports := make(corev1.ServicePort, 1)

    service := &corev1.Service{
        ObjectMeta: metav1.ObjectMeta{
            Name:      "test-webhook-admissions",
            Namespace: "test",
            Labels: map[string]string{
                "app.kubernetes.io/instance": "test",
                "app.kubernetes.io/name":     "test",
                "control-plane":              "controller-manager",
            },
        },
        Spec: corev1.ServiceSpec{
            Ports:    ports, // Not working
            Selector: nil,
            //ClusterIP:                "",

        },
    }

解决方法

这对我有用

func GetLabels() map[string]string {

    return map[string]string{
        "app.kubernetes.io/instance": "test",
        "app.kubernetes.io/name":     "test",
        "control-plane":              "controller-manager",
    }

}


    service := &corev1.Service{
        ObjectMeta: metav1.ObjectMeta{
            Name:      "test-webhook-admissions",
            Namespace: namespace,
            Labels:    GetLabels(),
        },
        Spec: corev1.ServiceSpec{
            Ports: []corev1.ServicePort{
                {
                    Name:       "webhook",
                    Port:       8443,
                    TargetPort: intstr.FromInt(8443),
                    Protocol:   "TCP",
                },
            },
            Selector: GetLabels(),
        },
    }

    err := w.Client.Create(context.Background(), service)
卓越飞翔博客
上一篇: 不接受来自 stdin 的重复输入
下一篇: 返回列表
留言与评论(共有 0 条评论)
   
验证码:
隐藏边栏