Add failed/success output to spec validate (#1471)

This commit is contained in:
Anubhav
2019-12-20 12:00:47 +08:00
committed by Ta-Ching Chen
parent 0274ceb312
commit 5eaf11b8aa
3 changed files with 28 additions and 18 deletions
+5 -1
View File
@@ -116,11 +116,15 @@ func (opts *ApplySubCommand) run(input cli.Input) error {
return errors.Wrap(err, "error reading specs")
}
var warnings []string
// validate
err = fr.Validate(input)
warnings, err = fr.Validate(input)
if err != nil {
return errors.Wrap(err, "error validating specs")
}
for _, warning := range warnings {
console.Warn(warning)
}
// make changes to the cluster based on the specs
pkgMetas, as, err := applyResources(opts.Client(), specDir, fr, deleteResources)
+14 -16
View File
@@ -276,8 +276,10 @@ func (fr *FissionResources) validateFunctionReference(functions map[string]bool,
return nil
}
func (fr *FissionResources) Validate(input cli.Input) error {
//Validate validates the spec file for irregular references
func (fr *FissionResources) Validate(input cli.Input) ([]string, error) {
result := utils.MultiErrorWithFormat()
var warnings []string
// check references: both dangling refs + garbage
// packages -> archives
@@ -375,7 +377,7 @@ func (fr *FissionResources) Validate(input cli.Input) error {
client, err := util.GetServer(input)
if err != nil {
return err
return warnings, err
}
for _, cm := range f.Spec.ConfigMaps {
_, err := client.V1().Misc().ConfigMapGet(&metav1.ObjectMeta{
@@ -383,7 +385,7 @@ func (fr *FissionResources) Validate(input cli.Input) error {
Namespace: cm.Namespace,
})
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("Configmap %s is referred in the spec but not present in the cluster", cm.Name))
warnings = append(warnings, fmt.Sprintf("Configmap %s is referred in the spec but not present in the cluster", cm.Name))
}
}
@@ -393,11 +395,9 @@ func (fr *FissionResources) Validate(input cli.Input) error {
Namespace: s.Namespace,
})
if k8serrors.IsNotFound(err) {
console.Warn(fmt.Sprintf("Secret %s is referred in the spec but not present in the cluster", s.Name))
warnings = append(warnings, fmt.Sprintf("Secret %s is referred in the spec but not present in the cluster", s.Name))
}
}
result = multierror.Append(result, f.Validate())
}
@@ -406,7 +406,7 @@ func (fr *FissionResources) Validate(input cli.Input) error {
ks := strings.Split(key, ":")
namespace, name := ks[0], ks[1]
if !referenced {
console.Warn(fmt.Sprintf(
warnings = append(warnings, fmt.Sprintf(
"%v: package '%v' is not used in any function",
fr.SourceMap.Locations["Package"][namespace][name],
name))
@@ -421,9 +421,8 @@ func (fr *FissionResources) Validate(input cli.Input) error {
}
if len(t.Spec.Host) > 0 {
console.Warn(fmt.Sprintf("Host in HTTPTrigger spec.Host is now marked as deprecated, see 'help' for details"))
warnings = append(warnings, (fmt.Sprintf("Host in HTTPTrigger spec.Host is now marked as deprecated, see 'help' for details")))
}
result = multierror.Append(result, t.Validate())
}
for _, t := range fr.KubernetesWatchTriggers {
@@ -456,30 +455,29 @@ func (fr *FissionResources) Validate(input cli.Input) error {
for _, e := range fr.Environments {
environments[fmt.Sprintf("%s:%s", e.Metadata.Name, e.Metadata.Namespace)] = struct{}{}
if ((e.Spec.Runtime.Container != nil) && (e.Spec.Runtime.PodSpec != nil)) || ((e.Spec.Builder.Container != nil) && (e.Spec.Builder.PodSpec != nil)) {
console.Warn("You have provided both - container spec and pod spec and while merging the pod spec will take precedence.")
warnings = append(warnings, fmt.Sprintf("You have provided both - container spec and pod spec and while merging the pod spec will take precedence."))
}
// Unlike CLI can change the environment version silently,
// we have to warn the user to modify spec file when this takes place.
if e.Spec.Poolsize != 3 && e.Spec.Version < 3 {
console.Warn("Poolsize can only be configured when environment version equals to 3, default poolsize 3 will be used for creating environment pool.")
warnings = append(warnings, fmt.Sprintf("Poolsize can only be configured when environment version equals to 3, default poolsize 3 will be used for creating environment pool."))
}
}
for _, f := range fr.Functions {
if _, ok := environments[fmt.Sprintf("%s:%s", f.Spec.Environment.Name, f.Spec.Environment.Namespace)]; !ok {
console.Warn(fmt.Sprintf("Environment %s is referenced in function %s but not declared in specs", f.Spec.Environment.Name, f.Metadata.Name))
warnings = append(warnings, fmt.Sprintf("Environment %s is referenced in function %s but not declared in specs", f.Spec.Environment.Name, f.Metadata.Name))
}
strategy := f.Spec.InvokeStrategy.ExecutionStrategy
if strategy.ExecutorType == fv1.ExecutorTypeNewdeploy && strategy.SpecializationTimeout < fv1.DefaultSpecializationTimeOut {
console.Warn(fmt.Sprintf("SpecializationTimeout in function spec.InvokeStrategy.ExecutionStrategy should be a value equal to or greater than %v", fv1.DefaultSpecializationTimeOut))
warnings = append(warnings, fmt.Sprintf("SpecializationTimeout in function spec.InvokeStrategy.ExecutionStrategy should be a value equal to or greater than %v", fv1.DefaultSpecializationTimeOut))
}
if f.Spec.FunctionTimeout <= 0 {
console.Warn(fmt.Sprintf("FunctionTimeout in function spec should be a field which should have a value greater than 0"))
warnings = append(warnings, fmt.Sprintf("FunctionTimeout in function spec should be a field which should have a value greater than 0"))
}
}
// (ErrorOrNil returns nil if there were no errors appended.)
return result.ErrorOrNil()
return warnings, result.ErrorOrNil()
}
// Keep track of source location of resources, and track duplicates
+9 -1
View File
@@ -18,6 +18,7 @@ package spec
import (
"bytes"
"fmt"
"io/ioutil"
"os"
"path/filepath"
@@ -29,6 +30,7 @@ import (
fv1 "github.com/fission/fission/pkg/apis/fission.io/v1"
"github.com/fission/fission/pkg/fission-cli/cliwrapper/cli"
"github.com/fission/fission/pkg/fission-cli/cmd"
"github.com/fission/fission/pkg/fission-cli/console"
"github.com/fission/fission/pkg/fission-cli/util"
)
@@ -55,12 +57,18 @@ func (opts *ValidateSubCommand) run(input cli.Input) error {
return errors.Wrap(err, "error reading specs")
}
var warnings []string
// this does the rest of the checks, like dangling refs
err = fr.Validate(input)
warnings, err = fr.Validate(input)
if err != nil {
return errors.Wrap(err, "error validating specs")
}
fmt.Printf("Spec validation successful\nSpec contains\n %v Functions\n %v Environments\n %v Packages \n %v Http Triggers \n %v MessageQueue Triggers\n %v Time Triggers\n %v Kube Watchers\n %v ArchiveUploadSpec\n",
len(fr.Functions), len(fr.Environments), len(fr.Packages), len(fr.HttpTriggers), len(fr.MessageQueueTriggers), len(fr.TimeTriggers), len(fr.KubernetesWatchTriggers), len(fr.ArchiveUploadSpecs))
for _, warning := range warnings {
console.Warn(warning)
}
return nil
}