Skip to content

Commit

Permalink
Include full cluster and ig spec yaml within user data
Browse files Browse the repository at this point in the history
  • Loading branch information
KashifSaadat committed Aug 7, 2017
1 parent 8340a54 commit c4b72db
Show file tree
Hide file tree
Showing 17 changed files with 82 additions and 397 deletions.
16 changes: 10 additions & 6 deletions cmd/kops/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,20 @@ import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"golang.org/x/crypto/ssh"
"io/ioutil"
"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/diff"
"k8s.io/kops/pkg/testutils"
"os"
"path"
"reflect"
"sort"
"strings"
"testing"
"time"

"golang.org/x/crypto/ssh"

"k8s.io/kops/cmd/kops/util"
"k8s.io/kops/pkg/diff"
"k8s.io/kops/pkg/testutils"
)

// TestMinimal runs the test on a minimum configuration, similar to kops create cluster minimal.example.com --zones us-west-1a
Expand Down Expand Up @@ -322,8 +324,10 @@ func runTestCloudformation(t *testing.T, clusterName string, srcDir string, vers
t.Fatalf("unexpected error reading expected cloudformation output: %v", err)
}

if !bytes.Equal(actualCF, expectedCF) {
diffString := diff.FormatDiff(string(expectedCF), string(actualCF))
expectedCFTrimmed := strings.TrimSpace(string(expectedCF))
actualCFTrimmed := strings.TrimSpace(string(actualCF))
if actualCFTrimmed != expectedCFTrimmed {
diffString := diff.FormatDiff(expectedCFTrimmed, actualCFTrimmed)
t.Logf("diff:\n%s\n", diffString)

t.Fatalf("cloudformation output differed from expected")
Expand Down
5 changes: 0 additions & 5 deletions pkg/apis/kops/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,11 +111,6 @@ type ClusterSpec struct {
// Additional policies to add for roles
AdditionalPolicies *map[string]string `json:"additionalPolicies,omitempty"`

// Include cluster spec in user data to detect component config changes
EnableClusterSpecInUserData bool `json:"enableClusterSpecInUserData,omitempty"`
// Hash cluster spec yaml in user data
EnableClusterSpecHash bool `json:"enableClusterSpecHash,omitempty"`

// EtcdClusters stores the configuration for each cluster
EtcdClusters []*EtcdClusterSpec `json:"etcdClusters,omitempty"`
// Component configurations
Expand Down
5 changes: 0 additions & 5 deletions pkg/apis/kops/v1alpha1/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,6 @@ type ClusterSpec struct {
// Additional policies to add for roles
AdditionalPolicies *map[string]string `json:"additionalPolicies,omitempty"`

// Include cluster spec in user data to detect component config changes
EnableClusterSpecInUserData bool `json:"enableClusterSpecInUserData,omitempty"`
// Hash cluster spec yaml in user data
EnableClusterSpecHash bool `json:"enableClusterSpecHash,omitempty"`

//HairpinMode string `json:",omitempty"`
//
//OpencontrailTag string `json:",omitempty"`
Expand Down
4 changes: 0 additions & 4 deletions pkg/apis/kops/v1alpha1/zz_generated.conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -524,8 +524,6 @@ func autoConvert_v1alpha1_ClusterSpec_To_kops_ClusterSpec(in *ClusterSpec, out *
out.IsolateMasters = in.IsolateMasters
out.UpdatePolicy = in.UpdatePolicy
out.AdditionalPolicies = in.AdditionalPolicies
out.EnableClusterSpecInUserData = in.EnableClusterSpecInUserData
out.EnableClusterSpecHash = in.EnableClusterSpecHash
if in.EtcdClusters != nil {
in, out := &in.EtcdClusters, &out.EtcdClusters
*out = make([]*kops.EtcdClusterSpec, len(*in))
Expand Down Expand Up @@ -711,8 +709,6 @@ func autoConvert_kops_ClusterSpec_To_v1alpha1_ClusterSpec(in *kops.ClusterSpec,
out.IsolateMasters = in.IsolateMasters
out.UpdatePolicy = in.UpdatePolicy
out.AdditionalPolicies = in.AdditionalPolicies
out.EnableClusterSpecInUserData = in.EnableClusterSpecInUserData
out.EnableClusterSpecHash = in.EnableClusterSpecHash
if in.EtcdClusters != nil {
in, out := &in.EtcdClusters, &out.EtcdClusters
*out = make([]*EtcdClusterSpec, len(*in))
Expand Down
5 changes: 0 additions & 5 deletions pkg/apis/kops/v1alpha2/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,6 @@ type ClusterSpec struct {
// Additional policies to add for roles
AdditionalPolicies *map[string]string `json:"additionalPolicies,omitempty"`

// Include cluster spec in user data to detect component config changes
EnableClusterSpecInUserData bool `json:"enableClusterSpecInUserData,omitempty"`
// Hash cluster spec yaml in user data
EnableClusterSpecHash bool `json:"enableClusterSpecHash,omitempty"`

// EtcdClusters stores the configuration for each cluster
EtcdClusters []*EtcdClusterSpec `json:"etcdClusters,omitempty"`

Expand Down
4 changes: 0 additions & 4 deletions pkg/apis/kops/v1alpha2/zz_generated.conversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,8 +562,6 @@ func autoConvert_v1alpha2_ClusterSpec_To_kops_ClusterSpec(in *ClusterSpec, out *
out.IsolateMasters = in.IsolateMasters
out.UpdatePolicy = in.UpdatePolicy
out.AdditionalPolicies = in.AdditionalPolicies
out.EnableClusterSpecInUserData = in.EnableClusterSpecInUserData
out.EnableClusterSpecHash = in.EnableClusterSpecHash
if in.EtcdClusters != nil {
in, out := &in.EtcdClusters, &out.EtcdClusters
*out = make([]*kops.EtcdClusterSpec, len(*in))
Expand Down Expand Up @@ -764,8 +762,6 @@ func autoConvert_kops_ClusterSpec_To_v1alpha2_ClusterSpec(in *kops.ClusterSpec,
out.IsolateMasters = in.IsolateMasters
out.UpdatePolicy = in.UpdatePolicy
out.AdditionalPolicies = in.AdditionalPolicies
out.EnableClusterSpecInUserData = in.EnableClusterSpecInUserData
out.EnableClusterSpecHash = in.EnableClusterSpecHash
if in.EtcdClusters != nil {
in, out := &in.EtcdClusters, &out.EtcdClusters
*out = make([]*EtcdClusterSpec, len(*in))
Expand Down
1 change: 1 addition & 0 deletions pkg/model/awsmodel/autoscalinggroup.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"

"github.com/golang/glog"

"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/model"
"k8s.io/kops/upup/pkg/fi"
Expand Down
75 changes: 30 additions & 45 deletions pkg/model/bootstrapscript.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,12 @@ import (
"os"
"text/template"

"github.com/ghodss/yaml"

"k8s.io/kops/pkg/apis/kops"
"k8s.io/kops/pkg/apis/nodeup"
"k8s.io/kops/pkg/model/resources"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/util/pkg/hashing"

"github.com/ghodss/yaml"
)

// BootstrapScript creates the bootstrap script
Expand All @@ -38,53 +37,14 @@ type BootstrapScript struct {
NodeUpConfigBuilder func(ig *kops.InstanceGroup) (*nodeup.Config, error)
}

// ResourceNodeUp generates and returns a nodeup (bootstrap) script from a
// template file, substituting in specific env vars & cluster spec configuration
func (b *BootstrapScript) ResourceNodeUp(ig *kops.InstanceGroup, cs *kops.ClusterSpec) (*fi.ResourceHolder, error) {
if ig.Spec.Role == kops.InstanceGroupRoleBastion {
// Bastions are just bare machines (currently), used as SSH jump-hosts
return nil, nil
}

var igSpec string
if cs.EnableClusterSpecInUserData {
spec := make(map[string]interface{})

spec["docker"] = cs.Docker
spec["kubeProxy"] = cs.KubeProxy
spec["kubelet"] = cs.Kubelet
spec["cloudConfig"] = cs.CloudConfig

if ig.IsMaster() {
spec["kubeAPIServer"] = cs.KubeAPIServer
spec["kubeControllerManager"] = cs.KubeControllerManager
spec["kubeScheduler"] = cs.KubeScheduler
spec["masterKubelet"] = cs.MasterKubelet
}

j, err := json.Marshal(spec)
if err != nil {
return nil, err
}
content, err := yaml.JSONToYAML(j)
if err != nil {
return nil, err
}

if cs.EnableClusterSpecHash {
h := &hashing.Hash{
Algorithm: hashing.HashAlgorithmSHA256,
HashValue: content,
}
igSpec = h.String()
} else {
igSpec = string(content)
}
}

context := map[string]interface{}{
"IncludeClusterSpec": cs.EnableClusterSpecInUserData,
"ClusterSpecContent": igSpec,
}

functions := template.FuncMap{
"NodeUpSource": func() string {
return b.NodeUpSource
Expand Down Expand Up @@ -125,9 +85,34 @@ func (b *BootstrapScript) ResourceNodeUp(ig *kops.InstanceGroup, cs *kops.Cluste
}
return ""
},

"ClusterSpec": func() (string, error) {
spec := make(map[string]interface{})
spec["docker"] = cs.Docker
spec["kubeProxy"] = cs.KubeProxy
spec["kubelet"] = cs.Kubelet
spec["cloudConfig"] = cs.CloudConfig

if ig.IsMaster() {
spec["kubeAPIServer"] = cs.KubeAPIServer
spec["kubeControllerManager"] = cs.KubeControllerManager
spec["kubeScheduler"] = cs.KubeScheduler
spec["masterKubelet"] = cs.MasterKubelet
}

j, err := json.Marshal(spec)
if err != nil {
return "", err
}
content, err := yaml.JSONToYAML(j)
if err != nil {
return "", err
}
return string(content), nil
},
}

templateResource, err := NewTemplateResource("nodeup", resources.AWSNodeUpTemplate, functions, context)
templateResource, err := NewTemplateResource("nodeup", resources.AWSNodeUpTemplate, functions, nil)
if err != nil {
return nil, err
}
Expand Down
42 changes: 11 additions & 31 deletions pkg/model/bootstrapscript_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,39 +26,21 @@ import (

func TestBootstrapUserData(t *testing.T) {
cs := []struct {
EnableClusterSpec bool
HashClusterSpec bool
Role kops.InstanceGroupRole
ExpectedFilePath string
Role kops.InstanceGroupRole
ExpectedFilePath string
}{
{
EnableClusterSpec: false,
HashClusterSpec: false,
Role: "Master",
ExpectedFilePath: "tests/data/bootstrapscript_0.txt",
Role: "Master",
ExpectedFilePath: "tests/data/bootstrapscript_0.txt",
},
{
EnableClusterSpec: true,
HashClusterSpec: false,
Role: "Master",
ExpectedFilePath: "tests/data/bootstrapscript_1.txt",
},
{
EnableClusterSpec: true,
HashClusterSpec: false,
Role: "Node",
ExpectedFilePath: "tests/data/bootstrapscript_2.txt",
},
{
EnableClusterSpec: true,
HashClusterSpec: true,
Role: "Master",
ExpectedFilePath: "tests/data/bootstrapscript_3.txt",
Role: "Node",
ExpectedFilePath: "tests/data/bootstrapscript_1.txt",
},
}

for i, x := range cs {
spec := makeTestCluster(x.EnableClusterSpec, x.HashClusterSpec).Spec
spec := makeTestCluster().Spec
group := makeTestInstanceGroup(x.Role)

renderNodeUpConfig := func(ig *kops.InstanceGroup) (*nodeup.Config, error) {
Expand Down Expand Up @@ -94,13 +76,11 @@ func TestBootstrapUserData(t *testing.T) {
}
}

func makeTestCluster(enableClusterSpec bool, hashClusterSpec bool) *kops.Cluster {
func makeTestCluster() *kops.Cluster {
return &kops.Cluster{
Spec: kops.ClusterSpec{
EnableClusterSpecInUserData: enableClusterSpec,
EnableClusterSpecHash: hashClusterSpec,
CloudProvider: "aws",
KubernetesVersion: "1.7.0",
CloudProvider: "aws",
KubernetesVersion: "1.7.0",
Subnets: []kops.ClusterSubnetSpec{
{Name: "test", Zone: "eu-west-1a"},
},
Expand All @@ -111,7 +91,7 @@ func makeTestCluster(enableClusterSpec bool, hashClusterSpec bool) *kops.Cluster
Members: []*kops.EtcdMemberSpec{
{
Name: "test",
InstanceGroup: s("master-1"),
InstanceGroup: s("ig-1"),
},
},
},
Expand Down
6 changes: 3 additions & 3 deletions pkg/model/resources/nodeup.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,11 +138,11 @@ function download-release() {
echo "== nodeup node config starting =="
ensure-install-dir
{{ if .IncludeClusterSpec }}
cat > cluster_spec.yaml << __EOF_CLUSTER_SPEC
{{ .ClusterSpecContent }}
{{ ClusterSpec }}
__EOF_CLUSTER_SPEC
{{ end }}
cat > kube_env.yaml << __EOF_KUBE_ENV
{{ KubeEnv }}
__EOF_KUBE_ENV
Expand Down
22 changes: 22 additions & 0 deletions pkg/model/tests/data/bootstrapscript_0.txt
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,28 @@ function download-release() {
echo "== nodeup node config starting =="
ensure-install-dir

cat > cluster_spec.yaml << __EOF_CLUSTER_SPEC
cloudConfig:
nodeTags: something
docker:
logLevel: INFO
kubeAPIServer:
image: CoreOS
kubeControllerManager:
cloudProvider: aws
kubeProxy:
cpuRequest: 30m
featureGates:
AdvancedAuditing: "true"
kubeScheduler:
image: SomeImage
kubelet:
kubeconfigPath: /etc/kubernetes/config.txt
masterKubelet:
kubeconfigPath: /etc/kubernetes/config.cfg
__EOF_CLUSTER_SPEC

cat > kube_env.yaml << __EOF_KUBE_ENV
{}
Expand Down
8 changes: 0 additions & 8 deletions pkg/model/tests/data/bootstrapscript_1.txt
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,12 @@ cloudConfig:
nodeTags: something
docker:
logLevel: INFO
kubeAPIServer:
image: CoreOS
kubeControllerManager:
cloudProvider: aws
kubeProxy:
cpuRequest: 30m
featureGates:
AdvancedAuditing: "true"
kubeScheduler:
image: SomeImage
kubelet:
kubeconfigPath: /etc/kubernetes/config.txt
masterKubelet:
kubeconfigPath: /etc/kubernetes/config.cfg
__EOF_CLUSTER_SPEC

Expand Down
Loading

0 comments on commit c4b72db

Please sign in to comment.