英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论