Skip to content

Commit

Permalink
make controller configable with file
Browse files Browse the repository at this point in the history
  • Loading branch information
jzwlqx committed Feb 2, 2024
1 parent 6c9e2e7 commit 3a315fa
Show file tree
Hide file tree
Showing 14 changed files with 168 additions and 74 deletions.
9 changes: 9 additions & 0 deletions deploy/controller-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
logLevel: debug
server:
httpPort: 10264
agentPort: 10263
controller:
database:
type: sqlite3
diagnose: {}

File renamed without changes.
28 changes: 24 additions & 4 deletions deploy/skoopbundle.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,23 @@ subjects:
name: default
namespace: kubeskoop
---
apiVersion: v1
data:
controller.yaml: |-
logLevel: debug
server:
httpPort: 10264
agentPort: 10263
controller:
prometheus: http://prometheus-service
database:
type: sqlite3
diagnose: {}
kind: ConfigMap
metadata:
name: kubeskoop-controller-config
namespace: kubeskoop
---
apiVersion: apps/v1
kind: Deployment
metadata:
Expand All @@ -585,16 +602,19 @@ spec:
image: kubeskoop/kubeskoop:v1.0.0
command: ["/bin/controller"]
env:
- name: PROMETHEUS_ENDPOINT
value: http://prometheus-service
- name: LOKI_ENDPOINT
value: http://loki-service:3100
volumeMounts:
- name: db
- name: lib
mountPath: /var/lib/kubeskoop
- name: config
mountPath: /etc/kubeskoop
volumes:
- name: db
- name: lib
emptyDir: {}
- name: config
configMap:
name: kubeskoop-controller-config
---
apiVersion: v1
kind: Service
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ require (
github.com/docker/docker v20.10.22+incompatible
github.com/fsnotify/fsnotify v1.6.0
github.com/gin-gonic/gin v1.9.1
github.com/go-sql-driver/mysql v1.6.0
github.com/golang/snappy v0.0.4
github.com/google/gops v0.3.26
github.com/gorilla/mux v1.8.0
Expand Down
15 changes: 7 additions & 8 deletions pkg/controller/cmd/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,22 @@ package cmd

import (
"fmt"
"github.com/alibaba/kubeskoop/pkg/controller/db"
"github.com/alibaba/kubeskoop/pkg/controller/diagnose"
"gopkg.in/yaml.v3"
"os"

"github.com/alibaba/kubeskoop/pkg/controller/service"
"gopkg.in/yaml.v3"
)

type ServerConfig struct {
AgentPort int `yaml:"agentPort"`
HttpPort int `yaml:"httpPort"`
HTTPPort int `yaml:"httpPort"`
KubeConfig string `yaml:"kubeConfig"`
}

type Config struct {
LogLevel string `yaml:"logLevel"`
Server ServerConfig `yaml:"server"`
DB db.Config `yaml:"database"`
Diagnose diagnose.Config `yaml:"diagnose"`
LogLevel string `yaml:"logLevel"`
Server ServerConfig `yaml:"server"`
Controller service.Config `yaml:"controller"`
}

func loadConfig(path string) (*Config, error) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/controller/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package cmd

import (
"fmt"
log "github.com/sirupsen/logrus"
"os"

log "github.com/sirupsen/logrus"

"github.com/spf13/cobra"
)

Expand Down Expand Up @@ -39,14 +40,14 @@ var (
if agentPort > 0 {
config.Server.AgentPort = agentPort
}
if config.Server.HttpPort == 0 {
config.Server.HttpPort = defaultHTTPPort
if config.Server.HTTPPort == 0 {
config.Server.HTTPPort = defaultHTTPPort
}
if httpPort > 0 {
config.Server.HttpPort = httpPort
config.Server.HTTPPort = httpPort
}

NewServer().Run(config.Server.AgentPort, config.Server.HttpPort)
NewServer(config).Run()
return nil
},
}
Expand Down
12 changes: 7 additions & 5 deletions pkg/controller/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,25 @@ const (
)

type Server struct {
config ServerConfig
controller service.ControllerService
}

func NewServer() *Server {
ctrlSVC, err := service.NewControllerService()
func NewServer(config *Config) *Server {
ctrlSVC, err := service.NewControllerService(&config.Controller)
if err != nil {
log.Fatalf("error create controller service: %v", err)
}
return &Server{
config: config.Server,
controller: ctrlSVC,
}
}

func (s *Server) Run(agentPort int, httpPort int) {
func (s *Server) Run() {
done := make(chan struct{})
go s.RunAgentServer(agentPort, done)
go s.RunHTTPServer(httpPort, done)
go s.RunAgentServer(s.config.AgentPort, done)
go s.RunHTTPServer(s.config.HTTPPort, done)
go s.controller.Run(done)

signals := make(chan os.Signal, 1)
Expand Down
5 changes: 3 additions & 2 deletions pkg/controller/db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ var (
type Driver func(config *Config) (ddl string, db *sqlx.DB, err error)

var drivers = map[string]Driver{
"sqlite": newSQLite3,
"mysql": newMySQL,
"sqlite": newSQLite3,
"sqlite3": newSQLite3,
"mysql": newMySQL,
}

func InitializeDB(config *Config) error {
Expand Down
19 changes: 10 additions & 9 deletions pkg/controller/db/mysql.ddl
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
create table if not exists tasks
create table if not exists `tasks`
(
id integer primary key autoincrement,
config text not null,
start_time timestamp default now(),
finish_time timestamp default null,
status varchar(16) not null,
result text default null,
message varchar(4096)
);
`id` integer AUTO_INCREMENT,
`config` text not null,
`start_time` timestamp default now(),
`finish_time` timestamp null default null,
`status` varchar(16) not null,
`result` text default null,
`message` varchar(4096),
PRIMARY KEY(`id`)
);
6 changes: 5 additions & 1 deletion pkg/controller/db/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package db
import (
_ "embed"
"fmt"

"github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
log "github.com/sirupsen/logrus"
)

//go:embed mysql.ddl
Expand All @@ -16,11 +18,13 @@ func newMySQL(config *Config) (string, *sqlx.DB, error) {
cfg.User = config.Username
cfg.Passwd = config.Password
cfg.DBName = config.DBName
cfg.Net = "tcp"
dsn := cfg.FormatDSN()
log.Infof("addr %s, dsn: %s", cfg.Addr, dsn)
db, err := sqlx.Open("mysql", dsn)
if err != nil {
return "", nil, fmt.Errorf("failed config mysql: %w", err)
}

return "", db, nil
return mysqlDDL, db, nil
}
16 changes: 8 additions & 8 deletions pkg/controller/db/sqlite.ddl
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
create table if not exists tasks
create table if not exists `tasks`
(
id integer primary key autoincrement,
config text not null,
start_time timestamp default current_timestamp,
finish_time timestamp default null,
status varchar(16) not null,
result text default null,
message varchar(4096)
`id` integer primary key autoincrement,
`config` text not null,
`start_time` timestamp default current_timestamp,
`finish_time` timestamp default null,
`status` varchar(16) not null,
`result` text default null,
`message` varchar(4096)
);
18 changes: 17 additions & 1 deletion pkg/controller/db/sqlite.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@ package db

import (
_ "embed"
"errors"
"fmt"
"os"
"path/filepath"

"github.com/jmoiron/sqlx"
)

Expand All @@ -17,10 +21,22 @@ func newSQLite3(config *Config) (string, *sqlx.DB, error) {
path = defaultSQLitePath
}

if _, err := os.Stat(path); errors.Is(err, os.ErrNotExist) {
dir := filepath.Dir(path)
if err := os.MkdirAll(dir, 0755); err != nil {
return "", nil, fmt.Errorf("failed open sqlite db on path %s: %w", path, err)
}
f, err := os.Create(path)
if err != nil {
return "", nil, fmt.Errorf("failed open sqlite db on path %s: %w", path, err)
}
f.Close()
}

var err error
db, err = sqlx.Connect("sqlite3", path)
if err != nil {
return "", nil, fmt.Errorf("failed open sqlite db on path %s, err: %v", path, err)
return "", nil, fmt.Errorf("failed open sqlite db on path %s: %w", path, err)
}

return sqliteDDL, db, nil
Expand Down
44 changes: 33 additions & 11 deletions pkg/controller/diagnose/diagnose.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,29 @@ import (
skoopContext "github.com/alibaba/kubeskoop/pkg/skoop/context"
)

type Config struct {
KubeConfig string `yaml:"kubeConfig"`
CloudProvider string `yaml:"cloudProvider"`
NetworkPlugin string `yaml:"networkPlugin"`
ProxyModel string `yaml:"proxyModel"`
ClusterCidr string `yaml:"clusterCidr"`
}

type Controller interface {
Diagnose(ctx context.Context, taskConfig *skoopContext.TaskConfig) (string, error)
}

func NewDiagnoseController(namespace string) Controller {
var diagnoseArgs []string
if diagnoseArgsFromEnv, ok := os.LookupEnv("KUBESKOOP_DIAGNOSE_ARGS"); ok {
diagnoseArgs = strings.Split(diagnoseArgsFromEnv, " ")
}
func NewDiagnoseController(namespace string, config *Config) Controller {
// 1. build skoop global context
return &Diagnostor{
namespace: namespace,
diagnoseArgs: diagnoseArgs,
namespace: namespace,
config: config,
}
}

type Diagnostor struct {
namespace string
diagnoseArgs []string
namespace string
config *Config
}

func (d *Diagnostor) Diagnose(ctx context.Context, taskConfig *skoopContext.TaskConfig) (string, error) {
Expand All @@ -47,8 +51,8 @@ func (d *Diagnostor) Diagnose(ctx context.Context, taskConfig *skoopContext.Task
"--protocol", taskConfig.Protocol,
"--collector-namespace", d.namespace,
}
if len(d.diagnoseArgs) != 0 {
args = append(args, d.diagnoseArgs...)
if d.config != nil {
args = append(args, buildArgsFromConfig(d.config))
}
cmd := exec.CommandContext(ctx, "skoop", args...)
output, err := cmd.CombinedOutput()
Expand All @@ -61,3 +65,21 @@ func (d *Diagnostor) Diagnose(ctx context.Context, taskConfig *skoopContext.Task
}
return string(diagnoseResult), nil
}

func buildArgsFromConfig(config *Config) string {
var args []string
m := map[string]string{
"--cloud-provider": config.CloudProvider,
"--network-plugin": config.NetworkPlugin,
"--proxy-mode": config.ProxyModel,
"--cluster-cidr": config.ClusterCidr,
}

for k, v := range m {
if v != "" {
args = append(args, k, v)
}
}

return strings.Join(args, " ")
}
Loading

0 comments on commit 3a315fa

Please sign in to comment.