-
Notifications
You must be signed in to change notification settings - Fork 787
/
logs.go
193 lines (174 loc) · 5 KB
/
logs.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
package cmd
import (
"fmt"
"github.com/jenkins-x/jx/pkg/jx/cmd/templates"
"github.com/jenkins-x/jx/pkg/kube"
"github.com/jenkins-x/jx/pkg/util"
"github.com/spf13/cobra"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
type LogsOptions struct {
*CommonOptions
Container string
Namespace string
Environment string
Filter string
Label string
EditEnvironment bool
KNativeBuild bool
}
var (
logs_long = templates.LongDesc(`
Tails the logs of the newest pod for a Deployment.
`)
logs_example = templates.Examples(`
# Tails the log of the latest pod in deployment myapp
jx logs myapp
# Tails the log of the container foo in the latest pod in deployment myapp
jx logs myapp -c foo
# Tails the log of the latest Knative build pod
jx logs -k
`)
)
func NewCmdLogs(commonOpts *CommonOptions) *cobra.Command {
options := &LogsOptions{
CommonOptions: commonOpts,
}
cmd := &cobra.Command{
Use: "logs [deployment]",
Short: "Tails the log of the latest pod for a deployment",
Long: logs_long,
Example: logs_example,
Aliases: []string{"log"},
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := options.Run()
CheckErr(err)
},
}
cmd.Flags().StringVarP(&options.Container, "container", "c", "", "The name of the container to log")
cmd.Flags().StringVarP(&options.Namespace, "namespace", "n", "", "the namespace to look for the Deployment. Defaults to the current namespace")
cmd.Flags().StringVarP(&options.Environment, "env", "e", "", "the Environment to look for the Deployment. Defaults to the current environment")
cmd.Flags().StringVarP(&options.Filter, "filter", "f", "", "Filters the available deployments if no deployment argument is provided")
cmd.Flags().StringVarP(&options.Label, "label", "l", "", "The label to filter the pods if no deployment argument is provided")
cmd.Flags().BoolVarP(&options.KNativeBuild, "knative-build", "k", false, "View the logs of the latest Knative build pod")
cmd.Flags().BoolVarP(&options.EditEnvironment, "edit", "d", false, "Use my Edit Environment to look for the Deployment pods")
return cmd
}
func (o *LogsOptions) Run() error {
args := o.Args
client, curNs, err := o.KubeClientAndNamespace()
if err != nil {
return err
}
jxClient, devNs, err := o.JXClientAndDevNamespace()
if err != nil {
return err
}
ns := o.Namespace
if ns == "" {
env := o.Environment
if env != "" {
ns, err = kube.GetEnvironmentNamespace(jxClient, devNs, env)
if err != nil {
return err
}
}
if ns == "" && o.EditEnvironment {
ns, err = kube.GetEditEnvironmentNamespace(jxClient, devNs)
if err != nil {
return err
}
}
}
if ns == "" {
ns = curNs
}
names, err := kube.GetDeploymentNames(client, ns, o.Filter)
if err != nil {
return fmt.Errorf("Could not find deployments in namespace %s with filter %s: %s", ns, o.Filter, err)
}
if len(names) == 0 {
if o.Filter == "" {
return fmt.Errorf("There are no Deployments")
} else {
return fmt.Errorf("There are no Deployments matching filter: " + o.Filter)
}
}
name := ""
if len(args) == 0 {
if o.Label == "" && !o.KNativeBuild {
n, err := util.PickName(names, "Pick Deployment:", "", o.In, o.Out, o.Err)
if err != nil {
return err
}
name = n
}
} else {
name = args[0]
if util.StringArrayIndex(names, name) < 0 {
return util.InvalidArg(name, names)
}
}
for {
pod := ""
if o.KNativeBuild {
pod, err = o.WaitForReadyKnativeBuildPod(client, ns, false)
if pod == "" {
return fmt.Errorf("No Knative build pod found for namespace %s", ns)
}
} else if o.Label != "" {
selector, err := parseSelector(o.Label)
if err != nil {
return err
}
pod, err = o.WaitForReadyPodForSelectorLabels(client, ns, selector, false)
if err != nil {
return err
}
if pod == "" {
return fmt.Errorf("No pod found for namespace %s with selector %s", ns, o.Label)
}
} else {
pod, err = o.WaitForReadyPodForDeployment(client, ns, name, names, false)
if err != nil {
return err
}
if pod == "" {
return fmt.Errorf("No pod found for namespace %s with name %s", ns, name)
}
}
err = o.TailLogs(ns, pod, o.Container)
if err != nil {
return nil
}
}
}
func parseSelector(selectorText string) (map[string]string, error) {
selector, err := metav1.ParseToLabelSelector(selectorText)
if err != nil {
return nil, err
}
return selector.MatchLabels, nil
}
// waitForReadyPodForDeployment waits for a ready pod in a Deployment in the given namespace with the given name
func isContainerStarted(state *corev1.ContainerState) bool {
if state == nil {
return false
}
if state.Running != nil {
return !state.Running.StartedAt.IsZero()
}
if state != nil && state.Terminated != nil {
return !state.Terminated.StartedAt.IsZero()
}
return false
}
func isContainerCompleted(state *corev1.ContainerState) bool {
if state != nil && state.Terminated != nil {
return !state.Terminated.FinishedAt.IsZero()
}
return false
}