Skip to content

Commit

Permalink
Add support for Access Logs
Browse files Browse the repository at this point in the history
  • Loading branch information
skmatti committed Mar 1, 2020
1 parent 5033b33 commit bf6d153
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 0 deletions.
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)
}
})
}
}
62 changes: 62 additions & 0 deletions pkg/backends/features/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
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"
)

// EnsureLog 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
// composite.BackendService.
func expectedBackendServiceLogConfig(sp utils.ServicePort) *composite.BackendServiceLogConfig {
logConfig := &composite.BackendServiceLogConfig{
Enable: sp.BackendConfig.Spec.Logging.Enable,
}
if !sp.BackendConfig.Spec.Logging.Enable || sp.BackendConfig.Spec.Logging.SampleRate == nil {
return logConfig
}
logConfig.SampleRate = *sp.BackendConfig.Spec.Logging.SampleRate
return logConfig
}
Loading

0 comments on commit bf6d153

Please sign in to comment.