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

[v3] Add validator in render v2. #5942

Merged
merged 2 commits into from
Jun 7, 2021
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
51 changes: 44 additions & 7 deletions pkg/skaffold/render/renderer/renderer.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
Expand All @@ -30,6 +31,7 @@ import (
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/kubernetes/manifest"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/render/generate"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/render/kptfile"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/render/validate"
latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2"
"github.com/GoogleContainerTools/skaffold/pkg/skaffold/util"
)
Expand All @@ -44,17 +46,37 @@ type Renderer interface {
}

// NewSkaffoldRenderer creates a new Renderer object from the latestV2 API schema.
func NewSkaffoldRenderer(config *latestV2.RenderConfig, workingDir string) Renderer {
func NewSkaffoldRenderer(config *latestV2.RenderConfig, workingDir string) (Renderer, error) {
// TODO(yuwenma): return instance of kpt-managed mode or skaffold-managed mode defer to the config.Path fields.
// The alpha implementation only has skaffold-managed mode.
// TODO(yuwenma): The current work directory may not be accurate if users use --filepath flag.
hydrationDir := filepath.Join(workingDir, DefaultHydrationDir)
generator := generate.NewGenerator(workingDir, *config.Generate)
return &SkaffoldRenderer{Generator: *generator, workingDir: workingDir, hydrationDir: hydrationDir}

var generator *generate.Generator
if config.Generate == nil {
// If render.generate is not given, default to current working directory.
defaultManifests := filepath.Join(workingDir, "*.yaml")
generator = generate.NewGenerator(workingDir, latestV2.Generate{Manifests: []string{defaultManifests}})
} else {
generator = generate.NewGenerator(workingDir, *config.Generate)
}

var validator *validate.Validator
if config.Validate != nil {
var err error
validator, err = validate.NewValidator(*config.Validate)
if err != nil {
return nil, err
}
} else {
validator, _ = validate.NewValidator([]latestV2.Validator{})
}
return &SkaffoldRenderer{Generator: *generator, Validator: *validator, workingDir: workingDir, hydrationDir: hydrationDir}, nil
}

type SkaffoldRenderer struct {
generate.Generator
validate.Validator
workingDir string
hydrationDir string
labels map[string]string
Expand All @@ -68,14 +90,16 @@ func (r *SkaffoldRenderer) prepareHydrationDir(ctx context.Context) error {
if _, err := os.Stat(r.hydrationDir); os.IsNotExist(err) {
logrus.Debugf("creating render directory: %v", r.hydrationDir)
if err := os.MkdirAll(r.hydrationDir, os.ModePerm); err != nil {
return fmt.Errorf("creating cache directory for hydration: %w", err)
return fmt.Errorf("creating render directory for hydration: %w", err)
}
}
kptFilePath := filepath.Join(r.hydrationDir, kptfile.KptFileName)
if _, err := os.Stat(kptFilePath); os.IsNotExist(err) {
cmd := exec.CommandContext(ctx, "kpt", "pkg", "init", r.hydrationDir)
if _, err := util.RunCmdOut(cmd); err != nil {
return err
// TODO: user error. need manual init
return fmt.Errorf("unable to initialize kpt directory in %v, please manually run `kpt pkg init %v`",
kptFilePath, kptFilePath)
}
}
return nil
Expand Down Expand Up @@ -111,10 +135,23 @@ func (r *SkaffoldRenderer) Render(ctx context.Context, out io.Writer, builds []g
defer file.Close()
kfConfig := &kptfile.KptFile{}
if err := yaml.NewDecoder(file).Decode(&kfConfig); err != nil {
return err
// TODO: user error.
return fmt.Errorf("unable to parse %v: %w, please check if the kptfile is updated to new apiVersion > v1alpha2",
kptFilePath, err)
}
if kfConfig.Pipeline == nil {
kfConfig.Pipeline = &kptfile.Pipeline{}
}

// TODO: Update the Kptfile with the new validators.
kfConfig.Pipeline.Validators = r.GetDeclarativeValidators()
// TODO: Update the Kptfile with the new mutators.

configByte, err := yaml.Marshal(kfConfig)
if err != nil {
return fmt.Errorf("unable to marshal Kptfile config %v", kfConfig)
}
if err = ioutil.WriteFile(kptFilePath, configByte, 0644); err != nil {
return fmt.Errorf("unable to update %v", kptFilePath)
}
return nil
}
106 changes: 90 additions & 16 deletions pkg/skaffold/render/renderer/renderer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,95 @@ metadata:
`
)

func TestRender_StoredInCache(t *testing.T) {
testutil.Run(t, "", func(t *testutil.T) {
r := NewSkaffoldRenderer(&latestV2.RenderConfig{Generate: &latestV2.Generate{
Manifests: []string{"pod.yaml"}}}, "")
fakeCmd := testutil.CmdRunOut(fmt.Sprintf("kpt pkg init %v", DefaultHydrationDir), "")
t.Override(&util.DefaultExecCommand, fakeCmd)
t.NewTempDir().
Write("pod.yaml", podYaml).
Write(filepath.Join(DefaultHydrationDir, kptfile.KptFileName), initKptfile).
Touch("empty.ignored").
Chdir()
func TestRender(t *testing.T) {
tests := []struct {
description string
renderConfig *latestV2.RenderConfig
originalKptfile string
updatedKptfile string
}{
{
description: "single manifests, no hydration rule",
renderConfig: &latestV2.RenderConfig{
Generate: &latestV2.Generate{Manifests: []string{"pod.yaml"}},
},
originalKptfile: initKptfile,
updatedKptfile: `apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: skaffold
pipeline: {}
`,
},

{
description: "manifests not given.",
renderConfig: &latestV2.RenderConfig{},
originalKptfile: initKptfile,
updatedKptfile: `apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: skaffold
pipeline: {}
`,
},
{
description: "single manifests with validation rule.",
renderConfig: &latestV2.RenderConfig{
Generate: &latestV2.Generate{Manifests: []string{"pod.yaml"}},
Validate: &[]latestV2.Validator{{Name: "kubeval"}},
},
originalKptfile: initKptfile,
updatedKptfile: `apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: skaffold
pipeline:
validators:
- image: gcr.io/kpt-fn/kubeval:v0.1
`,
},
{
description: "Validation rule needs to be updated.",
renderConfig: &latestV2.RenderConfig{
Generate: &latestV2.Generate{Manifests: []string{"pod.yaml"}},
Validate: &[]latestV2.Validator{{Name: "kubeval"}},
},
originalKptfile: `apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: skaffold
pipeline:
validators:
- image: gcr.io/kpt-fn/SOME-OTHER-FUNC
`,
updatedKptfile: `apiVersion: kpt.dev/v1alpha2
kind: Kptfile
metadata:
name: skaffold
pipeline:
validators:
- image: gcr.io/kpt-fn/kubeval:v0.1
`,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
r, err := NewSkaffoldRenderer(test.renderConfig, "")
t.CheckNoError(err)
fakeCmd := testutil.CmdRunOut(fmt.Sprintf("kpt pkg init %v", DefaultHydrationDir), "")
t.Override(&util.DefaultExecCommand, fakeCmd)
t.NewTempDir().
Write("pod.yaml", podYaml).
Write(filepath.Join(DefaultHydrationDir, kptfile.KptFileName), test.originalKptfile).
Touch("empty.ignored").
Chdir()

var b bytes.Buffer
err := r.Render(context.Background(), &b, []graph.Artifact{{ImageName: "leeroy-web", Tag: "leeroy-web:v1"}})
t.CheckNoError(err)
t.CheckFileExistAndContent(filepath.Join(DefaultHydrationDir, dryFileName), []byte(labeledPodYaml))
})
var b bytes.Buffer
err = r.Render(context.Background(), &b, []graph.Artifact{{ImageName: "leeroy-web", Tag: "leeroy-web:v1"}})
t.CheckNoError(err)
t.CheckFileExistAndContent(filepath.Join(DefaultHydrationDir, dryFileName), []byte(labeledPodYaml))
t.CheckFileExistAndContent(filepath.Join(DefaultHydrationDir, kptfile.KptFileName), []byte(test.updatedKptfile))
})
}
}
52 changes: 52 additions & 0 deletions pkg/skaffold/render/validate/validate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/*
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 validate

import (
"fmt"

"github.com/GoogleContainerTools/skaffold/pkg/skaffold/render/kptfile"
latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2"
)

var validatorWhitelist = map[string]kptfile.Function{
"kubeval": {Image: "gcr.io/kpt-fn/kubeval:v0.1"},
// TODO: Add conftest validator in kpt catalog.
}

// NewValidator instantiates a Validator object.
func NewValidator(config []latestV2.Validator) (*Validator, error) {
var fns []kptfile.Function
for _, c := range config {
fn, ok := validatorWhitelist[c.Name]
if !ok {
// TODO: kpt user error
return nil, fmt.Errorf("unsupported validator %v", c.Name)
}
fns = append(fns, fn)
}
return &Validator{kptFn: fns}, nil
}

type Validator struct {
kptFn []kptfile.Function
}

// GetDeclarativeValidators transforms and returns the skaffold validators defined in skaffold.yaml
func (v *Validator) GetDeclarativeValidators() []kptfile.Function {
// TODO: guarantee the v.kptFn is updated once users changed skaffold.yaml file.
return v.kptFn
}
57 changes: 57 additions & 0 deletions pkg/skaffold/render/validate/validate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
/*
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 validate

import (
"testing"

latestV2 "github.com/GoogleContainerTools/skaffold/pkg/skaffold/schema/latest/v2"
"github.com/GoogleContainerTools/skaffold/testutil"
)

func TestValidatorInit(t *testing.T) {
tests := []struct {
description string
config []latestV2.Validator
shouldErr bool
}{
{
description: "no validation",
config: []latestV2.Validator{},
shouldErr: false,
},
{
description: "kubeval validator",
config: []latestV2.Validator{
{Name: "kubeval"},
},
shouldErr: false,
},
{
description: "invalid validator",
config: []latestV2.Validator{
{Name: "bad-validator"},
},
shouldErr: true,
},
}
for _, test := range tests {
testutil.Run(t, test.description, func(t *testutil.T) {
_, err := NewValidator(test.config)
t.CheckError(test.shouldErr, err)
})
}
}