mirror of https://github.com/kubernetes/kops.git
Merge pull request #4982 from justinsb/aws_and_s3_region
Set AWS_REGION into bootstrapscript
This commit is contained in:
commit
079989f8d4
|
@ -180,7 +180,7 @@ func (b *Builder) Build(cluster *kops.Cluster, ig *kops.InstanceGroup) (*Data, e
|
|||
return nodeupConfig, err
|
||||
}
|
||||
|
||||
script, err := bootstrapScript.ResourceNodeUp(ig, &cluster.Spec)
|
||||
script, err := bootstrapScript.ResourceNodeUp(ig, cluster)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error building bootstrap script: %v", err)
|
||||
}
|
||||
|
|
|
@ -114,7 +114,7 @@ func (b *AutoscalingGroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
|
|||
return err
|
||||
}
|
||||
|
||||
if t.UserData, err = b.BootstrapScript.ResourceNodeUp(ig, &b.Cluster.Spec); err != nil {
|
||||
if t.UserData, err = b.BootstrapScript.ResourceNodeUp(ig, b.Cluster); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
|
|
@ -27,11 +27,13 @@ import (
|
|||
"text/template"
|
||||
|
||||
"github.com/ghodss/yaml"
|
||||
"github.com/golang/glog"
|
||||
|
||||
"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/upup/pkg/fi/cloudup/awsup"
|
||||
)
|
||||
|
||||
// BootstrapScript creates the bootstrap script
|
||||
|
@ -56,9 +58,40 @@ func (b *BootstrapScript) KubeEnv(ig *kops.InstanceGroup) (string, error) {
|
|||
return string(data), nil
|
||||
}
|
||||
|
||||
func (b *BootstrapScript) buildEnvironmentVariables(cluster *kops.Cluster) (map[string]string, error) {
|
||||
env := make(map[string]string)
|
||||
if os.Getenv("S3_ENDPOINT") != "" {
|
||||
env["S3_ENDPOINT"] = os.Getenv("S3_ENDPOINT")
|
||||
env["S3_REGION"] = os.Getenv("S3_REGION")
|
||||
env["S3_ACCESS_KEY_ID"] = os.Getenv("S3_ACCESS_KEY_ID")
|
||||
env["S3_SECRET_ACCESS_KEY"] = os.Getenv("S3_SECRET_ACCESS_KEY")
|
||||
}
|
||||
|
||||
if kops.CloudProviderID(cluster.Spec.CloudProvider) == kops.CloudProviderDO {
|
||||
doToken := os.Getenv("DIGITALOCEAN_ACCESS_TOKEN")
|
||||
if doToken != "" {
|
||||
env["DIGITALOCEAN_ACCESS_TOKEN"] = doToken
|
||||
}
|
||||
}
|
||||
|
||||
if kops.CloudProviderID(cluster.Spec.CloudProvider) == kops.CloudProviderAWS {
|
||||
region, err := awsup.FindRegion(cluster)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if region == "" {
|
||||
glog.Warningf("unable to determine cluster region")
|
||||
} else {
|
||||
env["AWS_REGION"] = region
|
||||
}
|
||||
}
|
||||
|
||||
return env, nil
|
||||
}
|
||||
|
||||
// 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) {
|
||||
func (b *BootstrapScript) ResourceNodeUp(ig *kops.InstanceGroup, cluster *kops.Cluster) (*fi.ResourceHolder, error) {
|
||||
// Bastions can have AdditionalUserData, but if there isn't any skip this part
|
||||
if ig.IsBastion() && len(ig.Spec.AdditionalUserData) == 0 {
|
||||
return nil, nil
|
||||
|
@ -75,44 +108,25 @@ func (b *BootstrapScript) ResourceNodeUp(ig *kops.InstanceGroup, cs *kops.Cluste
|
|||
return b.KubeEnv(ig)
|
||||
},
|
||||
|
||||
// Pass in extra environment variables for user-defined S3 service
|
||||
"S3Env": func() string {
|
||||
if os.Getenv("S3_ENDPOINT") != "" {
|
||||
return fmt.Sprintf("export S3_ENDPOINT=%s\nexport S3_REGION=%s\nexport S3_ACCESS_KEY_ID=%s\nexport S3_SECRET_ACCESS_KEY=%s\n",
|
||||
os.Getenv("S3_ENDPOINT"),
|
||||
os.Getenv("S3_REGION"),
|
||||
os.Getenv("S3_ACCESS_KEY_ID"),
|
||||
os.Getenv("S3_SECRET_ACCESS_KEY"))
|
||||
"EnvironmentVariables": func() (string, error) {
|
||||
env, err := b.buildEnvironmentVariables(cluster)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return ""
|
||||
},
|
||||
|
||||
"DO_ENV": func() string {
|
||||
if kops.CloudProviderID(cs.CloudProvider) != kops.CloudProviderDO {
|
||||
return ""
|
||||
var b bytes.Buffer
|
||||
for k, v := range env {
|
||||
b.WriteString(fmt.Sprintf("export %s=%s\n", k, v))
|
||||
}
|
||||
|
||||
doToken := os.Getenv("DIGITALOCEAN_ACCESS_TOKEN")
|
||||
if doToken != "" {
|
||||
return fmt.Sprintf("export DIGITALOCEAN_ACCESS_TOKEN=%s\n", doToken)
|
||||
}
|
||||
|
||||
return ""
|
||||
|
||||
return b.String(), nil
|
||||
},
|
||||
|
||||
"ProxyEnv": func() string {
|
||||
return b.createProxyEnv(cs.EgressProxy)
|
||||
},
|
||||
"AWS_REGION": func() string {
|
||||
if os.Getenv("AWS_REGION") != "" {
|
||||
return fmt.Sprintf("export AWS_REGION=%s\n",
|
||||
os.Getenv("AWS_REGION"))
|
||||
}
|
||||
return ""
|
||||
return b.createProxyEnv(cluster.Spec.EgressProxy)
|
||||
},
|
||||
|
||||
"ClusterSpec": func() (string, error) {
|
||||
cs := cluster.Spec
|
||||
|
||||
spec := make(map[string]interface{})
|
||||
spec["cloudConfig"] = cs.CloudConfig
|
||||
spec["docker"] = cs.Docker
|
||||
|
|
|
@ -111,7 +111,7 @@ func TestBootstrapUserData(t *testing.T) {
|
|||
}
|
||||
|
||||
for i, x := range cs {
|
||||
spec := makeTestCluster(x.HookSpecRoles, x.FileAssetSpecRoles).Spec
|
||||
cluster := makeTestCluster(x.HookSpecRoles, x.FileAssetSpecRoles)
|
||||
group := makeTestInstanceGroup(x.Role, x.HookSpecRoles, x.FileAssetSpecRoles)
|
||||
|
||||
renderNodeUpConfig := func(ig *kops.InstanceGroup) (*nodeup.Config, error) {
|
||||
|
@ -125,12 +125,12 @@ func TestBootstrapUserData(t *testing.T) {
|
|||
}
|
||||
|
||||
// Purposely running this twice to cover issue #3516
|
||||
_, err := bs.ResourceNodeUp(group, &spec)
|
||||
_, err := bs.ResourceNodeUp(group, cluster)
|
||||
if err != nil {
|
||||
t.Errorf("case %d failed to create nodeup resource. error: %s", i, err)
|
||||
continue
|
||||
}
|
||||
res, err := bs.ResourceNodeUp(group, &spec)
|
||||
res, err := bs.ResourceNodeUp(group, cluster)
|
||||
if err != nil {
|
||||
t.Errorf("case %d failed to create nodeup resource. error: %s", i, err)
|
||||
continue
|
||||
|
|
|
@ -63,7 +63,7 @@ func (d *DropletBuilder) Build(c *fi.ModelBuilderContext) error {
|
|||
droplet.SSHKey = fi.String(sshKeyFingerPrint)
|
||||
droplet.Tags = []string{clusterTag}
|
||||
|
||||
userData, err := d.BootstrapScript.ResourceNodeUp(ig, &d.Cluster.Spec)
|
||||
userData, err := d.BootstrapScript.ResourceNodeUp(ig, d.Cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -43,7 +43,7 @@ func (b *AutoscalingGroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
|
|||
for _, ig := range b.InstanceGroups {
|
||||
name := b.SafeObjectName(ig.ObjectMeta.Name)
|
||||
|
||||
startupScript, err := b.BootstrapScript.ResourceNodeUp(ig, &b.Cluster.Spec)
|
||||
startupScript, err := b.BootstrapScript.ResourceNodeUp(ig, b.Cluster)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -48,10 +48,7 @@ set -o pipefail
|
|||
NODEUP_URL={{ NodeUpSource }}
|
||||
NODEUP_HASH={{ NodeUpSourceHash }}
|
||||
|
||||
{{ S3Env }}
|
||||
{{ AWS_REGION }}
|
||||
|
||||
{{ DO_ENV }}
|
||||
{{ EnvironmentVariables }}
|
||||
|
||||
{{ ProxyEnv }}
|
||||
|
||||
|
|
|
@ -20,9 +20,7 @@ set -o pipefail
|
|||
NODEUP_URL=NUSource
|
||||
NODEUP_HASH=NUSHash
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=eu-west-1
|
||||
|
||||
|
||||
echo "http_proxy=http://example.com:80" >> /etc/environment
|
||||
|
|
|
@ -20,9 +20,7 @@ set -o pipefail
|
|||
NODEUP_URL=NUSource
|
||||
NODEUP_HASH=NUSHash
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=eu-west-1
|
||||
|
||||
|
||||
echo "http_proxy=http://example.com:80" >> /etc/environment
|
||||
|
|
|
@ -20,9 +20,7 @@ set -o pipefail
|
|||
NODEUP_URL=NUSource
|
||||
NODEUP_HASH=NUSHash
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=eu-west-1
|
||||
|
||||
|
||||
echo "http_proxy=http://example.com:80" >> /etc/environment
|
||||
|
|
|
@ -20,9 +20,7 @@ set -o pipefail
|
|||
NODEUP_URL=NUSource
|
||||
NODEUP_HASH=NUSHash
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=eu-west-1
|
||||
|
||||
|
||||
echo "http_proxy=http://example.com:80" >> /etc/environment
|
||||
|
|
|
@ -20,9 +20,7 @@ set -o pipefail
|
|||
NODEUP_URL=NUSource
|
||||
NODEUP_HASH=NUSHash
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=eu-west-1
|
||||
|
||||
|
||||
echo "http_proxy=http://example.com:80" >> /etc/environment
|
||||
|
|
|
@ -20,9 +20,7 @@ set -o pipefail
|
|||
NODEUP_URL=NUSource
|
||||
NODEUP_HASH=NUSHash
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=eu-west-1
|
||||
|
||||
|
||||
echo "http_proxy=http://example.com:80" >> /etc/environment
|
||||
|
|
|
@ -62,7 +62,7 @@ func (b *AutoscalingGroupModelBuilder) Build(c *fi.ModelBuilderContext) error {
|
|||
VM: createVmTask,
|
||||
IG: ig,
|
||||
BootstrapScript: b.BootstrapScript,
|
||||
Spec: &b.Cluster.Spec,
|
||||
Cluster: b.Cluster,
|
||||
}
|
||||
|
||||
c.AddTask(attachISOTask)
|
||||
|
|
|
@ -30,9 +30,7 @@ Resources.AWSAutoScalingLaunchConfigurationmasterustest1amastersadditionaluserda
|
|||
NODEUP_URL=https://kubeupv2.s3.amazonaws.com/kops/1.8.1/linux/amd64/nodeup
|
||||
NODEUP_HASH=bb41724c37d15ab7e039e06230e742b9b38d0808
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=us-test-1
|
||||
|
||||
|
||||
|
||||
|
@ -330,9 +328,7 @@ Resources.AWSAutoScalingLaunchConfigurationnodesadditionaluserdataexamplecom.Pro
|
|||
NODEUP_URL=https://kubeupv2.s3.amazonaws.com/kops/1.8.1/linux/amd64/nodeup
|
||||
NODEUP_HASH=bb41724c37d15ab7e039e06230e742b9b38d0808
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=us-test-1
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -21,9 +21,7 @@ Resources.AWSAutoScalingLaunchConfigurationmasterustest1amastersminimalexampleco
|
|||
NODEUP_URL=https://kubeupv2.s3.amazonaws.com/kops/1.8.1/linux/amd64/nodeup
|
||||
NODEUP_HASH=bb41724c37d15ab7e039e06230e742b9b38d0808
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=us-test-1
|
||||
|
||||
|
||||
|
||||
|
@ -300,9 +298,7 @@ Resources.AWSAutoScalingLaunchConfigurationnodesminimalexamplecom.Properties.Use
|
|||
NODEUP_URL=https://kubeupv2.s3.amazonaws.com/kops/1.8.1/linux/amd64/nodeup
|
||||
NODEUP_HASH=bb41724c37d15ab7e039e06230e742b9b38d0808
|
||||
|
||||
|
||||
|
||||
|
||||
export AWS_REGION=us-test-1
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -46,7 +46,7 @@ type AttachISO struct {
|
|||
VM *VirtualMachine
|
||||
IG *kops.InstanceGroup
|
||||
BootstrapScript *model.BootstrapScript
|
||||
Spec *kops.ClusterSpec
|
||||
Cluster *kops.Cluster
|
||||
}
|
||||
|
||||
var _ fi.HasName = &AttachISO{}
|
||||
|
@ -93,7 +93,7 @@ func (_ *AttachISO) CheckChanges(a, e, changes *AttachISO) error {
|
|||
|
||||
// RenderVSphere executes the actual task logic, for vSphere cloud.
|
||||
func (_ *AttachISO) RenderVSphere(t *vsphere.VSphereAPITarget, a, e, changes *AttachISO) error {
|
||||
startupScript, err := changes.BootstrapScript.ResourceNodeUp(changes.IG, changes.Spec)
|
||||
startupScript, err := e.BootstrapScript.ResourceNodeUp(e.IG, e.Cluster)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error on resource nodeup: %v", err)
|
||||
}
|
||||
|
@ -101,7 +101,7 @@ func (_ *AttachISO) RenderVSphere(t *vsphere.VSphereAPITarget, a, e, changes *At
|
|||
if err != nil {
|
||||
return fmt.Errorf("error rendering startup script: %v", err)
|
||||
}
|
||||
dir, err := ioutil.TempDir("", *changes.VM.Name)
|
||||
dir, err := ioutil.TempDir("", *e.VM.Name)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating tempdir: %v", err)
|
||||
}
|
||||
|
@ -109,18 +109,18 @@ func (_ *AttachISO) RenderVSphere(t *vsphere.VSphereAPITarget, a, e, changes *At
|
|||
defer os.RemoveAll(dir)
|
||||
|
||||
// Need this in cloud config file for vSphere CloudProvider
|
||||
vmUUID, err := t.Cloud.FindVMUUID(changes.VM.Name)
|
||||
vmUUID, err := t.Cloud.FindVMUUID(e.VM.Name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
isoFile, err := createISO(changes, startupStr, dir, t.Cloud.CoreDNSServer, vmUUID)
|
||||
isoFile, err := createISO(e, startupStr, dir, t.Cloud.CoreDNSServer, vmUUID)
|
||||
if err != nil {
|
||||
glog.Errorf("Failed to createISO for vspheretasks, err: %v", err)
|
||||
return err
|
||||
}
|
||||
|
||||
err = t.Cloud.UploadAndAttachISO(changes.VM.Name, isoFile)
|
||||
err = t.Cloud.UploadAndAttachISO(e.VM.Name, isoFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -128,7 +128,7 @@ func (_ *AttachISO) RenderVSphere(t *vsphere.VSphereAPITarget, a, e, changes *At
|
|||
return nil
|
||||
}
|
||||
|
||||
func createUserData(changes *AttachISO, startupStr string, dir string, dnsServer string, vmUUID string) error {
|
||||
func createUserData(e *AttachISO, startupStr string, dir string, dnsServer string, vmUUID string) error {
|
||||
|
||||
// Populate nodeup initialization script.
|
||||
|
||||
|
@ -163,7 +163,7 @@ func createUserData(changes *AttachISO, startupStr string, dir string, dnsServer
|
|||
data = strings.Replace(data, "$VM_UUID", vmUUIDStr, -1)
|
||||
|
||||
// Populate volume metadata.
|
||||
data, err = createVolumeScript(changes, data)
|
||||
data, err = createVolumeScript(e, data)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
@ -179,12 +179,12 @@ func createUserData(changes *AttachISO, startupStr string, dir string, dnsServer
|
|||
return nil
|
||||
}
|
||||
|
||||
func createVolumeScript(changes *AttachISO, data string) (string, error) {
|
||||
if changes.IG.Spec.Role != kops.InstanceGroupRoleMaster {
|
||||
return strings.Replace(data, "$VOLUME_SCRIPT", " No volume metadata needed for "+string(changes.IG.Spec.Role)+".", -1), nil
|
||||
func createVolumeScript(e *AttachISO, data string) (string, error) {
|
||||
if e.IG.Spec.Role != kops.InstanceGroupRoleMaster {
|
||||
return strings.Replace(data, "$VOLUME_SCRIPT", " No volume metadata needed for "+string(e.IG.Spec.Role)+".", -1), nil
|
||||
}
|
||||
|
||||
volsString, err := getVolMetadata(changes)
|
||||
volsString, err := getVolMetadata(e)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
|
@ -193,11 +193,11 @@ func createVolumeScript(changes *AttachISO, data string) (string, error) {
|
|||
return strings.Replace(data, "$VOLUME_SCRIPT", " "+volsString, -1), nil
|
||||
}
|
||||
|
||||
func getVolMetadata(changes *AttachISO) (string, error) {
|
||||
func getVolMetadata(e *AttachISO) (string, error) {
|
||||
var volsMetadata []vsphere.VolumeMetadata
|
||||
|
||||
// Creating vsphere.VolumeMetadata using clusters EtcdClusterSpec
|
||||
for i, etcd := range changes.Spec.EtcdClusters {
|
||||
for i, etcd := range e.Cluster.Spec.EtcdClusters {
|
||||
volMetadata := vsphere.VolumeMetadata{}
|
||||
volMetadata.EtcdClusterName = etcd.Name
|
||||
volMetadata.VolumeId = vsphere.GetVolumeId(i + 1)
|
||||
|
@ -205,7 +205,7 @@ func getVolMetadata(changes *AttachISO) (string, error) {
|
|||
var members []vsphere.EtcdMemberSpec
|
||||
var thisNode string
|
||||
for _, member := range etcd.Members {
|
||||
if *member.InstanceGroup == changes.IG.Name {
|
||||
if *member.InstanceGroup == e.IG.Name {
|
||||
thisNode = member.Name
|
||||
}
|
||||
etcdMember := vsphere.EtcdMemberSpec{
|
||||
|
@ -216,7 +216,7 @@ func getVolMetadata(changes *AttachISO) (string, error) {
|
|||
}
|
||||
|
||||
if thisNode == "" {
|
||||
return "", fmt.Errorf("Failed to construct volume metadata for %v InstanceGroup.", changes.IG.Name)
|
||||
return "", fmt.Errorf("Failed to construct volume metadata for %v InstanceGroup.", e.IG.Name)
|
||||
}
|
||||
|
||||
volMetadata.EtcdNodeName = thisNode
|
||||
|
@ -247,18 +247,18 @@ func createMetaData(dir string, vmName string) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func createISO(changes *AttachISO, startupStr string, dir string, dnsServer, vmUUID string) (string, error) {
|
||||
err := createUserData(changes, startupStr, dir, dnsServer, vmUUID)
|
||||
func createISO(e *AttachISO, startupStr string, dir string, dnsServer, vmUUID string) (string, error) {
|
||||
err := createUserData(e, startupStr, dir, dnsServer, vmUUID)
|
||||
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
err = createMetaData(dir, *changes.VM.Name)
|
||||
err = createMetaData(dir, *e.VM.Name)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
isoFile := filepath.Join(dir, *changes.VM.Name+".iso")
|
||||
isoFile := filepath.Join(dir, *e.VM.Name+".iso")
|
||||
var commandName string
|
||||
|
||||
switch os := runtime.GOOS; os {
|
||||
|
|
Loading…
Reference in New Issue