Skip to content

Commit

Permalink
feat: infer server if configured for localhost
Browse files Browse the repository at this point in the history
  • Loading branch information
mxyng committed Mar 17, 2022
1 parent 416239d commit 93c6fca
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 13 deletions.
2 changes: 1 addition & 1 deletion helm/charts/infra/templates/engine/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Common labels
*/}}
{{- define "engine.labels" -}}
helm.sh/chart: {{ include "engine.chart" . }}
app.infrahq.com/component: {{ .Values.engine.componentName }}
{{ include "engine.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
Expand All @@ -61,7 +62,6 @@ Selector labels
{{- define "engine.selectorLabels" -}}
app.kubernetes.io/name: {{ include "engine.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/component: {{ .Values.engine.componentName }}
{{- end }}

{{/*
Expand Down
2 changes: 1 addition & 1 deletion helm/charts/infra/templates/server/_helpers.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ Common labels
*/}}
{{- define "server.labels" -}}
helm.sh/chart: {{ include "server.chart" . }}
app.infrahq.com/component: {{ .Values.server.componentName }}
{{ include "server.selectorLabels" . }}
{{- if .Chart.AppVersion }}
app.kubernetes.io/version: {{ .Chart.AppVersion | quote }}
Expand All @@ -61,7 +62,6 @@ Selector labels
{{- define "server.selectorLabels" -}}
app.kubernetes.io/name: {{ include "server.name" . }}
app.kubernetes.io/instance: {{ .Release.Name }}
app.kubernetes.io/component: {{ .Values.server.componentName }}
{{- end }}

{{/*
Expand Down
7 changes: 3 additions & 4 deletions internal/cmd/destinations.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func newDestinationsAddCmd() *cobra.Command {
}

lifetime := time.Hour * 24 * 365
token, err := client.CreateAccessKey(&api.CreateAccessKeyRequest{
accessKey, err := client.CreateAccessKey(&api.CreateAccessKeyRequest{
MachineID: created.ID,
Name: fmt.Sprintf("access key presented by %s %s destination", args[1], args[0]),
TTL: lifetime.String(),
Expand All @@ -100,13 +100,13 @@ func newDestinationsAddCmd() *cobra.Command {
}

var sb strings.Builder
sb.WriteString(" helm install infra infrahq/infra")
sb.WriteString(" helm install infra-engine infrahq/infra")

if len(args) > 1 {
fmt.Fprintf(&sb, " --set engine.config.name=%s", args[1])
}

fmt.Fprintf(&sb, " --set engine.config.accessKey=%s", token.AccessKey)
fmt.Fprintf(&sb, " --set engine.config.accessKey=%s", accessKey.AccessKey)
fmt.Fprintf(&sb, " --set engine.config.server=%s", config.Host)

// TODO: replace me with a certificate fingerprint
Expand All @@ -121,7 +121,6 @@ func newDestinationsAddCmd() *cobra.Command {
fmt.Println()
fmt.Println(sb.String())
fmt.Println()
fmt.Println()
return nil
},
}
Expand Down
14 changes: 13 additions & 1 deletion internal/engine/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,18 @@ func Run(options Options) error {
logging.S.Errorf("server: %w", err)
}

// server is localhost which should never be the case. try to infer the actual host
if strings.HasPrefix(u.Host, "localhost") {
server, err := k8s.Service("server")
if err != nil {
logging.S.Warnf("no cluster-local infra server found for %q. check engine configurations", u.Host)
} else {
host := fmt.Sprintf("%s.%s", server.ObjectMeta.Name, server.ObjectMeta.Namespace)
logging.S.Debugf("using cluster-local infra server at %q instead of %q", host, u.Host)
u.Host = host
}
}

u.Scheme = "https"

localDetails := &localDetails{
Expand Down Expand Up @@ -548,7 +560,7 @@ func Run(options Options) error {
return fmt.Errorf("parsing host config: %w", err)
}

caCert, err := k8s.CA()
caCert, err := kubernetes.CA()
if err != nil {
return fmt.Errorf("reading CA file: %w", err)
}
Expand Down
12 changes: 6 additions & 6 deletions internal/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func NewKubernetes() (*Kubernetes, error) {

k.Config = config

namespace, err := k.Namespace()
namespace, err := Namespace()
if err != nil {
return k, err
}
Expand Down Expand Up @@ -478,7 +478,7 @@ func (k *Kubernetes) kubeControllerManagerClusterName() (string, error) {
}

func (k *Kubernetes) Name() (string, string, error) {
ca, err := k.CA()
ca, err := CA()
if err != nil {
return "", "", err
}
Expand Down Expand Up @@ -513,7 +513,7 @@ func (k *Kubernetes) Name() (string, string, error) {
return name, chksm, nil
}

func (k *Kubernetes) Namespace() (string, error) {
func Namespace() (string, error) {
contents, err := ioutil.ReadFile(namespaceFilePath)
if err != nil {
return "", err
Expand All @@ -522,7 +522,7 @@ func (k *Kubernetes) Namespace() (string, error) {
return string(contents), nil
}

func (k *Kubernetes) CA() ([]byte, error) {
func CA() ([]byte, error) {
contents, err := ioutil.ReadFile(caFilePath)
if err != nil {
return nil, err
Expand All @@ -538,13 +538,13 @@ func (k *Kubernetes) Service(component string) (*corev1.Service, error) {
return nil, err
}

namespace, err := k.Namespace()
namespace, err := Namespace()
if err != nil {
return nil, err
}

services, err := clientset.CoreV1().Services(namespace).List(context.Background(), metav1.ListOptions{
LabelSelector: fmt.Sprintf("app.kubernetes.io/component=%s,app.kubernetes.io/instance=infra", component),
LabelSelector: fmt.Sprintf("app.infrahq.com/component=%s", component),
})
if err != nil {
return nil, err
Expand Down

0 comments on commit 93c6fca

Please sign in to comment.