Skip to content

Commit

Permalink
Merge pull request #1143 from upmc-enterprises/registryCredsPrompt
Browse files Browse the repository at this point in the history
Allow for addons to prompt for data input
  • Loading branch information
aaron-prindle authored Feb 27, 2017
2 parents 3db1a4d + 50cb1f2 commit 9b539db
Show file tree
Hide file tree
Showing 4 changed files with 257 additions and 12 deletions.
92 changes: 92 additions & 0 deletions cmd/minikube/cmd/config/prompt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
/*
Copyright 2016 The Kubernetes Authors All rights reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package config

import (
"bufio"
"fmt"
"log"
"os"
"strings"
)

// AskForYesNoConfirmation asks the user for confirmation. A user must type in "yes" or "no" and
// then press enter. It has fuzzy matching, so "y", "Y", "yes", "YES", and "Yes" all count as
// confirmations. If the input is not recognized, it will ask again. The function does not return
// until it gets a valid response from the user.
func AskForYesNoConfirmation(s string, posResponses, negResponses []string) bool {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Printf("%s [y/n]: ", s)

response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}

response = strings.ToLower(strings.TrimSpace(response))

if containsString(posResponses, response) {
return true
} else if containsString(negResponses, response) {
return false
} else {
fmt.Println("Please type yes or no:")
return AskForYesNoConfirmation(s, posResponses, negResponses)
}
}
}

// AskForStaticValue asks for a single value to enter
func AskForStaticValue(s string) string {
reader := bufio.NewReader(os.Stdin)

for {
fmt.Printf("%s", s)

response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}

response = strings.TrimSpace(response)

// Can't have zero length
if len(response) == 0 {
fmt.Println("--Error, please enter a value:")
return AskForStaticValue(s)
}
return response
}
}

// posString returns the first index of element in slice.
// If slice does not contain element, returns -1.
func posString(slice []string, element string) int {
for index, elem := range slice {
if elem == element {
return index
}
}
return -1
}

// containsString returns true iff slice contains element
func containsString(slice []string, element string) bool {
return !(posString(slice, element) == -1)
}
84 changes: 84 additions & 0 deletions cmd/minikube/cmd/config/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package config

import (
"fmt"
"io/ioutil"
"os"
"strconv"

Expand All @@ -28,6 +29,7 @@ import (
"k8s.io/minikube/pkg/minikube/cluster"
"k8s.io/minikube/pkg/minikube/config"
"k8s.io/minikube/pkg/minikube/machine"
"k8s.io/minikube/pkg/minikube/service"
"k8s.io/minikube/pkg/minikube/sshutil"
)

Expand Down Expand Up @@ -88,11 +90,93 @@ func GetClientType() machine.ClientType {
}

func EnableOrDisableAddon(name string, val string) error {

enable, err := strconv.ParseBool(val)
if err != nil {
errors.Wrapf(err, "error attempted to parse enabled/disable value addon %s", name)
}

// allows for additional prompting of information when enabling addons
if enable {
switch name {
case "registry-creds":
posResponses := []string{"yes", "y"}
negResponses := []string{"no", "n"}

// Default values
awsAccessID := "changeme"
awsAccessKey := "changeme"
awsRegion := "changeme"
awsAccount := "changeme"
gcrApplicationDefaultCredentials := "changeme"

enableAWSECR := AskForYesNoConfirmation("\nDo you want to enable AWS Elastic Container Registry?", posResponses, negResponses)
if enableAWSECR {
awsAccessID = AskForStaticValue("-- Enter AWS Access Key ID: ")
awsAccessKey = AskForStaticValue("-- Enter AWS Secret Access Key: ")
awsRegion = AskForStaticValue("-- Enter AWS Region: ")
awsAccount = AskForStaticValue("-- Enter 12 digit AWS Account ID: ")
}

enableGCR := AskForYesNoConfirmation("\nDo you want to enable Google Container Registry?", posResponses, negResponses)
if enableGCR {
gcrPath := AskForStaticValue("-- Enter path to credentials (e.g. /home/user/.config/gcloud/application_default_credentials.json):")

// Read file from disk
dat, err := ioutil.ReadFile(gcrPath)

if err != nil {
fmt.Println("Could not read file for application_default_credentials.json")
} else {
gcrApplicationDefaultCredentials = string(dat)
}
}

// Create ECR Secret
err = service.CreateSecret(
"kube-system",
"registry-creds-ecr",
map[string]string{
"AWS_ACCESS_KEY_ID": awsAccessID,
"AWS_SECRET_ACCESS_KEY": awsAccessKey,
"aws-account": awsAccount,
"aws-region": awsRegion,
},
map[string]string{
"app": "registry-creds",
"cloud": "ecr",
"kubernetes.io/minikube-addons": "registry-creds",
})

if err != nil {
fmt.Println("ERROR creating `registry-creds-ecr` secret")
}

// Create GCR Secret
err = service.CreateSecret(
"kube-system",
"registry-creds-gcr",
map[string]string{
"application_default_credentials.json": gcrApplicationDefaultCredentials,
},
map[string]string{
"app": "registry-creds",
"cloud": "gcr",
"kubernetes.io/minikube-addons": "registry-creds",
})

if err != nil {
fmt.Println("ERROR creating `registry-creds-gcr` secret")
}

break
}
} else {
// Cleanup existing secrets
service.DeleteSecret("kube-system", "registry-creds-ecr")
service.DeleteSecret("kube-system", "registry-creds-gcr")
}

//TODO(r2d4): config package should not reference API, pull this out
api, err := machine.NewAPIClient(GetClientType())
if err != nil {
Expand Down
27 changes: 15 additions & 12 deletions deploy/addons/registry-creds/registry-creds-rc.yaml
Original file line number Diff line number Diff line change
@@ -1,52 +1,55 @@

apiVersion: v1
kind: ReplicationController
metadata:
name: registry-creds
namespace: kube-system
labels:
app: registry-creds
version: v1.5
version: v1.6
kubernetes.io/cluster-service: "true"
kubernetes.io/minikube-addons: registry-creds
spec:
replicas: 1
selector:
app: registry-creds
version: v1.5
name: registry-creds
version: v1.6
kubernetes.io/cluster-service: "true"
template:
metadata:
labels:
app: registry-creds
version: v1.5
name: registry-creds
version: v1.6
kubernetes.io/cluster-service: "true"
spec:
containers:
- image: upmcenterprises/registry-creds:1.5
- image: upmcenterprises/registry-creds:1.6
name: registry-creds
imagePullPolicy: Always
env:
- name: AWS_ACCESS_KEY_ID
valueFrom:
secretKeyRef:
name: registry-creds
name: registry-creds-ecr
key: AWS_ACCESS_KEY_ID
- name: AWS_SECRET_ACCESS_KEY
valueFrom:
secretKeyRef:
name: registry-creds
name: registry-creds-ecr
key: AWS_SECRET_ACCESS_KEY
- name: awsaccount
valueFrom:
secretKeyRef:
name: registry-creds
name: registry-creds-ecr
key: aws-account
- name: awsregion
valueFrom:
secretKeyRef:
name: registry-creds-ecr
key: aws-region
volumeMounts:
- name: gcr-creds
mountPath: "/root/.config/gcloud"
readOnly: true
volumes:
- name: gcr-creds
secret:
secretName: gcr-secret
secretName: registry-creds-gcr
66 changes: 66 additions & 0 deletions pkg/minikube/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,3 +261,69 @@ func getServiceListFromServicesByLabel(services corev1.ServiceInterface, key str

return serviceList, nil
}

// CreateSecret creates or modifies secrets
func CreateSecret(namespace, name string, dataValues map[string]string, labels map[string]string) error {
client, err := k8s.GetCoreClient()
if err != nil {
return &util.RetriableError{Err: err}
}
secrets := client.Secrets(namespace)
if err != nil {
return &util.RetriableError{Err: err}
}

secret, _ := secrets.Get(name)

// Delete existing secret
if len(secret.Name) > 0 {
err = DeleteSecret(namespace, name)
if err != nil {
return &util.RetriableError{Err: err}
}
}

// convert strings to data secrets
data := map[string][]byte{}
for key, value := range dataValues {
data[key] = []byte(value)
}

// Create Secret
secretObj := &v1.Secret{
ObjectMeta: v1.ObjectMeta{
Name: name,
Labels: labels,
},
Data: data,
Type: v1.SecretTypeOpaque,
}

_, err = secrets.Create(secretObj)
if err != nil {
fmt.Println("err: ", err)
return &util.RetriableError{Err: err}
}

return nil
}

// DeleteSecret deletes a secret from a namespace
func DeleteSecret(namespace, name string) error {
client, err := k8s.GetCoreClient()
if err != nil {
return &util.RetriableError{Err: err}
}

secrets := client.Secrets(namespace)
if err != nil {
return &util.RetriableError{Err: err}
}

err = secrets.Delete(name, &kubeapi.DeleteOptions{})
if err != nil {
return &util.RetriableError{Err: err}
}

return nil
}

0 comments on commit 9b539db

Please sign in to comment.