Skip to content

Commit

Permalink
Convert PortForwarder to Accessor, move to Deployer (#6026)
Browse files Browse the repository at this point in the history
  • Loading branch information
nkubala authored Jun 18, 2021
1 parent e01d089 commit 46c8933
Show file tree
Hide file tree
Showing 26 changed files with 316 additions and 148 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
Copyright 2019 The Skaffold Authors
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.
Expand All @@ -14,24 +14,25 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package access

import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/portforward"
)

func (r *SkaffoldRunner) createForwarder(out io.Writer) *portforward.ForwarderManager {
if !r.runCtx.PortForward() {
return nil
}

return portforward.NewForwarderManager(out,
r.kubectlCLI,
r.podSelector,
r.labeller.RunIDSelector(),
r.runCtx.Mode(),
r.runCtx.Opts.PortForward,
r.runCtx.PortForwardResources())
// Accessor defines the behavior for any implementation of a component
// that accesses and exposes deployed resources from Skaffold.
type Accessor interface {
// Start starts the resource accessor.
Start(context.Context, io.Writer, []string) error

// Stop stops the resource accessor.
Stop()
}

type NoopAccessor struct{}

func (n *NoopAccessor) Start(context.Context, io.Writer, []string) error { return nil }

func (n *NoopAccessor) Stop() {}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,26 @@ See the License for the specific language governing permissions and
limitations under the License.
*/

package v1
package access

import "context"
import (
"context"
"io"
)

type Watcher interface {
// Name returns the watcher name
Name() string
// Start starts the watcher
Start(ctx context.Context, namespaces []string) error
// Stop stops the watcher
Stop()
type AccessorMux []Accessor

func (a AccessorMux) Start(ctx context.Context, out io.Writer, namespaces []string) error {
for _, accessor := range a {
if err := accessor.Start(ctx, out, namespaces); err != nil {
return err
}
}
return nil
}

func (a AccessorMux) Stop() {
for _, accessor := range a {
accessor.Stop()
}
}
79 changes: 79 additions & 0 deletions pkg/skaffold/access/provider.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
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 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
GetNoopAccessor() Accessor
}

type fullProvider struct {
kubernetesAccessor func(*kubernetes.ImageList) 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 (p *fullProvider) GetKubernetesAccessor(s *kubernetes.ImageList) Accessor {
return p.kubernetesAccessor(s)
}

func (p *fullProvider) GetNoopAccessor() Accessor {
return &NoopAccessor{}
}

// NoopProvider is used in tests
type NoopProvider struct{}

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

func (p *NoopProvider) GetNoopAccessor() Accessor {
return &NoopAccessor{}
}
9 changes: 7 additions & 2 deletions pkg/skaffold/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ import (
"context"
"io"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/log"
)

// NoopComponentProvider is for tests
var NoopComponentProvider = ComponentProvider{Logger: &log.NoopProvider{}, Debugger: &debug.NoopProvider{}}
var NoopComponentProvider = ComponentProvider{Accessor: &access.NoopProvider{}, Debugger: &debug.NoopProvider{}, Logger: &log.NoopProvider{}}

// Deployer is the Deploy API of skaffold and responsible for deploying
// the build results to a Kubernetes cluster
Expand All @@ -52,13 +53,17 @@ type Deployer interface {
// GetLogger returns a Deployer's implementation of a Logger
GetLogger() log.Logger

// GetAccessor returns a Deployer's implementation of an Accessor
GetAccessor() access.Accessor

// TrackBuildArtifacts registers build artifacts to be tracked by a Deployer
TrackBuildArtifacts([]graph.Artifact)
}

// ComponentProvider serves as a clean way to send three providers
// as params to the Deployer constructors
type ComponentProvider struct {
Logger log.Provider
Accessor access.Provider
Debugger debug.Provider
Logger log.Provider
}
17 changes: 13 additions & 4 deletions pkg/skaffold/deploy/deploy_mux.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"strconv"
"strings"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug"
eventV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/event/v2"
Expand All @@ -39,12 +40,12 @@ import (
// it collects the results and returns it in bulk.
type DeployerMux []Deployer

func (m DeployerMux) GetLogger() log.Logger {
var loggers log.LoggerMux
func (m DeployerMux) GetAccessor() access.Accessor {
var accessors access.AccessorMux
for _, deployer := range m {
loggers = append(loggers, deployer.GetLogger())
accessors = append(accessors, deployer.GetAccessor())
}
return loggers
return accessors
}

func (m DeployerMux) GetDebugger() debug.Debugger {
Expand All @@ -55,6 +56,14 @@ func (m DeployerMux) GetDebugger() debug.Debugger {
return debuggers
}

func (m DeployerMux) GetLogger() log.Logger {
var loggers log.LoggerMux
for _, deployer := range m {
loggers = append(loggers, deployer.GetLogger())
}
return loggers
}

func (m DeployerMux) Deploy(ctx context.Context, w io.Writer, as []graph.Artifact) ([]string, error) {
seenNamespaces := util.NewStringSet()

Expand Down
9 changes: 7 additions & 2 deletions pkg/skaffold/deploy/deploy_mux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"os"
"testing"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/graph"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/log"
Expand All @@ -46,14 +47,18 @@ type MockDeployer struct {
renderErr error
}

func (m *MockDeployer) GetLogger() log.Logger {
return &log.NoopLogger{}
func (m *MockDeployer) GetAccessor() access.Accessor {
return &access.NoopAccessor{}
}

func (m *MockDeployer) GetDebugger() debug.Debugger {
return &debug.NoopDebugger{}
}

func (m *MockDeployer) GetLogger() log.Logger {
return &log.NoopLogger{}
}

func (m *MockDeployer) TrackBuildArtifacts(_ []graph.Artifact) {}

func (m *MockDeployer) Dependencies() ([]string, error) {
Expand Down
18 changes: 13 additions & 5 deletions pkg/skaffold/deploy/helm/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import (
shell "github.com/kballard/go-shellquote"
"github.com/sirupsen/logrus"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/constants"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug"
Expand Down Expand Up @@ -79,8 +80,10 @@ var (
type Deployer struct {
*latestV1.HelmDeploy

logger log.Logger
debugger debug.Debugger
accessor access.Accessor
debugger debug.Debugger
logger log.Logger

podSelector *kubernetes.ImageList
originalImages []graph.Artifact

Expand Down Expand Up @@ -131,8 +134,9 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
return &Deployer{
HelmDeploy: h,
podSelector: podSelector,
logger: provider.Logger.GetKubernetesLogger(podSelector),
accessor: provider.Accessor.GetKubernetesAccessor(podSelector),
debugger: provider.Debugger.GetKubernetesDebugger(podSelector),
logger: provider.Logger.GetKubernetesLogger(podSelector),
originalImages: originalImages,
kubeContext: cfg.GetKubeContext(),
kubeConfig: cfg.GetKubeConfig(),
Expand All @@ -146,14 +150,18 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
}, podSelector, nil
}

func (h *Deployer) GetLogger() log.Logger {
return h.logger
func (h *Deployer) GetAccessor() access.Accessor {
return h.accessor
}

func (h *Deployer) GetDebugger() debug.Debugger {
return h.debugger
}

func (h *Deployer) GetLogger() log.Logger {
return h.logger
}

func (h *Deployer) TrackBuildArtifacts(artifacts []graph.Artifact) {
deployutil.AddTagsToPodSelector(artifacts, h.originalImages, h.podSelector)
h.logger.RegisterArtifacts(artifacts)
Expand Down
18 changes: 13 additions & 5 deletions pkg/skaffold/deploy/kpt/kpt.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
k8syaml "sigs.k8s.io/yaml"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/access"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/config"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/debug"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/deploy"
Expand Down Expand Up @@ -69,8 +70,10 @@ const (
type Deployer struct {
*latestV1.KptDeploy

logger log.Logger
debugger debug.Debugger
accessor access.Accessor
debugger debug.Debugger
logger log.Logger

podSelector *kubernetes.ImageList
originalImages []graph.Artifact

Expand All @@ -93,8 +96,9 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
return &Deployer{
KptDeploy: d,
podSelector: podSelector,
logger: provider.Logger.GetKubernetesLogger(podSelector),
accessor: provider.Accessor.GetKubernetesAccessor(podSelector),
debugger: provider.Debugger.GetKubernetesDebugger(podSelector),
logger: provider.Logger.GetKubernetesLogger(podSelector),
insecureRegistries: cfg.GetInsecureRegistries(),
labels: labels,
globalConfig: cfg.GlobalConfig(),
Expand All @@ -105,14 +109,18 @@ func NewDeployer(cfg Config, labels map[string]string, provider deploy.Component
}, podSelector
}

func (k *Deployer) GetLogger() log.Logger {
return k.logger
func (k *Deployer) GetAccessor() access.Accessor {
return k.accessor
}

func (k *Deployer) GetDebugger() debug.Debugger {
return k.debugger
}

func (k *Deployer) GetLogger() log.Logger {
return k.logger
}

func (k *Deployer) TrackBuildArtifacts(artifacts []graph.Artifact) {
deployutil.AddTagsToPodSelector(artifacts, k.originalImages, k.podSelector)
k.logger.RegisterArtifacts(artifacts)
Expand Down
Loading

0 comments on commit 46c8933

Please sign in to comment.