@@ -18,7 +18,9 @@ import (
1818 "github.com/rancher/norman/types"
1919 v32 "github.com/rancher/rancher/pkg/apis/management.cattle.io/v3"
2020 "github.com/rancher/rancher/pkg/clusterrouter"
21+ "github.com/rancher/rancher/pkg/controllers/management/secretmigrator"
2122 clusterController "github.com/rancher/rancher/pkg/controllers/managementuser"
23+ v1 "github.com/rancher/rancher/pkg/generated/norman/core/v1"
2224 v3 "github.com/rancher/rancher/pkg/generated/norman/management.cattle.io/v3"
2325 "github.com/rancher/rancher/pkg/kontainer-engine/drivers/gke"
2426 "github.com/rancher/rancher/pkg/rbac"
@@ -33,7 +35,7 @@ import (
3335 "golang.org/x/sync/semaphore"
3436 "k8s.io/apiextensions-apiserver/pkg/client/clientset/clientset"
3537 apierrors "k8s.io/apimachinery/pkg/api/errors"
36- v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
38+ metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
3739 authv1 "k8s.io/client-go/kubernetes/typed/authorization/v1"
3840 "k8s.io/client-go/rest"
3941 clientcmdapi "k8s.io/client-go/tools/clientcmd/api"
@@ -44,6 +46,7 @@ type Manager struct {
4446 ScaledContext * config.ScaledContext
4547 clusterLister v3.ClusterLister
4648 clusters v3.ClusterInterface
49+ secretLister v1.SecretLister
4750 controllers sync.Map
4851 accessControl types.AccessControl
4952 rbac rbacv1.Interface
@@ -69,6 +72,7 @@ func NewManager(httpsPort int, context *config.ScaledContext, asl accesscontrol.
6972 accessControl : rbac .NewAccessControlWithASL ("" , asl ),
7073 clusterLister : context .Management .Clusters ("" ).Controller ().Lister (),
7174 clusters : context .Management .Clusters ("" ),
75+ secretLister : context .Core .Secrets ("" ).Controller ().Lister (),
7276 startSem : semaphore .NewWeighted (int64 (settings .ClusterControllerStartCount .GetInt ())),
7377 }
7478}
@@ -107,7 +111,7 @@ func (m *Manager) RESTConfig(cluster *v3.Cluster) (rest.Config, error) {
107111}
108112
109113func (m * Manager ) markUnavailable (clusterName string ) {
110- if cluster , err := m .clusters .Get (clusterName , v1 .GetOptions {}); err == nil {
114+ if cluster , err := m .clusters .Get (clusterName , metav1 .GetOptions {}); err == nil {
111115 if ! v32 .ClusterConditionReady .IsFalse (cluster ) {
112116 v32 .ClusterConditionReady .False (cluster )
113117 m .clusters .Update (cluster )
@@ -170,7 +174,7 @@ func (m *Manager) startController(r *record, controllers, clusterOwner bool) err
170174func (m * Manager ) changed (r * record , cluster * v3.Cluster , controllers , clusterOwner bool ) bool {
171175 existing := r .clusterRec
172176 if existing .Status .APIEndpoint != cluster .Status .APIEndpoint ||
173- existing .Status .ServiceAccountToken != cluster .Status .ServiceAccountToken ||
177+ existing .Status .ServiceAccountTokenSecret != cluster .Status .ServiceAccountTokenSecret ||
174178 existing .Status .CACert != cluster .Status .CACert ||
175179 existing .Status .AppliedSpec .LocalClusterAuthEndpoint .Enabled != cluster .Status .AppliedSpec .LocalClusterAuthEndpoint .Enabled {
176180 return true
@@ -194,7 +198,7 @@ func (m *Manager) doStart(rec *record, clusterOwner bool) (exit error) {
194198 // Prior to k8s v1.14, we simply did a DiscoveryClient.Version() check to see if the user cluster is alive
195199 // As of k8s v1.14, kubeapi returns a successful version response even if etcd is not available.
196200 // To work around this, now we try to get a namespace from the API, even if not found, it means the API is up.
197- if _ , err := rec .cluster .K8sClient .CoreV1 ().Namespaces ().Get (rec .ctx , "kube-system" , v1 .GetOptions {}); err != nil && ! apierrors .IsNotFound (err ) {
201+ if _ , err := rec .cluster .K8sClient .CoreV1 ().Namespaces ().Get (rec .ctx , "kube-system" , metav1 .GetOptions {}); err != nil && ! apierrors .IsNotFound (err ) {
198202 if i == 2 {
199203 m .markUnavailable (rec .cluster .ClusterName )
200204 }
@@ -252,7 +256,7 @@ func (m *Manager) doStart(rec *record, clusterOwner bool) (exit error) {
252256 }
253257}
254258
255- func ToRESTConfig (cluster * v3.Cluster , context * config.ScaledContext ) (* rest.Config , error ) {
259+ func ToRESTConfig (cluster * v3.Cluster , context * config.ScaledContext , secretLister v1. SecretLister ) (* rest.Config , error ) {
256260 if cluster == nil {
257261 return nil , nil
258262 }
@@ -261,7 +265,7 @@ func ToRESTConfig(cluster *v3.Cluster, context *config.ScaledContext) (*rest.Con
261265 return & context .RESTConfig , nil
262266 }
263267
264- if cluster .Status .APIEndpoint == "" || cluster .Status .CACert == "" || cluster .Status .ServiceAccountToken == "" {
268+ if cluster .Status .APIEndpoint == "" || cluster .Status .CACert == "" || cluster .Status .ServiceAccountTokenSecret == "" {
265269 return nil , nil
266270 }
267271
@@ -292,11 +296,15 @@ func ToRESTConfig(cluster *v3.Cluster, context *config.ScaledContext) (*rest.Con
292296 }
293297 }
294298
299+ secret , err := secretLister .Get (secretmigrator .SecretNamespace , cluster .Status .ServiceAccountTokenSecret )
300+ if err != nil {
301+ return nil , err
302+ }
295303 // adding suffix to make tlsConfig hashkey unique
296304 suffix := []byte ("\n " + cluster .Name )
297305 rc := & rest.Config {
298306 Host : u .String (),
299- BearerToken : cluster . Status . ServiceAccountToken ,
307+ BearerToken : string ( secret . Data [ secretmigrator . SecretKey ]) ,
300308 TLSClientConfig : rest.TLSClientConfig {
301309 CAData : append (caBytes , suffix ... ),
302310 NextProtos : []string {"http/1.1" },
@@ -397,7 +405,7 @@ func VerifyIgnoreDNSName(caCertsPEM []byte) (func(rawCerts [][]byte, verifiedCha
397405}
398406
399407func (m * Manager ) toRecord (ctx context.Context , cluster * v3.Cluster ) (* record , error ) {
400- kubeConfig , err := ToRESTConfig (cluster , m .ScaledContext )
408+ kubeConfig , err := ToRESTConfig (cluster , m .ScaledContext , m . secretLister )
401409 if kubeConfig == nil || err != nil {
402410 return nil , err
403411 }
@@ -484,7 +492,7 @@ func (m *Manager) UserContext(clusterName string) (*config.UserContext, error) {
484492// UserContextFromCluster accepts a pointer to a Cluster and returns a client
485493// for that cluster. It does not start any controllers.
486494func (m * Manager ) UserContextFromCluster (cluster * v3.Cluster ) (* config.UserContext , error ) {
487- kubeConfig , err := ToRESTConfig (cluster , m .ScaledContext )
495+ kubeConfig , err := ToRESTConfig (cluster , m .ScaledContext , m . secretLister )
488496 if err != nil {
489497 return nil , err
490498 }
0 commit comments