This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
deployments.go
121 lines (105 loc) · 3.5 KB
/
deployments.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
package cmd
import (
"fmt"
"os"
"github.com/bypasslane/gzr/comms"
"github.com/spf13/cobra"
)
// Package-global k8s connection
var k8sConn *comms.K8sConnection
// deploymentsCmd represents the deployments command
var deploymentsCmd = &cobra.Command{
Use: "deployments [subcommand]",
Short: "Manage k8s Deployments",
Long: `Used to get information on Deployments or update them
deployments list
deployments get <DEPLOYMENT NAME>
deployments update <DEPLOYMENT_NAME> <CONTAINER_NAME> <IMAGE>
`,
PersistentPreRun: func(cmd *cobra.Command, args []string) {
var connErr error
k8sConn, connErr = comms.NewK8sConnection()
if connErr != nil {
// TODO: figure out the Cobra way to handle this
erWithDetails(connErr, "problem establishing k8s connection")
}
},
}
// deploymentsListCmd returns a list of deployments
var deploymentsListCmd = &cobra.Command{
Use: "list [flags]",
Short: "List k8s Deployments",
Long: `Used to get ReplicaSet and PodSpec information on all Deployments.
deployments list
`,
Run: func(cmd *cobra.Command, args []string) {
listDeploymentsHandler()
},
}
// deploymentGetCmd returns a list of deployments
var deploymentGetCmd = &cobra.Command{
Use: "get <DEPLOYMENT_NAME> [flags]",
Short: "Get a k8s Deployment by name",
Long: `Used to get a single Deployment by name, showing ReplicaSet
and PodSpec information.
deployments get mah-deployment
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 1 {
erBadUsage("Not enough arguments", cmd)
}
getDeploymentHandler(args[0])
},
}
// deploymentUpdateCmd returns a list of deployments
var deploymentUpdateCmd = &cobra.Command{
Use: "update <DEPLOYMENT_NAME> <CONTAINER_NAME> <IMAGE> [flags]",
Short: "Update a container in a Deployment to a specific image",
Long: `Used to update a particular container in the Deployment's PodSpec by name.
deployments update mah-deployment some-pod-container coolthing:latest
`,
Run: func(cmd *cobra.Command, args []string) {
if len(args) < 3 {
erBadUsage("Not enough arguments", cmd)
}
updateDeploymentHandler(namespace, args[0], args[1], args[2])
},
}
// updateDeploymentHandler updates a Deployment container with the info described by the DeploymentContainerInfo argument
func updateDeploymentHandler(namespace string, deploymentName string, containerName string, image string) {
dci := &comms.DeploymentContainerInfo{
Namespace: namespace,
DeploymentName: deploymentName,
ContainerName: containerName,
Image: image,
}
deployment, err := k8sConn.UpdateDeployment(dci)
if err != nil {
erWithDetails(err, fmt.Sprintf("There was a problem updating container %q on deployment %q", containerName, deploymentName))
}
deployment.SerializeForCLI(os.Stdout)
}
// getDeploymentHandler fetches
func getDeploymentHandler(deploymentName string) {
deployment, err := k8sConn.GetDeployment(deploymentName)
if err != nil {
erWithDetails(err, fmt.Sprintf("There was a problem retrieving deployment %q", deploymentName))
}
deployment.SerializeForCLI(os.Stdout)
}
// listDeploymentsHandler fetches Deployments and prints them to the CLI
func listDeploymentsHandler() {
dlist, err := k8sConn.ListDeployments()
if err != nil {
erWithDetails(err, "Error retrieving deployments")
}
for _, deployment := range dlist.Deployments {
deployment.SerializeForCLI(os.Stdout)
}
}
func init() {
deploymentsCmd.AddCommand(deploymentsListCmd)
deploymentsCmd.AddCommand(deploymentGetCmd)
deploymentsCmd.AddCommand(deploymentUpdateCmd)
RootCmd.AddCommand(deploymentsCmd)
}