Skip to content

Commit 719304b

Browse files
committed
fix: support for multiple kubeContexts
1 parent ddb968b commit 719304b

File tree

6 files changed

+22
-109
lines changed

6 files changed

+22
-109
lines changed

pkg/skaffold/kubernetes/status/status_check.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ type Config interface {
7575
GetNamespaces() []string
7676
StatusCheckDeadlineSeconds() int
7777
Muted() config.Muted
78-
StatusCheck() (*bool, error)
78+
StatusCheck() *bool
7979
}
8080

8181
// Monitor runs status checks for pods and deployments

pkg/skaffold/runner/deployer.go

+9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,15 @@ func (d *deployerCtx) GetKubeContext() string {
4646
return d.RunContext.GetKubeContext()
4747
}
4848

49+
func (d *deployerCtx) StatusCheck() *bool {
50+
// runcontext StatusCheck method returns the value set by the cli flag `--status-check`
51+
// which overrides the value set in the individual configs.
52+
if cliValue := d.RunContext.StatusCheck(); cliValue != nil {
53+
return cliValue
54+
}
55+
return d.deploy.StatusCheck
56+
}
57+
4958
// GetDeployer creates a deployer from a given RunContext and deploy pipeline definitions.
5059
func GetDeployer(runCtx *runcontext.RunContext, provider deploy.ComponentProvider, labels map[string]string) (deploy.Deployer, kubernetes.ImageListMux, error) {
5160
var podSelectors kubernetes.ImageListMux

pkg/skaffold/runner/runcontext/context.go

+1-38
Original file line numberDiff line numberDiff line change
@@ -112,28 +112,6 @@ func (ps Pipelines) TestCases() []*latestV1.TestCase {
112112
return tests
113113
}
114114

115-
func (ps Pipelines) StatusCheck() (*bool, error) {
116-
var enabled, disabled bool
117-
for _, p := range ps.pipelines {
118-
if p.Deploy.StatusCheck != nil {
119-
if *p.Deploy.StatusCheck {
120-
enabled = true
121-
} else {
122-
disabled = true
123-
}
124-
if enabled && disabled {
125-
return nil, fmt.Errorf("cannot explicitly enable StatusCheck in one pipeline and explicitly disable it in another pipeline, see https://skaffold.dev/docs/workflows/ci-cd/#waiting-for-skaffold-deployments-using-healthcheck")
126-
}
127-
}
128-
}
129-
// set the group status check to disabled if any pipeline has StatusCheck
130-
// set to false.
131-
if disabled {
132-
return util.BoolPtr(false), nil
133-
}
134-
return util.BoolPtr(true), nil
135-
}
136-
137115
func (ps Pipelines) StatusCheckDeadlineSeconds() int {
138116
c := 0
139117
// set the group status check deadline to maximum of any individually specified value
@@ -170,22 +148,6 @@ func (rc *RunContext) Deployers() []latestV1.DeployConfig { return rc.Pipelines.
170148

171149
func (rc *RunContext) TestCases() []*latestV1.TestCase { return rc.Pipelines.TestCases() }
172150

173-
func (rc *RunContext) StatusCheck() (*bool, error) {
174-
scOpts := rc.Opts.StatusCheck.Value()
175-
scConfig, err := rc.Pipelines.StatusCheck()
176-
if err != nil {
177-
return nil, err
178-
}
179-
switch {
180-
case scOpts != nil:
181-
return util.BoolPtr(*scOpts), nil
182-
case scConfig != nil:
183-
return util.BoolPtr(*scConfig), nil
184-
default:
185-
return util.BoolPtr(true), nil
186-
}
187-
}
188-
189151
func (rc *RunContext) StatusCheckDeadlineSeconds() int {
190152
return rc.Pipelines.StatusCheckDeadlineSeconds()
191153
}
@@ -226,6 +188,7 @@ func (rc *RunContext) RenderOnly() bool { return rc
226188
func (rc *RunContext) RenderOutput() string { return rc.Opts.RenderOutput }
227189
func (rc *RunContext) SkipRender() bool { return rc.Opts.SkipRender }
228190
func (rc *RunContext) SkipTests() bool { return rc.Opts.SkipTests }
191+
func (rc *RunContext) StatusCheck() *bool { return rc.Opts.StatusCheck.Value() }
229192
func (rc *RunContext) Tail() bool { return rc.Opts.Tail }
230193
func (rc *RunContext) Trigger() string { return rc.Opts.Trigger }
231194
func (rc *RunContext) WaitForDeletions() config.WaitForDeletions { return rc.Opts.WaitForDeletions }

pkg/skaffold/runner/runcontext/context_test.go

-59
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ package runcontext
1919
import (
2020
"testing"
2121

22-
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
23-
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
2422
"github.com/GoogleContainerTools/skaffold/testutil"
2523
)
2624

@@ -91,60 +89,3 @@ func TestRunContext_UpdateNamespaces(t *testing.T) {
9189
})
9290
}
9391
}
94-
95-
func TestPipelines_StatusCheck(t *testing.T) {
96-
tests := []struct {
97-
description string
98-
statusCheckP1 *bool
99-
statusCheckP2 *bool
100-
wantEnabled *bool
101-
wantErr bool
102-
}{
103-
{
104-
description: "both pipelines' statusCheck values are unspecified",
105-
wantEnabled: util.BoolPtr(true),
106-
},
107-
{
108-
description: "one pipeline's statusCheck value is true",
109-
statusCheckP1: util.BoolPtr(true),
110-
wantEnabled: util.BoolPtr(true),
111-
},
112-
{
113-
description: "one pipeline's statusCheck value is false",
114-
statusCheckP1: util.BoolPtr(false),
115-
wantEnabled: util.BoolPtr(false),
116-
},
117-
{
118-
description: "one pipeline's statusCheck value is true, one pipeline's statusCheck value is false",
119-
statusCheckP1: util.BoolPtr(true),
120-
statusCheckP2: util.BoolPtr(false),
121-
wantErr: true,
122-
},
123-
}
124-
for _, test := range tests {
125-
testutil.Run(t, test.description, func(t *testutil.T) {
126-
p := &Pipelines{
127-
pipelines: []latestV1.Pipeline{
128-
{
129-
Deploy: latestV1.DeployConfig{
130-
StatusCheck: test.statusCheckP1,
131-
},
132-
},
133-
{
134-
Deploy: latestV1.DeployConfig{
135-
StatusCheck: test.statusCheckP2,
136-
},
137-
},
138-
},
139-
}
140-
gotEnabled, err := p.StatusCheck()
141-
if err != nil && !test.wantErr {
142-
t.Errorf("p.StatusCheck() got error %v, want no error", err)
143-
}
144-
if err == nil && test.wantErr {
145-
t.Errorf("p.StatusCheck() got no error, want error")
146-
}
147-
t.CheckDeepEqual(test.wantEnabled, gotEnabled)
148-
})
149-
}
150-
}

pkg/skaffold/status/provider.go

+8-10
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ limitations under the License.
1717
package status
1818

1919
import (
20-
"sync"
21-
2220
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/label"
2321
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/status"
2422
)
@@ -29,24 +27,24 @@ type Provider interface {
2927
}
3028

3129
type fullProvider struct {
32-
k8sMonitor Monitor
30+
k8sMonitor map[string]Monitor // keyed on KubeContext. TODO: make KubeContext a struct type.
3331
labeller *label.DefaultLabeller
34-
once sync.Once
3532
}
3633

3734
func NewMonitorProvider(l *label.DefaultLabeller) Provider {
38-
return &fullProvider{labeller: l}
35+
return &fullProvider{k8sMonitor: make(map[string]Monitor), labeller: l}
3936
}
4037

4138
func (p *fullProvider) GetKubernetesMonitor(config status.Config) Monitor {
42-
enabled, _ := config.StatusCheck()
39+
enabled := config.StatusCheck()
4340
if enabled != nil && !*enabled { // assume disabled only if explicitly set to false
4441
return &NoopMonitor{}
4542
}
46-
p.once.Do(func() {
47-
p.k8sMonitor = status.NewStatusMonitor(config, p.labeller)
48-
})
49-
return p.k8sMonitor
43+
context := config.GetKubeContext()
44+
if p.k8sMonitor[context] == nil {
45+
p.k8sMonitor[context] = status.NewStatusMonitor(config, p.labeller)
46+
}
47+
return p.k8sMonitor[context]
5048
}
5149

5250
func (p *fullProvider) GetNoopMonitor() Monitor {

pkg/skaffold/status/provider_test.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,9 @@ type mockConfig struct {
5959
statusCheck *bool
6060
}
6161

62-
func (m mockConfig) StatusCheck() (*bool, error) { return m.statusCheck, nil }
62+
func (m mockConfig) StatusCheck() *bool { return m.statusCheck }
63+
64+
func (m mockConfig) GetKubeContext() string { return "" }
6365

6466
func (m mockConfig) StatusCheckDeadlineSeconds() int { return 0 }
6567

0 commit comments

Comments
 (0)