Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(parser) add flag to log diff on failure #991

Merged
merged 22 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cli/ingress-controller/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
apiv1 "k8s.io/api/core/v1"

"github.com/kong/kubernetes-ingress-controller/pkg/annotations"
"github.com/kong/kubernetes-ingress-controller/pkg/util"
)

const (
Expand Down Expand Up @@ -80,6 +81,9 @@ type cliConfig struct {
LogLevel string
LogFormat string

// Diagnostics
DumpConfig util.ConfigDumpMode

// k8s connection details
APIServerHost string
KubeConfigFilePath string
Expand Down Expand Up @@ -199,6 +203,11 @@ trace, debug, info, warn, error, fatal and panic.`)
`Format of logs of the controller. Allowed values are
text and json.`)

// Diagnostics
flags.String("dump-config", "",
`Dump generated configuration to a temporary directory when set to "enabled".
When set to "sensitive", dumps will include certificate+key pairs and credentials.`)

// k8s connection details
flags.String("apiserver-host", "",
`The address of the Kubernetes Apiserver to connect to in the format of
Expand Down Expand Up @@ -311,6 +320,9 @@ func parseFlags() (cliConfig, error) {
config.LogLevel = viper.GetString("log-level")
config.LogFormat = viper.GetString("log-format")

// Diagnostics
config.DumpConfig = util.ParseConfigDumpMode(viper.GetString("dump-config"))
rainest marked this conversation as resolved.
Show resolved Hide resolved
rainest marked this conversation as resolved.
Show resolved Hide resolved

// k8s connection details
config.APIServerHost = viper.GetString("apiserver-host")
config.KubeConfigFilePath = viper.GetString("kubeconfig")
Expand Down
13 changes: 12 additions & 1 deletion cli/ingress-controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ func controllerConfigFromCLIConfig(cliConfig cliConfig) controller.Configuration
UpdateStatus: cliConfig.UpdateStatus,
UpdateStatusOnShutdown: cliConfig.UpdateStatusOnShutdown,
ElectionID: cliConfig.ElectionID,

DumpConfig: cliConfig.DumpConfig,
}
}

Expand Down Expand Up @@ -453,8 +455,17 @@ func main() {

store := store.New(cacheStores, cliConfig.IngressClass, cliConfig.ProcessClasslessIngressV1Beta1,
cliConfig.ProcessClasslessIngressV1, cliConfig.ProcessClasslessKongConsumer, log.WithField("component", "store"))

var dumpDir string
if cliConfig.DumpConfig > 0 {
rainest marked this conversation as resolved.
Show resolved Hide resolved
var err error
dumpDir, err = ioutil.TempDir("", "controller")
if err != nil {
log.Fatalf("failed to create a dump directory: %v", err)
}
}
kong, err := controller.NewKongController(ctx, &controllerConfig, updateChannel,
store)
store, dumpDir)
rainest marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
log.Fatalf("failed to create a controller: %v", err)
}
Expand Down
1 change: 1 addition & 0 deletions cli/ingress-controller/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ func TestHandleSigterm(t *testing.T) {
channels.NewRingChannel(1024),
store.New(store.CacheStores{}, conf.IngressClass, conf.ProcessClasslessIngressV1Beta1,
conf.ProcessClasslessIngressV1, conf.ProcessClasslessKongConsumer, logrus.New()),
"", // dump dir, not used in this test
)

exitCh := make(chan int, 1)
Expand Down
9 changes: 8 additions & 1 deletion internal/ingress/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ type Configuration struct {
EnableKnativeIngressSupport bool

Logger logrus.FieldLogger

DumpConfig util.ConfigDumpMode
}

// sync collects all the pieces required to assemble the configuration file and
Expand Down Expand Up @@ -129,7 +131,8 @@ func (n *KongController) syncIngress(interface{}) error {
func NewKongController(ctx context.Context,
config *Configuration,
updateCh *channels.RingChannel,
store store.Storer) (*KongController, error) {
store store.Storer,
dumpDir string) (*KongController, error) {
eventBroadcaster := record.NewBroadcaster()
eventBroadcaster.StartRecordingToSink(&v1core.EventSinkImpl{
Interface: config.KubeClient.CoreV1().Events(config.Namespace),
Expand All @@ -152,6 +155,8 @@ func NewKongController(ctx context.Context,
n.syncQueue = task.NewTaskQueue(n.syncIngress,
config.Logger.WithField("component", "sync-queue"))

n.dumpDir = dumpDir

electionID := config.ElectionID + "-" + config.IngressClass

electionConfig := election.Config{
Expand Down Expand Up @@ -231,6 +236,8 @@ type KongController struct {
PluginSchemaStore PluginSchemaStore

Logger logrus.FieldLogger

dumpDir string
}

// Start starts a new master process running in foreground, blocking until the next call to
Expand Down
22 changes: 22 additions & 0 deletions internal/ingress/controller/kong.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ import (
"crypto/sha256"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"reflect"
"sort"
"strings"
Expand Down Expand Up @@ -72,8 +74,28 @@ func (n *KongController) OnUpdate(ctx context.Context, state *kongstate.KongStat
} else {
err = n.onUpdateDBMode(targetContent)
}
var target []byte
if n.cfg.DumpConfig == util.ConfigDumpModeSensitive {
target, _ = json.Marshal(targetContent)
} else if n.cfg.DumpConfig == util.ConfigDumpModeEnabled {
sanitizedState := state.SanitizedCopy()
target, _ = json.Marshal(n.toDeckContent(ctx, sanitizedState))
rainest marked this conversation as resolved.
Show resolved Hide resolved
}
if err != nil {
if len(target) > 0 {
dumpErr := ioutil.WriteFile(filepath.Join(n.dumpDir, "target.json"), target, 0600)
if dumpErr != nil {
n.Logger.Warnf("failed to dump configuration: %s", dumpErr)
}
}
return err
} else {
if len(target) > 0 {
dumpErr := ioutil.WriteFile(filepath.Join(n.dumpDir, "last_good.json"), target, 0600)
if dumpErr != nil {
n.Logger.Warnf("failed to dump configuration: %s", dumpErr)
}
rainest marked this conversation as resolved.
Show resolved Hide resolved
}
}
n.runningConfigHash = shaSum
n.Logger.Info("successfully synced configuration to kong")
Expand Down
19 changes: 19 additions & 0 deletions pkg/util/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,22 @@ type RawSSLCert struct {
Cert []byte
Key []byte
}

type ConfigDumpMode int

const (
ConfigDumpModeOff ConfigDumpMode = iota
ConfigDumpModeEnabled ConfigDumpMode = iota
ConfigDumpModeSensitive ConfigDumpMode = iota
)

func ParseConfigDumpMode(in string) ConfigDumpMode {
switch in {
case "enabled":
return ConfigDumpModeEnabled
case "sensitive":
return ConfigDumpModeSensitive
default:
return ConfigDumpModeOff
}
}