Merge pull request #3838 from weaveworks/go1_14_4

Update to Go 1.14.4 and modules; update other dependencies
This commit is contained in:
Bryan Boreham authored and GitHub committed 2020-08-04 18:25:40 +01:00
commit 9cf4104de7
2338 files changed
+326843 -288875

No files matched your search

+13 -10
View File
@@ -6,6 +6,7 @@ Kubernetes uses etcd to distribute and synchronise these annotations so we don't
package main
import (
"context"
"log"
"time"
@@ -38,29 +39,31 @@ const (
retryPeriod = time.Second * 2
jitterFactor = 1.0
progName = "kube-peers.weave.works"
// Prefix all our annotation keys with this string so they don't clash with anyone else's
KubePeersPrefix = "kube-peers.weave.works/"
KubePeersPrefix = progName + "/"
// KubePeersAnnotationKey is the default annotation key
KubePeersAnnotationKey = KubePeersPrefix + "peers"
)
func (cml *configMapAnnotations) Init() error {
ctx := context.Background()
for {
// Since it's potentially racy to GET, then CREATE if not found, we wrap in a check loop
// so that if the configmap is created after our GET but before or CREATE, we'll gracefully
// re-try to get the configmap.
var err error
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Get(cml.ConfigMapName, api.GetOptions{})
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Get(ctx, cml.ConfigMapName, api.GetOptions{})
if err != nil {
if !kubeErrors.IsNotFound(err) {
return errors.Wrapf(err, "Unable to fetch ConfigMap %s/%s", cml.Namespace, cml.ConfigMapName)
}
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Create(&v1.ConfigMap{
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Create(ctx, &v1.ConfigMap{
ObjectMeta: api.ObjectMeta{
Name: cml.ConfigMapName,
Namespace: cml.Namespace,
},
})
}, api.CreateOptions{FieldManager: progName})
if err != nil {
if kubeErrors.IsAlreadyExists(err) {
continue
@@ -95,29 +98,29 @@ func (cml *configMapAnnotations) GetAnnotation(key string) (string, bool) {
return value, ok
}
func (cml *configMapAnnotations) UpdateAnnotation(key, value string) (err error) {
func (cml *configMapAnnotations) UpdateAnnotation(ctx context.Context, key, value string) (err error) {
if cml.cm == nil || cml.cm.Annotations == nil {
return errors.New("endpoint not initialized, call Init first")
}
// speculatively change the state, then replace with whatever comes back
// from Update(), which will be the latest on the server, or nil if error
cml.cm.Annotations[cleanKey(key)] = value
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Update(cml.cm)
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Update(ctx, cml.cm, api.UpdateOptions{FieldManager: progName})
return err
}
func (cml *configMapAnnotations) RemoveAnnotation(key string) (err error) {
func (cml *configMapAnnotations) RemoveAnnotation(ctx context.Context, key string) (err error) {
if cml.cm == nil || cml.cm.Annotations == nil {
return errors.New("endpoint not initialized, call Init first")
}
// speculatively change the state, then replace with whatever comes back
// from Update(), which will be the latest on the server, or nil if error
delete(cml.cm.Annotations, cleanKey(key))
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Update(cml.cm)
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Update(ctx, cml.cm, api.UpdateOptions{FieldManager: progName})
return err
}
func (cml *configMapAnnotations) RemoveAnnotationsWithValue(valueToRemove string) (err error) {
func (cml *configMapAnnotations) RemoveAnnotationsWithValue(ctx context.Context, valueToRemove string) (err error) {
if cml.cm == nil || cml.cm.Annotations == nil {
return errors.New("endpoint not initialized, call Init first")
}
@@ -128,7 +131,7 @@ func (cml *configMapAnnotations) RemoveAnnotationsWithValue(valueToRemove string
delete(cml.cm.Annotations, key) // don't need to clean this key as it came from the map
}
}
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Update(cml.cm)
cml.cm, err = cml.Client.ConfigMaps(cml.Namespace).Update(ctx, cml.cm, api.UpdateOptions{FieldManager: progName})
return err
}
+8 -5
View File
@@ -6,6 +6,7 @@ This involves peer management, such as getting the latest peers or removing defu
package main
import (
"context"
"flag"
"fmt"
"math/rand"
@@ -34,7 +35,8 @@ type nodeInfo struct {
// return the IP addresses of all nodes in the cluster
func getKubePeers(c kubernetes.Interface, includeWithNoIPAddr bool) ([]nodeInfo, error) {
nodeList, err := c.CoreV1().Nodes().List(api.ListOptions{})
ctx := context.Background()
nodeList, err := c.CoreV1().Nodes().List(ctx, api.ListOptions{})
if err != nil {
return nil, err
}
@@ -155,6 +157,7 @@ func reclaimRemovedPeers(kube kubernetes.Interface, cml *configMapAnnotations, m
// Return a bool to show whether we attempted to change anything,
// and an error if something went wrong.
func reclaimPeer(weave weaveClient, cml *configMapAnnotations, peerName string, myPeerName string) (changed bool, err error) {
ctx := context.Background()
common.Log.Debugln("[kube-peers] Preparing to remove disappeared peer", peerName)
okToRemove := false
nonExistentPeer := false
@@ -187,7 +190,7 @@ func reclaimPeer(weave weaveClient, cml *configMapAnnotations, peerName string,
if !found || nonExistentPeer {
// 5. If non-existent, write an annotation with key X and contents "my identity"
common.Log.Debugln("[kube-peers] Noting I plan to remove ", peerName)
if err := cml.UpdateAnnotation(KubePeersPrefix+peerName, myPeerName); err == nil {
if err := cml.UpdateAnnotation(ctx, KubePeersPrefix+peerName, myPeerName); err == nil {
okToRemove = true
} else {
common.Log.Errorln("[kube-peers] error from UpdateAnnotation: ", err)
@@ -209,7 +212,7 @@ func reclaimPeer(weave weaveClient, cml *configMapAnnotations, peerName string,
}
err = cml.LoopUpdate(func() error {
// 7aa. Remove any annotations Z* that have contents X
if err := cml.RemoveAnnotationsWithValue(peerName); err != nil {
if err := cml.RemoveAnnotationsWithValue(ctx, peerName); err != nil {
return err
}
// 7a. Remove X from peerList
@@ -219,14 +222,14 @@ func reclaimPeer(weave weaveClient, cml *configMapAnnotations, peerName string,
}
if storedPeerList.contains(peerName) {
storedPeerList.remove(peerName)
if err := cml.UpdatePeerList(*storedPeerList); err != nil {
if err := cml.UpdatePeerList(ctx, *storedPeerList); err != nil {
return err
}
} else {
common.Log.Infof("[kube-peers] annotation was removed already for %s", peerName)
}
// 7b. Remove annotation with key X
return cml.RemoveAnnotation(KubePeersPrefix + peerName)
return cml.RemoveAnnotation(ctx, KubePeersPrefix+peerName)
})
// 8. If step 5 failed due to optimistic lock conflict, stop: someone else is handling X
+3 -2
View File
@@ -1,10 +1,11 @@
package main
import (
"context"
"encoding/json"
"fmt"
"k8s.io/api/core/v1"
v1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)
@@ -24,6 +25,6 @@ func setNodeNetworkUnavailableFalse(c kubernetes.Interface, nodeName string) err
return err
}
patch := []byte(fmt.Sprintf(`{"status":{"conditions":%s}}`, raw))
_, err = c.CoreV1().Nodes().PatchStatus(nodeName, patch)
_, err = c.CoreV1().Nodes().PatchStatus(context.Background(), nodeName, patch)
return err
}
+4 -3
View File
@@ -4,6 +4,7 @@ This module deals with operations on the peerlist backed by Kubernetes' annotati
package main
import (
"context"
"encoding/json"
"github.com/pkg/errors"
@@ -62,12 +63,12 @@ func (cml *configMapAnnotations) GetPeerList() (*peerList, error) {
return &record, nil
}
func (cml *configMapAnnotations) UpdatePeerList(list peerList) error {
func (cml *configMapAnnotations) UpdatePeerList(ctx context.Context, list peerList) error {
recordBytes, err := json.Marshal(list)
if err != nil {
return err
}
return cml.UpdateAnnotation(KubePeersAnnotationKey, string(recordBytes))
return cml.UpdateAnnotation(ctx, KubePeersAnnotationKey, string(recordBytes))
}
// update the list of all peers that have gone through this code path
@@ -81,7 +82,7 @@ func addMyselfToPeerList(cml *configMapAnnotations, c kubernetes.Interface, peer
}
if !list.contains(peerName) {
list.add(peerName, name)
err = cml.UpdatePeerList(*list)
err = cml.UpdatePeerList(context.Background(), *list)
if err != nil {
return err
}
+2 -1
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"math/rand"
"sync"
@@ -44,7 +45,7 @@ func TestPeerListBasic(t *testing.T) {
storedPeerList.remove(peerName1)
require.Equal(t, 1, len(storedPeerList.Peers))
err = cml.UpdatePeerList(*storedPeerList)
err = cml.UpdatePeerList(context.Background(), *storedPeerList)
require.NoError(t, err)
check1, err = checkIamInPeerList(cml, c, peerName1)
require.NoError(t, err)
+18 -12
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"os"
"os/signal"
"syscall"
@@ -210,6 +211,11 @@ func createBaseRules(ipt *iptables.IPTables, ips ipset.Interface) error {
return nil
}
// Indirection in case we come up with a more interesting requirement
func hCtx() context.Context {
return context.Background()
}
func root(cmd *cobra.Command, args []string) {
var npController cache.Controller
@@ -251,46 +257,46 @@ func root(cmd *cobra.Command, args []string) {
nsController := makeController(client.CoreV1().RESTClient(), "namespaces", &coreapi.Namespace{},
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
handleError(npc.AddNamespace(obj.(*coreapi.Namespace)))
handleError(npc.AddNamespace(hCtx(), obj.(*coreapi.Namespace)))
},
DeleteFunc: func(obj interface{}) {
switch obj := obj.(type) {
case *coreapi.Namespace:
handleError(npc.DeleteNamespace(obj))
handleError(npc.DeleteNamespace(hCtx(), obj))
case cache.DeletedFinalStateUnknown:
// We know this object has gone away, but its final state is no longer
// available from the API server. Instead we use the last copy of it
// that we have, which is good enough for our cleanup.
handleError(npc.DeleteNamespace(obj.Obj.(*coreapi.Namespace)))
handleError(npc.DeleteNamespace(hCtx(), obj.Obj.(*coreapi.Namespace)))
}
},
UpdateFunc: func(old, new interface{}) {
handleError(npc.UpdateNamespace(old.(*coreapi.Namespace), new.(*coreapi.Namespace)))
handleError(npc.UpdateNamespace(hCtx(), old.(*coreapi.Namespace), new.(*coreapi.Namespace)))
}})
podController := makeController(client.CoreV1().RESTClient(), "pods", &coreapi.Pod{},
cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
handleError(npc.AddPod(obj.(*coreapi.Pod)))
handleError(npc.AddPod(hCtx(), obj.(*coreapi.Pod)))
},
DeleteFunc: func(obj interface{}) {
switch obj := obj.(type) {
case *coreapi.Pod:
handleError(npc.DeletePod(obj))
handleError(npc.DeletePod(hCtx(), obj))
case cache.DeletedFinalStateUnknown:
// We know this object has gone away, but its final state is no longer
// available from the API server. Instead we use the last copy of it
// that we have, which is good enough for our cleanup.
handleError(npc.DeletePod(obj.Obj.(*coreapi.Pod)))
handleError(npc.DeletePod(hCtx(), obj.Obj.(*coreapi.Pod)))
}
},
UpdateFunc: func(old, new interface{}) {
handleError(npc.UpdatePod(old.(*coreapi.Pod), new.(*coreapi.Pod)))
handleError(npc.UpdatePod(hCtx(), old.(*coreapi.Pod), new.(*coreapi.Pod)))
}})
npHandlers := cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
handleError(npc.AddNetworkPolicy(obj))
handleError(npc.AddNetworkPolicy(hCtx(), obj))
},
DeleteFunc: func(obj interface{}) {
switch obj := obj.(type) {
@@ -298,13 +304,13 @@ func root(cmd *cobra.Command, args []string) {
// We know this object has gone away, but its final state is no longer
// available from the API server. Instead we use the last copy of it
// that we have, which is good enough for our cleanup.
handleError(npc.DeleteNetworkPolicy(obj.Obj))
handleError(npc.DeleteNetworkPolicy(hCtx(), obj.Obj))
default:
handleError(npc.DeleteNetworkPolicy(obj))
handleError(npc.DeleteNetworkPolicy(hCtx(), obj))
}
},
UpdateFunc: func(old, new interface{}) {
handleError(npc.UpdateNetworkPolicy(old, new))
handleError(npc.UpdateNetworkPolicy(hCtx(), old, new))
},
}
npController = makeController(client.NetworkingV1().RESTClient(), "networkpolicies", &networkingv1.NetworkPolicy{}, npHandlers)
+4 -3
View File
@@ -1,6 +1,7 @@
package main
import (
"context"
"fmt"
"os"
"sync/atomic"
@@ -60,7 +61,7 @@ func checkForUpdates(dockerVersion string, router *weave.NetworkRouter, clusterS
"kernel-version": kernelVersion,
}
checkpointKubernetes(flags, clusterSize)
checkpointKubernetes(context.Background(), flags, clusterSize)
// Start background version checking
params := checkpoint.CheckParams{
@@ -88,7 +89,7 @@ func checkpointFlags(router *weave.NetworkRouter) []checkpoint.Flag {
}
// checkpoint Kubernetes specific details
func checkpointKubernetes(flags map[string]string, clusterSize uint) {
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 {
@@ -112,7 +113,7 @@ func checkpointKubernetes(flags map[string]string, clusterSize uint) {
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(configMapName, api.GetOptions{})
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
+5 -3
View File
@@ -461,6 +461,8 @@ func main() {
pluginConfig.ProcPath = procPath
plugin := plugin.NewPlugin(pluginConfig)
metricsHandler := metricsHandler(router, allocator, ns, dnsserver)
// The weave script always waits for a status call to succeed,
// so there is no point in doing "weave launch --http-addr ''".
// This is here to support stand-alone use of weaver.
@@ -476,7 +478,7 @@ func main() {
router.HandleHTTP(muxRouter)
HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver, proxy, plugin, &waitReady)
HandleHTTPPeer(muxRouter, allocator, discoveryEndpoint, token, name.String())
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver))
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler)
if proxy != nil {
muxRouter.Methods("GET").Path("/proxyaddrs").HandlerFunc(proxy.StatusHTTP)
}
@@ -488,7 +490,7 @@ func main() {
if statusAddr != "" {
muxRouter := mux.NewRouter()
HandleHTTP(muxRouter, version, router, allocator, defaultSubnet, ns, dnsserver, proxy, plugin, &waitReady)
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler(router, allocator, ns, dnsserver))
muxRouter.Methods("GET").Path("/metrics").Handler(metricsHandler)
statusMux := http.NewServeMux()
statusMux.Handle("/", muxRouter)
Log.Println("Listening for status+metrics requests on", statusAddr)
@@ -497,7 +499,7 @@ func main() {
if metricsAddr != "" {
metricsMux := http.NewServeMux()
metricsMux.Handle("/metrics", metricsHandler(router, allocator, ns, dnsserver))
metricsMux.Handle("/metrics", metricsHandler)
Log.Println("Listening for metrics requests on", metricsAddr)
go listenAndServeHTTP(metricsAddr, metricsMux)
}
+2 -4
View File
@@ -2,7 +2,6 @@ package main
import (
"net/http"
"os"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promhttp"
@@ -14,10 +13,9 @@ import (
)
func metricsHandler(router *weave.NetworkRouter, allocator *ipam.Allocator, ns *nameserver.Nameserver, dnsserver *nameserver.DNSServer) http.Handler {
reg := prometheus.NewRegistry()
reg.MustRegister(prometheus.NewProcessCollector(os.Getpid(), ""))
reg := prometheus.DefaultRegisterer
reg.MustRegister(newMetrics(router, allocator, ns, dnsserver))
return promhttp.HandlerFor(reg, promhttp.HandlerOpts{})
return promhttp.HandlerFor(prometheus.DefaultGatherer, promhttp.HandlerOpts{})
}
type collector struct {