@@ -19,26 +19,33 @@ type Config struct {
1919type Builder struct {
2020 kubeconfigProvider kubeconfigProvider
2121 kcpClient client.Client
22+ multipleContexts bool
2223}
2324
2425type kubeconfigProvider interface {
2526 KubeconfigForRuntimeID (runtimeID string ) ([]byte , error )
2627}
2728
28- func NewBuilder (kcpClient client.Client , provider kubeconfigProvider ) * Builder {
29+ func NewBuilder (kcpClient client.Client , provider kubeconfigProvider , multipleContexts bool ) * Builder {
2930 return & Builder {
3031 kcpClient : kcpClient ,
3132 kubeconfigProvider : provider ,
33+ multipleContexts : multipleContexts ,
3234 }
3335}
3436
3537type kubeconfigData struct {
36- ContextName string
37- CAData string
38- ServerURL string
39- OIDCIssuerURL string
40- OIDCClientID string
41- Token string
38+ ContextName string
39+ CAData string
40+ ServerURL string
41+ OIDCConfigs []OIDCConfig
42+ Token string
43+ }
44+
45+ type OIDCConfig struct {
46+ Name string
47+ IssuerURL string
48+ ClientID string
4249}
4350
4451func (b * Builder ) BuildFromAdminKubeconfigForBinding (runtimeID string , token string ) (string , error ) {
@@ -64,12 +71,9 @@ func (b *Builder) BuildFromAdminKubeconfig(instance *internal.Instance, adminKub
6471 if instance .RuntimeID == "" {
6572 return "" , fmt .Errorf ("RuntimeID must not be empty" )
6673 }
67- issuerURL , clientID , err := b .getOidcDataFromRuntimeResource (instance .RuntimeID )
68- if err != nil {
69- return "" , fmt .Errorf ("while fetching oidc data: %w" , err )
70- }
7174
7275 var kubeconfigContent []byte
76+ var err error
7377 if adminKubeconfig == "" {
7478 kubeconfigContent , err = b .kubeconfigProvider .KubeconfigForRuntimeID (instance .RuntimeID )
7579 if err != nil {
@@ -84,12 +88,16 @@ func (b *Builder) BuildFromAdminKubeconfig(instance *internal.Instance, adminKub
8488 return "" , fmt .Errorf ("during unmarshal invocation: %w" , err )
8589 }
8690
91+ OIDCConfigs , err := b .getOidcDataFromRuntimeResource (instance .RuntimeID , kubeCfg .CurrentContext )
92+ if err != nil {
93+ return "" , fmt .Errorf ("while fetching oidc data: %w" , err )
94+ }
95+
8796 return b .parseTemplate (kubeconfigData {
88- ContextName : kubeCfg .CurrentContext ,
89- CAData : kubeCfg .Clusters [0 ].Cluster .CertificateAuthorityData ,
90- ServerURL : kubeCfg .Clusters [0 ].Cluster .Server ,
91- OIDCIssuerURL : issuerURL ,
92- OIDCClientID : clientID ,
97+ ContextName : kubeCfg .CurrentContext ,
98+ CAData : kubeCfg .Clusters [0 ].Cluster .CertificateAuthorityData ,
99+ ServerURL : kubeCfg .Clusters [0 ].Cluster .Server ,
100+ OIDCConfigs : OIDCConfigs ,
93101 }, kubeconfigTemplate )
94102}
95103
@@ -158,25 +166,36 @@ func (b *Builder) validKubeconfig(kc kubeconfig) error {
158166 return nil
159167}
160168
161- func (b * Builder ) getOidcDataFromRuntimeResource (id string ) ( string , string , error ) {
169+ func (b * Builder ) getOidcDataFromRuntimeResource (id string , currentContext string ) ([] OIDCConfig , error ) {
162170 var runtime imv1.Runtime
171+ var oidcConfigs []OIDCConfig
163172 err := b .kcpClient .Get (context .Background (), client.ObjectKey {Name : id , Namespace : kcpNamespace }, & runtime )
164173 if err != nil {
165- return "" , "" , err
174+ return nil , err
166175 }
167-
168- oidcConfig := runtime .Spec .Shoot .Kubernetes .KubeAPIServer .AdditionalOidcConfig
169- if oidcConfig == nil || len (* oidcConfig ) == 0 {
170- return "" , "" , fmt .Errorf ("runtime resource contains no OIDC config" )
171- }
172-
173- config := (* oidcConfig )[0 ]
174- if config .IssuerURL == nil || * config .IssuerURL == "" {
175- return "" , "" , fmt .Errorf ("runtime resource contains an empty OIDC issuer URL" )
176+ additionalConfigs := runtime .Spec .Shoot .Kubernetes .KubeAPIServer .AdditionalOidcConfig
177+ if additionalConfigs == nil {
178+ return nil , fmt .Errorf ("Runtime Resource contains no additional OIDC config" )
176179 }
177- if config .ClientID == nil || * config .ClientID == "" {
178- return "" , "" , fmt .Errorf ("runtime resource contains an empty OIDC client ID" )
180+ for i , config := range * additionalConfigs {
181+ if config .IssuerURL == nil {
182+ return nil , fmt .Errorf ("Runtime Resource contains an empty OIDC issuer URL" )
183+ }
184+ if config .ClientID == nil {
185+ return nil , fmt .Errorf ("Runtime Resource contains an empty OIDC client ID" )
186+ }
187+ name := currentContext
188+ if i > 0 {
189+ name = fmt .Sprintf ("%s-%d" , currentContext , i + 1 )
190+ }
191+ oidcConfigs = append (oidcConfigs , OIDCConfig {
192+ Name : name ,
193+ IssuerURL : * config .IssuerURL ,
194+ ClientID : * config .ClientID ,
195+ })
196+ if ! b .multipleContexts {
197+ return oidcConfigs , nil
198+ }
179199 }
180-
181- return * config .IssuerURL , * config .ClientID , nil
200+ return oidcConfigs , nil
182201}
0 commit comments