Skip to content

Commit 3566e18

Browse files
authored
refactor(adc): translate gateway (#63)
1 parent b0ba08c commit 3566e18

File tree

11 files changed

+238
-83
lines changed

11 files changed

+238
-83
lines changed

Makefile

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,12 +119,11 @@ kind-down:
119119
|| echo "kind cluster does not exist"
120120

121121
.PHONY: kind-load-images
122-
kind-load-images: pull-infra-images
122+
kind-load-images: pull-infra-images kind-load-ingress-image
123123
@kind load docker-image hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-gateway:dev --name $(KIND_NAME)
124124
@kind load docker-image hkccr.ccs.tencentyun.com/api7-dev/api7-ee-dp-manager:$(DASHBOARD_VERSION) --name $(KIND_NAME)
125125
@kind load docker-image hkccr.ccs.tencentyun.com/api7-dev/api7-ee-3-integrated:$(DASHBOARD_VERSION) --name $(KIND_NAME)
126126
@kind load docker-image kennethreitz/httpbin:latest --name $(KIND_NAME)
127-
@kind load docker-image $(IMG) --name $(KIND_NAME)
128127
@kind load docker-image jmalloc/echo-server:latest --name $(KIND_NAME)
129128

130129
.PHONY: kind-load-ingress-image
@@ -139,11 +138,6 @@ pull-infra-images:
139138
@docker pull kennethreitz/httpbin:latest
140139
@docker pull jmalloc/echo-server:latest
141140

142-
143-
.PHONY: kind-load-image
144-
kind-load-image:
145-
@kind load docker-image $(IMG) --name $(KIND_NAME)
146-
147141
##@ Build
148142

149143
.PHONY: build

api/adc/types.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ type Resources struct {
7272
GlobalRules Plugins `json:"global_rules,omitempty" yaml:"global_rules,omitempty"`
7373
PluginMetadata Plugins `json:"plugin_metadata,omitempty" yaml:"plugin_metadata,omitempty"`
7474
Services []*Service `json:"services,omitempty" yaml:"services,omitempty"`
75-
Ssls []*SSL `json:"ssls,omitempty" yaml:"ssls,omitempty"`
75+
SSLs []*SSL `json:"ssls,omitempty" yaml:"ssls,omitempty"`
7676
}
7777

7878
type ConsumerGroup struct {
@@ -168,22 +168,22 @@ type TLSClass struct {
168168
type SSL struct {
169169
Metadata `json:",inline" yaml:",inline"`
170170

171-
Certificates []Certificate `json:"certificates"`
172-
Client *ClientClass `json:"client,omitempty"`
173-
Snis []string `json:"snis"`
174-
SSLProtocols []SSLProtocol `json:"ssl_protocols,omitempty"`
175-
Type *SSLType `json:"type,omitempty"`
171+
Certificates []Certificate `json:"certificates" yaml:"certificates"`
172+
Client *ClientClass `json:"client,omitempty" yaml:"client,omitempty"`
173+
Snis []string `json:"snis" yaml:"snis"`
174+
SSLProtocols []SSLProtocol `json:"ssl_protocols,omitempty" yaml:"ssl_protocols,omitempty"`
175+
Type *SSLType `json:"type,omitempty" yaml:"type,omitempty"`
176176
}
177177

178178
type Certificate struct {
179-
Certificate string `json:"certificate"`
180-
Key string `json:"key"`
179+
Certificate string `json:"certificate" yaml:"certificate"`
180+
Key string `json:"key" yaml:"key"`
181181
}
182182

183183
type ClientClass struct {
184-
CA string `json:"ca"`
185-
Depth *int64 `json:"depth,omitempty"`
186-
SkipMtlsURIRegex []string `json:"skip_mtls_uri_regex,omitempty"`
184+
CA string `json:"ca" yaml:"ca"`
185+
Depth *int64 `json:"depth,omitempty" yaml:"depth,omitempty"`
186+
SkipMtlsURIRegex []string `json:"skip_mtls_uri_regex,omitempty" yaml:"skip_mtls_uri_regex,omitempty"`
187187
}
188188

189189
type Method string

internal/controller/gateway_controller.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/api7/gopkg/pkg/log"
1313
"github.com/go-logr/logr"
1414
corev1 "k8s.io/api/core/v1"
15+
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
1516
"k8s.io/apimachinery/pkg/runtime"
1617
"k8s.io/apimachinery/pkg/types"
1718
ctrl "sigs.k8s.io/controller-runtime"
@@ -78,6 +79,11 @@ func (r *GatewayReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
7879
gateway.Namespace = req.Namespace
7980
gateway.Name = req.Name
8081

82+
gateway.TypeMeta = metav1.TypeMeta{
83+
Kind: KindGateway,
84+
APIVersion: gatewayv1.GroupVersion.String(),
85+
}
86+
8187
if err := r.Provider.Delete(ctx, gateway); err != nil {
8288
return ctrl.Result{}, err
8389
}

internal/provider/adc/adc.go

Lines changed: 44 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package adc
33
import (
44
"bytes"
55
"context"
6+
"errors"
67
"os"
78
"os/exec"
89

@@ -28,9 +29,10 @@ type adcClient struct {
2829
}
2930

3031
type Task struct {
31-
Name string
32-
Resources types.Resources
33-
Labels map[string]string
32+
Name string
33+
Resources types.Resources
34+
Labels map[string]string
35+
ResourceTypes []string
3436
}
3537

3638
func New() (provider.Provider, error) {
@@ -43,64 +45,59 @@ func New() (provider.Provider, error) {
4345
}
4446

4547
func (d *adcClient) Update(ctx context.Context, tctx *provider.TranslateContext, obj client.Object) error {
48+
log.Debugw("updating object", zap.Any("object", obj))
49+
4650
var result *translator.TranslateResult
51+
var resourceTypes []string
4752
var err error
4853

49-
var task = Task{
50-
Name: obj.GetName(),
51-
Labels: label.GenLabel(obj),
52-
}
53-
var extraArgs []string
54-
5554
switch obj := obj.(type) {
5655
case *gatewayv1.HTTPRoute:
57-
extraArgs = append(extraArgs, "--include-resource-type", "service")
58-
log.Debugw("translating http route", zap.Any("http route", obj))
5956
result, err = d.translator.TranslateHTTPRoute(tctx, obj.DeepCopy())
57+
resourceTypes = append(resourceTypes, "service")
6058
case *gatewayv1.Gateway:
61-
extraArgs = append(extraArgs, "--include-resource-type", "global_rule")
62-
log.Debugw("translating gateway", zap.Any("gateway", obj))
6359
result, err = d.translator.TranslateGateway(tctx, obj.DeepCopy())
60+
resourceTypes = append(resourceTypes, "global_rule", "ssl")
6461
}
6562
if err != nil {
6663
return err
6764
}
68-
log.Debugw("translated result", zap.Any("result", result))
6965
if result == nil {
7066
return nil
7167
}
7268

73-
resources := types.Resources{
74-
Services: result.Services,
75-
GlobalRules: result.GlobalRules,
76-
}
77-
log.Debugw("adc resources", zap.Any("resources", resources))
78-
79-
task.Resources = resources
80-
81-
return d.sync(task, extraArgs...)
82-
}
83-
84-
func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
85-
task := Task{
69+
return d.sync(Task{
8670
Name: obj.GetName(),
8771
Labels: label.GenLabel(obj),
88-
}
72+
Resources: types.Resources{
73+
Services: result.Services,
74+
SSLs: result.SSL,
75+
GlobalRules: result.GlobalRules,
76+
},
77+
ResourceTypes: resourceTypes,
78+
})
79+
}
8980

90-
var extraArgs []string
81+
func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
82+
log.Debugw("deleting object", zap.Any("object", obj))
9183

84+
resourceTypes := []string{}
9285
switch obj.(type) {
9386
case *gatewayv1.HTTPRoute:
94-
extraArgs = append(extraArgs, "--include-resource-type", "service")
87+
resourceTypes = append(resourceTypes, "service")
9588
case *gatewayv1.Gateway:
96-
extraArgs = append(extraArgs, "--include-resource-type", "global_rule")
89+
resourceTypes = append(resourceTypes, "global_rule", "ssl")
9790
}
9891

99-
return d.sync(task, extraArgs...)
92+
return d.sync(Task{
93+
Name: obj.GetName(),
94+
Labels: label.GenLabel(obj),
95+
ResourceTypes: resourceTypes,
96+
})
10097
}
10198

102-
func (d *adcClient) sync(task Task, extraArgs ...string) error {
103-
log.Debugw("syncing task", zap.Any("task", task))
99+
func (d *adcClient) sync(task Task) error {
100+
log.Debugw("syncing resources", zap.Any("task", task))
104101

105102
data, err := yaml.Marshal(task.Resources)
106103
if err != nil {
@@ -116,7 +113,7 @@ func (d *adcClient) sync(task Task, extraArgs ...string) error {
116113
_ = os.Remove(tmpFile.Name())
117114
}()
118115

119-
log.Debugw("syncing resources", zap.String("file", tmpFile.Name()), zap.String("yaml", string(data)))
116+
log.Debugw("generated adc yaml", zap.String("file", tmpFile.Name()), zap.String("yaml", string(data)))
120117

121118
if _, err := tmpFile.Write(data); err != nil {
122119
return err
@@ -126,11 +123,13 @@ func (d *adcClient) sync(task Task, extraArgs ...string) error {
126123
"-f", tmpFile.Name(),
127124
"--tls-skip-verify",
128125
}
129-
args = append(args, extraArgs...)
130126

131127
for k, v := range task.Labels {
132128
args = append(args, "--label-selector", k+"="+v)
133129
}
130+
for _, t := range task.ResourceTypes {
131+
args = append(args, "--include-resource-type", t)
132+
}
134133

135134
var stdout, stderr bytes.Buffer
136135
cmd := exec.Command("adc", args...)
@@ -145,15 +144,20 @@ func (d *adcClient) sync(task Task, extraArgs ...string) error {
145144
)
146145

147146
if err := cmd.Run(); err != nil {
147+
stderrStr := stderr.String()
148+
stdoutStr := stdout.String()
149+
errMsg := stderrStr
150+
if errMsg == "" {
151+
errMsg = stdoutStr
152+
}
148153
log.Errorw("failed to run adc",
149154
zap.Error(err),
150-
zap.String("output", stdout.String()),
151-
zap.String("stderr", stderr.String()),
155+
zap.String("output", stdoutStr),
156+
zap.String("stderr", stderrStr),
152157
)
153-
return err
158+
return errors.New("failed to sync resources: " + errMsg + ", exit err: " + err.Error())
154159
}
155160

156161
log.Debugw("adc sync success", zap.String("taskname", task.Name))
157-
158162
return nil
159163
}

0 commit comments

Comments
 (0)