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

Fix namespace compat #1547

Merged
merged 2 commits into from
Jan 1, 2024
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions api/v1/common.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package v1

import (
"encoding/json"
"fmt"
"io/fs"
"net/url"
Expand Down Expand Up @@ -347,8 +348,13 @@ type Description struct {
Description string `yaml:"description,omitempty" json:"description,omitempty" template:"true"`
// Name of the check
Name string `yaml:"name" json:"name" template:"true"`
// Namespace of the check
Namespace string `yaml:"namespace,omitempty" json:"namespace,omitempty"`
// TODO: namespace is a json.RawMessage for backwards compatibility when it used to be a resource selector
// can be removed in a few versions time

// Namespace to insert the check into, if different to the namespace the canary is defined, e.g.
// +kubebuilder:validation:Schemaless
// +kubebuilder:validation:Type=string
Namespace json.RawMessage `yaml:"namespace,omitempty" json:"namespace,omitempty" jsonschema:"type=string"`
// Icon for overwriting default icon on the dashboard
Icon string `yaml:"icon,omitempty" json:"icon,omitempty" template:"true"`
// Labels for the check
Expand Down Expand Up @@ -383,7 +389,18 @@ func (d Description) GetName() string {
}

func (d Description) GetNamespace() string {
return d.Namespace
s := string(d.Namespace)
if s == "" || s == "{}" {
return ""
}
if !strings.HasPrefix(s, "{}") {
return s
}
var r types.ResourceSelector
if err := json.Unmarshal(d.Namespace, &r); err != nil {
return r.Name
}
return ""
}

func (d Description) GetLabels() map[string]string {
Expand Down
5 changes: 5 additions & 0 deletions api/v1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 4 additions & 12 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ import (
"github.com/flanksource/commons/logger"
"github.com/flanksource/duty"
"github.com/flanksource/duty/context"
gomplate "github.com/flanksource/gomplate/v3"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"go.opentelemetry.io/otel"
Expand Down Expand Up @@ -48,16 +47,11 @@ func InitContext() (context.Context, error) {
var Root = &cobra.Command{
Use: "canary-checker",
PersistentPreRun: func(cmd *cobra.Command, args []string) {

canary.LogFail = logFail
canary.LogPass = logPass

logger.UseZap(cmd.Flags())
for _, script := range sharedLibrary {
if err := gomplate.LoadSharedLibrary(script); err != nil {
logger.Errorf("Failed to load shared library %s: %v", script, err)
}
}

db.ConnectionString = readFromEnv(db.ConnectionString)
if db.ConnectionString == "DB_URL" {
db.ConnectionString = ""
Expand All @@ -78,8 +72,6 @@ var Root = &cobra.Command{
var (
httpPort = 8080
publicEndpoint = "http://localhost:8080"
sharedLibrary []string
exposeEnv bool
logPass, logFail bool

otelcollectorURL string
Expand All @@ -98,6 +90,8 @@ func ServerFlags(flags *pflag.FlagSet) {
_ = flags.MarkDeprecated("dev", "")
_ = flags.MarkDeprecated("push-servers", "")
_ = flags.MarkDeprecated("pull-servers", "")
_ = flags.MarkDeprecated("expose-env", "")
_ = flags.MarkDeprecated("shared-library", "")

flags.StringVar(&publicEndpoint, "public-endpoint", publicEndpoint, "Host on which the health dashboard is exposed. Could be used for generting-links, redirects etc.")
flags.StringSliceVar(&runner.IncludeCanaries, "include-check", []string{}, "Run matching canaries - useful for debugging")
Expand Down Expand Up @@ -141,10 +135,8 @@ func init() {
Root.PersistentFlags().StringVar(&db.ConnectionString, "db", "DB_URL", "Connection string for the postgres database")
Root.PersistentFlags().BoolVar(&db.RunMigrations, "db-migrations", false, "Run database migrations")
Root.PersistentFlags().BoolVar(&db.DBMetrics, "db-metrics", false, "Expose db metrics")
Root.PersistentFlags().BoolVar(&logFail, "log-fail", true, "Log every failing check")
Root.PersistentFlags().BoolVar(&logFail, "log-fail", false, "Log every failing check")
Root.PersistentFlags().BoolVar(&logPass, "log-pass", false, "Log every passing check")
Root.PersistentFlags().StringArrayVar(&sharedLibrary, "shared-library", []string{}, "Add javascript files to be shared by all javascript templates")
Root.PersistentFlags().BoolVar(&exposeEnv, "expose-env", false, "Expose environment variables for use in all templates. Note this has serious security implications with untrusted canaries")
Root.AddCommand(Docs)
Root.AddCommand(Run, Serve, Operator)
Root.AddCommand(Serve, GoOffline)
Expand Down
Loading
Loading