Skip to content

Commit

Permalink
Merge pull request #3885 from weaveworks/move-k8s-checkpoint
Browse files Browse the repository at this point in the history
Move Kubernetes API calls into kube-utils program
  • Loading branch information
bboreham committed Jan 22, 2021
2 parents 1641751 + ad621c8 commit f9239c9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 35 deletions.
19 changes: 19 additions & 0 deletions prog/kube-utils/main.go
Expand Up @@ -314,6 +314,8 @@ func main() {
justReclaim bool
justCheck bool
justSetNodeStatus bool
justVersion bool
justUID bool
peerName string
nodeName string
logLevel string
Expand All @@ -323,6 +325,8 @@ func main() {
flag.BoolVar(&runReclaimDaemon, "run-reclaim-daemon", false, "run background process that reclaim IP space from dead peers ")
flag.BoolVar(&justCheck, "check-peer-new", false, "return success if peer name is not stored in annotation")
flag.BoolVar(&justSetNodeStatus, "set-node-status", false, "set NodeNetworkUnavailable to false")
flag.BoolVar(&justVersion, "print-k8s-version", false, "print the Kubernetes version and exit")
flag.BoolVar(&justUID, "print-uid", false, "print a UID for this installation and exit")
flag.StringVar(&peerName, "peer-name", "unknown", "name of this Weave Net peer")
flag.StringVar(&nodeName, "node-name", "unknown", "name of this Kubernetes node")
flag.StringVar(&logLevel, "log-level", "info", "logging level (debug, info, warning, error)")
Expand Down Expand Up @@ -371,6 +375,21 @@ func main() {
}
return
}
if justVersion {
k8sVersion, err := c.Discovery().ServerVersion()
if err != nil {
common.Log.Fatalf("[kube-peers] Could not get Kubernetes version: %v", err)
}
fmt.Println(k8sVersion.String())
}
if justUID {
// use UID of `weave-net` configmap as unique ID of the Kubernetes cluster
cm, err := c.CoreV1().ConfigMaps(configMapNamespace).Get(context.Background(), configMapName, api.GetOptions{})
if err != nil {
common.Log.Fatalf("Unable to fetch ConfigMap %s/%s to infer unique cluster ID", configMapNamespace, configMapName)
}
fmt.Println(cm.ObjectMeta.UID)
}
if runReclaimDaemon {
// Handle SIGINT and SIGTERM
ch := make(chan os.Signal, 1)
Expand Down
4 changes: 4 additions & 0 deletions prog/weave-kube/launch.sh
Expand Up @@ -118,6 +118,10 @@ fi
# weaver is running as the DB can only be opened by one process at a time
/usr/bin/weaveutil set-db-flag "$DB_PREFIX" peer-reclaim ok

# couple more variables
WEAVE_KUBERNETES_VERSION=$(/home/weave/kube-utils -print-k8s-version)
WEAVE_KUBERNETES_UID=$(/home/weave/kube-utils -print-uid)

post_start_actions() {
# Wait for weave process to become responsive
while true ; do
Expand Down
42 changes: 7 additions & 35 deletions prog/weaver/checkpoint.go
Expand Up @@ -10,19 +10,14 @@ import (

checkpoint "github.com/weaveworks/go-checkpoint"
weave "github.com/weaveworks/weave/router"
api "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/rest"
)

var checker *checkpoint.Checker
var newVersion atomic.Value
var success atomic.Value

const (
updateCheckPeriod = 6 * time.Hour
configMapName = "weave-net"
configMapNamespace = "kube-system"
updateCheckPeriod = 6 * time.Hour
)

func checkForUpdates(dockerVersion string, router *weave.NetworkRouter, clusterSize uint) {
Expand Down Expand Up @@ -88,36 +83,13 @@ func checkpointFlags(router *weave.NetworkRouter) []checkpoint.Flag {
return flags
}

// checkpoint Kubernetes specific details
// checkpoint Kubernetes specific details, passed in from launch script
func checkpointKubernetes(ctx context.Context, flags map[string]string, clusterSize uint) {
// checks if weaver is running in Kubernetes
host := os.Getenv("KUBERNETES_SERVICE_HOST")
if len(host) == 0 {
return
version := os.Getenv("WEAVE_KUBERNETES_VERSION")
if len(version) == 0 {
return // not running under Kubernetes
}
config, err := rest.InClusterConfig()
if err != nil {
Log.Printf("Could not get Kubernetes in-cluster config: %v", err)
return
}
c, err := kubernetes.NewForConfig(config)
if err != nil {
Log.Printf("Could not make Kubernetes client: %v", err)
return
}
k8sVersion, err := c.Discovery().ServerVersion()
if err != nil {
Log.Printf("Could not get Kubernetes version: %v", err)
return
}
flags["kubernetes-version"] = k8sVersion.String()

// use UID of `weave-net` configmap as unique ID of the Kubenerets cluster
cm, err := c.CoreV1().ConfigMaps(configMapNamespace).Get(ctx, configMapName, api.GetOptions{})
if err != nil {
Log.Printf("Unable to fetch ConfigMap %s/%s to infer unique cluster ID", configMapNamespace, configMapName)
return
}
flags["kubernetes-cluster-uid"] = string(cm.ObjectMeta.UID)
flags["kubernetes-version"] = version
flags["kubernetes-cluster-uid"] = string(os.Getenv("WEAVE_KUBERNETES_UID"))
flags["kubernetes-cluster-size"] = fmt.Sprint(clusterSize)
}

0 comments on commit f9239c9

Please sign in to comment.