Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Move Kubernetes API calls into kube-utils program #3885

Merged
merged 1 commit into from
Jan 22, 2021
Merged
Show file tree
Hide file tree
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
19 changes: 19 additions & 0 deletions prog/kube-utils/main.go
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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)
}