/
postgres.go
144 lines (136 loc) · 3.4 KB
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package kubes
import (
"context"
"fmt"
"github.com/xctf-io/xctf/pkg/util"
appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/util/intstr"
"k8s.io/client-go/kubernetes"
)
func deployToKubernetes(clientset *kubernetes.Clientset) error {
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "postgres",
Labels: map[string]string{
"io.kompose.service": "postgres",
},
},
Spec: appsv1.DeploymentSpec{
Replicas: util.Ptr(int32(1)),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"io.kompose.service": "postgres",
},
},
Strategy: appsv1.DeploymentStrategy{
Type: appsv1.RecreateDeploymentStrategyType,
},
Template: corev1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"io.kompose.service": "postgres",
},
},
Spec: corev1.PodSpec{
Containers: []corev1.Container{
{
Name: "postgres",
Image: "postgres",
Env: []corev1.EnvVar{
{
Name: "PGDATA",
Value: "/var/lib/postgresql/data/db",
},
{
Name: "POSTGRES_PASSWORD",
Value: "password",
},
},
Ports: []corev1.ContainerPort{
{
ContainerPort: 5432,
},
},
VolumeMounts: []corev1.VolumeMount{
{
Name: "db",
MountPath: "/var/lib/postgresql/data/",
},
},
},
},
Volumes: []corev1.Volume{
{
Name: "db",
VolumeSource: corev1.VolumeSource{
PersistentVolumeClaim: &corev1.PersistentVolumeClaimVolumeSource{
ClaimName: "db",
},
},
},
},
},
},
},
}
// Deploy the Deployment
_, err := clientset.AppsV1().Deployments("default").Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating deployment: %v", err)
}
// Define the Service
service := &corev1.Service{
ObjectMeta: metav1.ObjectMeta{
Name: "postgres",
Labels: map[string]string{
"io.kompose.service": "postgres",
},
},
Spec: corev1.ServiceSpec{
Ports: []corev1.ServicePort{
{
Name: "5432",
Port: 5432,
TargetPort: intstr.FromInt(5432),
},
},
Selector: map[string]string{
"io.kompose.service": "postgres",
},
Type: corev1.ServiceTypeClusterIP,
},
}
// Create the Service
_, err = clientset.CoreV1().Services("default").Create(context.TODO(), service, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating service: %v", err)
}
// Define the PersistentVolumeClaim
pvc := &corev1.PersistentVolumeClaim{
ObjectMeta: metav1.ObjectMeta{
Name: "db",
Labels: map[string]string{
"io.kompose.service": "db",
},
},
Spec: corev1.PersistentVolumeClaimSpec{
AccessModes: []corev1.PersistentVolumeAccessMode{
corev1.ReadWriteOnce,
},
Resources: corev1.VolumeResourceRequirements{
Requests: corev1.ResourceList{
corev1.ResourceStorage: resource.MustParse("100Mi"),
},
},
},
}
// Create the PersistentVolumeClaim
_, err = clientset.CoreV1().PersistentVolumeClaims("default").Create(context.TODO(), pvc, metav1.CreateOptions{})
if err != nil {
return fmt.Errorf("error creating persistent volume claim: %v", err)
}
return nil
}