Skip to content

Commit

Permalink
Migrate to structured logging (klog)
Browse files Browse the repository at this point in the history
  • Loading branch information
aledbf committed Sep 27, 2020
1 parent 93ac8d5 commit 108637b
Show file tree
Hide file tree
Showing 31 changed files with 161 additions and 171 deletions.
14 changes: 5 additions & 9 deletions cmd/nginx/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
flag.CommandLine.Parse([]string{})

pflag.VisitAll(func(flag *pflag.Flag) {
klog.V(2).Infof("FLAG: --%s=%q", flag.Name, flag.Value)
klog.V(2).InfoS("FLAG", flag.Name, flag.Value)
})

if *showVersion {
Expand All @@ -202,12 +202,12 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
}

if *ingressClass != "" {
klog.Infof("Watching for Ingress class: %s", *ingressClass)
klog.InfoS("Watching for Ingress", "class", *ingressClass)

if *ingressClass != class.DefaultClass {
klog.Warningf("Only Ingresses with class %q will be processed by this Ingress controller", *ingressClass)
} else {
klog.Warning("Ingresses with an empty class will also be processed by this Ingress controller", *ingressClass)
klog.Warning("Ingresses with an empty class will also be processed by this Ingress controller")
}

class.IngressClass = *ingressClass
Expand Down Expand Up @@ -248,10 +248,6 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
return false, nil, fmt.Errorf("port %v is already in use. Please check the flag --ssl-passthrough-proxy-port", *sslProxyPort)
}

if !*enableSSLChainCompletion {
klog.Warningf("SSL certificate chain completion is disabled (--enable-ssl-chain-completion=false)")
}

if *publishSvc != "" && *publishStatusAddress != "" {
return false, nil, fmt.Errorf("flags --publish-service and --publish-status-address are mutually exclusive")
}
Expand Down Expand Up @@ -307,9 +303,9 @@ https://blog.maxmind.com/2019/12/18/significant-changes-to-accessing-and-using-g
if err := nginx.ValidateGeoLite2DBEditions(); err != nil {
return false, nil, err
}
klog.Info("downloading maxmind GeoIP2 databases...")
klog.InfoS("downloading maxmind GeoIP2 databases")
if err := nginx.DownloadGeoLite2DB(); err != nil {
klog.Errorf("unexpected error downloading GeoIP2 database: %v", err)
klog.ErrorS(err, "unexpected error downloading GeoIP2 database")
}
config.MaxmindEditionFiles = nginx.MaxmindEditionFiles
}
Expand Down
37 changes: 21 additions & 16 deletions cmd/nginx/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func main() {
klog.Fatal(err)
}

klog.Infof("Validated %v as the default backend.", conf.DefaultService)
klog.InfoS("Valid default backend", "service", conf.DefaultService)
}

