Skip to content

Commit

Permalink
Merge branch 'main' into sourceranges
Browse files Browse the repository at this point in the history
  • Loading branch information
aojea committed Jun 2, 2024
2 parents 7a54479 + da6ad18 commit 5156616
Show file tree
Hide file tree
Showing 11 changed files with 233 additions and 89 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/k8s.yml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ jobs:
- name: Run cloud-provider-kind
run: |
make
nohup bin/cloud-provider-kind > ./_artifacts/ccm-kind.log 2>&1 &
nohup bin/cloud-provider-kind -v 2 --enable-log-dumping --logs-dir ./_artifacts/loadbalancers > ./_artifacts/ccm-kind.log 2>&1 &
- name: Create multi node cluster
run: |
Expand Down Expand Up @@ -175,6 +175,7 @@ jobs:
run: |
/usr/local/bin/kind export logs --name ${{ env.KIND_CLUSTER_NAME}} --loglevel=debug ./_artifacts/logs
cp ./_artifacts/ccm-kind.log ./_artifacts/logs
cp ./_artifacts/loadbalancers/* ./_artifacts/logs
- name: Upload logs
if: always()
Expand All @@ -187,4 +188,4 @@ jobs:
uses: mikepenz/action-junit-report@v2
if: always()
with:
report_paths: './_artifacts/*.xml'
report_paths: './_artifacts/*.xml'
13 changes: 10 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,18 @@ Another alternative is to run it as a container, but this will require to mount
the docker socket inside the container:

```sh
docker build . -t aojea/cloud-provider-kind:v0.1
docker build . -t cloud-provider-kind
# using the host network
docker run --rm --network host -v /var/run/docker.sock:/var/run/docker.sock aojea/cloud-provider-kind:v0.1
docker run --rm --network host -v /var/run/docker.sock:/var/run/docker.sock cloud-provider-kind
# or the kind network
docker run --rm --network kind -v /var/run/docker.sock:/var/run/docker.sock aojea/cloud-provider-kind:v0.1
docker run --rm --network kind -v /var/run/docker.sock:/var/run/docker.sock cloud-provider-kind
```

Or using `compose.yaml` file:

```sh
# using the `kind` network (`host` is the default value for NET_MODE)
NET_MODE=kind docker compose up -d
```

## How to use it
Expand Down
102 changes: 102 additions & 0 deletions cmd/app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package cmd

import (
"context"
"flag"
"fmt"
"os"
"os/signal"
"strconv"
"syscall"

"k8s.io/component-base/logs"
"k8s.io/klog/v2"

"sigs.k8s.io/cloud-provider-kind/pkg/config"
"sigs.k8s.io/cloud-provider-kind/pkg/controller"
kindcmd "sigs.k8s.io/kind/pkg/cmd"
)

var (
flagV int
enableLogDump bool
logDumpDir string
)

func init() {
flag.IntVar(&flagV, "v", 2, "Verbosity level")
flag.BoolVar(&enableLogDump, "enable-log-dumping", false, "store logs to a temporal directory or to the directory specified using the logs-dir flag")
flag.StringVar(&logDumpDir, "logs-dir", "", "store logs to the specified directory")

flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: cloud-provider-kind [options]\n\n")
flag.PrintDefaults()
}
}

func Main() {
// Parse command line flags and arguments
flag.Parse()
flag.VisitAll(func(flag *flag.Flag) {
klog.Infof("FLAG: --%s=%q", flag.Name, flag.Value)
})

// trap Ctrl+C and call cancel on the context
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)

// Enable signal handler
signalCh := make(chan os.Signal, 2)
defer func() {
close(signalCh)
cancel()
}()

signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
go func() {
select {
case <-signalCh:
klog.Infof("Exiting: received signal")
cancel()
case <-ctx.Done():
// cleanup
}
}()

// initialize loggers, kind logger and klog
logger := kindcmd.NewLogger()
type verboser interface {
SetVerbosity(int)
}
v, ok := logger.(verboser)
if ok {
v.SetVerbosity(flagV)
}

_, err := logs.GlogSetter(strconv.Itoa(flagV))
if err != nil {
logger.Errorf("error setting klog verbosity to %d : %v", flagV, err)
}

// initialize log directory
if enableLogDump {
if logDumpDir == "" {
dir, err := os.MkdirTemp(os.TempDir(), "kind-provider-")
if err != nil {
klog.Fatal(err)
}
logDumpDir = dir
}

if _, err := os.Stat(logDumpDir); os.IsNotExist(err) {
if err := os.MkdirAll(logDumpDir, 0755); err != nil {
klog.Fatalf("directory %s does not exist: %v", logDumpDir, err)
}
}
config.DefaultConfig.EnableLogDump = true
config.DefaultConfig.LogDir = logDumpDir
klog.Infof("**** Dumping load balancers logs to: %s", logDumpDir)
}

controller.New(logger).Run(ctx)
}
6 changes: 6 additions & 0 deletions compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
services:
cloud-provider:
build: .
network_mode: "${NET_MODE-host}"
volumes:
- /var/run/docker.sock:/var/run/docker.sock
2 changes: 1 addition & 1 deletion hack/ci/e2e.sh
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ main() {

# build cloud-provider-kind
make
nohup bin/cloud-provider-kind > ${ARTIFACTS}/ccm-kind.log 2>&1 &
nohup bin/cloud-provider-kind --enable-log-dumping --logs-dir ${ARTIFACTS}/loadbalancers > ${ARTIFACTS}/ccm-kind.log 2>&1 &

# build kubernetes
K8S_PATH=$(find ${GOPATH} -path '*/k8s.io/kubernetes/go.mod' -print -quit)
Expand Down
68 changes: 2 additions & 66 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,73 +1,9 @@
package main

