Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
The diff you're trying to view is too large. We only load the first 3000 changed files.
4 changes: 1 addition & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,7 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: go.mod
- uses: golangci/golangci-lint-action@v6
with:
version: v1.62
- run: make lint

test:
name: Unit Test
Expand Down
21 changes: 14 additions & 7 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
version: "2"
run:
build-tags:
- build
timeout: 10m

linters:
default: standard
enable:
- errcheck
- goimports
- gofmt
- gosec
- gocritic
- deadcode
- gosec
- misspell
- revive
- unused
exclusions:
generated: lax
presets:
- comments
formatters:
enable:
- gofmt
- goimports
exclusions:
generated: lax

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this coming from another project? Maybe we can have stricter rules than the project this came from?

7 changes: 5 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
TOOLS_DIR := tools
include tools/tools.mk

TAG ?= $(shell git describe --match=NeVeRmAtCh --always --abbrev=40 --dirty)
CONTAINER_RUNTIME ?= podman

Expand All @@ -17,8 +20,8 @@ test:
go test ./...

.PHONY: lint
lint:
golangci-lint run
lint: $(TOOLS_BINDIR)/golangci-lint
"$(TOOLS_BINDIR)"/golangci-lint run

.PHONY: image
image:
Expand Down
32 changes: 14 additions & 18 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@ package main

import (
"flag"
"os"
"os/signal"
"syscall"

nodeporthandler "github.com/crc-org/routes-controller/pkg/node-port-handler"
routeshandler "github.com/crc-org/routes-controller/pkg/routes-handler"
routeclientset "github.com/openshift/client-go/route/clientset/versioned"
log "github.com/sirupsen/logrus"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/clientcmd"
"os"
"os/signal"
"syscall"
)

var (
Expand All @@ -35,30 +36,28 @@ func main() {
config, err := clientcmd.BuildConfigFromFlags(master, kubeconfig)
if err != nil {
log.Fatal(err)
return
}

// setup informer stop channel
stop := make(chan struct{})
defer close(stop)

// run node port handler
nodePortClientSet, err := kubernetes.NewForConfig(config)
if err != nil {
log.Fatal(err)
return
}
nodePortHandler := nodeporthandler.NodePortHandler(nodePortClientSet)
go func() {
nodePortHandler.Run(stop)
}()

// run routes handler
routesClientSet, err := routeclientset.NewForConfig(config)
if err != nil {
log.Fatal(err)
return
}

// setup informer stop channel
stop := make(chan struct{})
defer close(stop)

nodePortHandler := nodeporthandler.NodePortHandler(nodePortClientSet)
go func() {
nodePortHandler.Run(stop)
}()
routePortHandler := routeshandler.RoutesHandler(routesClientSet)
go func() {
routePortHandler.Run(stop)
Expand All @@ -67,8 +66,5 @@ func main() {
// block until sigterm
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGTERM)
select {
case <-signalCh:
return
}
<-signalCh
}
12 changes: 8 additions & 4 deletions pkg/node-port-handler/node-port-handler.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
package node_port_handler
package nodeporthandler

import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"time"

log "github.com/sirupsen/logrus"
v1 "k8s.io/api/core/v1"
"k8s.io/client-go/informers"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/tools/cache"
"net/http"
"time"
)

const (
Expand All @@ -22,7 +23,7 @@ func NodePortHandler(clientset *kubernetes.Clientset) cache.SharedIndexInformer
factory := informers.NewSharedInformerFactory(clientset, 5*time.Minute)
informer := factory.Core().V1().Services().Informer()

informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
service := obj.(*v1.Service)
log.Infof("Added service '%s' of type '%s'", service.GetName(), service.Spec.Type)
Expand Down Expand Up @@ -76,6 +77,9 @@ func NodePortHandler(clientset *kubernetes.Clientset) cache.SharedIndexInformer
}
},
})
if err != nil {
log.Errorf("failed to add event handler: %v", err)
}

return informer
}
Expand Down
10 changes: 7 additions & 3 deletions pkg/routes-handler/routes-handler.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes_handler
package routeshandler

import (
"bytes"
Expand All @@ -18,7 +18,7 @@ import (
func RoutesHandler(clientset *routeclientset.Clientset) cache.SharedIndexInformer {
factory := informers.NewSharedInformerFactory(clientset, 5*time.Minute)
informer := factory.Route().V1().Routes().Informer()
informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
_, err := informer.AddEventHandler(cache.ResourceEventHandlerFuncs{
AddFunc: func(obj interface{}) {
route := obj.(*v1.Route)
log.Infof("added: %s %s", route.GetName(), route.Spec.Host)
Expand Down Expand Up @@ -47,6 +47,9 @@ func RoutesHandler(clientset *routeclientset.Clientset) cache.SharedIndexInforme
}
},
})
if err != nil {
log.Errorf("failed to add event handler: %v", err)
}
return informer
}

Expand All @@ -62,11 +65,12 @@ var removeHostURLs = []string{

// postJSON sends a POST request with JSON body to url and returns an error on failure or non-2xx status.
func postJSON(url string, body []byte) error {
//nolint:gosec // URL are constants from addHostURLs or removeHostURLs
resp, err := http.Post(url, "application/json", bytes.NewReader(body))
if err != nil {
return err
}
defer resp.Body.Close()
defer func() { _ = resp.Body.Close() }()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("%s: server returned %d", url, resp.StatusCode)
}
Expand Down
18 changes: 9 additions & 9 deletions pkg/routes-handler/routes-handler_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package routes_handler
package routeshandler

import (
"encoding/json"
Expand All @@ -24,7 +24,7 @@ func TestExpose_FirstURLSucceeds(t *testing.T) {
}))
defer srv1.Close()

srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
calledSecondURL.Add(1)
w.WriteHeader(http.StatusOK)
}))
Expand All @@ -46,7 +46,7 @@ func TestExpose_FirstURLSucceeds(t *testing.T) {
func TestExpose_FirstURLFails_SecondSucceeds(t *testing.T) {
var calledFirstURL, calledSecondURL atomic.Int32

srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
calledFirstURL.Add(1)
w.WriteHeader(http.StatusInternalServerError)
}))
Expand Down Expand Up @@ -82,7 +82,7 @@ func TestUnexpose_FirstURLSucceeds(t *testing.T) {
}))
defer srv1.Close()

srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
calledSecondURL.Add(1)
w.WriteHeader(http.StatusOK)
}))
Expand All @@ -104,7 +104,7 @@ func TestUnexpose_FirstURLSucceeds(t *testing.T) {
func TestUnexpose_FirstURLFails_SecondSucceeds(t *testing.T) {
var calledFirstURL, calledSecondURL atomic.Int32

srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
calledFirstURL.Add(1)
w.WriteHeader(http.StatusInternalServerError)
}))
Expand All @@ -131,12 +131,12 @@ func TestUnexpose_FirstURLFails_SecondSucceeds(t *testing.T) {
}

func TestExpose_BothURLsFail(t *testing.T) {
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer srv1.Close()

srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer srv2.Close()
Expand All @@ -149,12 +149,12 @@ func TestExpose_BothURLsFail(t *testing.T) {
}

func TestUnexpose_BothURLsFail(t *testing.T) {
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv1 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusBadGateway)
}))
defer srv1.Close()

srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
srv2 := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer srv2.Close()
Expand Down
1 change: 1 addition & 0 deletions tools/bin/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/*
Loading
Loading