add common kubernetes labels

Signed-off-by: Pascal Sthamer <10992664+P4sca1@users.noreply.github.com>
This commit is contained in:
Pascal Sthamer
2025-02-18 16:22:48 +01:00
parent 440a6bcb23
commit e554943c8d
2 changed files with 40 additions and 20 deletions
+16 -5
View File
@@ -1078,8 +1078,17 @@ func deployK8sWorker(image string, identifier string, env []string) error {
} }
} }
containerLabels := map[string]string{ labels := map[string]string{
"container": "shuffle-worker", "app.kubernetes.io/name": "shuffle-worker",
"app.kubernetes.io/instance": identifier,
// "app.kubernetes.io/version": "",
"app.kuvernetes.io/part-of": "shuffle",
"app.kubernetes.io/managed-by": "shuffle-orborus",
}
matchLabels := map[string]string{
"app.kubernetes.io/name": "shuffle-worker",
"app.kubernetes.io/instance": identifier,
} }
containerAttachment := corev1.Container{ containerAttachment := corev1.Container{
@@ -1185,15 +1194,16 @@ func deployK8sWorker(image string, identifier string, env []string) error {
deployment := &appsv1.Deployment{ deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: identifier, Name: identifier,
Labels: labels,
}, },
Spec: appsv1.DeploymentSpec{ Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(replicaNumberInt32), Replicas: int32Ptr(replicaNumberInt32),
Selector: &metav1.LabelSelector{ Selector: &metav1.LabelSelector{
MatchLabels: containerLabels, MatchLabels: matchLabels,
}, },
Template: corev1.PodTemplateSpec{ Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Labels: containerLabels, Labels: labels,
}, },
Spec: corev1.PodSpec{ Spec: corev1.PodSpec{
Containers: []corev1.Container{ Containers: []corev1.Container{
@@ -1215,9 +1225,10 @@ func deployK8sWorker(image string, identifier string, env []string) error {
service := &corev1.Service{ service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: identifier, Name: identifier,
Labels: labels,
}, },
Spec: corev1.ServiceSpec{ Spec: corev1.ServiceSpec{
Selector: containerLabels, Selector: matchLabels,
Ports: []corev1.ServicePort{ Ports: []corev1.ServicePort{
{ {
Protocol: "TCP", Protocol: "TCP",
+22 -13
View File
@@ -487,9 +487,22 @@ func deployk8sApp(image string, identifier string, env []string) error {
//fix naming convention //fix naming convention
// podUuid := uuid.NewV4().String() // podUuid := uuid.NewV4().String()
// podName := fmt.Sprintf("%s-%s", value, podUuid) // name := fmt.Sprintf("%s-%s", value, podUuid)
// replace identifier "_" with "-" // replace identifier "_" with "-"
podName := strings.ReplaceAll(identifier, "_", "-") name := strings.ReplaceAll(identifier, "_", "-")
labels := map[string]string{
"app.kubernetes.io/name": "shuffle-app",
"app.kubernetes.io/instance": name,
// "app.kubernetes.io/version": "",
"app.kuvernetes.io/part-of": "shuffle",
"app.kubernetes.io/managed-by": "shuffle-worker",
}
matchLabels := map[string]string{
"app.kubernetes.io/name": "shuffle-app",
"app.kubernetes.io/instance": name,
}
// pod := &corev1.Pod{ // pod := &corev1.Pod{
// ObjectMeta: metav1.ObjectMeta{ // ObjectMeta: metav1.ObjectMeta{
@@ -569,20 +582,17 @@ func deployk8sApp(image string, identifier string, env []string) error {
deployment := &appsv1.Deployment{ deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: podName, Name: name,
Labels: labels,
}, },
Spec: appsv1.DeploymentSpec{ Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(replicaNumberInt32), Replicas: int32Ptr(replicaNumberInt32),
Selector: &metav1.LabelSelector{ Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{ MatchLabels: matchLabels,
"app": podName,
},
}, },
Template: corev1.PodTemplateSpec{ Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{ Labels: labels,
"app": podName,
},
}, },
Spec: corev1.PodSpec{ Spec: corev1.PodSpec{
Containers: []corev1.Container{ Containers: []corev1.Container{
@@ -606,12 +616,11 @@ func deployk8sApp(image string, identifier string, env []string) error {
// kubectl expose deployment {podName} --type=NodePort --port=80 --target-port=80 // kubectl expose deployment {podName} --type=NodePort --port=80 --target-port=80
service := &corev1.Service{ service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{ ObjectMeta: metav1.ObjectMeta{
Name: podName, Name: name,
Labels: labels,
}, },
Spec: corev1.ServiceSpec{ Spec: corev1.ServiceSpec{
Selector: map[string]string{ Selector: matchLabels,
"app": podName,
},
Ports: []corev1.ServicePort{ Ports: []corev1.ServicePort{
{ {
Protocol: "TCP", Protocol: "TCP",