istio.io/istio@v0.0.0-20240520182934-d79c90f27776/pilot/pkg/credentials/kube/multicluster.go (about) 1 // Copyright Istio Authors 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package kube 16 17 import ( 18 "fmt" 19 20 "istio.io/istio/pilot/pkg/credentials" 21 "istio.io/istio/pkg/cluster" 22 "istio.io/istio/pkg/kube/multicluster" 23 ) 24 25 // Multicluster structure holds the remote kube Controllers and multicluster specific attributes. 26 type Multicluster struct { 27 configCluster cluster.ID 28 secretHandlers []func(name string, namespace string) 29 component *multicluster.Component[*CredentialsController] 30 } 31 32 var _ credentials.MulticlusterController = &Multicluster{} 33 34 func NewMulticluster(configCluster cluster.ID, controller multicluster.ComponentBuilder) *Multicluster { 35 m := &Multicluster{ 36 configCluster: configCluster, 37 } 38 39 m.component = multicluster.BuildMultiClusterComponent(controller, func(cluster *multicluster.Cluster) *CredentialsController { 40 return NewCredentialsController(cluster.Client, m.secretHandlers) 41 }) 42 return m 43 } 44 45 func (m *Multicluster) ForCluster(clusterID cluster.ID) (credentials.Controller, error) { 46 cc := m.component.ForCluster(clusterID) 47 if cc == nil { 48 return nil, fmt.Errorf("cluster %v is not configured", clusterID) 49 } 50 agg := &AggregateController{} 51 agg.controllers = []*CredentialsController{} 52 agg.authController = *cc 53 if clusterID != m.configCluster { 54 // If the request cluster is not the config cluster, we will append it and use it for auth 55 // This means we will prioritize the proxy cluster, then the config cluster for credential lookup 56 // Authorization will always use the proxy cluster. 57 agg.controllers = append(agg.controllers, *cc) 58 } 59 if cc := m.component.ForCluster(m.configCluster); cc != nil { 60 agg.controllers = append(agg.controllers, *cc) 61 } 62 return agg, nil 63 } 64 65 func (m *Multicluster) AddSecretHandler(h func(name string, namespace string)) { 66 // Intentionally no lock. The controller today requires that handlers are registered before execution and not in parallel. 67 m.secretHandlers = append(m.secretHandlers, h) 68 } 69 70 type AggregateController struct { 71 // controllers to use to look up certs. Generally this will consistent of the primary (config) cluster 72 // and a single remote cluster where the proxy resides 73 controllers []*CredentialsController 74 authController *CredentialsController 75 } 76 77 var _ credentials.Controller = &AggregateController{} 78 79 func (a *AggregateController) GetCertInfo(name, namespace string) (certInfo *credentials.CertInfo, err error) { 80 // Search through all clusters, find first non-empty result 81 var firstError error 82 for _, c := range a.controllers { 83 certInfo, err := c.GetCertInfo(name, namespace) 84 if err != nil { 85 if firstError == nil { 86 firstError = err 87 } 88 } else { 89 return certInfo, nil 90 } 91 } 92 return nil, firstError 93 } 94 95 func (a *AggregateController) GetCaCert(name, namespace string) (certInfo *credentials.CertInfo, err error) { 96 // Search through all clusters, find first non-empty result 97 var firstError error 98 for _, c := range a.controllers { 99 k, err := c.GetCaCert(name, namespace) 100 if err != nil { 101 if firstError == nil { 102 firstError = err 103 } 104 } else { 105 return k, nil 106 } 107 } 108 return nil, firstError 109 } 110 111 func (a *AggregateController) Authorize(serviceAccount, namespace string) error { 112 return a.authController.Authorize(serviceAccount, namespace) 113 } 114 115 func (a *AggregateController) AddEventHandler(f func(name string, namespace string)) { 116 // no ops 117 } 118 119 func (a *AggregateController) GetDockerCredential(name, namespace string) ([]byte, error) { 120 // Search through all clusters, find first non-empty result 121 var firstError error 122 for _, c := range a.controllers { 123 k, err := c.GetDockerCredential(name, namespace) 124 if err != nil { 125 if firstError == nil { 126 firstError = err 127 } 128 } else { 129 return k, nil 130 } 131 } 132 return nil, firstError 133 }