Home  >  Article  >  Backend Development  >  How to create service port in Client go

How to create service port in Client go

王林
王林forward
2024-02-08 22:48:33837browse

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

Creating service ports in Client go is an important skill and crucial for developers. By creating a service port, communication between the client and the server can be achieved, thereby realizing data transmission and interaction. In this article, PHP editor Xinyi will introduce how to create a service port in Client go to help developers better master this skill. Let’s find out together!

Question content

I have a problem adding the port field in servicespec. What did i do wrong?

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:                "",

        },
    }

Solution

This worked for me

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)

The above is the detailed content of How to create service port in Client go. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete