-
Notifications
You must be signed in to change notification settings - Fork 243
/
secret.go
58 lines (50 loc) · 2.03 KB
/
secret.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package secret
import (
"fmt"
"strings"
"github.com/openshift/odo/pkg/occlient"
corev1 "k8s.io/api/core/v1"
)
import (
applabels "github.com/openshift/odo/pkg/application/labels"
componentlabels "github.com/openshift/odo/pkg/component/labels"
)
// DetermineSecretName resolves the name of the secret that corresponds to the supplied component name and port
func DetermineSecretName(client *occlient.Client, componentName, applicationName, port string) (string, error) {
labelSelector := fmt.Sprintf("%v=%v", applabels.ApplicationLabel, applicationName) +
fmt.Sprintf(",%v=%v", componentlabels.ComponentLabel, componentName)
secrets, err := client.ListSecrets(labelSelector)
if err != nil {
return "", err
}
if len(secrets) == 0 {
return "", fmt.Errorf(`A secret should have been created for component %s.
Please delete the component and recreate it using 'odo create'`, componentName)
}
// when the port is not supplied, then we either select the only one exposed is so is the case,
// or when there multiple ports exposed, we fail
if len(port) == 0 {
if len(secrets) == 1 {
return secrets[0].Name, nil
}
return "", fmt.Errorf("Unable to properly link to component %s. "+
"Please select one of the following ports: '%s' "+
"by supplying the --port option and rerun the command", componentName, strings.Join(availablePorts(secrets), ","))
}
// search each secret to see which port is corresponds to
for _, secret := range secrets {
if secret.Annotations[occlient.ComponentPortAnnotationName] == port {
return secret.Name, nil
}
}
return "", fmt.Errorf("Unable to properly link to component %s using port %s. "+
"Please select one of the following ports: '%s' "+
"by supplying the --port option and rerun the command", componentName, port, strings.Join(availablePorts(secrets), ","))
}
func availablePorts(secrets []corev1.Secret) []string {
ports := make([]string, 0, len(secrets))
for _, secret := range secrets {
ports = append(ports, secret.Annotations[occlient.ComponentPortAnnotationName])
}
return ports
}