if len(conf.PublishService) > 0 {
Expand All @@ -102,15 +102,15 @@ func main() {
}

conf.FakeCertificate = ssl.GetFakeSSLCert()
klog.Infof("SSL fake certificate created %v", conf.FakeCertificate.PemFileName)
klog.InfoS("SSL fake certificate created", "file", conf.FakeCertificate.PemFileName)

k8s.IsNetworkingIngressAvailable, k8s.IsIngressV1Ready = k8s.NetworkingIngressAvailable(kubeClient)
if !k8s.IsNetworkingIngressAvailable {
klog.Fatalf("ingress-nginx requires Kubernetes v1.14.0 or higher")
}

if k8s.IsIngressV1Ready {
klog.Infof("Enabling new Ingress features available since Kubernetes v1.18")
klog.InfoS("Enabling new Ingress features available since Kubernetes v1.18")
k8s.IngressClass, err = kubeClient.NetworkingV1beta1().IngressClasses().
Get(context.TODO(), class.IngressClass, metav1.GetOptions{})
if err != nil {
Expand All @@ -119,7 +119,7 @@ func main() {
klog.Fatalf("Error searching IngressClass: %v", err)
}

klog.Errorf("Unexpected error searching IngressClass: %v", err)
klog.ErrorS(err, "Searching IngressClass", "class", class.IngressClass)
}

klog.Warningf("No IngressClass resource with name %v found. Only annotation will be used.", class.IngressClass)
Expand Down Expand Up @@ -181,18 +181,18 @@ func handleSigterm(ngx *controller.NGINXController, exit exiter) {
signalChan := make(chan os.Signal, 1)
signal.Notify(signalChan, syscall.SIGTERM)
<-signalChan
klog.Info("Received SIGTERM, shutting down")
klog.InfoS("Received SIGTERM, shutting down")

exitCode := 0
if err := ngx.Stop(); err != nil {
klog.Infof("Error during shutdown: %v", err)
klog.Warningf("Error during shutdown: %v", err)
exitCode = 1
}

klog.Info("Handled quit, awaiting Pod deletion")
klog.InfoS("Handled quit, awaiting Pod deletion")
time.Sleep(10 * time.Second)

klog.Infof("Exiting with %v", exitCode)
klog.InfoS("Exiting", "code", exitCode)
exit(exitCode)
}

Expand Down Expand Up @@ -225,15 +225,15 @@ func createApiserverClient(apiserverHost, rootCAFile, kubeConfig string) (*kuber
tlsClientConfig := rest.TLSClientConfig{}

if _, err := certutil.NewPool(rootCAFile); err != nil {
klog.Errorf("Expected to load root CA config from %s, but got err: %v", rootCAFile, err)
klog.ErrorS(err, "Loading CA config", "file", rootCAFile)
} else {
tlsClientConfig.CAFile = rootCAFile
}

cfg.TLSClientConfig = tlsClientConfig
}

klog.Infof("Creating API client for %s", cfg.Host)
klog.InfoS("Creating API client", "host", cfg.Host)

client, err := kubernetes.NewForConfig(cfg)
if err != nil {
Expand All @@ -253,7 +253,7 @@ func createApiserverClient(apiserverHost, rootCAFile, kubeConfig string) (*kuber

var lastErr error
retries := 0
klog.V(2).Info("Trying to discover Kubernetes version")
klog.V(2).InfoS("Trying to discover Kubernetes version")
err = wait.ExponentialBackoff(defaultRetry, func() (bool, error) {
v, err = client.Discovery().ServerVersion()

Expand All @@ -262,7 +262,7 @@ func createApiserverClient(apiserverHost, rootCAFile, kubeConfig string) (*kuber
}

lastErr = err
klog.V(2).Infof("Unexpected error discovering Kubernetes version (attempt %v): %v", retries, err)
klog.V(2).ErrorS(err, "Unexpected error discovering Kubernetes version", "attempt", retries)
retries++
return false, nil
})
Expand All @@ -277,8 +277,14 @@ func createApiserverClient(apiserverHost, rootCAFile, kubeConfig string) (*kuber
klog.Warningf("Initial connection to the Kubernetes API server was retried %d times.", retries)
}

klog.Infof("Running in Kubernetes cluster version v%v.%v (%v) - git (%v) commit %v - platform %v",
v.Major, v.Minor, v.GitVersion, v.GitTreeState, v.GitCommit, v.Platform)
klog.InfoS("Running in Kubernetes cluster",
"major", v.Major,
"minor", v.Minor,
"git", v.GitVersion,
"state", v.GitTreeState,
"commit", v.GitCommit,
"platform", v.Platform,
)

return client, nil
}
Expand Down Expand Up @@ -310,7 +316,6 @@ func registerMetrics(reg *prometheus.Registry, mux *http.ServeMux) {
promhttp.HandlerFor(reg, promhttp.HandlerOpts{}),
),
)

}

func registerProfiler() {
Expand Down Expand Up @@ -355,7 +360,7 @@ func checkService(key string, kubeClient *kubernetes.Clientset) error {
_, err = kubeClient.CoreV1().Services(ns).Get(context.TODO(), name, metav1.GetOptions{})
if err != nil {
if errors.IsUnauthorized(err) || errors.IsForbidden(err) {
return fmt.Errorf("✖ The cluster seems to be running with a restrictive Authorization mode and the Ingress controller does not have the required permissions to operate normally.")
return fmt.Errorf("✖ the cluster seems to be running with a restrictive Authorization mode and the Ingress controller does not have the required permissions to operate normally")
}

if errors.IsNotFound(err) {
Expand Down
2 changes: 1 addition & 1 deletion cmd/waitshutdown/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import (
func main() {
err := exec.Command("bash", "-c", "pkill -SIGTERM -f nginx-ingress-controller").Run()
if err != nil {
klog.Errorf("unexpected error terminating ingress controller: %v", err)
klog.ErrorS(err, "terminating ingress controller")
os.Exit(1)
}

Expand Down
9 changes: 3 additions & 6 deletions internal/admission/controller/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ func (ia *IngressAdmission) HandleAdmission(ar *admissionv1.AdmissionReview) {
ingress := networking.Ingress{}
deserializer := codecs.UniversalDeserializer()
if _, _, err := deserializer.Decode(ar.Request.Object.Raw, nil, &ingress); err != nil {
klog.Errorf("failed to decode ingress %s in namespace %s: %s, refusing it",
ar.Request.Name, ar.Request.Namespace, err.Error())
klog.ErrorS(err, "failed to decode ingress", "ingress", ar.Request.Name, "namespace", ar.Request.Namespace)

ar.Response = &admissionv1.AdmissionResponse{
UID: ar.Request.UID,
Expand All @@ -97,8 +96,7 @@ func (ia *IngressAdmission) HandleAdmission(ar *admissionv1.AdmissionReview) {
}

if err := ia.Checker.CheckIngress(&ingress); err != nil {
klog.Errorf("failed to generate configuration for ingress %s in namespace %s: %s, refusing it",
ar.Request.Name, ar.Request.Namespace, err.Error())
klog.ErrorS(err, "failed to generate configuration for ingress", "ingress", ar.Request.Name, "namespace", ar.Request.Namespace)
ar.Response = &admissionv1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: false,
Expand All @@ -111,8 +109,7 @@ func (ia *IngressAdmission) HandleAdmission(ar *admissionv1.AdmissionReview) {
return
}

klog.Infof("successfully validated configuration, accepting ingress %s in namespace %s",
ar.Request.Name, ar.Request.Namespace)
klog.InfoS("successfully validated configuration, accepting", "ingress", ar.Request.Name, "namespace", ar.Request.Namespace)
ar.Response = &admissionv1.AdmissionResponse{
UID: ar.Request.UID,
Allowed: true,
Expand Down
4 changes: 2 additions & 2 deletions internal/admission/controller/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,14 +60,14 @@ func NewAdmissionControllerServer(ac AdmissionController) *AdmissionControllerSe
func (acs *AdmissionControllerServer) ServeHTTP(w http.ResponseWriter, r *http.Request) {
review, err := parseAdmissionReview(acs.Decoder, r.Body)
if err != nil {
klog.Errorf("Unexpected error decoding request: %v", err)
klog.ErrorS(err, "Unexpected error decoding request")
w.WriteHeader(http.StatusBadRequest)
return
}

acs.AdmissionController.HandleAdmission(review)
if err := writeAdmissionReview(w, review); err != nil {
klog.Errorf("Unexpected returning admission review: %v", err)
klog.ErrorS(err, "Unexpected returning admission review")
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ func SHA1(filename string) string {
hasher := sha1.New()
s, err := ioutil.ReadFile(filename)
if err != nil {
klog.Errorf("Error reading file %v", err)
klog.ErrorS(err, "Error reading file", "path", filename)
return ""
}

Expand Down
8 changes: 4 additions & 4 deletions internal/ingress/annotations/annotations.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (e Extractor) Extract(ing *networking.Ingress) *Ingress {
data := make(map[string]interface{})
for name, annotationParser := range e.annotations {
val, err := annotationParser.Parse(ing)
klog.V(5).Infof("annotation %v in Ingress %v/%v: %v", name, ing.GetNamespace(), ing.GetName(), val)
klog.V(5).InfoS("Parsing Ingress annotation", "name", name, "namespace", ing.GetNamespace(), "ingress", ing.GetName(), "value", val)
if err != nil {
if errors.IsMissingAnnotations(err) {
continue
Expand All @@ -197,11 +197,11 @@ func (e Extractor) Extract(ing *networking.Ingress) *Ingress {
if !alreadyDenied {
errString := err.Error()
data[DeniedKeyName] = &errString
klog.Errorf("error reading %v annotation in Ingress %v/%v: %v", name, ing.GetNamespace(), ing.GetName(), err)
klog.ErrorS(err, "error reading Ingress annotation", "name", name, "namespace", ing.GetNamespace(), "ingress", ing.GetName())
continue
}

klog.V(5).Infof("error reading %v annotation in Ingress %v/%v: %v", name, ing.GetNamespace(), ing.GetName(), err)
klog.V(5).ErrorS(err, "error reading Ingress annotation", "name", name, "namespace", ing.GetNamespace(), "ingress", ing.GetName())
}

if val != nil {
Expand All @@ -211,7 +211,7 @@ func (e Extractor) Extract(ing *networking.Ingress) *Ingress {

err := mergo.MapWithOverwrite(pia, data)
if err != nil {
klog.Errorf("unexpected error merging extracted annotations: %v", err)
klog.ErrorS(err, "unexpected error merging extracted annotations")
}

return pia
Expand Down
8 changes: 4 additions & 4 deletions internal/ingress/annotations/authreq/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,17 @@ func (a authReq) Parse(ing *networking.Ingress) (interface{}, error) {
// Optional Parameters
signIn, err := parser.GetStringAnnotation("auth-signin", ing)
if err != nil {
klog.V(3).Infof("auth-signin annotation is undefined and will not be set")
klog.V(3).InfoS("auth-signin annotation is undefined and will not be set")
}

authSnippet, err := parser.GetStringAnnotation("auth-snippet", ing)
if err != nil {
klog.V(3).Infof("auth-snippet annotation is undefined and will not be set")
klog.V(3).InfoS("auth-snippet annotation is undefined and will not be set")
}

authCacheKey, err := parser.GetStringAnnotation("auth-cache-key", ing)
if err != nil {
klog.V(3).Infof("auth-cache-key annotation is undefined and will not be set")
klog.V(3).InfoS("auth-cache-key annotation is undefined and will not be set")
}

durstr, _ := parser.GetStringAnnotation("auth-cache-duration", ing)
Expand All @@ -207,7 +207,7 @@ func (a authReq) Parse(ing *networking.Ingress) (interface{}, error) {

proxySetHeaderMap, err := parser.GetStringAnnotation("auth-proxy-set-headers", ing)
if err != nil {
klog.V(3).Infof("auth-set-proxy-headers annotation is undefined and will not be set")
klog.V(3).InfoS("auth-set-proxy-headers annotation is undefined and will not be set")
}

var proxySetHeaders map[string]string
Expand Down
2 changes: 1 addition & 1 deletion internal/ingress/annotations/secureupstream/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func NewParser(r resolver.Resolver) parser.IngressAnnotation {
// rule used to indicate if the upstream servers should use SSL
func (a su) Parse(ing *networking.Ingress) (secure interface{}, err error) {
if ca, _ := parser.GetStringAnnotation("secure-verify-ca-secret", ing); ca != "" {
klog.Errorf("NOTE! secure-verify-ca-secret is not suppored anymore. Please use proxy-ssl-secret instead")
klog.Warningf("NOTE! secure-verify-ca-secret is not suppored anymore. Please use proxy-ssl-secret instead")
}
return
}
16 changes: 8 additions & 8 deletions internal/ingress/annotations/sessionaffinity/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,40 +96,40 @@ func (a affinity) cookieAffinityParse(ing *networking.Ingress) *Cookie {

cookie.Name, err = parser.GetStringAnnotation(annotationAffinityCookieName, ing)
if err != nil {
klog.V(3).Infof("Ingress %v: No value found in annotation %v. Using the default %v", ing.Name, annotationAffinityCookieName, defaultAffinityCookieName)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieExpires, "default", defaultAffinityCookieName)
cookie.Name = defaultAffinityCookieName
}

cookie.Expires, err = parser.GetStringAnnotation(annotationAffinityCookieExpires, ing)
if err != nil || !affinityCookieExpiresRegex.MatchString(cookie.Expires) {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieExpires)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieExpires)
cookie.Expires = ""
}

cookie.MaxAge, err = parser.GetStringAnnotation(annotationAffinityCookieMaxAge, ing)
if err != nil || !affinityCookieExpiresRegex.MatchString(cookie.MaxAge) {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieMaxAge)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieMaxAge)
cookie.MaxAge = ""
}

cookie.Path, err = parser.GetStringAnnotation(annotationAffinityCookiePath, ing)
if err != nil {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieMaxAge)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieMaxAge)
}

cookie.SameSite, err = parser.GetStringAnnotation(annotationAffinityCookieSameSite, ing)
if err != nil {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieSameSite)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieSameSite)
}

cookie.ConditionalSameSiteNone, err = parser.GetBoolAnnotation(annotationAffinityCookieConditionalSameSiteNone, ing)
if err != nil {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieConditionalSameSiteNone)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieConditionalSameSiteNone)
}

cookie.ChangeOnFailure, err = parser.GetBoolAnnotation(annotationAffinityCookieChangeOnFailure, ing)
if err != nil {
klog.V(3).Infof("Invalid or no annotation value found in Ingress %v: %v. Ignoring it", ing.Name, annotationAffinityCookieChangeOnFailure)
klog.V(3).InfoS("Invalid or no annotation value found. Ignoring", "ingress", ing.Name, "annotation", annotationAffinityCookieChangeOnFailure)
}

return cookie
Expand Down Expand Up @@ -164,7 +164,7 @@ func (a affinity) Parse(ing *networking.Ingress) (interface{}, error) {
case "cookie":
cookie = a.cookieAffinityParse(ing)
default:
klog.V(3).Infof("No default affinity was found for Ingress %v", ing.Name)
klog.V(3).InfoS("No default affinity found", "ingress", ing.Name)

}

Expand Down
Loading

0 comments on commit 108637b

Please sign in to comment.