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

How to create service port in Client go

王林
Release: 2024-02-08 22:48:33
forward
864 people have browsed it

如何在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:                "",

        },
    }
Copy after login

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)
Copy after login

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!

source:stackoverflow.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template