Skip to content
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

add cloud-creds-secret #427

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/user/environment-variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ The installer accepts a number of environment variable that allow the interactiv

## Platform-Specific

* `AWS_PROFILE`:
sallyom marked this conversation as resolved.
Show resolved Hide resolved
The AWS profile that corresponds to value in `${HOME}/.aws/credentials`. If not provided, the default is "default".
* `OPENSHIFT_INSTALL_AWS_REGION`:
sallyom marked this conversation as resolved.
Show resolved Hide resolved
The AWS region to be used for installation.
* `OPENSHIFT_INSTALL_LIBVIRT_URI`:
Expand Down
28 changes: 28 additions & 0 deletions pkg/asset/manifests/content/tectonic/cloud-creds-secret.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package tectonic

import (
"text/template"
)

var (
// CloudCredsSecret is the constant to represent contents of corresponding yaml file
CloudCredsSecret = template.Must(template.New("cloud-creds-secret.yaml").Parse(`
---
kind: Secret
apiVersion: v1
metadata:
namespace: kube-system
{{- if .CloudCreds.AWS}}
name: aws-creds
{{- else if .CloudCreds.OpenStack}}
name: openstack-creds
{{- end}}
data:
{{- if .CloudCreds.AWS}}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add the type of the cluster (AWS, OpenStack, etc.) so that consumers know what to expect?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and while we're at it (providing AWS coordinates), the registry operator would also like to know what s3 bucket name should be used for the registry (if you don't supply a name we'll create one, but it's likely users may want to create their own bucket).

Copy link
Contributor Author

@sallyom sallyom Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dmage made it so, yes

Copy link
Contributor Author

@sallyom sallyom Oct 23, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bparees, makes sense, might not be within the scope of this PR, I'll discuss with the installer team. In a follow-up PR, we can add registry s3 bucket name to the secret if/when necessary.

aws_access_key_id: {{.CloudCreds.AWS.Base64encodeAccessKeyID}}
aws_secret_access_key: {{.CloudCreds.AWS.Base64encodeSecretAccessKey}}
{{- else if .CloudCreds.OpenStack}}
clouds.yaml: {{.CloudCreds.OpenStack.Base64encodeCloudCreds}}
{{- end}}
`))
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package tectonic

import (
"text/template"
)

var (
// RoleCloudCredsSecretReader is the variable to represent contents of corresponding file
RoleCloudCredsSecretReader = template.Must(template.New("role-cloud-creds-secret-reader.yaml").Parse(`
---
kind: Role
apiVersion: rbac.authorization.k8s.io/v1beta1
metadata:
namespace: kube-system
{{- if .CloudCreds.AWS}}
name: aws-creds-secret-reader
{{- else if .CloudCreds.OpenStack}}
name: openstack-creds-secret-reader
{{- end}}
rules:
- apiGroups: [""]
resources: ["secrets"]
{{- if .CloudCreds.AWS}}
resourceNames: ["aws-creds"]
{{- else if .CloudCreds.OpenStack}}
resourceNames: ["openstack-creds"]
{{- end}}
verbs: ["get"]
`))
)
49 changes: 49 additions & 0 deletions pkg/asset/manifests/tectonic.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package manifests

import (
"bufio"
"encoding/base64"
"io/ioutil"
"os"
"path/filepath"

"github.com/ghodss/yaml"
"github.com/pkg/errors"

"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/openshift/installer/pkg/asset"
"github.com/openshift/installer/pkg/asset/installconfig"
"github.com/openshift/installer/pkg/asset/machines"
Expand All @@ -16,6 +20,7 @@ import (

const (
tectonicManifestDir = "tectonic"
openStackCredsFile = "/etc/openstack/clouds.yaml"
)

var (
Expand Down Expand Up @@ -57,10 +62,37 @@ func (t *Tectonic) Generate(dependencies asset.Parents) error {
master := &machines.Master{}
addon := &kubeAddonOperator{}
dependencies.Get(installConfig, clusterk8sio, worker, master, addon)
var cloudCreds cloudCredsSecretData
platform := installConfig.Config.Platform.Name()
switch platform {
case "aws":
p := credentials.SharedCredentialsProvider{}
creds, err := p.Retrieve()
if err != nil {
return err
}
cloudCreds = cloudCredsSecretData{
AWS: &AwsCredsSecretData{
Base64encodeAccessKeyID: base64.StdEncoding.EncodeToString([]byte(creds.AccessKeyID)),
Base64encodeSecretAccessKey: base64.StdEncoding.EncodeToString([]byte(creds.SecretAccessKey)),
},
}
case "openstack":
credsEncoded, err := credsFileEncode(openStackCredsFile)
if err != nil {
return err
}
cloudCreds = cloudCredsSecretData{
OpenStack: &OpenStackCredsSecretData{
Base64encodeCloudCreds: credsEncoded,
},
}
}
sallyom marked this conversation as resolved.
Show resolved Hide resolved

templateData := &tectonicTemplateData{
KubeAddonOperatorImage: "quay.io/coreos/kube-addon-operator-dev:70cae49142ff69e83ed7b41fa81a585b02cdea7d",
PullSecret: base64.StdEncoding.EncodeToString([]byte(installConfig.Config.PullSecret)),
CloudCreds: cloudCreds,
}

assetData := map[string][]byte{
Expand All @@ -78,6 +110,12 @@ func (t *Tectonic) Generate(dependencies asset.Parents) error {
"99_tectonic-system-02-pull.json": applyTemplateData(content.PullTectonicSystem, templateData),
}

switch platform {
case "aws", "openstack":
assetData["99_cloud-creds-secret.yaml"] = applyTemplateData(content.CloudCredsSecret, templateData)
assetData["99_role-cloud-creds-secret-reader.yaml"] = applyTemplateData(content.RoleCloudCredsSecretReader, templateData)
}

// addon goes to openshift system
t.TectonicConfig = configMap("tectonic-system", "cluster-config-v1", genericData{
"addon-config": string(addon.Files()[0].Data),
Expand Down Expand Up @@ -136,3 +174,14 @@ func (t *Tectonic) Load(f asset.FileFetcher) (bool, error) {
t.FileList, t.TectonicConfig = fileList, tectonicConfig
return true, nil
}

// credsFileEncode returns contents of a file as base64 encoded string
func credsFileEncode(credsFile string) (string, error) {
f, _ := os.Open(credsFile)
reader := bufio.NewReader(f)
credsData, err := ioutil.ReadAll(reader)
if err != nil {
return "", err
}
return base64.StdEncoding.EncodeToString(credsData), nil
}
17 changes: 17 additions & 0 deletions pkg/asset/manifests/template.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,21 @@
package manifests

// AwsCredsSecretData holds encoded credentials and is used to generate cloud-creds secret
type AwsCredsSecretData struct {
Base64encodeAccessKeyID string
Base64encodeSecretAccessKey string
}

// OpenStackCredsSecretData holds encoded credentials and is used to generate cloud-creds secret
type OpenStackCredsSecretData struct {
Base64encodeCloudCreds string
}

type cloudCredsSecretData struct {
AWS *AwsCredsSecretData
OpenStack *OpenStackCredsSecretData
}

type bootkubeTemplateData struct {
AggregatorCaCert string
AggregatorCaKey string
Expand Down Expand Up @@ -37,4 +53,5 @@ type bootkubeTemplateData struct {
type tectonicTemplateData struct {
KubeAddonOperatorImage string
PullSecret string
CloudCreds cloudCredsSecretData
}