From 7d0cb372315d5c32f72cd7172a71991b17cf7d8c Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Fri, 7 Jun 2024 13:18:53 +0200 Subject: [PATCH 1/5] schema: Change schema version to `0.2.0` --- schema/mysql/schema.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/schema/mysql/schema.sql b/schema/mysql/schema.sql index 1a41b63..3aa0be2 100644 --- a/schema/mysql/schema.sql +++ b/schema/mysql/schema.sql @@ -959,4 +959,4 @@ CREATE TABLE kubernetes_schema ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin; INSERT INTO kubernetes_schema (version, timestamp, success, reason) -VALUES ('0.1.0', UNIX_TIMESTAMP() * 1000, 'y', 'Initial import'); +VALUES ('0.2.0', UNIX_TIMESTAMP() * 1000, 'y', 'Initial import'); From c1fc2daadb0a4b8d4207df3055a7335e87b2684b Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Fri, 7 Jun 2024 13:26:30 +0200 Subject: [PATCH 2/5] Drop all tables if current schema version is not equal `0.2.0` --- cmd/icinga-kubernetes/main.go | 67 +++++++++++++++++++++++++++++++++-- 1 file changed, 65 insertions(+), 2 deletions(-) diff --git a/cmd/icinga-kubernetes/main.go b/cmd/icinga-kubernetes/main.go index 246f343..b8af1a3 100644 --- a/cmd/icinga-kubernetes/main.go +++ b/cmd/icinga-kubernetes/main.go @@ -11,9 +11,11 @@ import ( "github.com/icinga/icinga-go-library/periodic" "github.com/icinga/icinga-go-library/types" "github.com/icinga/icinga-kubernetes/internal" + "github.com/icinga/icinga-kubernetes/pkg/backoff" "github.com/icinga/icinga-kubernetes/pkg/com" "github.com/icinga/icinga-kubernetes/pkg/database" "github.com/icinga/icinga-kubernetes/pkg/metrics" + "github.com/icinga/icinga-kubernetes/pkg/retry" schemav1 "github.com/icinga/icinga-kubernetes/pkg/schema/v1" "github.com/icinga/icinga-kubernetes/pkg/sync" syncv1 "github.com/icinga/icinga-kubernetes/pkg/sync/v1" @@ -33,6 +35,8 @@ import ( "time" ) +const expectedSchemaVersion = "0.2.0" + func main() { runtime.ReallyCrash = true @@ -100,6 +104,67 @@ func main() { klog.Fatal(err) } + g, ctx := errgroup.WithContext(context.Background()) + + if hasSchema { + var version string + + err = retry.WithBackoff( + ctx, + func(ctx context.Context) (err error) { + query := "SELECT version FROM kubernetes_schema ORDER BY id DESC LIMIT 1" + err = db.QueryRowxContext(ctx, query).Scan(&version) + if err != nil { + err = database.CantPerformQuery(err, query) + } + return + }, + retry.Retryable, + backoff.NewExponentialWithJitter(128*time.Millisecond, 1*time.Minute), + retry.Settings{}) + if err != nil { + klog.Fatal(err) + } + + if version != expectedSchemaVersion { + err = retry.WithBackoff( + ctx, + func(ctx context.Context) (err error) { + rows, err := db.Query( + db.Rebind("SELECT table_name FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA=?"), + cfg.Database.Database, + ) + if err != nil { + klog.Fatal(err) + } + defer rows.Close() + + dbLog.Info("Dropping schema") + + for rows.Next() { + var tableName string + if err := rows.Scan(&tableName); err != nil { + klog.Fatal(err) + } + + _, err := db.Exec("DROP TABLE " + tableName) + if err != nil { + klog.Fatal(err) + } + } + return + }, + retry.Retryable, + backoff.NewExponentialWithJitter(128*time.Millisecond, 1*time.Minute), + retry.Settings{}) + if err != nil { + klog.Fatal(err) + } + + hasSchema = false + } + } + if !hasSchema { dbLog.Info("Importing schema") @@ -112,8 +177,6 @@ func main() { } } - g, ctx := errgroup.WithContext(context.Background()) - if _, err := db.ExecContext(ctx, "DELETE FROM kubernetes_instance"); err != nil { klog.Fatal(errors.Wrap(err, "can't delete instance")) } From 3338dafe7602838297c8be25054846c46cd73918 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 26 Sep 2024 12:27:40 +0200 Subject: [PATCH 3/5] Set version to `0.2.0` --- internal/version.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/version.go b/internal/version.go index 2e1ac13..0bddead 100644 --- a/internal/version.go +++ b/internal/version.go @@ -7,4 +7,4 @@ import ( // Version contains version and Git commit information. // // The placeholders are replaced on `git archive` using the `export-subst` attribute. -var Version = version.Version("Icinga Kubernetes", "0.1.0", "$Format:%(describe)$", "$Format:%H$") +var Version = version.Version("Icinga Kubernetes", "0.2.0", "$Format:%(describe)$", "$Format:%H$") From fab982498a0c9bc28371c7bff848a26233d65806 Mon Sep 17 00:00:00 2001 From: Eric Lippmann Date: Thu, 26 Sep 2024 14:48:57 +0200 Subject: [PATCH 4/5] `Docs`: Drop `edge` tag from container --- doc/02-Installation.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/02-Installation.md b/doc/02-Installation.md index 1517581..f08c737 100644 --- a/doc/02-Installation.md +++ b/doc/02-Installation.md @@ -98,8 +98,8 @@ run the `icinga/icinga-kubernetes` image using a container runtime of you choice ```bash export KUBECONFIG=$HOME/.kube/config -export ICINGA_KUBERNETES_CONFIG=config.yml -docker run --rm -v $ICINGA_KUBERNETES_CONFIG:/config.yml -v $KUBECONFIG:/.kube/config icinga/icinga-kubernetes:edge +export ICINGA_KUBERNETES_CONFIG=./config.yml +docker run --rm -v $ICINGA_KUBERNETES_CONFIG:/config.yml -v $KUBECONFIG:/.kube/config icinga/icinga-kubernetes ``` #### From Source From a30d8c648272ddec0f5934839910ff45fee4efd6 Mon Sep 17 00:00:00 2001 From: Jonada Hoxha Date: Thu, 26 Sep 2024 15:18:35 +0200 Subject: [PATCH 5/5] Log Icinga for Kubernetes version --- cmd/icinga-kubernetes/main.go | 2 ++ 1 file changed, 2 insertions(+) diff --git a/cmd/icinga-kubernetes/main.go b/cmd/icinga-kubernetes/main.go index b8af1a3..a57bb52 100644 --- a/cmd/icinga-kubernetes/main.go +++ b/cmd/icinga-kubernetes/main.go @@ -65,6 +65,8 @@ func main() { os.Exit(0) } + klog.Infof("Starting Icinga for Kubernetes (%s)", internal.Version.Version) + kconfig, err := kclientcmd.NewNonInteractiveDeferredLoadingClientConfig(loadingRules, &overrides).ClientConfig() if err != nil { if kclientcmd.IsEmptyConfig(err) {