Skip to content

Commit 213f9a8

Browse files
committed
break remaining deps on pkg/oc
1 parent 14a5503 commit 213f9a8

File tree

19 files changed

+183
-233
lines changed

19 files changed

+183
-233
lines changed

pkg/client/config/smart_merge.go

+96
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package config
2+
3+
import (
4+
"crypto/x509"
5+
"fmt"
6+
"net"
7+
"net/url"
8+
"strconv"
9+
"strings"
10+
11+
x509request "k8s.io/apiserver/pkg/authentication/request/x509"
12+
13+
"k8s.io/apimachinery/third_party/forked/golang/netutil"
14+
)
15+
16+
// GetClusterNicknameFromURL returns host:port of the apiServerLocation, with .'s replaced by -'s
17+
func GetClusterNicknameFromURL(apiServerLocation string) (string, error) {
18+
u, err := url.Parse(apiServerLocation)
19+
if err != nil {
20+
return "", err
21+
}
22+
hostPort := netutil.CanonicalAddr(u)
23+
24+
// we need a character other than "." to avoid conflicts with. replace with '-'
25+
return strings.Replace(hostPort, ".", "-", -1), nil
26+
}
27+
28+
func GetUserNicknameFromCert(clusterNick string, chain ...*x509.Certificate) (string, error) {
29+
userInfo, _, err := x509request.CommonNameUserConversion(chain)
30+
if err != nil {
31+
return "", err
32+
}
33+
34+
return userInfo.GetName() + "/" + clusterNick, nil
35+
}
36+
37+
func GetContextNickname(namespace, clusterNick, userNick string) string {
38+
tokens := strings.SplitN(userNick, "/", 2)
39+
return namespace + "/" + clusterNick + "/" + tokens[0]
40+
}
41+
42+
var validURLSchemes = []string{"https://", "http://", "tcp://"}
43+
44+
// NormalizeServerURL is opinionated normalization of a string that represents a URL. Returns the URL provided matching the format
45+
// expected when storing a URL in a config. Sets a scheme and port if not present, removes unnecessary trailing
46+
// slashes, etc. Can be used to normalize a URL provided by user input.
47+
func NormalizeServerURL(s string) (string, error) {
48+
// normalize scheme
49+
if !hasScheme(s) {
50+
s = validURLSchemes[0] + s
51+
}
52+
53+
addr, err := url.Parse(s)
54+
if err != nil {
55+
return "", fmt.Errorf("Not a valid URL: %v.", err)
56+
}
57+
58+
// normalize host:port
59+
if strings.Contains(addr.Host, ":") {
60+
_, port, err := net.SplitHostPort(addr.Host)
61+
if err != nil {
62+
return "", fmt.Errorf("Not a valid host:port: %v.", err)
63+
}
64+
_, err = strconv.ParseUint(port, 10, 16)
65+
if err != nil {
66+
return "", fmt.Errorf("Not a valid port: %v. Port numbers must be between 0 and 65535.", port)
67+
}
68+
} else {
69+
port := 0
70+
switch addr.Scheme {
71+
case "http":
72+
port = 80
73+
case "https":
74+
port = 443
75+
default:
76+
return "", fmt.Errorf("No port specified.")
77+
}
78+
addr.Host = net.JoinHostPort(addr.Host, strconv.FormatInt(int64(port), 10))
79+
}
80+
81+
// remove trailing slash if that's the only path we have
82+
if addr.Path == "/" {
83+
addr.Path = ""
84+
}
85+
86+
return addr.String(), nil
87+
}
88+
89+
func hasScheme(s string) bool {
90+
for _, p := range validURLSchemes {
91+
if strings.HasPrefix(s, p) {
92+
return true
93+
}
94+
}
95+
return false
96+
}

pkg/cmd/infra/router/f5.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
clientcmd "github.com/openshift/origin/pkg/client/cmd"
1616
"github.com/openshift/origin/pkg/cmd/util"
1717
cmdversion "github.com/openshift/origin/pkg/cmd/version"
18-
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
1918
projectinternalclientset "github.com/openshift/origin/pkg/project/generated/internalclientset"
2019
routeapi "github.com/openshift/origin/pkg/route/apis/route"
2120
routeinternalclientset "github.com/openshift/origin/pkg/route/generated/internalclientset"

pkg/cmd/infra/router/template.go

