forked from openshift/builder
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
136 lines (117 loc) · 4.17 KB
/
main.go
File metadata and controls
136 lines (117 loc) · 4.17 KB
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
package main
import (
"flag"
"fmt"
"os"
"os/signal"
"path/filepath"
"runtime"
"strings"
"syscall"
"github.com/containers/storage"
"github.com/containers/storage/pkg/reexec"
"github.com/spf13/cobra"
"k8s.io/component-base/cli"
klog "k8s.io/klog/v2"
kcmdutil "k8s.io/kubectl/pkg/cmd/util"
"github.com/openshift/library-go/pkg/serviceability"
s2ifs "github.com/openshift/source-to-image/pkg/util/fs"
"github.com/openshift/builder/pkg/build/builder"
"github.com/openshift/builder/pkg/version"
)
func main() {
if reexec.Init() {
return
}
sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGTERM)
go func() {
<-sigs
fmt.Println("Error: received unexpected terminate signal")
os.Exit(1)
}()
defer serviceability.BehaviorOnPanic(os.Getenv("OPENSHIFT_ON_PANIC"), version.Get())()
defer serviceability.Profile(os.Getenv("OPENSHIFT_PROFILE")).Stop()
if len(os.Getenv("GOMAXPROCS")) == 0 {
runtime.GOMAXPROCS(runtime.NumCPU())
}
installCAcerts := func() {
const tlsCertRoot = "/etc/pki/tls/certs"
const runtimeCertRoot = "/etc/docker/certs.d"
clusterCASrc := fmt.Sprintf("%s/ca.crt", builder.SecretCertsMountPath)
clusterCADst := fmt.Sprintf("%s/cluster.crt", tlsCertRoot)
// copy the cluster CA certificate from where OpenShift provides it for
// us to where the standard library and assorted binaries look for CA
// certificates
fs := s2ifs.NewFileSystem()
err := fs.Copy(clusterCASrc, clusterCADst, func(path string) bool { return false })
if err != nil {
fmt.Printf("Error setting up cluster CA cert: %v\n", err)
os.Exit(1)
}
// copy CA certificates from where OpenShift provides them for us to
// where docker and docker-like clients look for them
runtimeCASrc := fmt.Sprintf("%s/certs.d", builder.ConfigMapCertsMountPath)
err = fs.CopyContents(runtimeCASrc, runtimeCertRoot, func(path string) bool { return false })
if err != nil {
fmt.Printf("Error setting up service CA cert: %v\n", err)
os.Exit(1)
}
}
basename := filepath.Base(os.Args[0])
command := CommandFor(basename)
flags := flag.NewFlagSet(basename, flag.ExitOnError)
klog.InitFlags(flags)
pflags := command.Flags()
var uidmap, gidmap string
var useNewuidmap, useNewgidmap bool
flags.StringVar(&uidmap, "uidmap", "", "re-exec in a user namespace using the specified UID map")
flags.StringVar(&gidmap, "gidmap", "", "re-exec in a user namespace using the specified GID map")
flags.BoolVar(&useNewuidmap, "use-newuidmap", os.Geteuid() != 0, "use newuidmap to set up UID mappings")
flags.BoolVar(&useNewgidmap, "use-newgidmap", os.Geteuid() != 0, "use newgidmap to set up GID mappings")
vflag := flags.Lookup("v")
flags.Var(vflag.Value, "loglevel", "logging verbosity")
pflags.AddGoFlagSet(flags)
wrapped := command.Run
command.Run = func(c *cobra.Command, args []string) {
switch basename {
case "openshift-sti-build", "openshift-docker-build", "openshift-extract-image-content":
storeOptions, err := storage.DefaultStoreOptions()
kcmdutil.CheckErr(err)
os.MkdirAll(storeOptions.GraphRoot, 0775)
os.MkdirAll(storeOptions.RunRoot, 0775)
installCAcerts()
logUserNamespaceIDMappings()
maybeReexecUsingUserNamespace(uidmap, useNewuidmap, gidmap, useNewgidmap)
default:
if !strings.HasSuffix(basename, "-in-a-user-namespace") {
installCAcerts()
logUserNamespaceIDMappings()
}
}
wrapped(c, args)
}
code := cli.Run(command)
os.Exit(code)
}
// CommandFor returns the appropriate command for this base name,
// or the OpenShift CLI command.
func CommandFor(basename string) *cobra.Command {
var cmd *cobra.Command
switch basename {
case "openshift-sti-build", "openshift-sti-build-in-a-user-namespace":
cmd = NewCommandS2IBuilder(basename)
case "openshift-docker-build", "openshift-docker-build-in-a-user-namespace":
cmd = NewCommandDockerBuilder(basename)
case "openshift-git-clone":
cmd = NewCommandGitClone(basename)
case "openshift-manage-dockerfile":
cmd = NewCommandManageDockerfile(basename)
case "openshift-extract-image-content", "openshift-extract-image-content-in-a-user-namespace":
cmd = NewCommandExtractImageContent(basename)
default:
fmt.Printf("unknown command name: %s\n", basename)
os.Exit(1)
}
return cmd
}