Skip to content

Commit

Permalink
Issue kube-scheduler cert in nodeup
Browse files Browse the repository at this point in the history
  • Loading branch information
johngmyers committed Jun 9, 2020
1 parent 99adb56 commit a92cd0c
Show file tree
Hide file tree
Showing 6 changed files with 318 additions and 75 deletions.
84 changes: 43 additions & 41 deletions nodeup/pkg/model/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package model

import (
"crypto/x509/pkix"
"fmt"
"os"
"path/filepath"
Expand All @@ -27,7 +28,6 @@ import (
"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/kops/util"
"k8s.io/kops/pkg/apis/nodeup"
"k8s.io/kops/pkg/kubeconfig"
"k8s.io/kops/pkg/systemd"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
Expand Down Expand Up @@ -193,6 +193,33 @@ func (c *NodeupModelContext) KubeletKubeConfig() string {
return "/var/lib/kubelet/kubeconfig"
}

// BuildIssuedKubeconfig generates a kubeconfig with a locally issued client certificate
func (c *NodeupModelContext) BuildIssuedKubeconfig(name string, subject pkix.Name, ctx *fi.ModelBuilderContext) *fi.TaskDependentResource {
issueCert := &nodetasks.IssueCert{
Name: name,
Signer: fi.CertificateIDCA,
Type: "client",
Subject: subject,
}
ctx.AddTask(issueCert)
certResource, keyResource, caResource := issueCert.GetResources()

kubeConfig := &nodetasks.KubeConfig{
Name: name,
Cert: certResource,
Key: keyResource,
CA: caResource,
}
if c.IsMaster {
// @note: use https even for local connections, so we can turn off the insecure port
kubeConfig.ServerURL = "https://127.0.0.1"
} else {
kubeConfig.ServerURL = "https://" + c.Cluster.Spec.MasterInternalName
}
ctx.AddTask(kubeConfig)
return kubeConfig.GetConfig()
}

// BuildPKIKubeconfig generates a kubeconfig
func (c *NodeupModelContext) BuildPKIKubeconfig(name string) (string, error) {
ca, err := c.GetCert(fi.CertificateIDCA)
Expand All @@ -215,54 +242,29 @@ func (c *NodeupModelContext) BuildPKIKubeconfig(name string) (string, error) {

// BuildKubeConfig is responsible for building a kubeconfig
func (c *NodeupModelContext) BuildKubeConfig(username string, ca, certificate, privateKey []byte) (string, error) {
user := kubeconfig.KubectlUser{
ClientCertificateData: certificate,
ClientKeyData: privateKey,
}
cluster := kubeconfig.KubectlCluster{
CertificateAuthorityData: ca,
kubeConfig := &nodetasks.KubeConfig{
Name: username,
Cert: fi.NewBytesResource(certificate),
Key: fi.NewBytesResource(privateKey),
CA: fi.NewBytesResource(ca),
}

if c.IsMaster {
// @note: use https even for local connections, so we can turn off the insecure port
cluster.Server = "https://127.0.0.1"
kubeConfig.ServerURL = "https://127.0.0.1"
} else {
cluster.Server = "https://" + c.Cluster.Spec.MasterInternalName
}

config := &kubeconfig.KubectlConfig{
ApiVersion: "v1",
Kind: "Config",
Users: []*kubeconfig.KubectlUserWithName{
{
Name: username,
User: user,
},
},
Clusters: []*kubeconfig.KubectlClusterWithName{
{
Name: "local",
Cluster: cluster,
},
},
Contexts: []*kubeconfig.KubectlContextWithName{
{
Name: "service-account-context",
Context: kubeconfig.KubectlContext{
Cluster: "local",
User: username,
},
},
},
CurrentContext: "service-account-context",
}

yaml, err := kops.ToRawYaml(config)
kubeConfig.ServerURL = "https://" + c.Cluster.Spec.MasterInternalName
}

err := kubeConfig.Run(nil)
if err != nil {
return "", fmt.Errorf("error marshaling kubeconfig to yaml: %v", err)
}

return string(yaml), nil
config, err := fi.ResourceAsString(kubeConfig.GetConfig())
if err != nil {
return "", err
}
return config, nil
}

// IsKubernetesGTE checks if the version is greater-than-or-equal
Expand Down
9 changes: 4 additions & 5 deletions nodeup/pkg/model/kube_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package model

import (
"crypto/x509/pkix"
"fmt"
"strconv"

"k8s.io/kops/pkg/configbuilder"
"k8s.io/kops/pkg/flagbuilder"
"k8s.io/kops/pkg/k8scodecs"
"k8s.io/kops/pkg/kubemanifest"
"k8s.io/kops/pkg/rbac"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/nodeup/nodetasks"
"k8s.io/kops/util/pkg/exec"
Expand Down Expand Up @@ -83,14 +85,11 @@ func (b *KubeSchedulerBuilder) Build(c *fi.ModelBuilderContext) error {
}

{
kubeconfig, err := b.BuildPKIKubeconfig("kube-scheduler")
if err != nil {
return err
}
kubeconfig := b.BuildIssuedKubeconfig("kube-scheduler", pkix.Name{CommonName: rbac.KubeScheduler}, c)

c.AddTask(&nodetasks.File{
Path: "/var/lib/kube-scheduler/kubeconfig",
Contents: fi.NewStringResource(kubeconfig),
Contents: kubeconfig,
Type: nodetasks.FileType_File,
Mode: s("0400"),
})
Expand Down
151 changes: 132 additions & 19 deletions nodeup/pkg/model/tests/golden/minimal/tasks-kube-scheduler.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,65 @@ mode: "0400"
path: /var/lib/kube-scheduler/config.yaml
type: file
---
contents: |
apiVersion: v1
clusters:
- cluster:
certificate-authority-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMyRENDQWNDZ0F3SUJBZ0lSQUxKWEFrVmo5NjR0cTY3d01TSThvSlF3RFFZSktvWklodmNOQVFFTEJRQXcKRlRFVE1CRUdBMVVFQXhNS2EzVmlaWEp1WlhSbGN6QWVGdzB4TnpFeU1qY3lNelV5TkRCYUZ3MHlOekV5TWpjeQpNelV5TkRCYU1CVXhFekFSQmdOVkJBTVRDbXQxWW1WeWJtVjBaWE13Z2dFaU1BMEdDU3FHU0liM0RRRUJBUVVBCkE0SUJEd0F3Z2dFS0FvSUJBUURnbkNrU210bm1meEVnUzNxTlBhVUNINVFPQkdESC9pbkhiV0NPRExCQ0s5Z2QKWEVjQmw3RlZ2OFQya0ZyMURZYjBIVkR0TUk3dGl4UlZGRExna3dObFczNHh3V2RaWEI3R2VvRmdVMXhXT1FTWQpPQUNDOEpnWVRRLzEzOUhCRXZncTRzZWo2N3ArL3MvU05jdzM0S2s3SEl1RmhsazFyUms1a01leEtJbEpCS1AxCllZVVlldHNKL1FwVU9rcUo1SFc0R29ldEU3Nll0SG5PUmZZdm55YnZpU01yaDJ3R0dhTjZyL3M0Q2hPYUliWkMKQW44L1lpUEtHSURhWkdwajZHWG5tWEFSUlgvVElkZ1NRa0x3dDBhVERCblBaNFh2dHBJOGFhTDhEWUpJcUF6QQpOUEgyYjQvdU55bGF0NWpEbzBiMEc1NGFnTWk5NysyQVVyQzlVVVhwQWdNQkFBR2pJekFoTUE0R0ExVWREd0VCCi93UUVBd0lCQmpBUEJnTlZIUk1CQWY4RUJUQURBUUgvTUEwR0NTcUdTSWIzRFFFQkN3VUFBNElCQVFCVkdSMnIKaHpYelJNVTV3cmlQUUFKU2Nzek5PUnZvQnBYZlpvWjA5Rkl1cHVkRnhCVlUzZDRoVjlTdEtuUWdQU0dBNVhRTwpIRTk3K0J4SkR1QS9yQjVvQlVzTUJqYzd5MWNkZS9UNmhtaTNyTG9FWUJTblN1ZENPWEpFNEc5LzBmOGJ5QUplCnJOOCtObzFyMlZnWnZaaDZwNzRURWtYdi9sM0hCUFdNN0lkVVYwSE85SkRoU2dPVkYxZnlRS0p4UnVMSlI4anQKTzZtUEgyVVgwdk13VmE0anZ3dGtkZHFrMk9BZFlRdkg5cmJEampiemFpVzBLbm1kdWVSbzkyS0hBTjdCc0RaeQpWcFhIcHFvMUt6ZzdEM2ZwYVhDZjVzaTdscXFyZEpWWEg0SkM3Mnp4c1BlaHFnaThlSXVxT0JraURXbVJ4QXhoCjh5R2VSeDlBYmtuSGg0SWEKLS0tLS1FTkQgQ0VSVElGSUNBVEUtLS0tLQo=
server: https://127.0.0.1
name: local
contexts:
- context:
cluster: local
user: kube-scheduler
name: service-account-context
current-context: service-account-context
kind: Config
users:
- name: kube-scheduler
user:
client-certificate-data: LS0tLS1CRUdJTiBDRVJUSUZJQ0FURS0tLS0tCk1JSUMyRENDQWNDZ0F3SUJBZ0lSQUxKWEFrVmo5NjR0cTY3d01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1N0x0a2xEenFPZjFhS0czUQpkR1hQS2lia25xcUpZSCtiblVOanVZWU5lckVUVjU3bGlqTUdIdVNZQ2Y4dndMbjNveEJmRVJSWDYxTS9EVThaCndvcnovUUtCZ1FEQ1RKSTIramRYZzI2WHVZVW1NNFhYZm5vY2Z6QVhoWEJVTHQxbkVOY29nTmYxZmNwdEFWdHUKQkFpejQvSGlwUUtxb1dWVVlteGZnYmJMUktLTEswczBsT1dLYllkVmpoRW0vbTJaVTh3dFhUYWdOd2tJR295cQpZL0MxTG94NGYxUk9KbkNqYy9oZmNPamN4WDVNOEE4cGVlY0hXbFZ0VVBLVEpneFE3b01LY3c9PQotLS0tLUVORCBSU0EgUFJJVkFURSBLRVktLS0tLQo=
contents:
Resource: null
Task:
CA:
Resource: null
Task:
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
Cert:
Resource: null
Task:
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
Key:
Resource: null
Task:
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
Name: kube-scheduler
ServerURL: https://127.0.0.1
mode: "0400"
path: /var/lib/kube-scheduler/kubeconfig
type: file
Expand All @@ -91,3 +131,76 @@ ifNotExists: true
mode: "0400"
path: /var/log/kube-scheduler.log
type: file
---
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
---
CA:
Resource: null
Task:
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
Cert:
Resource: null
Task:
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
Key:
Resource: null
Task:
Name: kube-scheduler
signer: ca
subject:
CommonName: system:kube-scheduler
Country: null
ExtraNames: null
Locality: null
Names: null
Organization: null
OrganizationalUnit: null
PostalCode: null
Province: null
SerialNumber: ""
StreetAddress: null
type: client
Name: kube-scheduler
ServerURL: https://127.0.0.1
10 changes: 0 additions & 10 deletions pkg/model/pki.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,16 +72,6 @@ func (b *PKIModelBuilder) Build(c *fi.ModelBuilderContext) error {
Signer: defaultCA,
})
}
{
t := &fitasks.Keypair{
Name: fi.String("kube-scheduler"),
Lifecycle: b.Lifecycle,
Subject: "cn=" + rbac.KubeScheduler,
Type: "client",
Signer: defaultCA,
}
c.AddTask(t)
}

{
t := &fitasks.Keypair{
Expand Down
3 changes: 3 additions & 0 deletions upup/pkg/fi/nodeup/nodetasks/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ go_library(
"file.go",
"group.go",
"issue_cert.go",
"kubeconfig.go",
"load_image.go",
"package.go",
"service.go",
Expand All @@ -20,7 +21,9 @@ go_library(
importpath = "k8s.io/kops/upup/pkg/fi/nodeup/nodetasks",
visibility = ["//visibility:public"],
deps = [
"//pkg/apis/kops:go_default_library",
"//pkg/backoff:go_default_library",
"//pkg/kubeconfig:go_default_library",
"//pkg/pki:go_default_library",
"//upup/pkg/fi:go_default_library",
"//upup/pkg/fi/nodeup/cloudinit:go_default_library",
Expand Down
Loading

0 comments on commit a92cd0c

Please sign in to comment.