Skip to content
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
40 changes: 31 additions & 9 deletions pkg/app/piped/cloudprovider/kubernetes/applier.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,20 @@ type Applier interface {
}

type applier struct {
input config.KubernetesDeploymentInput
logger *zap.Logger
input config.KubernetesDeploymentInput
cloudProvider config.CloudProviderKubernetesConfig
logger *zap.Logger

kubectl *Kubectl
initOnce sync.Once
initErr error
}

func NewApplier(input config.KubernetesDeploymentInput, logger *zap.Logger) Applier {
func NewApplier(input config.KubernetesDeploymentInput, cp config.CloudProviderKubernetesConfig, logger *zap.Logger) Applier {
return &applier{
input: input,
logger: logger.Named("kubernetes-applier"),
input: input,
cloudProvider: cp,
logger: logger.Named("kubernetes-applier"),
}
}

Expand All @@ -62,7 +64,12 @@ func (a *applier) ApplyManifest(ctx context.Context, manifest Manifest) error {
return a.initErr
}

return a.kubectl.Apply(ctx, a.getNamespaceToRun(manifest.Key), manifest)
return a.kubectl.Apply(
ctx,
a.cloudProvider.KubeConfigPath,
a.getNamespaceToRun(manifest.Key),
manifest,
)
}

// CreateManifest uses kubectl to create the given manifests.
Expand All @@ -74,7 +81,12 @@ func (a *applier) CreateManifest(ctx context.Context, manifest Manifest) error {
return a.initErr
}

return a.kubectl.Create(ctx, a.getNamespaceToRun(manifest.Key), manifest)
return a.kubectl.Create(
ctx,
a.cloudProvider.KubeConfigPath,
a.getNamespaceToRun(manifest.Key),
manifest,
)
}

// ReplaceManifest uses kubectl to replace the given manifests.
Expand All @@ -86,7 +98,12 @@ func (a *applier) ReplaceManifest(ctx context.Context, manifest Manifest) error
return a.initErr
}

err := a.kubectl.Replace(ctx, a.getNamespaceToRun(manifest.Key), manifest)
err := a.kubectl.Replace(
ctx,
a.cloudProvider.KubeConfigPath,
a.getNamespaceToRun(manifest.Key),
manifest,
)
if err == nil {
return nil
}
Expand All @@ -107,7 +124,12 @@ func (a *applier) Delete(ctx context.Context, k ResourceKey) (err error) {
return a.initErr
}

return a.kubectl.Delete(ctx, a.getNamespaceToRun(k), k)
return a.kubectl.Delete(
ctx,
a.cloudProvider.KubeConfigPath,
a.getNamespaceToRun(k),
k,
)
}

// getNamespaceToRun returns namespace used on kubectl apply/delete commands.
Expand Down
36 changes: 24 additions & 12 deletions pkg/app/piped/cloudprovider/kubernetes/kubectl.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func NewKubectl(version, path string) *Kubectl {
}
}

