Skip to content

Commit

Permalink
nodeup: create kubeconfig under admin or root
Browse files Browse the repository at this point in the history
While the admin account is created on stock debian images, it isn't on
all of them.  Check admin first, then check root, and don't treat it as
an error if neither is found - this is only a convenience.
  • Loading branch information
justinsb committed Dec 15, 2017
1 parent 3a10a8c commit 51fd5d5
Showing 1 changed file with 44 additions and 9 deletions.
53 changes: 44 additions & 9 deletions nodeup/pkg/model/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,29 +74,64 @@ func (b *KubectlBuilder) Build(c *fi.ModelBuilderContext) error {
}
c.AddTask(t)

switch b.Distribution {
case distros.DistributionJessie, distros.DistributionDebian9:
adminUser, adminGroup, err := b.findKubeconfigUser()
if err != nil {
return err
}

if adminUser != "" {
c.AddTask(&nodetasks.File{
Path: "/home/admin/.kube/",
Type: nodetasks.FileType_Directory,
Mode: s("0700"),
Owner: s("admin"),
Group: s("admin"),
Owner: s(adminUser),
Group: s(adminGroup),
})

c.AddTask(&nodetasks.File{
Path: "/home/admin/.kube/config",
Contents: fi.NewStringResource(kubeconfig),
Type: nodetasks.FileType_File,
Mode: s("0400"),
Owner: s("admin"),
Group: s("admin"),
Owner: s(adminUser),
Group: s(adminGroup),
})

default:
glog.Warningf("Unknown distro; won't write kubeconfig to homedir %s", b.Distribution)
}
}

return nil
}

// findKubeconfigUser finds the default user for whom we should create a kubeconfig
func (b *KubectlBuilder) findKubeconfigUser() (string, string, error) {
var users []string
switch b.Distribution {
case distros.DistributionJessie, distros.DistributionDebian9:
users = []string{"admin", "root"}
default:
glog.Warningf("Unknown distro; won't write kubeconfig to homedir %s", b.Distribution)
return "", "", nil
}

for _, s := range users {
user, err := fi.LookupUser(s)
if err != nil {
glog.Warningf("error looking up user %q: %v", s, err)
continue
}
if user == nil {
continue
}
group, err := fi.LookupGroupById(user.Gid)
if err != nil {
glog.Warningf("unable to find group %d for user %q", user.Gid, s)
continue
}
if group == nil {
continue
}
return user.Name, group.Name, nil
}

return "", "", nil
}

0 comments on commit 51fd5d5

Please sign in to comment.