Skip to content

Commit 4aafb68

Browse files
committed
Merge branch origin/release-v2-dev
2 parents 13f3912 + 497da5f commit 4aafb68

File tree

9 files changed

+148
-3
lines changed

9 files changed

+148
-3
lines changed

Dockerfile

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ RUN apt update \
1616
&& apt autoremove -y wget
1717

1818
FROM gcr.io/distroless/cc-debian12:${BASE_IMAGE_TAG}
19+
20+
ARG TARGETARCH
21+
1922
WORKDIR /app
2023

2124
COPY --from=deps /bin/adc /bin/adc
22-
COPY ./bin/api7-ingress-controller .
25+
COPY ./bin/api7-ingress-controller_${TARGETARCH} ./api7-ingress-controller
2326
COPY ./config/samples/config.yaml ./conf/config.yaml
2427

2528
ENTRYPOINT ["/app/api7-ingress-controller"]

Makefile

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,14 +156,28 @@ pull-infra-images:
156156

157157
.PHONY: build
158158
build: manifests generate fmt vet ## Build manager binary.
159-
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build -o bin/api7-ingress-controller -ldflags $(GO_LDFLAGS) cmd/main.go
159+
GOOS=$(GOOS) GOARCH=$(GOARCH) CGO_ENABLED=0 go build -o bin/api7-ingress-controller_$(GOARCH) -ldflags $(GO_LDFLAGS) cmd/main.go
160160

161161
linux-build:
162162
GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bin/api7-ingress-controller -ldflags $(GO_LDFLAGS) cmd/main.go
163163

164164
.PHONY: build-image
165165
build-image: docker-build
166166

167+
.PHONY: build-multi-arch
168+
build-multi-arch:
169+
@CGO_ENABLED=0 GOARCH=amd64 go build -o bin/api7-ingress-controller_amd64 -ldflags $(GO_LDFLAGS) cmd/main.go
170+
@CGO_ENABLED=0 GOARCH=arm64 go build -o bin/api7-ingress-controller_arm64 -ldflags $(GO_LDFLAGS) cmd/main.go
171+
172+
.PHONY: build-multi-arch-image
173+
build-multi-arch-image: build-multi-arch
174+
# daemon.json: "features":{"containerd-snapshotter": true}
175+
@docker buildx build --load --platform linux/amd64,linux/arm64 -t $(IMG) .
176+
177+
.PHONY: build-push-multi-arch-image
178+
build-push-multi-arch-image: build-multi-arch
179+
@docker buildx build --push --platform linux/amd64,linux/arm64 -t $(IMG) .
180+
167181
.PHONY: run
168182
run: manifests generate fmt vet ## Run a controller from your host.
169183
go run ./cmd/main.go

config/manager/manager.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ spec:
6060
containers:
6161
- image: controller:latest
6262
name: manager
63+
env:
64+
- name: POD_NAMESPACE
65+
valueFrom:
66+
fieldRef:
67+
fieldPath: metadata.namespace
68+
- name: POD_NAME
69+
valueFrom:
70+
fieldRef:
71+
fieldPath: metadata.name
6372
volumeMounts:
6473
- name: config-volume
6574
mountPath: /app/conf/config.yaml

config/samples/config.yaml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ controller_name: gateway.api7.io/api7-ingress-controller # The controller name
66
# The default value is "gateway.api7.io/api7-ingress-controller".
77
leader_election_id: "api7-ingress-controller-leader" # The leader election ID for the API7 Ingress Controller.
88
# The default value is "api7-ingress-controller-leader".
9+
leader_election:
10+
lease_duration: 15s # lease_duration is the duration that non-leader candidates will wait
11+
# after observing a leadership renewal until attempting to acquire leadership of a
12+
# leader election.
13+
renew_deadline: 10s # renew_deadline is the time in seconds that the acting controller
14+
# will retry refreshing leadership before giving up.
15+
retry_period: 2s # retry_period is the time in seconds that the acting controller
16+
# will wait between tries of actions with the controller.
17+
disable: false # Whether to disable leader election.
18+
919
ingress_class: api7 # The ingress class name of the API7 Ingress Controller.
1020
ingress_publish_service: "" # The service name of the ingress publish service.
1121
ingress_status_address: [] # The status address of the ingress.

internal/controller/config/config.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import (
77
"os"
88
"strings"
99
"text/template"
10+
"time"
1011

12+
"github.com/api7/api7-ingress-controller/internal/types"
1113
"gopkg.in/yaml.v2"
1214
)
1315

@@ -29,6 +31,16 @@ func NewDefaultConfig() *Config {
2931
ProbeAddr: DefaultProbeAddr,
3032
MetricsAddr: DefaultMetricsAddr,
3133
IngressClass: DefaultIngressClass,
34+
LeaderElection: NewLeaderElection(),
35+
}
36+
}
37+
38+
func NewLeaderElection() *LeaderElection {
39+
return &LeaderElection{
40+
LeaseDuration: types.TimeDuration{Duration: 15 * time.Second},
41+
RenewDeadline: types.TimeDuration{Duration: 10 * time.Second},
42+
RetryPeriod: types.TimeDuration{Duration: 2 * time.Second},
43+
Disable: false,
3244
}
3345
}
3446

internal/controller/config/types.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
package config
22