-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ import (
3131
"github.com/openshift/origin/pkg/cmd/server/crypto"
3232
"github.com/openshift/origin/pkg/cmd/util"
3333
cmdversion "github.com/openshift/origin/pkg/cmd/version"
34-
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
3534
projectinternalclientset "github.com/openshift/origin/pkg/project/generated/internalclientset"
3635
routeinternalclientset "github.com/openshift/origin/pkg/route/generated/internalclientset"
3736
"github.com/openshift/origin/pkg/router"

pkg/cmd/openshift/openshift.go

+4-42
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"strings"
88

99
"github.com/spf13/cobra"
10-
"github.com/spf13/pflag"
1110

1211
ktemplates "k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1312
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
@@ -20,8 +19,9 @@ import (
2019
"github.com/openshift/origin/pkg/cmd/server/start"
2120
"github.com/openshift/origin/pkg/cmd/templates"
2221
cmdutil "github.com/openshift/origin/pkg/cmd/util"
22+
cmdversion "github.com/openshift/origin/pkg/cmd/version"
2323
"github.com/openshift/origin/pkg/oc/cli/cmd"
24-
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
24+
osversion "github.com/openshift/origin/pkg/version/openshift"
2525
)
2626

2727
var (
@@ -91,12 +91,10 @@ func NewCommandOpenShift(name string) *cobra.Command {
9191
Run: kcmdutil.DefaultSubCommandRun(out),
9292
}
9393

94-
f := clientcmd.New(pflag.NewFlagSet("", pflag.ContinueOnError))
95-
9694
startAllInOne, _ := start.NewCommandStartAllInOne(name, out, errout)
9795
root.AddCommand(startAllInOne)
9896
root.AddCommand(newCompletionCommand("completion", name+" completion"))
99-
root.AddCommand(cmd.NewCmdVersion(name, f, out, cmd.VersionOptions{PrintEtcdVersion: true, IsServer: true}))
97+
root.AddCommand(cmdversion.NewCmdVersion(name, osversion.Get(), os.Stdout))
10098
root.AddCommand(cmd.NewCmdOptions(out))
10199

102100
// TODO: add groups
@@ -105,43 +103,7 @@ func NewCommandOpenShift(name string) *cobra.Command {
105103
return root
106104
}
107105

108-
var (
109-
completion_long = ktemplates.LongDesc(`
110-
Output shell completion code for the given shell (bash or zsh).
111-
112-
This command prints shell code which must be evaluation to provide interactive
113-
completion of kubectl commands.`)
114-
115-
completion_example = ktemplates.Examples(`
116-
$ source <(kubectl completion bash)
117-
118-
will load the kubectl completion code for bash. Note that this depends on the bash-completion
119-
framework. It must be sourced before sourcing the kubectl completion, i.e. on the Mac:
120-
121-
$ brew install bash-completion
122-
$ source $(brew --prefix)/etc/bash_completion
123-
$ source <(kubectl completion bash)
124-
125-
If you use zsh, the following will load kubectl zsh completion:
126-
127-
$ source <(kubectl completion zsh)`)
128-
)
129-
130106
func newCompletionCommand(name, fullName string) *cobra.Command {
131-
out := os.Stdout
132-
133-
completion := &cobra.Command{
134-
Use: fmt.Sprintf("%s SHELL", name),
135-
Short: "Output shell completion code for the given shell (bash or zsh)",
136-
Long: completion_long,
137-
Example: completion_example,
138-
Run: func(cmd *cobra.Command, args []string) {
139-
140-
},
141-
}
142-
143-
f := clientcmd.New(completion.PersistentFlags())
144-
145-
return cmd.NewCmdCompletion(fullName, f, out)
107+
return cmd.NewCmdCompletion(fullName, os.Stdout)
146108

147109
}

pkg/cmd/server/admin/create_kubeconfig.go

+6-7
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ import (
1616
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1717
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1818

19+
"github.com/openshift/origin/pkg/client/config"
1920
"github.com/openshift/origin/pkg/cmd/server/crypto"
20-
"github.com/openshift/origin/pkg/oc/cli/config"
21-
cliconfig "github.com/openshift/origin/pkg/oc/cli/config"
2221
"k8s.io/client-go/tools/clientcmd"
2322
clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2423
)
@@ -161,15 +160,15 @@ func (o CreateKubeConfigOptions) CreateKubeConfig() (*clientcmdapi.Config, error
161160
}
162161

163162
// determine all the nicknames
164-
clusterNick, err := cliconfig.GetClusterNicknameFromURL(o.APIServerURL)
163+
clusterNick, err := config.GetClusterNicknameFromURL(o.APIServerURL)
165164
if err != nil {
166165
return nil, err
167166
}
168-
userNick, err := cliconfig.GetUserNicknameFromCert(clusterNick, certConfig.Certs...)
167+
userNick, err := config.GetUserNicknameFromCert(clusterNick, certConfig.Certs...)
169168
if err != nil {
170169
return nil, err
171170
}
172-
contextNick := cliconfig.GetContextNickname(o.ContextNamespace, clusterNick, userNick)
171+
contextNick := config.GetContextNickname(o.ContextNamespace, clusterNick, userNick)
173172

174173
credentials := make(map[string]*clientcmdapi.AuthInfo)
175174
credentials[userNick] = &clientcmdapi.AuthInfo{
@@ -194,11 +193,11 @@ func (o CreateKubeConfigOptions) CreateKubeConfig() (*clientcmdapi.Config, error
194193

195194
createPublic := (len(o.PublicAPIServerURL) > 0) && o.APIServerURL != o.PublicAPIServerURL
196195
if createPublic {
197-
publicClusterNick, err := cliconfig.GetClusterNicknameFromURL(o.PublicAPIServerURL)
196+
publicClusterNick, err := config.GetClusterNicknameFromURL(o.PublicAPIServerURL)
198197
if err != nil {
199198
return nil, err
200199
}
201-
publicContextNick := cliconfig.GetContextNickname(o.ContextNamespace, publicClusterNick, userNick)
200+
publicContextNick := config.GetContextNickname(o.ContextNamespace, publicClusterNick, userNick)
202201

203202
clusters[publicClusterNick] = &clientcmdapi.Cluster{
204203
Server: o.PublicAPIServerURL,

pkg/cmd/server/origin/controller/config.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
configapi "github.com/openshift/origin/pkg/cmd/server/api"
1616
"github.com/openshift/origin/pkg/cmd/server/crypto"
1717
"github.com/openshift/origin/pkg/cmd/util/variable"
18-
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
1918
)
2019

2120
func envVars(host string, caData []byte, insecure bool, bearerTokenFile string) []kapi.EnvVar {

pkg/oc/admin/admin.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func NewCommandAdmin(name, fullName string, in io.Reader, out io.Writer, errout
138138
cmds.AddCommand(
139139
// part of every root command
140140
cmd.NewCmdConfig(fullName, "config", out, errout),
141-
cmd.NewCmdCompletion(fullName, f, out),
141+
cmd.NewCmdCompletion(fullName, out),
142142

143143
// hidden
144144
cmd.NewCmdOptions(out),

pkg/oc/admin/diagnostics/diagnostics/pod/auth.go

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
knet "k8s.io/apimachinery/pkg/util/net"
1616
restclient "k8s.io/client-go/rest"
1717

18-
clientcmd "github.com/openshift/origin/pkg/client/cmd"
1918
"github.com/openshift/origin/pkg/cmd/flagtypes"
2019
"github.com/openshift/origin/pkg/oc/admin/diagnostics/diagnostics/types"
2120
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"

pkg/oc/cli/cli.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ func NewCommandCLI(name, fullName string, in io.Reader, out, errout io.Writer) *
179179
login.NewCmdLogout("logout", fullName+" logout", fullName+" login", f, in, out),
180180
cmd.NewCmdConfig(fullName, "config", out, errout),
181181
cmd.NewCmdWhoAmI(cmd.WhoAmIRecommendedCommandName, fullName+" "+cmd.WhoAmIRecommendedCommandName, f, out),
182-
cmd.NewCmdCompletion(fullName, f, out),
182+
cmd.NewCmdCompletion(fullName, out),
183183
},
184184
},
185185
}

pkg/oc/cli/cmd/login/loginoptions.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ import (
1919
kclientcmdapi "k8s.io/client-go/tools/clientcmd/api"
2020
kterm "k8s.io/kubernetes/pkg/kubectl/util/term"
2121

22+
"github.com/openshift/origin/pkg/client/config"
2223
cmdutil "github.com/openshift/origin/pkg/cmd/util"
2324
"github.com/openshift/origin/pkg/cmd/util/term"
2425
"github.com/openshift/origin/pkg/cmd/util/tokencmd"
2526
"github.com/openshift/origin/pkg/oc/cli/cmd/errors"
2627
loginutil "github.com/openshift/origin/pkg/oc/cli/cmd/login/util"
27-
"github.com/openshift/origin/pkg/oc/cli/config"
28+
cliconfig "github.com/openshift/origin/pkg/oc/cli/config"
2829
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
2930
cmderr "github.com/openshift/origin/pkg/oc/errors"
3031
projectclient "github.com/openshift/origin/pkg/project/generated/internalclientset"
@@ -347,7 +348,7 @@ func (o *LoginOptions) SaveConfig() (bool, error) {
347348
globalExistedBefore = false
348349
}
349350

350-
newConfig, err := config.CreateConfig(o.Project, o.Config)
351+
newConfig, err := cliconfig.CreateConfig(o.Project, o.Config)
351352
if err != nil {
352353
return false, err
353354
}
@@ -360,11 +361,11 @@ func (o *LoginOptions) SaveConfig() (bool, error) {
360361
if err != nil {
361362
return false, err
362363
}
363-
if err := config.RelativizeClientConfigPaths(newConfig, baseDir); err != nil {
364+
if err := cliconfig.RelativizeClientConfigPaths(newConfig, baseDir); err != nil {
364365
return false, err
365366
}
366367

367-
configToWrite, err := config.MergeConfig(*o.StartingKubeConfig, *newConfig)
368+
configToWrite, err := cliconfig.MergeConfig(*o.StartingKubeConfig, *newConfig)
368369
if err != nil {
369370
return false, err
370371
}

pkg/oc/cli/cmd/login/loginoptions_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ import (
1414

1515
"github.com/MakeNowJust/heredoc"
1616

17+
"github.com/openshift/origin/pkg/client/config"
1718
"github.com/openshift/origin/pkg/oauth/util"
18-
"github.com/openshift/origin/pkg/oc/cli/config"
1919
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
2020

2121
kapierrs "k8s.io/apimachinery/pkg/api/errors"

pkg/oc/cli/cmd/project.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"k8s.io/kubernetes/pkg/kubectl/cmd/templates"
1717
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1818

19+
clientcfg "github.com/openshift/origin/pkg/client/config"
1920
cliconfig "github.com/openshift/origin/pkg/oc/cli/config"
2021
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
2122
projectapi "github.com/openshift/origin/pkg/project/apis/project"
@@ -186,7 +187,7 @@ func (o ProjectOptions) RunProject() error {
186187
return err
187188
}
188189

189-
defaultContextName := cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
190+
defaultContextName := clientcfg.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
190191

191192
// if they specified a project name and got a generated context, then only show the information they care about. They won't recognize
192193
// a context name they didn't choose
@@ -286,7 +287,7 @@ func (o ProjectOptions) RunProject() error {
286287

287288
// calculate what name we'd generate for the context. If the context has the same name, don't drop it into the output, because the user won't
288289
// recognize the name since they didn't choose it.
289-
defaultContextName := cliconfig.GetContextNickname(namespaceInUse, config.Contexts[contextInUse].Cluster, config.Contexts[contextInUse].AuthInfo)
290+
defaultContextName := clientcfg.GetContextNickname(namespaceInUse, config.Contexts[contextInUse].Cluster, config.Contexts[contextInUse].AuthInfo)
290291

291292
switch {
292293
// if there is no namespace, then the only information we can provide is the context and server

pkg/oc/cli/cmd/projects.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
kcmdutil "k8s.io/kubernetes/pkg/kubectl/cmd/util"
1414

1515
oapi "github.com/openshift/origin/pkg/api"
16+
clientcfg "github.com/openshift/origin/pkg/client/config"
1617
cliconfig "github.com/openshift/origin/pkg/oc/cli/config"
1718
"github.com/openshift/origin/pkg/oc/cli/util/clientcmd"
1819
projectapi "github.com/openshift/origin/pkg/project/apis/project"
@@ -140,7 +141,7 @@ func (o ProjectsOptions) RunProjects() error {
140141

141142
var defaultContextName string
142143
if currentContext != nil {
143-
defaultContextName = cliconfig.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
144+
defaultContextName = clientcfg.GetContextNickname(currentContext.Namespace, currentContext.Cluster, currentContext.AuthInfo)
144145
}
145146

146147
var msg string

pkg/oc/cli/cmd/wrappers.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ var (
229229
* zsh completions are only supported in versions of zsh >= 5.2`)
230230
)
231231

232-
func NewCmdCompletion(fullName string, f *clientcmd.Factory, out io.Writer) *cobra.Command {
232+
func NewCmdCompletion(fullName string, out io.Writer) *cobra.Command {
233233
cmdHelpName := fullName
234234

235235
if strings.HasSuffix(fullName, "completion") {

0 commit comments

Comments
 (0)