import (
"context"
"flag"
"fmt"
"log"
"os"
"os/signal"
"strconv"
"syscall"

"k8s.io/component-base/logs"

"sigs.k8s.io/cloud-provider-kind/pkg/controller"

kindcmd "sigs.k8s.io/kind/pkg/cmd"
"sigs.k8s.io/cloud-provider-kind/cmd"
)

var (
flagV int
)

func init() {
flag.IntVar(&flagV, "v", 2, "Verbosity level")

flag.Usage = func() {
fmt.Fprint(os.Stderr, "Usage: cloud-provider-kind [options]\n\n")
flag.PrintDefaults()
}
}

func main() {
// Parse command line flags and arguments
flag.Parse()

// trap Ctrl+C and call cancel on the context
ctx := context.Background()
ctx, cancel := context.WithCancel(ctx)

// Enable signal handler
signalCh := make(chan os.Signal, 2)
defer func() {
close(signalCh)
cancel()
}()

signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
go func() {
select {
case <-signalCh:
log.Printf("Exiting: received signal")
cancel()
case <-ctx.Done():
// cleanup
}
}()

logger := kindcmd.NewLogger()
type verboser interface {
SetVerbosity(int)
}
v, ok := logger.(verboser)
if ok {
v.SetVerbosity(flagV)
}

_, err := logs.GlogSetter(strconv.Itoa(flagV))
if err != nil {
logger.Errorf("error setting klog verbosity to %d : %v", flagV, err)
}
controller.New(logger).Run(ctx)
cmd.Main()
}
10 changes: 10 additions & 0 deletions pkg/config/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package config

// DefaultConfig is a global variable that is initialized at startup with the flags options.
// It can not be modified after that.
var DefaultConfig = &Config{}

type Config struct {
EnableLogDump bool
LogDir string
}
31 changes: 29 additions & 2 deletions pkg/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ import (
"encoding/json"
"fmt"
"io"
"os"
"os/exec"
"path"
"strings"

"k8s.io/klog/v2"
Expand Down Expand Up @@ -43,6 +45,31 @@ func init() {
}
}

func Logs(name string, w io.Writer) error {
cmd := exec.Command(containerRuntime, []string{"logs", name}...)
cmd.Stderr = w
cmd.Stdout = w
err := cmd.Run()
if err != nil {
return fmt.Errorf("failed to get container logs: %w", err)
}
return nil
}

func LogDump(containerName string, dir string) error {
f, err := os.Create(path.Join(dir, containerName+".log"))
if err != nil {
return err
}
defer f.Close()

err = Logs(containerName, f)
if err != nil {
return err
}
return nil
}

func Create(name string, args []string) error {
if err := exec.Command(containerRuntime, append([]string{"run", "--name", name}, args...)...).Run(); err != nil {
return err
Expand Down Expand Up @@ -157,8 +184,8 @@ func PortMaps(name string) (map[string]string, error) {
if len(parts) == 2 {
protocol = strings.ToLower(parts[1])
}
if protocol != "tcp" {
klog.Infof("skipping protocol %s not supported, only TCP", protocol)
if protocol != "tcp" && protocol != "udp" {
klog.Infof("skipping protocol %s not supported, only UDP and TCP", protocol)
continue
}

Expand Down
11 changes: 5 additions & 6 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,6 @@ func New(logger log.Logger) *Controller {
func (c *Controller) Run(ctx context.Context) {
defer c.cleanup()
for {
select {
case <-ctx.Done():
return
default:
}
// get existing kind clusters
clusters, err := c.kind.List()
if err != nil {
Expand Down Expand Up @@ -105,7 +100,11 @@ func (c *Controller) Run(ctx context.Context) {
delete(c.clusters, cluster)
}
}
time.Sleep(30 * time.Second)
select {
case <-ctx.Done():
return
case <-time.After(30 * time.Second):
}
}
}

Expand Down
Loading

0 comments on commit 5156616

Please sign in to comment.