Skip to content

Commit

Permalink
fix: port-forward for multi-config projects (#6090)
Browse files Browse the repository at this point in the history
  • Loading branch information
gsquared94 authored Jun 28, 2021
1 parent 9cd80d3 commit 044c65f
Show file tree
Hide file tree
Showing 12 changed files with 59 additions and 56 deletions.
51 changes: 21 additions & 30 deletions pkg/skaffold/access/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,50 +17,41 @@ limitations under the License.
package access

import (
"sync"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy/label"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
)

type Provider interface {
GetKubernetesAccessor(*kubernetes.ImageList) Accessor
GetKubernetesAccessor(portforward.Config, *kubernetes.ImageList) Accessor
GetNoopAccessor() Accessor
}

type fullProvider struct {
kubernetesAccessor func(*kubernetes.ImageList) Accessor
label label.Config
k8sAccessor map[string]Accessor
}

var (
provider *fullProvider
once sync.Once
)

func NewAccessorProvider(config portforward.Config, labelConfig label.Config, cli *kubectl.CLI) Provider {
once.Do(func() {
provider = &fullProvider{
kubernetesAccessor: func(podSelector *kubernetes.ImageList) Accessor {
if !config.PortForwardOptions().Enabled() {
return &NoopAccessor{}
}

return portforward.NewForwarderManager(cli,
podSelector,
labelConfig.RunIDSelector(),
config.Mode(),
config.PortForwardOptions(),
config.PortForwardResources())
},
}
})
return provider
func NewAccessorProvider(labelConfig label.Config) Provider {
return &fullProvider{label: labelConfig, k8sAccessor: make(map[string]Accessor)}
}

func (p *fullProvider) GetKubernetesAccessor(s *kubernetes.ImageList) Accessor {
return p.kubernetesAccessor(s)
func (p *fullProvider) GetKubernetesAccessor(config portforward.Config, podSelector *kubernetes.ImageList) Accessor {
if !config.PortForwardOptions().Enabled() {
return &NoopAccessor{}
}
context := config.GetKubeContext()

if p.k8sAccessor[context] == nil {
p.k8sAccessor[context] = portforward.NewForwarderManager(kubectl.NewCLI(config, ""),
podSelector,
p.label.RunIDSelector(),
config.Mode(),
config.PortForwardOptions(),
config.PortForwardResources())
}
return p.k8sAccessor[context]
}

func (p *fullProvider) GetNoopAccessor() Accessor {
Expand All @@ -70,7 +61,7 @@ func (p *fullProvider) GetNoopAccessor() Accessor {
// NoopProvider is used in tests
type NoopProvider struct{}

func (p *NoopProvider) GetKubernetesAccessor(_ *kubernetes.ImageList) Accessor {
func (p *NoopProvider) GetKubernetesAccessor(_ portforward.Config, _ *kubernetes.ImageList) Accessor {
return &NoopAccessor{}
}

Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/deploy/helm/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
kstatus "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/status"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
Expand Down Expand Up @@ -112,6 +113,7 @@ type Deployer struct {
type Config interface {
kubectl.Config
kstatus.Config
portforward.Config
IsMultiConfig() bool
}

Expand Down Expand Up @@ -140,7 +142,7 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
return &Deployer{
HelmDeploy: h,
podSelector: podSelector,
accessor: provider.Accessor.GetKubernetesAccessor(podSelector),
accessor: provider.Accessor.GetKubernetesAccessor(cfg, podSelector),
debugger: provider.Debugger.GetKubernetesDebugger(podSelector),
logger: provider.Logger.GetKubernetesLogger(podSelector),
statusMonitor: provider.Monitor.GetKubernetesMonitor(cfg),
Expand Down
11 changes: 6 additions & 5 deletions pkg/skaffold/deploy/helm/helm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1539,11 +1539,12 @@ type helmConfig struct {
configFile string
}

func (c *helmConfig) ForceDeploy() bool { return c.force }
func (c *helmConfig) GetKubeConfig() string { return kubectl.TestKubeConfig }
func (c *helmConfig) GetKubeContext() string { return kubectl.TestKubeContext }
func (c *helmConfig) GetKubeNamespace() string { return c.namespace }
func (c *helmConfig) ConfigurationFile() string { return c.configFile }
func (c *helmConfig) ForceDeploy() bool { return c.force }
func (c *helmConfig) GetKubeConfig() string { return kubectl.TestKubeConfig }
func (c *helmConfig) GetKubeContext() string { return kubectl.TestKubeContext }
func (c *helmConfig) GetKubeNamespace() string { return c.namespace }
func (c *helmConfig) ConfigurationFile() string { return c.configFile }
func (c *helmConfig) PortForwardResources() []*latestV1.PortForwardResource { return nil }

// helmReleaseInfo returns the result of `helm --namespace <namespace> get all <name>` with the given KRM manifest.
func helmReleaseInfo(namespace, manifest string) string {
Expand Down
4 changes: 3 additions & 1 deletion pkg/skaffold/deploy/kpt/kpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
kstatus "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/status"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/log"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/output"
Expand Down Expand Up @@ -94,6 +95,7 @@ type Deployer struct {
type Config interface {
kubectl.Config
kstatus.Config
portforward.Config
}

// NewDeployer generates a new Deployer object contains the kptDeploy schema.
Expand All @@ -102,7 +104,7 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
return &Deployer{
KptDeploy: d,
podSelector: podSelector,
accessor: provider.Accessor.GetKubernetesAccessor(podSelector),
accessor: provider.Accessor.GetKubernetesAccessor(cfg, podSelector),
debugger: provider.Debugger.GetKubernetesDebugger(podSelector),
logger: provider.Logger.GetKubernetesLogger(podSelector),
statusMonitor: provider.Monitor.GetKubernetesMonitor(cfg),
Expand Down
9 changes: 5 additions & 4 deletions pkg/skaffold/deploy/kpt/kpt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1190,7 +1190,8 @@ type kptConfig struct {
config string
}

func (c *kptConfig) WorkingDir() string { return c.workingDir }
func (c *kptConfig) GetKubeContext() string { return kubectl.TestKubeContext }
func (c *kptConfig) GetKubeNamespace() string { return kubectl.TestNamespace }
func (c *kptConfig) GetKubeConfig() string { return c.config }
func (c *kptConfig) WorkingDir() string { return c.workingDir }
func (c *kptConfig) GetKubeContext() string { return kubectl.TestKubeContext }
func (c *kptConfig) GetKubeNamespace() string { return kubectl.TestNamespace }
func (c *kptConfig) GetKubeConfig() string { return c.config }
func (c *kptConfig) PortForwardResources() []*latestV1.PortForwardResource { return nil }
2 changes: 2 additions & 0 deletions pkg/skaffold/deploy/kubectl/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/instrumentation"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubectl"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
kstatus "github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/status"
latestV1 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v1"
)
Expand All @@ -49,6 +50,7 @@ type CLI struct {
type Config interface {
kubectl.Config
kstatus.Config
portforward.Config
deploy.Config
ForceDeploy() bool
WaitForDeletions() config.WaitForDeletions
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/kubectl/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
return &Deployer{
KubectlDeploy: d,
podSelector: podSelector,
accessor: provider.Accessor.GetKubernetesAccessor(podSelector),
accessor: provider.Accessor.GetKubernetesAccessor(cfg, podSelector),
debugger: provider.Debugger.GetKubernetesDebugger(podSelector),
logger: provider.Logger.GetKubernetesLogger(podSelector),
statusMonitor: provider.Monitor.GetKubernetesMonitor(cfg),
Expand Down
15 changes: 8 additions & 7 deletions pkg/skaffold/deploy/kubectl/kubectl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -739,10 +739,11 @@ type kubectlConfig struct {
waitForDeletions config.WaitForDeletions
}

func (c *kubectlConfig) GetKubeContext() string { return "kubecontext" }
func (c *kubectlConfig) GetKubeNamespace() string { return c.Opts.Namespace }
func (c *kubectlConfig) WorkingDir() string { return c.workingDir }
func (c *kubectlConfig) SkipRender() bool { return c.skipRender }
func (c *kubectlConfig) ForceDeploy() bool { return c.force }
func (c *kubectlConfig) DefaultRepo() *string { return &c.defaultRepo }
func (c *kubectlConfig) WaitForDeletions() config.WaitForDeletions { return c.waitForDeletions }
func (c *kubectlConfig) GetKubeContext() string { return "kubecontext" }
func (c *kubectlConfig) GetKubeNamespace() string { return c.Opts.Namespace }
func (c *kubectlConfig) WorkingDir() string { return c.workingDir }
func (c *kubectlConfig) SkipRender() bool { return c.skipRender }
func (c *kubectlConfig) ForceDeploy() bool { return c.force }
func (c *kubectlConfig) DefaultRepo() *string { return &c.defaultRepo }
func (c *kubectlConfig) WaitForDeletions() config.WaitForDeletions { return c.waitForDeletions }
func (c *kubectlConfig) PortForwardResources() []*latestV1.PortForwardResource { return nil }
2 changes: 1 addition & 1 deletion pkg/skaffold/deploy/kustomize/kustomize.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func NewDeployer(cfg kubectl.Config, labels map[string]string, provider deploy.C
return &Deployer{
KustomizeDeploy: d,
podSelector: podSelector,
accessor: provider.Accessor.GetKubernetesAccessor(podSelector),
accessor: provider.Accessor.GetKubernetesAccessor(cfg, podSelector),
debugger: provider.Debugger.GetKubernetesDebugger(podSelector),
logger: provider.Logger.GetKubernetesLogger(podSelector),
statusMonitor: provider.Monitor.GetKubernetesMonitor(cfg),
Expand Down
11 changes: 6 additions & 5 deletions pkg/skaffold/deploy/kustomize/kustomize_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -719,8 +719,9 @@ type kustomizeConfig struct {
waitForDeletions config.WaitForDeletions
}

func (c *kustomizeConfig) ForceDeploy() bool { return c.force }
func (c *kustomizeConfig) WaitForDeletions() config.WaitForDeletions { return c.waitForDeletions }
func (c *kustomizeConfig) WorkingDir() string { return c.workingDir }
func (c *kustomizeConfig) GetKubeContext() string { return kubectl.TestKubeContext }
func (c *kustomizeConfig) GetKubeNamespace() string { return c.Opts.Namespace }
func (c *kustomizeConfig) ForceDeploy() bool { return c.force }
func (c *kustomizeConfig) WaitForDeletions() config.WaitForDeletions { return c.waitForDeletions }
func (c *kustomizeConfig) WorkingDir() string { return c.workingDir }
func (c *kustomizeConfig) GetKubeContext() string { return kubectl.TestKubeContext }
func (c *kustomizeConfig) GetKubeNamespace() string { return c.Opts.Namespace }
func (c *kustomizeConfig) PortForwardResources() []*latestV1.PortForwardResource { return nil }
2 changes: 2 additions & 0 deletions pkg/skaffold/kubernetes/portforward/forwarder_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ import (
)

type Config interface {
kubectl.Config

Mode() config.RunMode
PortForwardResources() []*latestV1.PortForwardResource
PortForwardOptions() config.PortForwardOptions
Expand Down
2 changes: 1 addition & 1 deletion pkg/skaffold/runner/v1/new.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func NewForConfig(runCtx *runcontext.RunContext) (*SkaffoldRunner, error) {

var deployer deploy.Deployer
provider := deploy.ComponentProvider{
Accessor: access.NewAccessorProvider(runCtx, labeller, kubectlCLI),
Accessor: access.NewAccessorProvider(labeller),
Debugger: debug.NewDebugProvider(runCtx),
Logger: log.NewLogProvider(runCtx, kubectlCLI),
Monitor: status.NewMonitorProvider(labeller),
Expand Down

0 comments on commit 044c65f

Please sign in to comment.