-
Notifications
You must be signed in to change notification settings - Fork 0
/
deployment.go
90 lines (81 loc) · 2.17 KB
/
deployment.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
package api
import (
"context"
"fmt"
appsv1 "k8s.io/api/apps/v1"
apiv1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func CreateDeployment() {
clientset := CreateClientSet()
deploymentsClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)
deployment := &appsv1.Deployment{
ObjectMeta: metav1.ObjectMeta{
Name: "go-api-server",
},
Spec: appsv1.DeploymentSpec{
Replicas: int32Ptr(2),
Selector: &metav1.LabelSelector{
MatchLabels: map[string]string{
"app": "go-rest-api",
},
},
Template: apiv1.PodTemplateSpec{
ObjectMeta: metav1.ObjectMeta{
Labels: map[string]string{
"app": "go-rest-api",
},
},
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "go-rest-api",
Image: "pkbhowmick/go-rest-api:2.0.1",
Ports: []apiv1.ContainerPort{
{
Name: "http",
Protocol: apiv1.ProtocolTCP,
ContainerPort: 8080,
},
},
},
},
},
},
},
}
fmt.Println("Creating deployment...")
result, err := deploymentsClient.Create(context.TODO(), deployment, metav1.CreateOptions{})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("Created deployment %q\n", result.GetObjectMeta().GetName())
}
func GetDeployment() {
fmt.Println("Listing all deployment objects ...")
clientset := CreateClientSet()
deploymentClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)
list, err := deploymentClient.List(context.TODO(), metav1.ListOptions{})
if err != nil {
fmt.Println(err)
return
}
for _, item := range list.Items {
fmt.Printf("%s (%d replicas)\n", item.Name, *item.Spec.Replicas)
}
}
func DeleteDeployment() {
fmt.Printf("Deleting deployment : %s \n", deploymentName)
clientset := CreateClientSet()
deploymentClient := clientset.AppsV1().Deployments(apiv1.NamespaceDefault)
err := deploymentClient.Delete(context.TODO(), deploymentName, metav1.DeleteOptions{})
if err != nil {
fmt.Println(err)
return
}
fmt.Printf("%s deleted successfully.\n", deploymentName)
}
func UpdateDeployment() {
fmt.Printf("Scaling Deployment -> %s replicas to -> %d\n", deploymentName, replicas)
}