Skip to content
Merged
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
21 changes: 21 additions & 0 deletions cmd/machine-config-daemon/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@ import (
"bufio"
"flag"
"io"
"net/url"
"os"
"os/exec"
"path/filepath"
"syscall"

"github.com/google/renameio"
"k8s.io/client-go/tools/clientcmd"

"github.com/golang/glog"
"github.com/openshift/machine-config-operator/internal/clients"
Expand Down Expand Up @@ -167,6 +169,25 @@ func runStartCmd(cmd *cobra.Command, args []string) {
return
}

// Use kubelet kubeconfig file to get the URL to kube-api-server
kubeconfig, err := clientcmd.LoadFromFile("/etc/kubernetes/kubeconfig")
if err != nil {
glog.Errorf("failed to load kubelet kubeconfig: %v", err)
}
clusterName := kubeconfig.Contexts[kubeconfig.CurrentContext].Cluster
apiURL := kubeconfig.Clusters[clusterName].Server

url, err := url.Parse(apiURL)
if err != nil {
glog.Fatalf("failed to parse api url from kubelet kubeconfig: %v", err)
}

// The kubernetes in-cluster functions don't let you override the apiserver
// directly; gotta "pass" it via environment vars.
glog.Infof("overriding kubernetes api to %s", apiURL)
os.Setenv("KUBERNETES_SERVICE_HOST", url.Hostname())
Copy link
Member

Choose a reason for hiding this comment

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

One thing to be aware of is that setenv() is unsafe in the presence of threads that may be executing C code. https://internals.rust-lang.org/t/synchronized-ffi-access-to-posix-environment-variable-functions/15475 is a Rust thread on this; which links to e.g. https://sourceware.org/bugzilla/show_bug.cgi?id=15607

The Go runtime uses a middle ground trick of only calling the C setenv if cgo is in use which...I think will happen with us when linking to openssl at least.

So we should (at least eventually) fix the client API to allow overriding these things without setenv().

But...for now it's probably OK, I would think (hope) that we're not running any other active goroutines at this point.

os.Setenv("KUBERNETES_SERVICE_PORT", url.Port())

cb, err := clients.NewBuilder(startOpts.kubeconfig)
if err != nil {
glog.Fatalf("Failed to initialize ClientBuilder: %v", err)
Expand Down