@@ -20,6 +20,7 @@ import (
2020 "github.com/api7/api7-ingress-controller/internal/provider"
2121 "github.com/api7/api7-ingress-controller/internal/provider/adc/translator"
2222 "github.com/api7/gopkg/pkg/log"
23+ "k8s.io/apimachinery/pkg/types"
2324)
2425
2526type ResourceKind struct {
@@ -64,22 +65,64 @@ func (d *adcClient) getConfigs(rk ResourceKind) []adcConfig {
6465 return d .configs [rk ]
6566}
6667
67- func (d * adcClient ) updateGatewayConfigs (rk ResourceKind , tctx * provider.TranslateContext ) ([] adcConfig , error ) {
68- // get gateway proxy from tctx
69- return nil , nil
70- }
68+ func (d * adcClient ) getConfigsForGatewayProxy (rk ResourceKind , tctx * provider.TranslateContext , gatewayProxy * v1alpha1. GatewayProxy ) (* adcConfig , error ) {
69+ if gatewayProxy == nil || gatewayProxy . Spec . Provider == nil {
70+ return nil , nil
71+ }
7172
72- func (d * adcClient ) updateIngressConfigs (rk ResourceKind , tctx * provider.TranslateContext ) ([]adcConfig , error ) {
73- // get gateway proxy from tctx
74- return nil , nil
75- }
73+ provider := gatewayProxy .Spec .Provider
74+ if provider .Type != v1alpha1 .ProviderTypeControlPlane || provider .ControlPlane == nil {
75+ return nil , nil
76+ }
77+
78+ endpoints := provider .ControlPlane .Endpoints
79+ if len (endpoints ) == 0 {
80+ return nil , errors .New ("no endpoints found" )
81+ }
82+
83+ endpoint := endpoints [0 ]
84+ config := adcConfig {
85+ ServerAddr : endpoint ,
86+ }
87+
88+ if provider .ControlPlane .Auth .Type == v1alpha1 .AuthTypeAdminKey && provider .ControlPlane .Auth .AdminKey != nil {
89+ if provider .ControlPlane .Auth .AdminKey .ValueFrom != nil && provider .ControlPlane .Auth .AdminKey .ValueFrom .SecretKeyRef != nil {
90+ secretRef := provider .ControlPlane .Auth .AdminKey .ValueFrom .SecretKeyRef
91+ secret , ok := tctx .Secrets [types.NamespacedName {
92+ Namespace : rk .Namespace ,
93+ Name : secretRef .Name ,
94+ }]
95+ if ok {
96+ if token , ok := secret .Data [secretRef .Key ]; ok {
97+ config .Token = string (token )
98+ }
99+ }
100+ } else if provider .ControlPlane .Auth .AdminKey .Value != "" {
101+ config .Token = provider .ControlPlane .Auth .AdminKey .Value
102+ }
103+ }
76104
77- func (d * adcClient ) updateHTTPRouteConfigs (rk ResourceKind , tctx * provider.TranslateContext ) ([]adcConfig , error ) {
78- return nil , nil
105+ if config .Token == "" {
106+ return nil , errors .New ("no token found" )
107+ }
108+
109+ return & config , nil
79110}
80111
81- func (d * adcClient ) updateConsumerConfigs (rk ResourceKind , tctx * provider.TranslateContext ) ([]adcConfig , error ) {
82- return nil , nil
112+ func (d * adcClient ) updateConfigs (rk ResourceKind , tctx * provider.TranslateContext ) error {
113+ var configs []adcConfig
114+ for _ , gatewayProxy := range tctx .GatewayProxies {
115+ config , err := d .getConfigsForGatewayProxy (rk , tctx , & gatewayProxy )
116+ if err != nil {
117+ return err
118+ }
119+ if config != nil {
120+ configs = append (configs , * config )
121+ }
122+ }
123+
124+ d .configs [rk ] = configs
125+ return nil
83126}
84127
85128func (d * adcClient ) Update (ctx context.Context , tctx * provider.TranslateContext , obj client.Object ) error {
@@ -90,60 +133,47 @@ func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext,
90133 err error
91134 )
92135
93- var configs []adcConfig
94-
95- rk := ResourceKind {
96- Kind : obj .GetObjectKind ().GroupVersionKind ().Kind ,
97- Namespace : obj .GetNamespace (),
98- Name : obj .GetName (),
99- }
100-
101136 switch t := obj .(type ) {
102137 case * gatewayv1.HTTPRoute :
103138 result , err = d .translator .TranslateHTTPRoute (tctx , t .DeepCopy ())
104- if err != nil {
105- return err
106- }
107139 resourceTypes = append (resourceTypes , "service" )
108- configs , err = d .updateHTTPRouteConfigs (rk , tctx )
109- if err != nil {
110- return err
111- }
112140 case * gatewayv1.Gateway :
113141 result , err = d .translator .TranslateGateway (tctx , t .DeepCopy ())
114142 if err != nil {
115143 return err
116144 }
117145 resourceTypes = append (resourceTypes , "global_rule" , "ssl" , "plugin_metadata" )
118- configs , err = d .updateGatewayConfigs (rk , tctx )
119- if err != nil {
120- return err
121- }
122146 case * networkingv1.Ingress :
123147 result , err = d .translator .TranslateIngress (tctx , t .DeepCopy ())
124148 if err != nil {
125149 return err
126150 }
127151 resourceTypes = append (resourceTypes , "service" , "ssl" )
128- configs , err = d .updateIngressConfigs (rk , tctx )
129- if err != nil {
130- return err
131- }
132152 case * v1alpha1.Consumer :
133153 result , err = d .translator .TranslateConsumerV1alpha1 (tctx , t .DeepCopy ())
134154 if err != nil {
135155 return err
136156 }
137157 resourceTypes = append (resourceTypes , "consumer" )
138- configs , err = d .updateConsumerConfigs (rk , tctx )
139- if err != nil {
140- return err
141- }
158+ }
159+ if err != nil {
160+ return err
142161 }
143162 if result == nil {
144163 return nil
145164 }
146165
166+ // update adc configs
167+ rk := ResourceKind {
168+ Kind : obj .GetObjectKind ().GroupVersionKind ().Kind ,
169+ Namespace : obj .GetNamespace (),
170+ Name : obj .GetName (),
171+ }
172+ if err := d .updateConfigs (rk , tctx ); err != nil {
173+ return err
174+ }
175+ configs := d .getConfigs (rk )
176+
147177 return d .sync (Task {
148178 Name : obj .GetName (),
149179 Labels : label .GenLabel (obj ),
0 commit comments