mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-09-16 06:32:32 +00:00
@@ -17,7 +17,6 @@ limitations under the License.
|
||||
package master
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/cloudprovider"
|
||||
@@ -26,19 +25,6 @@ import (
|
||||
"github.com/golang/glog"
|
||||
)
|
||||
|
||||
type ipCacheEntry struct {
|
||||
ip string
|
||||
lastUpdate time.Time
|
||||
}
|
||||
|
||||
type ipCache struct {
|
||||
clock util.Clock
|
||||
cloudProvider cloudprovider.Interface
|
||||
cache map[string]ipCacheEntry
|
||||
lock sync.Mutex
|
||||
ttl time.Duration
|
||||
}
|
||||
|
||||
// NewIPCache makes a new ip caching layer, which will get IP addresses from cp,
|
||||
// and use clock for deciding when to re-get an IP address.
|
||||
// Thread-safe.
|
||||
@@ -47,30 +33,24 @@ type ipCache struct {
|
||||
// that could be produced from a template and a type via `go generate`.
|
||||
func NewIPCache(cp cloudprovider.Interface, clock util.Clock, ttl time.Duration) *ipCache {
|
||||
return &ipCache{
|
||||
clock: clock,
|
||||
cloudProvider: cp,
|
||||
cache: map[string]ipCacheEntry{},
|
||||
ttl: ttl,
|
||||
cache: util.NewTimeCache(
|
||||
clock,
|
||||
ttl,
|
||||
func(host string) util.T {
|
||||
return getInstanceIPFromCloud(cp, host)
|
||||
},
|
||||
),
|
||||
}
|
||||
}
|
||||
|
||||
type ipCache struct {
|
||||
cache util.TimeCache
|
||||
}
|
||||
|
||||
// GetInstanceIP returns the IP address of host, from the cache
|
||||
// if possible, otherwise it asks the cloud provider.
|
||||
func (c *ipCache) GetInstanceIP(host string) string {
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
data, ok := c.cache[host]
|
||||
now := c.clock.Now()
|
||||
|
||||
if !ok || now.Sub(data.lastUpdate) > c.ttl {
|
||||
ip := getInstanceIPFromCloud(c.cloudProvider, host)
|
||||
data = ipCacheEntry{
|
||||
ip: ip,
|
||||
lastUpdate: now,
|
||||
}
|
||||
c.cache[host] = data
|
||||
}
|
||||
return data.ip
|
||||
return c.cache.Get(host).(string)
|
||||
}
|
||||
|
||||
func getInstanceIPFromCloud(cloud cloudprovider.Interface, host string) string {
|
||||
|
@@ -125,6 +125,7 @@ type Master struct {
|
||||
admissionControl admission.Interface
|
||||
masterCount int
|
||||
v1beta3 bool
|
||||
nodeIPCache IPGetter
|
||||
|
||||
readOnlyServer string
|
||||
readWriteServer string
|
||||
@@ -256,6 +257,7 @@ func New(c *Config) *Master {
|
||||
authorizer: c.Authorizer,
|
||||
admissionControl: c.AdmissionControl,
|
||||
v1beta3: c.EnableV1Beta3,
|
||||
nodeIPCache: NewIPCache(c.Cloud, util.RealClock{}, 30*time.Second),
|
||||
|
||||
masterCount: c.MasterCount,
|
||||
readOnlyServer: net.JoinHostPort(c.PublicAddress, strconv.Itoa(int(c.ReadOnlyPort))),
|
||||
@@ -319,8 +321,9 @@ func logStackOnRecover(panicReason interface{}, httpWriter http.ResponseWriter)
|
||||
|
||||
func makeMinionRegistry(c *Config) minion.Registry {
|
||||
var minionRegistry minion.Registry = etcd.NewRegistry(c.EtcdHelper, nil)
|
||||
// TODO: plumb in nodeIPCache here
|
||||
if c.HealthCheckMinions {
|
||||
minionRegistry = minion.NewHealthyRegistry(minionRegistry, c.KubeletClient)
|
||||
minionRegistry = minion.NewHealthyRegistry(minionRegistry, c.KubeletClient, util.RealClock{}, 20*time.Second)
|
||||
}
|
||||
return minionRegistry
|
||||
}
|
||||
@@ -331,9 +334,8 @@ func (m *Master) init(c *Config) {
|
||||
var authenticator = c.Authenticator
|
||||
|
||||
nodeRESTStorage := minion.NewREST(m.minionRegistry)
|
||||
ipCache := NewIPCache(c.Cloud, util.RealClock{}, 30*time.Second)
|
||||
podCache := NewPodCache(
|
||||
ipCache,
|
||||
m.nodeIPCache,
|
||||
c.KubeletClient,
|
||||
RESTStorageToNodes(nodeRESTStorage).Nodes(),
|
||||
m.podRegistry,
|
||||
|
Reference in New Issue
Block a user