From 57394ba17afb377f2a1a4dba2b4086b53c4d54b4 Mon Sep 17 00:00:00 2001 From: Karthik Bhat Date: Wed, 25 Sep 2024 13:22:01 +0530 Subject: [PATCH] Add support for json log format (#1974) --- docs/book/src/developer/tilt.md | 36 +++++++++++++++++++++++++++++++++ go.mod | 4 +++- main.go | 11 ++++++++++ 3 files changed, 50 insertions(+), 1 deletion(-) diff --git a/docs/book/src/developer/tilt.md b/docs/book/src/developer/tilt.md index bf0a411d3..68323811c 100644 --- a/docs/book/src/developer/tilt.md +++ b/docs/book/src/developer/tilt.md @@ -155,6 +155,42 @@ kustomize_substitutions: IBMCLOUD_AUTH_URL: "https://iam.test.cloud.ibm.com" ``` +### 4. Configuration to use observability tools + +- cluster-api provides support for deploying observability tools, More information about it is available in cluster-api [book](https://cluster-api.sigs.k8s.io/developer/logging#developing-and-testing-logs). + +```yaml +default_registry: "gcr.io/you-project-name-here" +deploy_observability: + - promtail + - loki + - grafana + - prometheus +provider_repos: + - ../cluster-api-provider-ibmcloud +enable_providers: + - ibmcloud + - kubeadm-bootstrap + - kubeadm-control-plane +kustomize_substitutions: + IBMCLOUD_API_KEY: "XXXXXXXXXXXXXXXXXX" + PROVIDER_ID_FORMAT: "v2" + EXP_CLUSTER_RESOURCE_SET: "true" +extra_args: + core: + - "--logging-format=json" + - "--v=5" + kubeadm-bootstrap: + - "--v=5" + - "--logging-format=json" + kubeadm-control-plane: + - "--v=5" + - "--logging-format=json" + ibmcloud: + - "--v=5" + - "--logging-format=json" +``` + **NOTE**: For information about all the fields that can be used in the `tilt-settings.yaml` file, check them [here](https://cluster-api.sigs.k8s.io/developer/tilt.html#tilt-settings-fields). ## Run Tilt diff --git a/go.mod b/go.mod index 8379e6383..4f8a7a80d 100644 --- a/go.mod +++ b/go.mod @@ -37,6 +37,7 @@ require ( k8s.io/apimachinery v0.30.3 k8s.io/cli-runtime v0.30.3 k8s.io/client-go v0.30.3 + k8s.io/component-base v0.30.3 k8s.io/klog/v2 v2.120.1 k8s.io/utils v0.0.0-20231127182322-b307cd553661 sigs.k8s.io/cluster-api v1.8.3 @@ -80,6 +81,7 @@ require ( github.com/fsnotify/fsnotify v1.7.0 // indirect github.com/gabriel-vasile/mimetype v1.4.3 // indirect github.com/go-logr/stdr v1.2.2 // indirect + github.com/go-logr/zapr v1.3.0 // indirect github.com/go-openapi/analysis v0.21.5 // indirect github.com/go-openapi/errors v0.22.0 // indirect github.com/go-openapi/jsonpointer v0.20.1 // indirect @@ -162,6 +164,7 @@ require ( go.opentelemetry.io/otel/trace v1.24.0 // indirect go.opentelemetry.io/proto/otlp v1.0.0 // indirect go.uber.org/multierr v1.11.0 // indirect + go.uber.org/zap v1.27.0 // indirect golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect golang.org/x/oauth2 v0.21.0 // indirect golang.org/x/sync v0.8.0 // indirect @@ -180,7 +183,6 @@ require ( gopkg.in/yaml.v3 v3.0.1 // indirect k8s.io/apiserver v0.30.3 // indirect k8s.io/cluster-bootstrap v0.30.3 // indirect - k8s.io/component-base v0.30.3 // indirect k8s.io/kube-openapi v0.0.0-20240228011516-70dd3763d340 // indirect sigs.k8s.io/apiserver-network-proxy/konnectivity-client v0.30.0 // indirect sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect diff --git a/main.go b/main.go index 763367686..dc765c9e9 100644 --- a/main.go +++ b/main.go @@ -30,6 +30,8 @@ import ( "k8s.io/apimachinery/pkg/runtime" clientgoscheme "k8s.io/client-go/kubernetes/scheme" cgrecord "k8s.io/client-go/tools/record" + "k8s.io/component-base/logs" + logsv1 "k8s.io/component-base/logs/api/v1" "k8s.io/klog/v2" ctrl "sigs.k8s.io/controller-runtime" @@ -48,6 +50,7 @@ import ( "sigs.k8s.io/cluster-api-provider-ibmcloud/pkg/record" _ "k8s.io/client-go/plugin/pkg/client/auth/gcp" + _ "k8s.io/component-base/logs/json/register" ) var ( @@ -56,6 +59,7 @@ var ( healthAddr string syncPeriod time.Duration managerOptions = flags.ManagerOptions{} + logOptions = logs.NewOptions() webhookPort int webhookCertDir string @@ -126,6 +130,7 @@ func initFlags(fs *pflag.FlagSet) { fs.StringVar(&webhookCertDir, "webhook-cert-dir", "/tmp/k8s-webhook-server/serving-certs/", "The webhook certificate directory, where the server should find the TLS certificate and key.") + logsv1.AddFlags(logOptions, fs) flags.AddManagerOptions(fs, &managerOptions) } @@ -139,6 +144,12 @@ func validateFlags() error { default: return fmt.Errorf("invalid value for flag provider-id-fmt: %s, Supported values: %s, %s ", options.ProviderIDFormat, options.ProviderIDFormatV1, options.ProviderIDFormatV2) } + + if err := logsv1.ValidateAndApply(logOptions, nil); err != nil { + setupLog.Error(err, "unable to validate and apply log options") + return err + } + return nil }