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
2 changes: 2 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
go_library(
name = "go_default_library",
srcs = [
"applier.go",
"cache.go",
"deployment.go",
"diff.go",
Expand All @@ -12,6 +13,7 @@ go_library(
"kubectl.go",
"kubernetes.go",
"kustomize.go",
"loader.go",
"manifest.go",
"resourcekey.go",
"state.go",
Expand Down
131 changes: 131 additions & 0 deletions pkg/app/piped/cloudprovider/kubernetes/applier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2022 The PipeCD 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 kubernetes

import (
"context"
"errors"
"fmt"
"sync"

"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/app/piped/toolregistry"
"github.com/pipe-cd/pipecd/pkg/config"
)

type Applier interface {
// ApplyManifest does applying the given manifest.
ApplyManifest(ctx context.Context, manifest Manifest) error
// CreateManifest does creating resource from given manifest.
CreateManifest(ctx context.Context, manifest Manifest) error
// ReplaceManifest does replacing resource from given manifest.
ReplaceManifest(ctx context.Context, manifest Manifest) error
// Delete deletes the given resource from Kubernetes cluster.
Delete(ctx context.Context, key ResourceKey) error
}

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

kubectl *Kubectl
initOnce sync.Once
initErr error
}

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

// ApplyManifest does applying the given manifest.
func (a *applier) ApplyManifest(ctx context.Context, manifest Manifest) error {
a.initOnce.Do(func() {
a.kubectl, a.initErr = a.findKubectl(ctx, a.input.KubectlVersion)
})
if a.initErr != nil {
return a.initErr
}

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

// CreateManifest uses kubectl to create the given manifests.
func (a *applier) CreateManifest(ctx context.Context, manifest Manifest) error {
a.initOnce.Do(func() {
a.kubectl, a.initErr = a.findKubectl(ctx, a.input.KubectlVersion)
})
if a.initErr != nil {
return a.initErr
}

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

// ReplaceManifest uses kubectl to replace the given manifests.
func (a *applier) ReplaceManifest(ctx context.Context, manifest Manifest) error {
a.initOnce.Do(func() {
a.kubectl, a.initErr = a.findKubectl(ctx, a.input.KubectlVersion)
})
if a.initErr != nil {
return a.initErr
}

err := a.kubectl.Replace(ctx, a.getNamespaceToRun(manifest.Key), manifest)
if err == nil {
return nil
}

if errors.Is(err, errorReplaceNotFound) {
return ErrNotFound
}

return err
}

// Delete deletes the given resource from Kubernetes cluster.
func (a *applier) Delete(ctx context.Context, k ResourceKey) (err error) {
a.initOnce.Do(func() {
a.kubectl, a.initErr = a.findKubectl(ctx, a.input.KubectlVersion)
})
if a.initErr != nil {
return a.initErr
}

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

// getNamespaceToRun returns namespace used on kubectl apply/delete commands.
// priority: config.KubernetesDeploymentInput > kubernetes.ResourceKey
func (a *applier) getNamespaceToRun(k ResourceKey) string {
if a.input.Namespace != "" {
return a.input.Namespace
}
return k.Namespace
}

func (a *applier) findKubectl(ctx context.Context, version string) (*Kubectl, error) {
path, installed, err := toolregistry.DefaultRegistry().Kubectl(ctx, version)
if err != nil {
return nil, fmt.Errorf("no kubectl %s (%v)", version, err)
}
if installed {
a.logger.Info(fmt.Sprintf("kubectl %s has just been installed because of no pre-installed binary for that version", version))
}
return NewKubectl(version, path), nil
}
Loading