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

Adding support for Kustomize output #698

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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: 1 addition & 1 deletion cmd/kev/cmd/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func init() {
"format",
"f",
"kubernetes", // default: native kubernetes manifests
"Deployment files format. Default: Kubernetes manifests.",
"Deployment files format. Default: Kubernetes manifests. Supports kubernets and kustomize outputs.",
)

flags.BoolP(
Expand Down
10 changes: 9 additions & 1 deletion pkg/kev/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ package converter

import (
"github.com/appvia/kev/pkg/kev/converter/dummy"
"github.com/appvia/kev/pkg/kev/converter/kubernetes"
kubernetes "github.com/appvia/kev/pkg/kev/converter/kubernetes"
kustomize "github.com/appvia/kev/pkg/kev/converter/kustomize"
kmd "github.com/appvia/komando"
composego "github.com/compose-spec/compose-go/types"
)
Expand All @@ -40,6 +41,13 @@ func Factory(name string, ui kmd.UI) Converter {
case "dummy":
// Dummy converter example
return dummy.New()
case "kustomize":
// Kubernetes manifests converter by default
if ui == nil {
return kustomize.New()
}
return kustomize.NewWithUI(ui)

default:
// Kubernetes manifests converter by default
if ui == nil {
Expand Down
213 changes: 213 additions & 0 deletions pkg/kev/converter/kustomize/converter.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,213 @@
/**
* Copyright 2020 Appvia Ltd <info@appvia.io>
*
* 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 kustomize

import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"sort"

"github.com/appvia/kev/pkg/kev/log"
kmd "github.com/appvia/komando"
composego "github.com/compose-spec/compose-go/types"
"github.com/kubernetes-sigs/kustomize/pkg/types"
"github.com/pkg/errors"
"gopkg.in/yaml.v3"
)

const (
// Name of the converter
Name = "kustomize"
singleFileDefaultName = "k8s.yaml"

// MultiFileSubDir is default output directory name for kubernetes manifests
MultiFileSubDir = "kustomize"
KustomizeBaseFolderName = "base"
)

// K8s is a native kubernetes manifests converter
type K8s struct {
UI kmd.UI
}

// New return a native Kubernetes converter
func New() *K8s {
return &K8s{}
}

func NewWithUI(ui kmd.UI) *K8s {
return &K8s{UI: ui}
}

// Render generates outcome
func (c *K8s) Render(singleFile bool,
dir, workDir string,
projects map[string]*composego.Project,
files map[string][]string,
rendered map[string][]byte,
excluded map[string][]string) (map[string]string, error) {

renderOutputPaths := map[string]string{}
envs := getSortedEnvs(projects)

var outputFolder string
if dir != "" {
outputFolder = dir
} else {
outputFolder = filepath.Join(workDir, MultiFileSubDir)
}

err := ensureFolder(filepath.Join(outputFolder, KustomizeBaseFolderName))
if err != nil {
return nil, err
}

env := KustomizeBaseFolderName
project := projects["dev"]

log.Debugf("Rendering environment [%s]", env)

envFile := files["dev"][len(files["dev"])-1]
c.UI.Output(fmt.Sprintf("%s: %s", env, envFile))

outEnvPath := filepath.Join(outputFolder, env)

// @step generate multiple files - single file output not supported for Kustomize
outFilePath := outEnvPath

// @step kubernetes manifests output options
convertOpts := ConvertOptions{
InputFiles: files["dev"],
OutFile: outFilePath,
}

renderOutputPaths[env] = outFilePath

// @step set excluded docker compose services for current project
exc := []string{}
if excluded != nil {
if e, ok := excluded[env]; ok {
exc = e
}
}

// @step Get Kubernetes transformer that maps compose project to Kubernetes primitives
k := &Kustomize{Opt: convertOpts, Project: project, Excluded: exc, UI: c.UI}

// @step Do the transformation
objects, err := k.Transform()
if err != nil {
return nil, err
}

// @step Produce objects
err = PrintList(objects, convertOpts, rendered)
if err != nil {
return nil, errors.Wrapf(err, "Could not render %s manifests to disk, details:\n", Name)
}

for _, env := range envs {
project := projects[env]

log.Debugf("Rendering environment [%s]", env)

fmt.Println(files)

envFile := files[env][len(files[env])-1]
c.UI.Output(fmt.Sprintf("%s: %s", env, envFile))

outEnvPath := filepath.Join(outputFolder, env)
err := ensureFolder(outEnvPath)
if err != nil {
return nil, err
}

// @step generate multiple files - single file output not supported for Kustomize
outFilePath := outEnvPath

// @step kubernetes manifests output options
convertOpts := ConvertOptions{
InputFiles: files[env],
OutFile: outFilePath,
Environment: env,
}

renderOutputPaths[env] = outFilePath

// @step set excluded docker compose services for current project
exc := []string{}
if excluded != nil {
if e, ok := excluded[env]; ok {
exc = e
}
}

// @step Get Kubernetes transformer that maps compose project to Kubernetes primitives
k := &Kustomize{Opt: convertOpts, Project: project, Excluded: exc, UI: c.UI}

err = k.GenerateOverlay()
if err != nil {
return nil, err
}
}

return renderOutputPaths, nil
}

func (k *Kustomize) GenerateOverlay() error {
kustomization := &types.Kustomization{}
kustomization.Bases = []string{"../base"}
kustomization.NamePrefix = fmt.Sprintf("%s-", k.Opt.Environment)
kustomization.GeneratorOptions = &types.GeneratorOptions{
DisableNameSuffixHash: true,
}

bytes, err := yaml.Marshal(kustomization)
if err != nil {
return err
}

err = ioutil.WriteFile(filepath.Join(k.Opt.OutFile, "kustomization.yaml"), bytes, os.ModePerm)
if err != nil {
return err
}

return nil
}

func ensureFolder(folderPath string) error {
// @step create output directory
// To generate outcome as a set of separate manifests first must create out directory
// as Kompose logic checks for this and only will do that for existing directories,
// otherwise will treat OutFile as regular file and output all manifests to that single file.
if err := os.MkdirAll(folderPath, os.ModePerm); err != nil {
return err
}

return nil
}

func getSortedEnvs(projects map[string]*composego.Project) []string {
var out []string
for env := range projects {
out = append(out, env)
}
sort.Strings(out)
return out
}
45 changes: 45 additions & 0 deletions pkg/kev/converter/kustomize/helpers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/**
* Copyright 2020 Appvia Ltd <info@appvia.io>
*
* 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 kustomize

import (
"bytes"

"github.com/appvia/kev/pkg/kev/log"
"github.com/google/go-cmp/cmp"
"github.com/sirupsen/logrus"
"github.com/sirupsen/logrus/hooks/test"

. "github.com/onsi/gomega"
)

var hook *test.Hook

func init() {
// Use mem buffer in test instead of Stdout
logBuffer := &bytes.Buffer{}
log.SetOutput(logBuffer)
hook = test.NewLocal(log.GetLogger())
}

func assertLog(level logrus.Level, message string, fields map[string]string) {
Expect(hook.LastEntry().Level).To(Equal(level))
Expect(cmp.Diff(hook.LastEntry().Message, message)).To(BeEmpty())
for k, v := range fields {
Expect(hook.LastEntry().Data).To(HaveKeyWithValue(k, v))
}
}
29 changes: 29 additions & 0 deletions pkg/kev/converter/kustomize/kubernetes_suite_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Copyright 2020 Appvia Ltd <info@appvia.io>
*
* 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 kustomize_test

import (
"testing"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)

func TestKubernetes(t *testing.T) {
RegisterFailHandler(Fail)
RunSpecs(t, "Kubernetes Suite")
}
66 changes: 66 additions & 0 deletions pkg/kev/converter/kustomize/probes.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package kustomize

import (
"errors"

v1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/util/intstr"

"github.com/appvia/kev/pkg/kev/config"
)

func LivenessProbeToV1Probe(lp config.LivenessProbe) (*v1.Probe, error) {
lp.SuccessThreshold = config.DefaultProbeSuccessThreshold
return v1probe(lp.Type, lp.ProbeConfig)
}

func ReadinessProbeToV1Probe(rp config.ReadinessProbe) (*v1.Probe, error) {
return v1probe(rp.Type, rp.ProbeConfig)
}

func v1probe(probeType string, pc config.ProbeConfig) (*v1.Probe, error) {
pt, ok := config.ProbeTypeFromString(probeType)
if !ok {
return nil, errors.New("invalid probe type")
}

if pt == config.ProbeTypeNone {
return nil, nil
}

return &v1.Probe{
Handler: handlerFromType(pt, pc),
InitialDelaySeconds: int32(pc.InitialDelay.Seconds()),
TimeoutSeconds: int32(pc.Timeout.Seconds()),
PeriodSeconds: int32(pc.Period.Seconds()),
SuccessThreshold: int32(pc.SuccessThreshold),
FailureThreshold: int32(pc.FailureThreshold),
}, nil
}

func handlerFromType(probeType config.ProbeType, pc config.ProbeConfig) v1.Handler {
switch probeType {
case config.ProbeTypeTCP:
return v1.Handler{
TCPSocket: &v1.TCPSocketAction{
Port: intstr.FromInt(pc.TCP.Port),
},
}
case config.ProbeTypeHTTP:
return v1.Handler{
HTTPGet: &v1.HTTPGetAction{
Path: pc.HTTP.Path,
Port: intstr.FromInt(pc.HTTP.Port),
},
}
case config.ProbeTypeExec:
return v1.Handler{
Exec: &v1.ExecAction{
Command: pc.Exec.Command,
},
}
default:
}

return v1.Handler{}
}
Loading