Skip to content

Commit

Permalink
Do more stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
rikatz committed May 5, 2024
1 parent 7678c7c commit b045e9b
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 42 deletions.
5 changes: 3 additions & 2 deletions cmd/dataplane/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ limitations under the License.
package main

import (
"fmt"
"net/http"
"os"
"os/signal"
Expand All @@ -30,13 +31,13 @@ import (
dataplanenginx "k8s.io/ingress-nginx/cmd/dataplane/pkg/nginx"
"k8s.io/ingress-nginx/internal/nginx"
"k8s.io/ingress-nginx/pkg/metrics"
"k8s.io/ingress-nginx/version"
)

func main() {
klog.InitFlags(nil)

//fmt.Println(version.String())
//var err error
fmt.Println(version.String())

reg := prometheus.NewRegistry()

Expand Down
7 changes: 6 additions & 1 deletion deploy/static/provider/kind/deploy.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ spec:
fieldPath: metadata.namespace
- name: LD_PRELOAD
value: /usr/local/lib/libmimalloc.so
image: gcr.io/k8s-staging-ingress-nginx/dataplane:v0.0.16
image: gcr.io/k8s-staging-ingress-nginx/dataplane:v0.0.21
imagePullPolicy: IfNotPresent
lifecycle:
preStop:
Expand Down Expand Up @@ -534,6 +534,8 @@ spec:
name: ingress-controller
- mountPath: /etc/nginx/conf
name: nginx-conf
- mountPath: /var/run/secrets/kubernetes.io/serviceaccount
name: emptysecret
dnsPolicy: ClusterFirst
nodeSelector:
ingress-ready: "true"
Expand All @@ -557,6 +559,9 @@ spec:
- name: nginx-conf
emptyDir:
sizeLimit: 500Mi
- name: emptysecret
emptyDir:
sizeLimit: 1Mi
---
apiVersion: batch/v1
kind: Job
Expand Down
9 changes: 6 additions & 3 deletions internal/dataplane/nginx/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os/exec"
"path/filepath"
"syscall"
"time"

"k8s.io/klog/v2"
)
Expand Down Expand Up @@ -75,9 +76,7 @@ func (nc NginxCommand) Start(errch chan error) error {
return err
}
}
if err := os.WriteFile(ReadyFile, []byte("OK"), 0644); err != nil {
return err
}

cmd := nc.execCommand(true)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
Expand All @@ -88,6 +87,10 @@ func (nc NginxCommand) Start(errch chan error) error {
go func() {
errch <- cmd.Wait()
}()
now := time.Now().String()
if err := os.WriteFile(ReadyFile, []byte(now), 0644); err != nil {
return err
}
return nil
}

Expand Down
17 changes: 0 additions & 17 deletions internal/dataplane/nginx/remote.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,6 @@ func NewNginxRemote(host string) NginxExecutor {
}

func (nc NginxRemote) Start(errch chan error) error {
/*getStart, err := url.JoinPath(nc.host, "start") // TODO: Turn this path a constant on dataplane
if err != nil {
return err
}
resp, err := http.Get(getStart)
if err != nil {
return err
}
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("error executing start: %s", string(body))
}*/

// TODO: Add a ping/watcher to backend and populate error channel
return nil
}

Expand Down
16 changes: 15 additions & 1 deletion internal/ingress/controller/nginx.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,21 @@ func NewNGINXController(config *Configuration, mc metric.Collector) *NGINXContro
klog.Fatalf("Error creating file watcher for %v: %v", nginx.TemplatePath, err)
}

filesToWatch := []string{nginxdataplane.ReadyFile}
// With this test, we will check if the readiness file is not changed. This file is always created or
// changed by dataplane on its start. In case the dataplane dies and restarts, the file will be
// updated with the current time.
// This watcher will then detect a change on the file, meaning controller should restart and reconfigure
// everything.
// It should be guaranteed by the dataplane that this file is changed just once
// NGINX finishes starting
_, err = file.NewFileWatcherUpdateOnly(nginxdataplane.ReadyFile, true, func() {
klog.Fatalf("readiness file changed, restarting contorller")
})
if err != nil {
klog.Fatalf("error creating file watcher for %v: %v", nginxdataplane.ReadyFile, err)
}

filesToWatch := []string{}

