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 missing timeout when notifying health channel #15876

Merged
merged 9 commits into from Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Expand Up @@ -23,6 +23,10 @@ import (
serverlessv1alpha2 "github.com/kyma-project/kyma/components/function-controller/pkg/apis/serverless/v1alpha2"
)

const (
healthCheckTimeout = time.Second
)

//go:generate mockery --name=GitClient --output=automock --outpkg=automock --case=underscore
type GitClient interface {
LastCommit(options git.Options) (string, error)
Expand Down Expand Up @@ -98,10 +102,7 @@ func (r *FunctionReconciler) SetupWithManager(mgr ctrl.Manager) (controller.Cont

func (r *FunctionReconciler) Reconcile(ctx context.Context, request ctrl.Request) (ctrl.Result, error) {
if IsHealthCheckRequest(request) {
r.Log.Debug("health check request received")
r.healthCh <- true

r.Log.Debug("health check request responded")
r.sendHealthCheck()
return ctrl.Result{}, nil
}

Expand Down Expand Up @@ -156,6 +157,17 @@ func (r *FunctionReconciler) Reconcile(ctx context.Context, request ctrl.Request
return stateReconciler.reconcile(ctx, instance)
}

func (r *FunctionReconciler) sendHealthCheck() {
r.Log.Debug("health check request received")

select {
case r.healthCh <- true:
r.Log.Debug("health check request responded")
case <-time.After(healthCheckTimeout):
r.Log.Warn(errors.New("timeout when responding to health check"))
}
}

func (r *FunctionReconciler) readDockerConfig(ctx context.Context, instance *serverlessv1alpha2.Function) (DockerConfig, error) {
var secret corev1.Secret
// try reading user config
Expand Down Expand Up @@ -183,7 +195,6 @@ func (r *FunctionReconciler) readDockerConfig(ctx context.Context, instance *ser
PushAddress: data["registryAddress"],
PullAddress: data["registryAddress"],
}, nil

}

return DockerConfig{}, errors.Errorf("Docker registry configuration not found, none of configuration secrets (%s, %s) found in function namespace", r.config.ImageRegistryDefaultDockerConfigSecretName, r.config.ImageRegistryExternalDockerConfigSecretName)
Expand Down
@@ -0,0 +1,46 @@
package serverless

import (
"context"
"testing"

"github.com/onsi/gomega"
"go.uber.org/zap"
"k8s.io/apimachinery/pkg/types"
ctrl "sigs.k8s.io/controller-runtime"
)

func TestFunctionReconciler_Reconcile_HealthCheck(t *testing.T) {
g := gomega.NewGomegaWithT(t)

t.Run("should receive health check and notify to health channel", func(t *testing.T) {
healthCh := make(chan bool)
reconciler := NewFunctionReconciler(nil, zap.NewNop().Sugar(), FunctionConfig{}, nil, nil, nil, healthCh)

healthRequest := ctrl.Request{NamespacedName: types.NamespacedName{
Name: HealthEvent,
}}

go func() {
result, err := reconciler.Reconcile(context.Background(), healthRequest)

g.Expect(result).To(gomega.Equal(ctrl.Result{}))
g.Expect(err).To(gomega.BeNil())
}()

g.Expect(<-healthCh).To(gomega.BeTrue())
})

t.Run("should receive health check and return nil after timeout", func(t *testing.T) {
reconciler := NewFunctionReconciler(nil, zap.NewNop().Sugar(), FunctionConfig{}, nil, nil, nil, make(chan bool))

healthRequest := ctrl.Request{NamespacedName: types.NamespacedName{
Name: HealthEvent,
}}

result, err := reconciler.Reconcile(context.Background(), healthRequest)

g.Expect(result).To(gomega.Equal(ctrl.Result{}))
g.Expect(err).To(gomega.BeNil())
})
}
6 changes: 3 additions & 3 deletions resources/serverless/values.yaml
Expand Up @@ -80,13 +80,13 @@ global:
directory: "tpi"
function_controller:
name: "function-controller"
version: "PR-15868"
version: "PR-15876"
function_webhook:
name: "function-webhook"
version: "PR-15868"
version: "PR-15876"
function_build_init:
name: "function-build-init"
version: "PR-15868"
version: "PR-15876"
function_runtime_nodejs12:
name: "function-runtime-nodejs12"
version: "v20221011-831da39d"
Expand Down