Skip to content

Commit

Permalink
Merge pull request #4272 from mengqiy/pointer
Browse files Browse the repository at this point in the history
Make ResourceList follow k8s api conventions
  • Loading branch information
k8s-ci-robot committed Nov 9, 2021
2 parents e0c8ebc + 374d790 commit 3945670
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
4 changes: 2 additions & 2 deletions kyaml/fn/framework/command/example_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,11 +322,11 @@ func ExampleBuild_validate() {
validationResults = append(validationResults, &framework.Result{
Severity: framework.Error,
Message: "field is required",
ResourceRef: yaml.ResourceIdentifier{
ResourceRef: &yaml.ResourceIdentifier{
TypeMeta: meta.TypeMeta,
NameMeta: meta.ObjectMeta.NameMeta,
},
Field: framework.Field{
Field: &framework.Field{
Path: "spec.replicas",
ProposedValue: "1",
},
Expand Down
4 changes: 2 additions & 2 deletions kyaml/fn/framework/framework.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ type ResourceList struct {
// kind: Example
// spec:
// foo: var
FunctionConfig *yaml.RNode `yaml:"functionConfig" json:"functionConfig"`
FunctionConfig *yaml.RNode `yaml:"functionConfig,omitempty" json:"functionConfig,omitempty"`

// Results is ResourceList.results output value.
// Validating functions can optionally use this field to communicate structured
// validation error data to downstream functions.
Results Results `yaml:"results" json:"results"`
Results Results `yaml:"results,omitempty" json:"results,omitempty"`
}

// ResourceListProcessor is implemented by configuration functions built with this framework
Expand Down
8 changes: 4 additions & 4 deletions kyaml/fn/framework/framework_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ func TestExecute_Result(t *testing.T) {
{
Message: "bad value for replicas",
Severity: framework.Error,
ResourceRef: yaml.ResourceIdentifier{
ResourceRef: &yaml.ResourceIdentifier{
TypeMeta: yaml.TypeMeta{APIVersion: "v1", Kind: "Deployment"},
NameMeta: yaml.NameMeta{Name: "tester", Namespace: "default"},
},
Field: framework.Field{
Field: &framework.Field{
Path: ".spec.Replicas",
CurrentValue: "0",
ProposedValue: "3",
},
File: framework.File{
File: &framework.File{
Path: "/path/to/deployment.yaml",
Index: 0,
},
Expand Down Expand Up @@ -59,7 +59,7 @@ items:
err := framework.Execute(p, source)
assert.EqualError(t, err, `[error] v1/Deployment/default/tester .spec.Replicas: bad value for replicas
[error] : some error`)
[error]: some error`)
assert.Equal(t, 1, err.(*framework.Results).ExitCode())
assert.Equal(t, `apiVersion: config.kubernetes.io/v1
kind: ResourceList
Expand Down
44 changes: 27 additions & 17 deletions kyaml/fn/framework/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ type Result struct {

// ResourceRef is a reference to a resource.
// Required fields: apiVersion, kind, name.
ResourceRef yaml.ResourceIdentifier `yaml:"resourceRef,omitempty" json:"resourceRef,omitempty"`
ResourceRef *yaml.ResourceIdentifier `yaml:"resourceRef,omitempty" json:"resourceRef,omitempty"`

// Field is a reference to the field in a resource this result refers to
Field Field `yaml:"field,omitempty" json:"field,omitempty"`
Field *Field `yaml:"field,omitempty" json:"field,omitempty"`

// File references a file containing the resource this result refers to
File File `yaml:"file,omitempty" json:"file,omitempty"`
File *File `yaml:"file,omitempty" json:"file,omitempty"`

// Tags is an unstructured key value map stored with a result that may be set
// by external tools to store and retrieve arbitrary metadata
Expand All @@ -49,23 +49,33 @@ type Result struct {
func (i Result) String() string {
identifier := i.ResourceRef
var idStringList []string
if identifier.APIVersion != "" {
idStringList = append(idStringList, identifier.APIVersion)
}
if identifier.Kind != "" {
idStringList = append(idStringList, identifier.Kind)
}
if identifier.Namespace != "" {
idStringList = append(idStringList, identifier.Namespace)
}
if identifier.Name != "" {
idStringList = append(idStringList, identifier.Name)
if identifier != nil {
if identifier.APIVersion != "" {
idStringList = append(idStringList, identifier.APIVersion)
}
if identifier.Kind != "" {
idStringList = append(idStringList, identifier.Kind)
}
if identifier.Namespace != "" {
idStringList = append(idStringList, identifier.Namespace)
}
if identifier.Name != "" {
idStringList = append(idStringList, identifier.Name)
}
}
formatString := "[%s]"
list := []interface{}{i.Severity}
if len(idStringList) > 0 {
idString := strings.Join(idStringList, "/")
return fmt.Sprintf("[%s] %s %s: %s", i.Severity, idString, i.Field.Path, i.Message)
formatString += " %s"
list = append(list, strings.Join(idStringList, "/"))
}
if i.Field != nil {
formatString += " %s"
list = append(list, i.Field.Path)
}
return fmt.Sprintf("[%s] %s: %s", i.Severity, i.Field.Path, i.Message)
formatString += ": %s"
list = append(list, i.Message)
return fmt.Sprintf(formatString, list...)
}

// File references a file containing a resource
Expand Down

0 comments on commit 3945670

Please sign in to comment.