if err := os.Mkdir("/etc/ingress-controller/geoip/", 0o755); err != nil && !os.IsExist(err) {
klog.Fatalf("Error creating geoip dir: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions pkg/flags/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
parser.EnableAnnotationValidation = *enableAnnotationValidation

// check port collisions
if !ing_net.IsPortAvailable(*httpPort) {
/*if !ing_net.IsPortAvailable(*httpPort) {
return false, nil, fmt.Errorf("port %v is already in use. Please check the flag --http-port", *httpPort)
}
Expand All @@ -283,7 +283,7 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
if !ing_net.IsPortAvailable(*profilerPort) {
return false, nil, fmt.Errorf("port %v is already in use. Please check the flag --profiler-port", *profilerPort)
}
}*/

nginx.StatusPort = *statusPort
nginx.StreamPort = *streamPort
Expand Down
11 changes: 9 additions & 2 deletions pkg/util/file/file_watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,18 +37,25 @@ type OSFileWatcher struct {
watcher *fsnotify.Watcher
// onEvent callback to be invoked after the file being watched changes
onEvent func()
updateOnly bool
}

// NewFileWatcher creates a new FileWatcher
func NewFileWatcher(file string, onEvent func()) (Watcher, error) {
func NewFileWatcherUpdateOnly(file string, updateOnly bool, onEvent func()) (Watcher, error) {
fw := OSFileWatcher{
file: file,
onEvent: onEvent,
updateOnly: updateOnly,
}
err := fw.watch()
return fw, err
}

// NewFileWatcher creates a new FileWatcher
func NewFileWatcher(file string, onEvent func()) (Watcher, error) {
return NewFileWatcherUpdateOnly(file, false, onEvent)
}

// Close ends the watch
func (f OSFileWatcher) Close() error {
return f.watcher.Close()
Expand All @@ -72,7 +79,7 @@ func (f *OSFileWatcher) watch() error {
for {
select {
case event := <-watcher.Events:
if event.Has(fsnotify.Create) ||
if (!f.updateOnly && event.Has(fsnotify.Create)) ||
event.Has(fsnotify.Write) {
if finfo, err := os.Lstat(event.Name); err != nil {
log.Printf("can not lstat file: %v\n", err)
Expand Down
10 changes: 1 addition & 9 deletions rootfs/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ RUN apk update \
&& adduser -S -D -H -u 101 -h /usr/local/nginx \
-s /sbin/nologin -G www-data -g www-data www-data

COPY --chown=www-data:www-data etc /etc
COPY --chown=www-data:www-data etc/nginx/template /etc/nginx/template

COPY --chown=www-data:www-data bin/${TARGETARCH}/dbg /
COPY --chown=www-data:www-data bin/${TARGETARCH}/nginx-ingress-controller /
Expand All @@ -54,9 +54,6 @@ RUN bash -xeu -c ' \
/etc/ingress-controller/geoip \
/etc/ingress-controller/telemetry \
/etc/ingress-controller/tempconf \
/etc/nginx/conf \
/var/log \
/var/log/nginx \
/tmp/nginx \
); \
for dir in "${writeDirs[@]}"; do \
Expand All @@ -74,9 +71,4 @@ RUN apk add --no-cache libcap \
&& setcap -v cap_net_bind_service=+ep /nginx-ingress-controller \
&& apk del libcap
USER www-data

# Create symlinks to redirect nginx logs to stdout and stderr docker log collector
RUN ln -sf /dev/stdout /var/log/nginx/access.log \
&& ln -sf /dev/stderr /var/log/nginx/error.log

ENTRYPOINT ["/nginx-ingress-controller"]
4 changes: 2 additions & 2 deletions test/e2e/run-kind-e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ if [ "${SKIP_INGRESS_IMAGE_CREATION}" = "false" ]; then
make BASE_IMAGE="${NGINX_BASE_IMAGE}" -C "${DIR}"/../../ clean-image build image-chroot
docker tag ${REGISTRY}/controller-chroot:${TAG} ${REGISTRY}/controller:${TAG}
else
make BASE_IMAGE="${NGINX_BASE_IMAGE}" -C "${DIR}"/../../ clean-image build image
make BASE_IMAGE="${NGINX_BASE_IMAGE}" -C "${DIR}"/../../ build image image-dataplane
fi

echo "[dev-env] .. done building controller images"
Expand All @@ -111,6 +111,6 @@ KIND_WORKERS=$(kind get nodes --name="${KIND_CLUSTER_NAME}" | grep worker | awk
echo "[dev-env] copying docker images to cluster..."

kind load docker-image --name="${KIND_CLUSTER_NAME}" --nodes="${KIND_WORKERS}" nginx-ingress-controller:e2e
kind load docker-image --name="${KIND_CLUSTER_NAME}" --nodes="${KIND_WORKERS}" "${REGISTRY}"/controller:"${TAG}"
kind load docker-image --name="${KIND_CLUSTER_NAME}" --nodes="${KIND_WORKERS}" "${REGISTRY}"/controller:"${TAG}" "${REGISTRY}"/dataplane:"${TAG}"
echo "[dev-env] running e2e tests..."
make -C "${DIR}"/../../ e2e-test
12 changes: 9 additions & 3 deletions version/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,18 @@ var (

// String returns information about the release.
func String() string {
ngxVer := nginx.Version()
controllerType := "controller"
if ngxVer != "N/A" {
ngxVer = fmt.Sprintf("NGINX: %s", ngxVer)
controllerType = "dataplane"
}
return fmt.Sprintf(`-------------------------------------------------------------------------------
NGINX Ingress controller
NGINX Ingress %s
Release: %v
Build: %v
Repository: %v
%v
%s
-------------------------------------------------------------------------------
`, RELEASE, COMMIT, REPO, nginx.Version())
`, controllerType, RELEASE, COMMIT, REPO, ngxVer)
}

0 comments on commit b045e9b

Please sign in to comment.