How to create a Service Port in Client go

huangapple go评论94阅读模式
英文:

How to create a Service Port in Client go

问题

我在添加ServiceSpec中的Ports字段时遇到了问题。我做错了什么?

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, // 不起作用
		Selector: nil,
		//ClusterIP:                "",
	},
}
英文:

I am having trouble adding the Ports field in ServiceSpec. What am I doing 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:                "",

		},
	}

答案1

得分: 0

你需要将对象端口附加到切片端口。

英文:

Think you have to append the object port to your slice ports.

答案2

得分: 0

这对我有用

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)
英文:

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)

huangapple
  • 本文由 发表于 2022年12月2日 20:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/74655705.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定