Repository navigation
Expand file tree
/
Copy pathoc.go
More file actions
247 lines (212 loc) · 7.93 KB
/
Copy pathoc.go
File metadata and controls
247 lines (212 loc) · 7.93 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
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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
Copyright (C) 2017 Red Hat, Inc.
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 oc
import (
"bytes"
"fmt"
"io"
"os"
"os/user"
"path/filepath"
"regexp"
"strings"
"github.com/golang/glog"
"github.com/minishift/minishift/pkg/minikube/constants"
minishiftConstants "github.com/minishift/minishift/pkg/minishift/constants"
"github.com/minishift/minishift/pkg/util"
"github.com/minishift/minishift/pkg/util/cmd"
"github.com/minishift/minishift/pkg/util/filehelper"
"github.com/pkg/errors"
"k8s.io/client-go/tools/clientcmd"
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
"runtime"
)
const (
invalidOcPathError = "The specified path to oc '%s' does not exist"
invalidKubeConfigPathError = "The specified path to the kube config '%s' does not exist"
)
type OcRunner struct {
OcPath string
KubeConfigPath string
Runner util.Runner
}
// NewOcRunner creates a new OcRunner which uses the oc binary specified via the ocPath parameter. An error is returned
// in case the oc binary does not exist or is not executable.
func NewOcRunner(ocPath string, kubeConfigPath string) (*OcRunner, error) {
if !filehelper.Exists(ocPath) {
return nil, errors.New(fmt.Sprintf(invalidOcPathError, ocPath))
}
if !filehelper.Exists(kubeConfigPath) {
return nil, errors.New(fmt.Sprintf(invalidKubeConfigPathError, kubeConfigPath))
}
return &OcRunner{OcPath: ocPath, KubeConfigPath: kubeConfigPath, Runner: util.RealRunner{}}, nil
}
func (oc *OcRunner) Run(command string, stdOut io.Writer, stdErr io.Writer) int {
args := cmd.SplitCmdString(command)
// make sure we run with our copy of kube config to not influence the user
args = append([]string{fmt.Sprintf("--config=%s", oc.KubeConfigPath)}, args...)
return oc.Runner.Run(stdOut, stdErr, oc.OcPath, args...)
}
func (oc *OcRunner) RunAsUser(command string, stdOut io.Writer, stdErr io.Writer) int {
args := strings.Split(command, " ")
return oc.Runner.Run(stdOut, stdErr, oc.OcPath, args...)
}
// AddSudoerRoleForUser gives the specified user the sudoer role
// See also https://docs.okd.io/latest/architecture/additional_concepts/authentication.html#authentication-impersonation
func (oc *OcRunner) AddSudoerRoleForUser(user string) error {
cmd := fmt.Sprintf("adm policy add-cluster-role-to-user sudoer %s", user)
errorBuffer := new(bytes.Buffer)
exitCode := oc.Run(cmd, nil, errorBuffer)
if exitCode != 0 {
return fmt.Errorf("Unable to add sudoer role: %v", errorBuffer)
}
return nil
}
// AddSystemAdminEntrytoKubeConfig adds the system:admin certs to ~/.kube/config
func (oc *OcRunner) AddSystemAdminEntryToKubeConfig(ocPath string) error {
var existingKubeConfig, newKubeConfig *clientcmdapi.Config
minishiftKubeConfigPath := oc.KubeConfigPath
globalKubeConfigPath, err := getGlobalKubeConfigPath()
if err != nil {
return err
}
if glog.V(2) {
fmt.Println("Using Kubeconfig Path: ", globalKubeConfigPath)
}
dir, _ := filepath.Split(globalKubeConfigPath)
if err := os.MkdirAll(dir, 0755); err != nil {
return fmt.Errorf("unable to create kubeconfig dir %s: %v", dir, err)
}
// Make sure .kube/config exist if not then this will create
os.OpenFile(globalKubeConfigPath, os.O_RDONLY|os.O_CREATE, 0600)
existingKubeConfig, err = clientcmd.LoadFromFile(globalKubeConfigPath)
if err != nil {
return fmt.Errorf("Not able to load %s: %s", globalKubeConfigPath, err)
}
newKubeConfig, err = clientcmd.LoadFromFile(minishiftKubeConfigPath)
if err != nil {
return fmt.Errorf("Not able to load %s: %s", minishiftKubeConfigPath, err)
}
merged := mergeKubeConfigs([]*clientcmdapi.Config{existingKubeConfig, newKubeConfig})
return clientcmd.WriteToFile(*merged, globalKubeConfigPath)
}
// AddCliContext adds a CLI context for the user and namespace for the current OpenShift cluster. See also
// https://docs.openshift.com/enterprise/3.0/cli_reference/manage_cli_profiles.html
func (oc *OcRunner) AddCliContext(context string, ip string, username string, namespace string, runner util.Runner, ocPath string) error {
cmdArgs := []string{"login",
fmt.Sprintf("-u=%s", username),
fmt.Sprintf("-p=%s", minishiftConstants.DefaultUserPassword),
fmt.Sprintf("%s:8443", ip)}
stdBuffer := new(bytes.Buffer)
exitCode := runner.Run(stdBuffer, os.Stderr, ocPath, cmdArgs...)
if exitCode != 0 {
return fmt.Errorf("Unable to login to cluster")
}
ip = strings.Replace(ip, ".", "-", -1)
cmd := fmt.Sprintf("config set-context %s --cluster=%s:%d --user=%s/%s:
9502
%d --namespace=%s", context, ip, constants.APIServerPort, username, ip, constants.APIServerPort, namespace)
errorBuffer := new(bytes.Buffer)
exitCode = oc.RunAsUser(cmd, nil, errorBuffer)
if exitCode != 0 {
return fmt.Errorf("Unable to create CLI context: %v", errorBuffer)
}
cmd = fmt.Sprintf("config use-context %s", context)
exitCode = oc.RunAsUser(cmd, nil, errorBuffer)
if exitCode != 0 {
return fmt.Errorf("Unable to switch CLI context: %v", errorBuffer)
}
return nil
}
func SupportFlag(flag string, ocPath string, runner util.Runner) bool {
var buffer bytes.Buffer
cmdArgs := []string{"cluster", "up", "-h"}
runner.Run(&buffer, os.Stderr, ocPath, cmdArgs...)
ocCommandOptions := parseOcHelpCommand(buffer.Bytes())
if ocCommandOptions != nil {
return flagExist(ocCommandOptions, flag)
}
return false
}
func parseOcHelpCommand(cmdOut []byte) []string {
ocOptions := []string{}
ocOptionRegex := regexp.MustCompile(`(?s)Options(.*)host config dir`)
matches := ocOptionRegex.FindSubmatch(cmdOut)
if matches != nil {
tmpOptionsList := string(matches[0])
for _, value := range strings.Split(tmpOptionsList, "\n")[1:] {
tmpOption := strings.Split(strings.Split(strings.TrimSpace(value), "=")[0], "--")
if len(tmpOption) > 1 {
ocOptions = append(ocOptions, tmpOption[1])
}
}
} else {
return nil
}
return ocOptions
}
func flagExist(ocCommandOptions []string, flag string) bool {
for _, v := range ocCommandOptions {
if v == flag {
return true
}
}
return false
}
func mergeKubeConfigs(configs []*clientcmdapi.Config) *clientcmdapi.Config {
mergedConfig := clientcmdapi.NewConfig()
for _, config := range configs {
if config == nil {
continue
}
// merge clusters
for cName, c := range config.Clusters {
mergedConfig.Clusters[cName] = c
}
// merge authinfos
for aName, a := range config.AuthInfos {
mergedConfig.AuthInfos[aName] = a
}
// merge contexts
for ctxName, ctx := range config.Contexts {
mergedConfig.Contexts[ctxName] = ctx
}
// merge extensions
for extName, ext := range config.Extensions {
mergedConfig.Extensions[extName] = ext
}
}
return mergedConfig
}
// getGlobalKubeConfigPath returns the path to the first entry in KUBECONFIG environment variable
// or if KUBECONFIG not set then $HOME/.kube/config
func getGlobalKubeConfigPath() (string, error) {
globalKubeConfigPath := os.Getenv("KUBECONFIG")
globalKubeConfigPathList := strings.FieldsFunc(globalKubeConfigPath, splitKubeConfig)
if len(globalKubeConfigPathList) > 0 {
// Tools should write to last entry in the KUBECONFIG file instead first one.
// oc cluster up also follow same.
globalKubeConfigPath = strings.FieldsFunc(globalKubeConfigPath, splitKubeConfig)[len(globalKubeConfigPathList)-1]
} else {
usr, err := user.Current()
if err != nil {
return "", fmt.Errorf("unable to find current user: %v", err)
}
globalKubeConfigPath = filepath.Join(usr.HomeDir, ".kube", "config")
}
return globalKubeConfigPath, nil
}
func splitKubeConfig(r rune) bool {
if runtime.GOOS == "windows" {
return r == ';'
}
return r == ':'
}