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

Add support for Access Logs in BackendConfig #1041

Merged
merged 2 commits into from
Mar 12, 2020
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
2 changes: 1 addition & 1 deletion pkg/apis/backendconfig/v1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ type BackendConfigSpec struct {
SessionAffinity *SessionAffinityConfig `json:"sessionAffinity,omitempty"`
CustomRequestHeaders *CustomRequestHeadersConfig `json:"customRequestHeaders,omitempty"`
HealthCheck *HealthCheckConfig `json:"healthCheck,omitempty"`
// Log specifies logging specification for Backend Services.
// Logging specifies the configuration for access logs.
Logging *LogConfig `json:"log,omitempty"`
}

Expand Down
17 changes: 17 additions & 0 deletions pkg/backendconfig/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ func Validate(kubeClient kubernetes.Interface, beConfig *backendconfigv1.Backend
return err
}

if err := validateLogging(beConfig); err != nil {
return err
}

return nil
}

Expand Down Expand Up @@ -104,3 +108,16 @@ func validateSessionAffinity(kubeClient kubernetes.Interface, beConfig *backendc

return nil
}

func validateLogging(beConfig *backendconfigv1.BackendConfig) error {
if beConfig.Spec.Logging == nil || beConfig.Spec.Logging.SampleRate == nil {
return nil
}

if *beConfig.Spec.Logging.SampleRate < 0.0 || *beConfig.Spec.Logging.SampleRate > 1.0 {
return fmt.Errorf("unsupported SampleRate: %f, should be between 0.0 and 1.0",
*beConfig.Spec.Logging.SampleRate)
}

return nil
}
88 changes: 88 additions & 0 deletions pkg/backendconfig/validation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/fake"
backendconfigv1 "k8s.io/ingress-gce/pkg/apis/backendconfig/v1"
testutils "k8s.io/ingress-gce/pkg/test"
)

var (
Expand Down Expand Up @@ -239,3 +240,90 @@ func TestValidateSessionAffinity(t *testing.T) {
}
}
}

func TestValidateLogging(t *testing.T) {
for _, tc := range []struct {
desc string
beConfig *backendconfigv1.BackendConfig
expectError bool
}{
{
desc: "nil access log config",
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1.BackendConfigSpec{},
},
expectError: false,
},
{
desc: "empty access log config",
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1.BackendConfigSpec{
Logging: &backendconfigv1.LogConfig{},
},
},
expectError: false,
},
{
desc: "invalid sample rate",
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1.BackendConfigSpec{
Logging: &backendconfigv1.LogConfig{
Enable: false,
SampleRate: testutils.Float64ToPtr(1.01),
},
},
},
expectError: true,
},
{
desc: "valid sample rate",
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1.BackendConfigSpec{
Logging: &backendconfigv1.LogConfig{
Enable: true,
SampleRate: testutils.Float64ToPtr(0.5),
},
},
},
expectError: false,
},
{
desc: "valid integer sample rate",
beConfig: &backendconfigv1.BackendConfig{
ObjectMeta: meta_v1.ObjectMeta{
Namespace: "default",
},
Spec: backendconfigv1.BackendConfigSpec{
Logging: &backendconfigv1.LogConfig{
Enable: true,
SampleRate: testutils.Float64ToPtr(1),
},
},
},
expectError: false,
},
} {
t.Run(tc.desc, func(t *testing.T) {
kubeClient := fake.NewSimpleClientset()
err := Validate(kubeClient, tc.beConfig)
if tc.expectError && err == nil {
t.Errorf("Expected error but got nil")
}
if !tc.expectError && err != nil {
t.Errorf("Did not expect error but got: %v", err)
}
})
}
}
4 changes: 4 additions & 0 deletions pkg/backends/backends.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ func (b *Backends) Create(sp utils.ServicePort, hcLink string) (*composite.Backe
Port: namedPort.Port,
PortName: namedPort.Name,
HealthChecks: []string{hcLink},
// LogConfig is using GA API so this is not considered for computing API version.
LogConfig: &composite.BackendServiceLogConfig{
Enable: true,
},
}

if sp.L7ILBEnabled {
Expand Down
63 changes: 63 additions & 0 deletions pkg/backends/features/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
Copyright 2020 The Kubernetes Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package features

import (
"fmt"

"k8s.io/ingress-gce/pkg/composite"
"k8s.io/ingress-gce/pkg/utils"
"k8s.io/klog"
)

// EnsureLogging reads the log configurations specified in the ServicePort.BackendConfig
// and applies it to the BackendService. It returns true if there were existing settings
// on the BackendService that were overwritten.
func EnsureLogging(sp utils.ServicePort, be *composite.BackendService) bool {
if sp.BackendConfig.Spec.Logging == nil {
return false
}
svcKey := fmt.Sprintf("%s/%s", sp.ID.Service.Namespace, sp.ID.Service.Name)
expectedLogConfig := expectedBackendServiceLogConfig(sp)
if !expectedLogConfig.Enable && !be.LogConfig.Enable {
klog.V(3).Infof("Logging continues to stay disabled for service %s, skipping update", svcKey)
return false
}
if be.LogConfig == nil || expectedLogConfig.Enable != be.LogConfig.Enable ||
expectedLogConfig.SampleRate != be.LogConfig.SampleRate {
be.LogConfig = expectedLogConfig
klog.V(2).Infof("Updated Logging settings for service %s", svcKey)
return true
}
return false
}

// expectedBackendServiceLogConfig returns composite.BackendServiceLogConfig for
// the access log settings specified in the BackendConfig to the passed in
// service port.
// This method assumes that sp.BackendConfig.Spec.Logging is not nil.
func expectedBackendServiceLogConfig(sp utils.ServicePort) *composite.BackendServiceLogConfig {
logConfig := &composite.BackendServiceLogConfig{
Enable: sp.BackendConfig.Spec.Logging.Enable,
}
// Ignore sample rate if logging is not enabled.
if !sp.BackendConfig.Spec.Logging.Enable || sp.BackendConfig.Spec.Logging.SampleRate == nil {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you need to check if Logging is nil?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checked in caller function. Added a comment that this is assumed to be not nil. Would it be cleaner if I explicitly include a check here as well?.

return logConfig
}
logConfig.SampleRate = *sp.BackendConfig.Spec.Logging.SampleRate
return logConfig
}
Loading