-
Notifications
You must be signed in to change notification settings - Fork 1.5k
asset: Add asset generation for kubeconfigs #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
openshift-merge-robot
merged 5 commits into
openshift:master
from
yifan-gu:generate_kubeconfig
Aug 30, 2018
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da6286f
vendor: Update vendor for installconfig generation.
dea3968
kubeconfig: Add asset generation for kubeconfigs
a343bc3
kubeconfig: Add tests for kubeconfig generation.
fc954ec
tls: Export key and cert filenames in the tls package.
dfd7deb
graph: Update the dependency graph.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| // Package kubeconfig defines and generates the kubeconfig assets. | ||
| package kubeconfig |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| package kubeconfig | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "path/filepath" | ||
|
|
||
| "github.com/ghodss/yaml" | ||
| "github.com/openshift/installer/pkg/asset" | ||
| "github.com/openshift/installer/pkg/asset/installconfig" | ||
| "github.com/openshift/installer/pkg/asset/tls" | ||
| clientcmd "k8s.io/client-go/tools/clientcmd/api/v1" | ||
| ) | ||
|
|
||
| const ( | ||
| // KubeconfigUserNameAdmin is the user name of the admin kubeconfig. | ||
| KubeconfigUserNameAdmin = "admin" | ||
| // KubeconfigUserNameKubelet is the user name of the kubelet kubeconfig. | ||
| KubeconfigUserNameKubelet = "kubelet" | ||
| ) | ||
|
|
||
| // Kubeconfig implements the asset.Asset interface that generates | ||
| // the admin kubeconfig and kubelet kubeconfig. | ||
| type Kubeconfig struct { | ||
| rootDir string | ||
| userName string // admin or kubelet. | ||
| rootCA asset.Asset | ||
| certKey asset.Asset | ||
| installConfig asset.Asset | ||
| } | ||
|
|
||
| var _ asset.Asset = (*Kubeconfig)(nil) | ||
|
|
||
| // Dependencies returns the dependency of the kubeconfig. | ||
| func (k *Kubeconfig) Dependencies() []asset.Asset { | ||
| return []asset.Asset{ | ||
| k.rootCA, | ||
| k.certKey, | ||
| k.installConfig, | ||
| } | ||
| } | ||
|
|
||
| // Generate generates the kubeconfig. | ||
| func (k *Kubeconfig) Generate(parents map[asset.Asset]*asset.State) (*asset.State, error) { | ||
| var err error | ||
|
|
||
| caCertData, err := asset.GetDataByFilename(k.rootCA, parents, tls.RootCACertName) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| var keyFilename, certFilename string | ||
| switch k.userName { | ||
| case KubeconfigUserNameAdmin: | ||
| keyFilename, certFilename = tls.AdminKeyName, tls.AdminCertName | ||
| case KubeconfigUserNameKubelet: | ||
| keyFilename, certFilename = tls.KubeletKeyName, tls.KubeletCertName | ||
| } | ||
| clientKeyData, err := asset.GetDataByFilename(k.certKey, parents, keyFilename) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| clientCertData, err := asset.GetDataByFilename(k.certKey, parents, certFilename) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| installConfig, err := installconfig.GetInstallConfig(k.installConfig, parents) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| kubeconfig := clientcmd.Config{ | ||
| Clusters: []clientcmd.NamedCluster{ | ||
| { | ||
| Name: installConfig.Name, | ||
| Cluster: clientcmd.Cluster{ | ||
| Server: fmt.Sprintf("https://%s-api.%s:6443", installConfig.Name, installConfig.BaseDomain), | ||
| CertificateAuthorityData: caCertData, | ||
| }, | ||
| }, | ||
| }, | ||
| AuthInfos: []clientcmd.NamedAuthInfo{ | ||
| { | ||
| Name: k.userName, | ||
| AuthInfo: clientcmd.AuthInfo{ | ||
| ClientCertificateData: clientCertData, | ||
| ClientKeyData: clientKeyData, | ||
| }, | ||
| }, | ||
| }, | ||
| Contexts: []clientcmd.NamedContext{ | ||
| { | ||
| Name: k.userName, | ||
| Context: clientcmd.Context{ | ||
| Cluster: installConfig.Name, | ||
| AuthInfo: k.userName, | ||
| }, | ||
| }, | ||
| }, | ||
| CurrentContext: k.userName, | ||
| } | ||
|
|
||
| data, err := yaml.Marshal(kubeconfig) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| st := &asset.State{ | ||
| Contents: []asset.Content{ | ||
| { | ||
| // E.g. generated/auth/kubeconfig-admin. | ||
| Name: filepath.Join(k.rootDir, "auth", fmt.Sprintf("kubeconfig-%s", k.userName)), | ||
| Data: data, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| if err := st.PersistToFile(); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return st, nil | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why this being persisted to file?
Only targeted asset states are written to file.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
https://github.com/openshift/installer/blob/master/Documentation/design/assetgeneration.md#target-generation states
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, will address in another PR ( for the installconfig too)