-
Notifications
You must be signed in to change notification settings - Fork 311
/
main.go
128 lines (108 loc) · 3.69 KB
/
main.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
// Copyright 2020 The Okteto Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"context"
"fmt"
"os"
"github.com/okteto/okteto/cmd"
"github.com/okteto/okteto/cmd/namespace"
"github.com/okteto/okteto/cmd/stack"
"github.com/okteto/okteto/pkg/config"
"github.com/okteto/okteto/pkg/errors"
"github.com/okteto/okteto/pkg/log"
"github.com/opentracing/opentracing-go"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"go.undefinedlabs.com/scopeagent/agent"
"go.undefinedlabs.com/scopeagent/instrumentation/process"
"k8s.io/apimachinery/pkg/util/runtime"
// Load the different library for authentication
_ "k8s.io/client-go/plugin/pkg/client/auth/azure"
_ "k8s.io/client-go/plugin/pkg/client/auth/gcp"
_ "k8s.io/client-go/plugin/pkg/client/auth/oidc"
)
func init() {
// override client-go error handlers to downgrade the "logging before flag.Parse" error
errorHandlers := []func(error){
func(e error) {
// Error when there's no service on the other side: an error occurred forwarding 8080 -> 8080: error forwarding port 8080 to pod df05e7e1c85d6b256df779c0b2deb417eb53a299c8581fc0945264301d8fa4b3, uid : exit status 1: 2020/03/16 02:11:09 socat[42490] E connect(5, AF=2 127.0.0.1:8080, 16): Connection refused\n
log.Debugf("port-forward: %s", e)
},
}
runtime.ErrorHandlers = errorHandlers
}
func main() {
log.Init(logrus.WarnLevel, config.GetOktetoHome(), config.VersionString)
log.Info("start")
var logLevel string
agent, span, ctx := getTracing()
root := &cobra.Command{
Use: fmt.Sprintf("%s COMMAND [ARG...]", config.GetBinaryName()),
Short: "Manage development containers",
SilenceErrors: true,
PersistentPreRun: func(ccmd *cobra.Command, args []string) {
log.SetLevel(logLevel)
ccmd.SilenceUsage = true
},
}
root.PersistentFlags().StringVarP(&logLevel, "loglevel", "l", "warn", "amount of information outputted (debug, info, warn, error)")
root.AddCommand(cmd.Analytics())
root.AddCommand(cmd.Version())
root.AddCommand(cmd.Login())
root.AddCommand(cmd.Build(ctx))
root.AddCommand(cmd.Create(ctx))
root.AddCommand(cmd.Delete(ctx))
root.AddCommand(namespace.Namespace(ctx))
root.AddCommand(stack.Stack(ctx))
root.AddCommand(cmd.Init())
root.AddCommand(cmd.Up())
root.AddCommand(cmd.Down())
root.AddCommand(cmd.Push(ctx))
root.AddCommand(cmd.Status())
root.AddCommand(cmd.Doctor())
root.AddCommand(cmd.Exec())
root.AddCommand(cmd.Restart())
err := root.Execute()
if span != nil {
span.Finish()
}
if agent != nil {
agent.Stop()
}
if err != nil {
log.Fail(err.Error())
if uErr, ok := err.(errors.UserError); ok {
if len(uErr.Hint) > 0 {
log.Hint(" %s", uErr.Hint)
}
}
os.Exit(1)
}
}
func getTracing() (*agent.Agent, opentracing.Span, context.Context) {
ctx := context.Background()
if apiKey, ok := os.LookupEnv("SCOPE_APIKEY"); ok {
scope, err := agent.NewAgent(agent.WithApiKey(apiKey))
if err != nil {
log.Errorf("couldn't instantiate scope agent: %s", err)
os.Exit(1)
}
span := process.StartSpan()
span.SetTag("okteto.version", config.VersionString)
ctx = opentracing.ContextWithSpan(ctx, span)
log.Info("scope agent configured")
return scope, span, ctx
}
return nil, nil, ctx
}