3+
import (
4+
"github.com/api7/api7-ingress-controller/internal/types"
5+
)
6+
37
const (
48
// IngressAPISIXLeader is the default election id for the controller
59
// leader election.
@@ -36,6 +40,7 @@ type Config struct {
3640
IngressClass string `json:"ingress_class" yaml:"ingress_class"`
3741
IngressPublishService string `json:"ingress_publish_service" yaml:"ingress_publish_service"`
3842
IngressStatusAddress []string `json:"ingress_status_address" yaml:"ingress_status_address"`
43+
LeaderElection *LeaderElection `json:"leader_election" yaml:"leader_election"`
3944
}
4045

4146
type GatewayConfig struct {
@@ -49,3 +54,10 @@ type ControlPlaneConfig struct {
4954
Endpoints []string `json:"endpoints" yaml:"endpoints"`
5055
TLSVerify *bool `json:"tls_verify" yaml:"tls_verify"`
5156
}
57+
58+
type LeaderElection struct {
59+
LeaseDuration types.TimeDuration `json:"leaseDuration,omitempty" yaml:"leaseDuration,omitempty"`
60+
RenewDeadline types.TimeDuration `json:"renewDeadline,omitempty" yaml:"renewDeadline,omitempty"`
61+
RetryPeriod types.TimeDuration `json:"retryPeriod,omitempty" yaml:"retryPeriod,omitempty"`
62+
Disable bool `json:"disable,omitempty" yaml:"disable,omitempty"`
63+
}

internal/manager/run.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"k8s.io/apimachinery/pkg/runtime"
1010
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
1111
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
12+
"k8s.io/utils/ptr"
1213
ctrl "sigs.k8s.io/controller-runtime"
1314
"sigs.k8s.io/controller-runtime/pkg/healthz"
1415
"sigs.k8s.io/controller-runtime/pkg/metrics/filters"
@@ -97,9 +98,12 @@ func Run(ctx context.Context, logger logr.Logger) error {
9798
Metrics: metricsServerOptions,
9899
WebhookServer: webhookServer,
99100
HealthProbeBindAddress: cfg.ProbeAddr,
100-
LeaderElection: true,
101+
LeaderElection: !config.ControllerConfig.LeaderElection.Disable,
101102
LeaderElectionID: cfg.LeaderElectionID,
102103
LeaderElectionNamespace: namespace,
104+
LeaseDuration: ptr.To(config.ControllerConfig.LeaderElection.LeaseDuration.Duration),
105+
RenewDeadline: ptr.To(config.ControllerConfig.LeaderElection.RenewDeadline.Duration),
106+
RetryPeriod: ptr.To(config.ControllerConfig.LeaderElection.RetryPeriod.Duration),
103107
// LeaderElectionReleaseOnCancel defines if the leader should step down voluntarily
104108
// when the Manager ends. This requires the binary to immediately end when the
105109
// Manager is stopped, otherwise, this setting is unsafe. Setting this significantly

internal/types/duration.go

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one or more
2+
// contributor license agreements. See the NOTICE file distributed with
3+
// this work for additional information regarding copyright ownership.
4+
// The ASF licenses this file to You under the Apache License, Version 2.0
5+
// (the "License"); you may not use this file except in compliance with
6+
// the License. You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
package types
16+
17+
import (
18+
"encoding/json"
19+
"fmt"
20+
"time"
21+
)
22+
23+
// TimeDuration is yet another time.Duration but implements json.Unmarshaler
24+
// and json.Marshaler, yaml.Unmarshaler and yaml.Marshaler interfaces so one
25+
// can use "1h", "5s" and etc in their json/yaml configurations.
26+
//
27+
// Note the format to represent time is same as time.Duration.
28+
// See the comments about time.ParseDuration for more details.
29+
type TimeDuration struct {
30+
time.Duration `json:",inline"`
31+
}
32+
33+
func (d *TimeDuration) MarshalJSON() ([]byte, error) {
34+
return json.Marshal(d.Duration.String())
35+
}
36+
37+
func (d *TimeDuration) UnmarshalJSON(data []byte) error {
38+
var value any
39+
if err := json.Unmarshal(data, &value); err != nil {
40+
return err
41+
}
42+
switch v := value.(type) {
43+
case float64:
44+
d.Duration = time.Duration(v)
45+
case string:
46+
dur, err := time.ParseDuration(v)
47+
if err != nil {
48+
return err
49+
}
50+
d.Duration = dur
51+
default:
52+
return fmt.Errorf("unknown type: %T", v)
53+
}
54+
return nil
55+
}
56+
57+
func (d *TimeDuration) MarshalYAML() (any, error) {
58+
return d.Duration.String(), nil
59+
}
60+
61+
func (d *TimeDuration) UnmarshalYAML(unmarshal func(any) error) error {
62+
var s string
63+
if err := unmarshal(&s); err != nil {
64+
return err
65+
}
66+
dur, err := time.ParseDuration(s)
67+
if err != nil {
68+
return err
69+
}
70+
d.Duration = dur
71+
return nil
72+
}

test/e2e/framework/manifests/ingress.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,15 @@ spec:
365365
spec:
366366
containers:
367367
- image: api7/api7-ingress-controller:dev
368+
env:
369+
- name: POD_NAMESPACE
370+
valueFrom:
371+
fieldRef:
372+
fieldPath: metadata.namespace
373+
- name: POD_NAME
374+
valueFrom:
375+
fieldRef:
376+
fieldPath: metadata.name
368377
volumeMounts:
369378
- name: ingress-config
370379
mountPath: /app/conf/config.yaml

0 commit comments

Comments
 (0)