mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-24 20:24:09 +00:00
Optimize secret manager to refresh secrets from apiserver cache
This commit is contained in:
parent
82a5cab1ce
commit
220cfdff91
@ -32,6 +32,7 @@ go_library(
|
|||||||
deps = [
|
deps = [
|
||||||
"//pkg/api/v1:go_default_library",
|
"//pkg/api/v1:go_default_library",
|
||||||
"//pkg/client/clientset_generated/clientset:go_default_library",
|
"//pkg/client/clientset_generated/clientset:go_default_library",
|
||||||
|
"//pkg/kubelet/util:go_default_library",
|
||||||
"//pkg/storage/etcd:go_default_library",
|
"//pkg/storage/etcd:go_default_library",
|
||||||
"//vendor:k8s.io/apimachinery/pkg/api/errors",
|
"//vendor:k8s.io/apimachinery/pkg/api/errors",
|
||||||
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
|
"//vendor:k8s.io/apimachinery/pkg/apis/meta/v1",
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
|
|
||||||
"k8s.io/kubernetes/pkg/api/v1"
|
"k8s.io/kubernetes/pkg/api/v1"
|
||||||
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
clientset "k8s.io/kubernetes/pkg/client/clientset_generated/clientset"
|
||||||
|
"k8s.io/kubernetes/pkg/kubelet/util"
|
||||||
storageetcd "k8s.io/kubernetes/pkg/storage/etcd"
|
storageetcd "k8s.io/kubernetes/pkg/storage/etcd"
|
||||||
|
|
||||||
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
apierrors "k8s.io/apimachinery/pkg/api/errors"
|
||||||
@ -169,7 +170,14 @@ func (s *secretStore) Get(namespace, name string) (*v1.Secret, error) {
|
|||||||
data.Lock()
|
data.Lock()
|
||||||
defer data.Unlock()
|
defer data.Unlock()
|
||||||
if data.err != nil || !s.clock.Now().Before(data.lastUpdateTime.Add(s.ttl)) {
|
if data.err != nil || !s.clock.Now().Before(data.lastUpdateTime.Add(s.ttl)) {
|
||||||
secret, err := s.kubeClient.Core().Secrets(namespace).Get(name, metav1.GetOptions{})
|
opts := metav1.GetOptions{}
|
||||||
|
if data.secret != nil && data.err == nil {
|
||||||
|
// This is just a periodic refresh of a secret we successfully fetched previously.
|
||||||
|
// In this case, server data from apiserver cache to reduce the load on both
|
||||||
|
// etcd and apiserver (the cache is eventually consistent).
|
||||||
|
util.FromApiserverCache(&opts)
|
||||||
|
}
|
||||||
|
secret, err := s.kubeClient.Core().Secrets(namespace).Get(name, opts)
|
||||||
// Update state, unless we got error different than "not-found".
|
// Update state, unless we got error different than "not-found".
|
||||||
if err == nil || apierrors.IsNotFound(err) {
|
if err == nil || apierrors.IsNotFound(err) {
|
||||||
// Ignore the update to the older version of a secret.
|
// Ignore the update to the older version of a secret.
|
||||||
|
@ -9,8 +9,12 @@ load(
|
|||||||
|
|
||||||
go_library(
|
go_library(
|
||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
srcs = ["doc.go"],
|
srcs = [
|
||||||
|
"doc.go",
|
||||||
|
"util.go",
|
||||||
|
],
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
|
deps = ["//vendor:k8s.io/apimachinery/pkg/apis/meta/v1"],
|
||||||
)
|
)
|
||||||
|
|
||||||
filegroup(
|
filegroup(
|
||||||
|
27
pkg/kubelet/util/util.go
Normal file
27
pkg/kubelet/util/util.go
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
/*
|
||||||
|
Copyright 2017 The Kubernetes Authors.
|
||||||
|
|
||||||
|
Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
you may not use this file except in compliance with the License.
|
||||||
|
You may obtain a copy of the License at
|
||||||
|
|
||||||
|
http://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
|
||||||
|
Unless required by applicable law or agreed to in writing, software
|
||||||
|
distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
See the License for the specific language governing permissions and
|
||||||
|
limitations under the License.
|
||||||
|
*/
|
||||||
|
|
||||||
|
package util
|
||||||
|
|
||||||
|
import (
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
)
|
||||||
|
|
||||||
|
// FromApiserverCache modifies <opts> so that the GET request will
|
||||||
|
// be served from apiserver cache instead of from etcd.
|
||||||
|
func FromApiserverCache(opts *metav1.GetOptions) {
|
||||||
|
opts.ResourceVersion = "0"
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user