Skip to content

Commit c3987d4

Browse files
committed
Merge branch origin/release-v2-dev
2 parents 4c3c27b + 3566e18 commit c3987d4

File tree

11 files changed

+245
-93
lines changed

11 files changed

+245
-93
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: 46 additions & 45 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,69 +45,61 @@ 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))
4649
var (
47-
task = Task{
48-
Name: obj.GetName(),
49-
Labels: label.GenLabel(obj),
50-
}
51-
extraArgs []string
52-
result *translator.TranslateResult
53-
err error
50+
result *translator.TranslateResult
51+
resourceTypes []string
52+
err error
5453
)
5554

5655
switch obj := obj.(type) {
5756
case *gatewayv1.HTTPRoute:
58-
extraArgs = append(extraArgs, "--include-resource-type", "service")
59-
log.Debugw("translating http route", zap.Any("http route", obj))
6057
result, err = d.translator.TranslateHTTPRoute(tctx, obj.DeepCopy())
58+
resourceTypes = append(resourceTypes, "service")
6159
case *gatewayv1.Gateway:
62-
extraArgs = append(extraArgs, "--include-resource-type", "global_rule",
63-
"--include-resource-type", "plugin_metadata")
64-
log.Debugw("translating gateway", zap.Any("gateway", obj))
6560
result, err = d.translator.TranslateGateway(tctx, obj.DeepCopy())
61+
resourceTypes = append(resourceTypes, "global_rule", "ssl")
6662
}
6763
if err != nil {
6864
return err
6965
}
70-
log.Debugw("translated result", zap.Any("result", result))
7166
if result == nil {
7267
return nil
7368
}
7469

75-
resources := types.Resources{
76-
GlobalRules: result.GlobalRules,
77-
PluginMetadata: result.PluginMetadata,
78-
Services: result.Services,
79-
}
80-
log.Debugw("adc resources", zap.Any("resources", resources))
81-
82-
task.Resources = resources
83-
84-
return d.sync(task, extraArgs...)
70+
return d.sync(Task{
71+
Name: obj.GetName(),
72+
Labels: label.GenLabel(obj),
73+
Resources: types.Resources{
74+
GlobalRules: result.GlobalRules,
75+
PluginMetadata: result.PluginMetadata,
76+
Services: result.Services,
77+
SSLs: result.SSL,
78+
},
79+
ResourceTypes: resourceTypes,
80+
})
8581
}
8682

8783
func (d *adcClient) Delete(ctx context.Context, obj client.Object) error {
88-
var (
89-
task = Task{
90-
Name: obj.GetName(),
91-
Labels: label.GenLabel(obj),
92-
}
93-
extraArgs []string
94-
)
84+
log.Debugw("deleting object", zap.Any("object", obj))
9585

86+
var resourceTypes []string
9687
switch obj.(type) {
9788
case *gatewayv1.HTTPRoute:
98-
extraArgs = append(extraArgs, "--include-resource-type", "service")
89+
resourceTypes = append(resourceTypes, "service")
9990
case *gatewayv1.Gateway:
100-
extraArgs = append(extraArgs, "--include-resource-type", "global_rule",
101-
"--include-resource-type", "plugin_metadata")
91+
resourceTypes = append(resourceTypes, "global_rule", "ssl")
10292
}
10393

104-
return d.sync(task, extraArgs...)
94+
return d.sync(Task{
95+
Name: obj.GetName(),
96+
Labels: label.GenLabel(obj),
97+
ResourceTypes: resourceTypes,
98+
})
10599
}
106100

107-
func (d *adcClient) sync(task Task, extraArgs ...string) error {
108-
log.Debugw("syncing task", zap.Any("task", task))
101+
func (d *adcClient) sync(task Task) error {
102+
log.Debugw("syncing resources", zap.Any("task", task))
109103

110104
data, err := yaml.Marshal(task.Resources)
111105
if err != nil {
@@ -121,7 +115,7 @@ func (d *adcClient) sync(task Task, extraArgs ...string) error {
121115
_ = os.Remove(tmpFile.Name())
122116
}()
123117

124-
log.Debugw("syncing resources", zap.String("file", tmpFile.Name()), zap.String("yaml", string(data)))
118+
log.Debugw("generated adc yaml", zap.String("file", tmpFile.Name()), zap.String("yaml", string(data)))
125119

126120
if _, err := tmpFile.Write(data); err != nil {
127121
return err
@@ -131,11 +125,13 @@ func (d *adcClient) sync(task Task, extraArgs ...string) error {
131125
"-f", tmpFile.Name(),
132126
"--tls-skip-verify",
133127
}
134-
args = append(args, extraArgs...)
135128

136129
for k, v := range task.Labels {
137130
args = append(args, "--label-selector", k+"="+v)
138131
}
132+
for _, t := range task.ResourceTypes {
133+
args = append(args, "--include-resource-type", t)
134+
}
139135

140136
var stdout, stderr bytes.Buffer
141137
cmd := exec.Command("adc", args...)
@@ -151,15 +147,20 @@ func (d *adcClient) sync(task Task, extraArgs ...string) error {
151147

152148
log.Debugf("exec: %s\n", cmd.String())
153149
if err := cmd.Run(); err != nil {
150+
stderrStr := stderr.String()
151+
stdoutStr := stdout.String()
152+
errMsg := stderrStr
153+
if errMsg == "" {
154+
errMsg = stdoutStr
155+
}
154156
log.Errorw("failed to run adc",
155157
zap.Error(err),
156-
zap.String("output", stdout.String()),
157-
zap.String("stderr", stderr.String()),
158+
zap.String("output", stdoutStr),
159+
zap.String("stderr", stderrStr),
158160
)
159-
return err
161+
return errors.New("failed to sync resources: " + errMsg + ", exit err: " + err.Error())
160162
}
161163

162164
log.Debugw("adc sync success", zap.String("taskname", task.Name))
163-
164165
return nil
165166
}

0 commit comments

Comments
 (0)