func (c *Kubectl) Apply(ctx context.Context, namespace string, manifest Manifest) (err error) {
func (c *Kubectl) Apply(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error) {
defer func() {
kubernetesmetrics.IncKubectlCallsCounter(
c.version,
Expand All @@ -59,9 +59,12 @@ func (c *Kubectl) Apply(ctx context.Context, namespace string, manifest Manifest
return err
}

args := make([]string, 0, 5)
args := make([]string, 0, 7)
if kubeconfig != "" {
args = append(args, "--kubeconfig", kubeconfig)
}
if namespace != "" {
args = append(args, "-n", namespace)
args = append(args, "--namespace", namespace)
}
args = append(args, "apply", "-f", "-")

Expand All @@ -76,7 +79,7 @@ func (c *Kubectl) Apply(ctx context.Context, namespace string, manifest Manifest
return nil
}

func (c *Kubectl) Create(ctx context.Context, namespace string, manifest Manifest) (err error) {
func (c *Kubectl) Create(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error) {
defer func() {
kubernetesmetrics.IncKubectlCallsCounter(
c.version,
Expand All @@ -90,9 +93,12 @@ func (c *Kubectl) Create(ctx context.Context, namespace string, manifest Manifes
return err
}

args := make([]string, 0, 5)
args := make([]string, 0, 7)
if kubeconfig != "" {
args = append(args, "--kubeconfig", kubeconfig)
}
if namespace != "" {
args = append(args, "-n", namespace)
args = append(args, "--namespace", namespace)
}
args = append(args, "create", "-f", "-")

Expand All @@ -107,7 +113,7 @@ func (c *Kubectl) Create(ctx context.Context, namespace string, manifest Manifes
return nil
}

func (c *Kubectl) Replace(ctx context.Context, namespace string, manifest Manifest) (err error) {
func (c *Kubectl) Replace(ctx context.Context, kubeconfig, namespace string, manifest Manifest) (err error) {
defer func() {
kubernetesmetrics.IncKubectlCallsCounter(
c.version,
Expand All @@ -121,9 +127,12 @@ func (c *Kubectl) Replace(ctx context.Context, namespace string, manifest Manife
return err
}

args := make([]string, 0, 5)
args := make([]string, 0, 7)
if kubeconfig != "" {
args = append(args, "--kubeconfig", kubeconfig)
}
if namespace != "" {
args = append(args, "-n", namespace)
args = append(args, "--namespace", namespace)
}
args = append(args, "replace", "-f", "-")

Expand All @@ -143,7 +152,7 @@ func (c *Kubectl) Replace(ctx context.Context, namespace string, manifest Manife
return fmt.Errorf("failed to replace: %s (%w)", string(out), err)
}

func (c *Kubectl) Delete(ctx context.Context, namespace string, r ResourceKey) (err error) {
func (c *Kubectl) Delete(ctx context.Context, kubeconfig, namespace string, r ResourceKey) (err error) {
defer func() {
kubernetesmetrics.IncKubectlCallsCounter(
c.version,
Expand All @@ -152,9 +161,12 @@ func (c *Kubectl) Delete(ctx context.Context, namespace string, r ResourceKey) (
)
}()

args := make([]string, 0, 5)
args := make([]string, 0, 7)
if kubeconfig != "" {
args = append(args, "--kubeconfig", kubeconfig)
}
if namespace != "" {
args = append(args, "-n", namespace)
args = append(args, "--namespace", namespace)
}
args = append(args, "delete", r.Kind, r.Name)

Expand Down
7 changes: 7 additions & 0 deletions pkg/app/piped/executor/kubernetes/kubernetes.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ func (e *deployExecutor) Execute(sig executor.StopSignal) model.StageStatus {
}
}

cp, ok := e.PipedConfig.FindCloudProvider(e.Deployment.CloudProvider, model.ApplicationKind_KUBERNETES)
if !ok {
e.LogPersister.Errorf("Not found cloud provider %q", e.Deployment.CloudProvider)
return model.StageStatus_STAGE_FAILURE
}

e.loader = provider.NewLoader(
e.Deployment.ApplicationName,
ds.AppDir,
Expand All @@ -105,6 +111,7 @@ func (e *deployExecutor) Execute(sig executor.StopSignal) model.StageStatus {
)
e.applier = provider.NewApplier(
e.appCfg.Input,
*cp.KubernetesConfig,
e.Logger,
)
e.Logger.Info("start executing kubernetes stage",
Expand Down
7 changes: 7 additions & 0 deletions pkg/app/piped/executor/kubernetes/rollback.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,8 +135,15 @@ func (e *rollbackExecutor) ensureRollback(ctx context.Context) model.StageStatus
return model.StageStatus_STAGE_FAILURE
}

cp, ok := e.PipedConfig.FindCloudProvider(e.Deployment.CloudProvider, model.ApplicationKind_KUBERNETES)
if !ok {
e.LogPersister.Errorf("Not found cloud provider %q", e.Deployment.CloudProvider)
return model.StageStatus_STAGE_FAILURE
}

applier := provider.NewApplier(
appCfg.Input,
*cp.KubernetesConfig,
e.Logger,
)

Expand Down