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

Refactoring events to use Config interface for init #5532

Merged
merged 2 commits into from
Mar 11, 2021
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
6 changes: 3 additions & 3 deletions pkg/skaffold/build/local/local_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/docker"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/warnings"
"github.com/GoogleContainerTools/skaffold/testutil"
testEvent "github.com/GoogleContainerTools/skaffold/testutil/event"
)

type testAuthHelper struct{}
Expand Down Expand Up @@ -229,13 +229,13 @@ func TestLocalRun(t *testing.T) {
t.Override(&docker.EvalBuildArgs, func(_ config.RunMode, _ string, _ string, args map[string]*string, _ map[string]*string) (map[string]*string, error) {
return args, nil
})
event.InitializeState([]latest.Pipeline{{
testEvent.InitializeState([]latest.Pipeline{{
Deploy: latest.DeployConfig{},
Build: latest.BuildConfig{
BuildType: latest.BuildType{
LocalBuild: &latest.LocalBuild{},
},
}}}, "", true, true, true)
}}})

builder, err := NewBuilder(&mockConfig{},
&latest.LocalBuild{
Expand Down
4 changes: 2 additions & 2 deletions pkg/skaffold/build/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ import (
"github.com/google/go-cmp/cmp"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/build/tag"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/testutil"
testEvent "github.com/GoogleContainerTools/skaffold/testutil/event"
)

func TestGetBuild(t *testing.T) {
Expand Down Expand Up @@ -393,7 +393,7 @@ func initializeEvents() {
},
},
}}
event.InitializeState(pipes, "temp", true, true, true)
testEvent.InitializeState(pipes)
}

func errorsComparer(a, b error) bool {
Expand Down
10 changes: 5 additions & 5 deletions pkg/skaffold/deploy/status/status_check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,12 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/label"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/resource"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/event"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/runner/runcontext"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
"github.com/GoogleContainerTools/skaffold/proto/v1"
"github.com/GoogleContainerTools/skaffold/testutil"
testEvent "github.com/GoogleContainerTools/skaffold/testutil/event"
)

func TestGetDeployments(t *testing.T) {
Expand Down Expand Up @@ -293,7 +293,7 @@ func TestGetDeployStatus(t *testing.T) {

for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
event.InitializeState([]latest.Pipeline{{}}, "test", true, true, true)
testEvent.InitializeState([]latest.Pipeline{{}})
errCode, err := getSkaffoldDeployStatus(test.counter, test.deployments)
t.CheckError(test.shouldErr, err)
if test.shouldErr {
Expand Down Expand Up @@ -370,7 +370,7 @@ func TestPrintSummaryStatus(t *testing.T) {
out := new(bytes.Buffer)
rc := newCounter(10)
rc.pending = test.pending
event.InitializeState([]latest.Pipeline{{}}, "test", true, true, true)
testEvent.InitializeState([]latest.Pipeline{{}})
r := withStatus(resource.NewDeployment(test.deployment, test.namespace, 0), test.ae)
// report status once and set it changed to false.
r.ReportSinceLastUpdated(false)
Expand Down Expand Up @@ -460,7 +460,7 @@ func TestPrintStatus(t *testing.T) {
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
out := new(bytes.Buffer)
event.InitializeState([]latest.Pipeline{{}}, "test", true, true, true)
testEvent.InitializeState([]latest.Pipeline{{}})
checker := statusChecker{labeller: labeller}
actual := checker.printStatus(test.rs, out)
t.CheckDeepEqual(test.expectedOut, out.String())
Expand Down Expand Up @@ -591,7 +591,7 @@ func TestPollDeployment(t *testing.T) {
testutil.Run(t, test.description, func(t *testutil.T) {
t.Override(&util.DefaultExecCommand, test.command)
t.Override(&defaultPollPeriodInMilliseconds, 100)
event.InitializeState([]latest.Pipeline{{}}, "test", true, true, true)
testEvent.InitializeState([]latest.Pipeline{{}})
mockVal := mockValidator{runs: test.runs}
dep := test.dep.WithValidator(mockVal)

Expand Down
29 changes: 29 additions & 0 deletions pkg/skaffold/event/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
Copyright 2021 The Skaffold 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 event

import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
)

type Config interface {
GetKubeContext() string
AutoBuild() bool
AutoDeploy() bool
AutoSync() bool
GetPipelines() []latest.Pipeline
}
13 changes: 6 additions & 7 deletions pkg/skaffold/event/event.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ import (

sErrors "github.com/GoogleContainerTools/skaffold/pkg/skaffold/errors"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/util"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/version"
"github.com/GoogleContainerTools/skaffold/proto/v1"
Expand Down Expand Up @@ -157,15 +156,15 @@ func (ev *eventHandler) forEachEvent(callback func(*proto.LogEntry) error) error
return <-listener.errors
}

func emptyState(pipelines []latest.Pipeline, kubeContext string, autoBuild, autoDeploy, autoSync bool) proto.State {
func emptyState(cfg Config) proto.State {
builds := map[string]string{}
for _, p := range pipelines {
for _, p := range cfg.GetPipelines() {
for _, a := range p.Build.Artifacts {
builds[a.ImageName] = NotStarted
}
}
metadata := initializeMetadata(pipelines, kubeContext)
return emptyStateWithArtifacts(builds, metadata, autoBuild, autoDeploy, autoSync)
metadata := initializeMetadata(cfg.GetPipelines(), cfg.GetKubeContext())
return emptyStateWithArtifacts(builds, metadata, cfg.AutoBuild(), cfg.AutoDeploy(), cfg.AutoSync())
}

func emptyStateWithArtifacts(builds map[string]string, metadata *proto.Metadata, autoBuild, autoDeploy, autoSync bool) proto.State {
Expand All @@ -191,8 +190,8 @@ func emptyStateWithArtifacts(builds map[string]string, metadata *proto.Metadata,
}

// InitializeState instantiates the global state of the skaffold runner, as well as the event log.
func InitializeState(c []latest.Pipeline, kc string, autoBuild, autoDeploy, autoSync bool) {
handler.setState(emptyState(c, kc, autoBuild, autoDeploy, autoSync))
func InitializeState(cfg Config) {
handler.setState(emptyState(cfg))
}

// DeployInProgress notifies that a deployment has been started.
Expand Down
62 changes: 40 additions & 22 deletions pkg/skaffold/event/event_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func TestGetLogEvents(t *testing.T) {

func TestGetState(t *testing.T) {
ev := newHandler()
ev.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
ev.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

ev.stateLock.Lock()
ev.state.BuildState.Artifacts["img"] = Complete
Expand All @@ -82,7 +82,7 @@ func TestDeployInProgress(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().DeployState.Status == NotStarted })
DeployInProgress()
Expand All @@ -93,7 +93,7 @@ func TestDeployFailed(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().DeployState.Status == NotStarted })
DeployFailed(errors.New("BUG"))
Expand All @@ -107,7 +107,7 @@ func TestDeployComplete(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().DeployState.Status == NotStarted })
DeployComplete()
Expand All @@ -121,11 +121,11 @@ func TestBuildInProgress(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{Build: latest.BuildConfig{
handler.state = emptyState(mockCfg([]latest.Pipeline{{Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{{
ImageName: "img",
}},
}}}, "test", true, true, true)
}}}, "test"))

wait(t, func() bool { return handler.getState().BuildState.Artifacts["img"] == NotStarted })
BuildInProgress("img")
Expand All @@ -136,11 +136,11 @@ func TestBuildFailed(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{Build: latest.BuildConfig{
handler.state = emptyState(mockCfg([]latest.Pipeline{{Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{{
ImageName: "img",
}},
}}}, "test", true, true, true)
}}}, "test"))

wait(t, func() bool { return handler.getState().BuildState.Artifacts["img"] == NotStarted })
BuildFailed("img", errors.New("BUG"))
Expand All @@ -154,11 +154,11 @@ func TestBuildComplete(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{Build: latest.BuildConfig{
handler.state = emptyState(mockCfg([]latest.Pipeline{{Build: latest.BuildConfig{
Artifacts: []*latest.Artifact{{
ImageName: "img",
}},
}}}, "test", true, true, true)
}}}, "test"))

wait(t, func() bool { return handler.getState().BuildState.Artifacts["img"] == NotStarted })
BuildComplete("img")
Expand All @@ -169,7 +169,7 @@ func TestPortForwarded(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().ForwardedPorts[8080] == nil })
PortForwarded(8080, schemautil.FromInt(8888), "pod", "container", "ns", "portname", "resourceType", "resourceName", "127.0.0.1")
Expand All @@ -186,7 +186,7 @@ func TestStatusCheckEventStarted(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
StatusCheckEventStarted()
Expand All @@ -197,7 +197,7 @@ func TestStatusCheckEventInProgress(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
StatusCheckEventInProgress("[2/5 deployment(s) are still pending]")
Expand All @@ -208,7 +208,7 @@ func TestStatusCheckEventSucceeded(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
statusCheckEventSucceeded()
Expand All @@ -219,7 +219,7 @@ func TestStatusCheckEventFailed(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
StatusCheckEventEnded(proto.StatusCode_STATUSCHECK_FAILED_SCHEDULING, errors.New("one or more deployments failed"))
Expand All @@ -233,7 +233,7 @@ func TestResourceStatusCheckEventUpdated(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
ResourceStatusCheckEventUpdated("ns:pod/foo", proto.ActionableErr{
Expand All @@ -247,7 +247,7 @@ func TestResourceStatusCheckEventSucceeded(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
resourceStatusCheckEventSucceeded("ns:pod/foo")
Expand All @@ -258,7 +258,7 @@ func TestResourceStatusCheckEventFailed(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().StatusCheckState.Status == NotStarted })
resourceStatusCheckEventFailed("ns:pod/foo", proto.ActionableErr{
Expand All @@ -272,7 +272,7 @@ func TestFileSyncInProgress(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().FileSyncState.Status == NotStarted })
FileSyncInProgress(5, "image")
Expand All @@ -283,7 +283,7 @@ func TestFileSyncFailed(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().FileSyncState.Status == NotStarted })
FileSyncFailed(5, "image", errors.New("BUG"))
Expand All @@ -294,7 +294,7 @@ func TestFileSyncSucceeded(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

wait(t, func() bool { return handler.getState().FileSyncState.Status == NotStarted })
FileSyncSucceeded(5, "image")
Expand All @@ -305,7 +305,7 @@ func TestDebuggingContainer(t *testing.T) {
defer func() { handler = newHandler() }()

handler = newHandler()
handler.state = emptyState([]latest.Pipeline{{}}, "test", true, true, true)
handler.state = emptyState(mockCfg([]latest.Pipeline{{}}, "test"))

found := func() bool {
for _, dc := range handler.getState().DebuggingContainers {
Expand Down Expand Up @@ -598,3 +598,21 @@ func TestSaveEventsToFile(t *testing.T) {
testutil.CheckDeepEqual(t, 1, buildCompleteEvent)
testutil.CheckDeepEqual(t, 1, devLoopCompleteEvent)
}

type config struct {
pipes []latest.Pipeline
kubectx string
}

func (c config) GetKubeContext() string { return c.kubectx }
func (c config) AutoBuild() bool { return true }
func (c config) AutoDeploy() bool { return true }
func (c config) AutoSync() bool { return true }
func (c config) GetPipelines() []latest.Pipeline { return c.pipes }

func mockCfg(pipes []latest.Pipeline, kubectx string) config {
return config{
pipes: pipes,
kubectx: kubectx,
}
}
2 changes: 1 addition & 1 deletion pkg/skaffold/event/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func TestEmptyState(t *testing.T) {
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
handler = &eventHandler{
state: emptyState([]latest.Pipeline{test.cfg}, test.cluster, true, true, true),
state: emptyState(mockCfg([]latest.Pipeline{test.cfg}, test.cluster)),
}
metadata := handler.state.Metadata
builders := metadata.Build.Builders
Expand Down
Loading