Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement LoadManifests with Kustomize template #5291

Merged
merged 2 commits into from
Oct 24, 2024
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
65 changes: 65 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/provider/kustomize.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// Copyright 2024 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 provider

import (
"bytes"
"context"
"fmt"
"os/exec"

"go.uber.org/zap"
)

type Kustomize struct {
execPath string
logger *zap.Logger
}

func NewKustomize(path string, logger *zap.Logger) *Kustomize {
return &Kustomize{
execPath: path,
logger: logger,
}
}

func (c *Kustomize) Template(ctx context.Context, appName, appDir string, opts map[string]string) (string, error) {
args := []string{
"build",
".",
}

for k, v := range opts {
args = append(args, fmt.Sprintf("--%s", k))
if v != "" {
args = append(args, v)
}
}

var stdout, stderr bytes.Buffer
cmd := exec.CommandContext(ctx, c.execPath, args...)
cmd.Dir = appDir
cmd.Stdout = &stdout
cmd.Stderr = &stderr

c.logger.Info(fmt.Sprintf("start templating a Kustomize application %s", appName),
zap.Any("args", args),
)

if err := cmd.Run(); err != nil {
return stdout.String(), fmt.Errorf("%w: %s", err, stderr.String())
}

Check warning on line 63 in pkg/app/pipedv1/plugin/kubernetes/provider/kustomize.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/provider/kustomize.go#L62-L63

Added lines #L62 - L63 were not covered by tests
return stdout.String(), nil
}
55 changes: 55 additions & 0 deletions pkg/app/pipedv1/plugin/kubernetes/provider/kustomize_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// Copyright 2024 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 provider

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/kubernetes/toolregistry"
"github.com/pipe-cd/pipecd/pkg/app/pipedv1/plugin/toolregistry/toolregistrytest"
)

func TestKustomizeTemplate(t *testing.T) {
t.Parallel()

var (
ctx = context.TODO()
appName = "testapp"
appDir = "testdata/testkustomize"
)

c, err := toolregistrytest.NewToolRegistry(t)
require.NoError(t, err)

r := toolregistry.NewRegistry(c)

t.Cleanup(func() { c.Close() })

kustomizePath, err := r.Kustomize(context.Background(), "5.4.3")
require.NoError(t, err)
require.NotEmpty(t, kustomizePath)

kustomize := NewKustomize(kustomizePath, zap.NewNop())
out, err := kustomize.Template(ctx, appName, appDir, map[string]string{
"load-restrictor": "LoadRestrictionsNone",
})
require.NoError(t, err)
assert.True(t, len(out) > 0)
}
27 changes: 25 additions & 2 deletions pkg/app/pipedv1/plugin/kubernetes/provider/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package provider

import (
"context"
"errors"
"fmt"
"io/fs"
Expand All @@ -24,6 +25,7 @@
"strconv"
"strings"

"go.uber.org/zap"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/yaml"

Expand All @@ -39,20 +41,30 @@
)

type LoaderInput struct {
AppName string
AppDir string
ConfigFilename string
Manifests []string

Namespace string
TemplatingMethod TemplatingMethod

KustomizeVersion string
KustomizeOptions map[string]string

// TODO: define fields for LoaderInput.
}

type Loader struct {
toolRegistry ToolRegistry
}

type ToolRegistry interface {
Kustomize(ctx context.Context, version string) (string, error)
Helm(ctx context.Context, version string) (string, error)
}

func (l *Loader) LoadManifests(input LoaderInput) (manifests []Manifest, err error) {
func (l *Loader) LoadManifests(ctx context.Context, input LoaderInput) (manifests []Manifest, err error) {

Check warning on line 67 in pkg/app/pipedv1/plugin/kubernetes/provider/loader.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/provider/loader.go#L67

Added line #L67 was not covered by tests
defer func() {
// Override namespace if set because ParseManifests does not parse it
// if namespace is not explicitly specified in the manifests.
Expand All @@ -64,7 +76,18 @@
case TemplatingMethodHelm:
return nil, errors.New("not implemented yet")
case TemplatingMethodKustomize:
return nil, errors.New("not implemented yet")
kustomizePath, err := l.toolRegistry.Kustomize(ctx, input.KustomizeVersion)
if err != nil {
return nil, fmt.Errorf("failed to get kustomize tool: %w", err)
}

Check warning on line 82 in pkg/app/pipedv1/plugin/kubernetes/provider/loader.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/provider/loader.go#L79-L82

Added lines #L79 - L82 were not covered by tests

k := NewKustomize(kustomizePath, zap.NewNop()) // TODO: pass logger
data, err := k.Template(ctx, input.AppName, input.AppDir, input.KustomizeOptions)
if err != nil {
return nil, fmt.Errorf("failed to template kustomize manifests: %w", err)
}

Check warning on line 88 in pkg/app/pipedv1/plugin/kubernetes/provider/loader.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/provider/loader.go#L84-L88

Added lines #L84 - L88 were not covered by tests

return ParseManifests(data)

Check warning on line 90 in pkg/app/pipedv1/plugin/kubernetes/provider/loader.go

View check run for this annotation

Codecov / codecov/patch

pkg/app/pipedv1/plugin/kubernetes/provider/loader.go#L90

Added line #L90 was not covered by tests
case TemplatingMethodNone:
return LoadPlainYAMLManifests(input.AppDir, input.Manifests, input.ConfigFilename)
default:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
apiVersion: apps/v1
kind: Deployment
metadata:
name: the-deployment
spec:
replicas: 3
selector:
matchLabels:
deployment: hello
template:
metadata:
labels:
deployment: hello
spec:
containers:
- name: helloworld
image: ghcr.io/pipe-cd/helloworld:v0.49.2
args:
- server
ports:
- containerPort: 9085
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
commonLabels:
app: hello

resources:
- deployment.yaml
Loading