Skip to content
Open
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
12 changes: 6 additions & 6 deletions pkg/compare/compare.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ var (
)

const (
noRefFileWasPassed = "\"Reference config file is required\""
refFileNotExistsError = "\"Reference config file doesn't exist\""
noRefFileWasPassed = "Reference config file is required"
refFileNotExistsError = "Reference config file doesn't exist"
emptyTypes = "templates don't contain any types (kind) of resources that are supported by the cluster"
// DiffSeparator separates diff outputs.
DiffSeparator = "**********************************\n"
Expand Down Expand Up @@ -737,7 +737,7 @@ func diffAgainstTemplate(temp ReferenceTemplate, clusterCR *unstructured.Unstruc

err = differ.Diff(obj, diff.Printer{}, o.ShowManagedFields, false)
if err != nil {
return res, fmt.Errorf("error occurered during diff: %w", err)
return res, fmt.Errorf("error occurred during diff: %w", err)
}
err = differ.Run(&diff.DiffProgram{Exec: exec.New(), IOStreams: genericiooptions.IOStreams{In: o.In, Out: diffOutput, ErrOut: o.ErrOut}})

Expand Down Expand Up @@ -1084,19 +1084,19 @@ func (obj InfoObject) runInlineDiffFuncs() error {
}
value, exist, err := NestedString(obj.injectedObjFromTemplate.Object, listedPath...)
if err != nil {
errs = append(errs, fmt.Errorf("failed to acces value in template of field %s that uses inline diff func: %w", pathToKey, err))
errs = append(errs, fmt.Errorf("failed to access value in template of field %s that uses inline diff func: %w", pathToKey, err))
continue
}
if !exist {
errs = append(errs, fmt.Errorf("failed to acces value in template of field %s that uses inline diff func: Not found", pathToKey))
errs = append(errs, fmt.Errorf("failed to access value in template of field %s that uses inline diff func: Not found", pathToKey))
continue
}
clusterValue, exist, err := NestedString(obj.clusterObj.Object, listedPath...)
if !exist {
continue // if value does not appear in cluster CR then there will be a diff anyway and this is not an error
}
if err != nil {
errs = append(errs, fmt.Errorf("failed to acces value in cluster cr of field %s that uses inline diff func: %w", pathToKey, err))
errs = append(errs, fmt.Errorf("failed to access value in cluster cr of field %s that uses inline diff func: %w", pathToKey, err))
Comment thread
coderabbitai[bot] marked this conversation as resolved.
continue
}
diffFn := InlineDiffs[inlineDiffFunc]
Expand Down
16 changes: 8 additions & 8 deletions pkg/compare/correlator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ const (
coreAPIVersion = "v1"
)

// Correlator provides an abstraction that allow the usage of different Resource correlation logics
// in the kubectl cluster-compare. The correlation process Matches for each Resource a template.
// Correlator provides an abstraction that allows the usage of different Resource correlation logics
// in the kubectl cluster-compare. The correlation process matches a template for each resource.
type Correlator[T CorrelationEntry] interface {
Match(*unstructured.Unstructured) ([]T, error)
}
Expand Down Expand Up @@ -81,7 +81,7 @@ type CorrelationEntry interface {
GetMetadata() *unstructured.Unstructured
}

// ExactMatchCorrelator Matches templates by exact match between a predefined config including pairs of Resource names and there equivalent template.
// ExactMatchCorrelator matches templates by exact match between a predefined config including pairs of Resource names and their equivalent template.
// The names of the resources are in the apiVersion-kind-namespace-name format.
// For fields that are not namespaced apiVersion-kind-name format will be used.
type ExactMatchCorrelator[T CorrelationEntry] struct {
Expand Down Expand Up @@ -116,18 +116,18 @@ func (c ExactMatchCorrelator[T]) Match(object *unstructured.Unstructured) ([]T,
return []T{temp}, nil
}

// GroupCorrelator Matches templates by hashing predefined fields.
// All The templates are indexed by hashing groups of `indexed` fields. The `indexed` fields can be nested.
// GroupCorrelator matches templates by hashing predefined fields.
// All the templates are indexed by hashing groups of `indexed` fields. The `indexed` fields can be nested.
// Resources will be attempted to be matched with hashing by the group with the largest amount of `indexed` fields.
// In case a Resource Matches by a hash a group of templates the group correlator will continue looking for a match
// In case a resource matches by a hash a group of templates the group correlator will continue looking for a match
// (with groups with less `indexed fields`) until it finds a distinct match, in case it doesn't, MultipleMatches error
// will be returned.
// Templates will be only indexed by a group of fields only if all fields in group are not templated.
type GroupCorrelator[T CorrelationEntry] struct {
fieldCorrelators []*FieldCorrelator[T]
}

// NewGroupCorrelator creates a new GroupCorrelator using inputted fieldGroups and generated GroupFunctions and templatesByGroups.
// NewGroupCorrelator creates a new GroupCorrelator using input fieldGroups and generated GroupFunctions and templatesByGroups.
// The templates will be divided into different kinds of groups based on the fields that are templated. Templates will be added
// to the kind of group that contains the biggest amount of fully defined `indexed` fields.
// For fieldsGroups = {{{"metadata", "namespace"}, {"kind"}}, {{"kind"}}} and the following templates: [fixedKindTemplate, fixedNamespaceKindTemplate]
Expand Down Expand Up @@ -219,7 +219,7 @@ func (c *GroupCorrelator[T]) Match(object *unstructured.Unstructured) ([]T, erro
return []T{}, UnknownMatch{Resource: object}
}

// MetricsTracker Matches templates by using an existing correlator and gathers summary info related the correlation.
// MetricsTracker matches templates by using an existing correlator and gathers summary info related to the correlation.
type MetricsTracker struct {
UnMatchedCRs []*unstructured.Unstructured
unMatchedLock sync.Mutex
Expand Down
2 changes: 1 addition & 1 deletion pkg/compare/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (s DiffSum) WasPatched() bool {

// Summary Contains all info included in the Summary output of the compare command
type Summary struct {
ValidationIssues map[string]map[string]ValidationIssue `json:"ValidationIssuses"`
ValidationIssues map[string]map[string]ValidationIssue `json:"ValidationIssues"`
NumMissing int `json:"NumMissing"`
UnmatchedCRS []string `json:"UnmatchedCRS"`
NumDiffCRs int `json:"NumDiffCRs"`
Expand Down
14 changes: 7 additions & 7 deletions pkg/compare/referenceV1.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,23 +250,23 @@ func (rf ReferenceTemplateV1) GetFieldsToOmit(fieldsToOmit FieldsToOmit) []*Mani
return result
}

for _, feildsRef := range rf.Config.FieldsToOmitRefs {
result = append(result, items[feildsRef]...)
for _, fieldsRef := range rf.Config.FieldsToOmitRefs {
result = append(result, items[fieldsRef]...)
}
return result
}

const (
fieldsToOmitRefsNotFound = `fieldsToOmitRefs entry "%s" not found it fieldsToOmit Items`
fieldsToOmitRefsNotFound = `fieldsToOmitRefs entry "%s" not found in fieldsToOmit Items`
)

// ValidateFieldsToOmit validates the fields to omit.
func (rf ReferenceTemplateV1) ValidateFieldsToOmit(fieldsToOmit FieldsToOmit) error {
errs := make([]error, 0)
items := fieldsToOmit.GetItems()
for _, feildsRef := range rf.Config.FieldsToOmitRefs {
if _, ok := items[feildsRef]; !ok {
errs = append(errs, fmt.Errorf(fieldsToOmitRefsNotFound, feildsRef))
for _, fieldsRef := range rf.Config.FieldsToOmitRefs {
if _, ok := items[fieldsRef]; !ok {
errs = append(errs, fmt.Errorf(fieldsToOmitRefsNotFound, fieldsRef))
}
}
return errors.Join(errs...)
Expand All @@ -279,7 +279,7 @@ func (rf ReferenceTemplateV1) Exec(params map[string]any) (*unstructured.Unstruc
var buf bytes.Buffer
err := rf.Execute(&buf, params)
if err != nil {
return nil, fmt.Errorf("failed to constuct template: %w", err)
return nil, fmt.Errorf("failed to construct template: %w", err)
}
data := make(map[string]any)
content := buf.Bytes()
Expand Down
4 changes: 2 additions & 2 deletions pkg/compare/referenceV2.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func (rf ReferenceTemplateV2) validateConfigPerField() error {
listedPath, err := pathToList(pathToKey)
if err != nil {
return fmt.Errorf("reference contains template with config per field with pathToKey that is not in "+
"supoorted format. path: %s. error: %v", pathToKey, err)
"supported format. path: %s. error: %v", pathToKey, err)
}
diffFn, ok := InlineDiffs[inlineDiffFunc]
if !ok {
Expand Down Expand Up @@ -603,7 +603,7 @@ func (comp ComponentV2) getTemplates(component *PartV2) []*ReferenceTemplateV2 {
}

func (comp ComponentV2) getValidationIssues(matchedTemplates map[string]int) (ValidationIssue, int) {
// Because of the validation in ComponentV2.validate we should ave one and only one
// Because of the validation in ComponentV2.validate we should have one and only one
return comp.parts[0].getMissingCRs(matchedTemplates)
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: fieldsToOmitRefs entry "IDontExist" not found it fieldsToOmit Items
error: fieldsToOmitRefs entry "IDontExist" not found in fieldsToOmit Items
error code:2
2 changes: 1 addition & 1 deletion pkg/compare/testdata/JSONOutput/localout.golden
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"Summary":{"ValidationIssuses":{"ExamplePart":{"Dashboard":{"Msg":"Missing CRs","CRs":["deploymentDashboard.yaml"]}}},"NumMissing":1,"UnmatchedCRS":[],"NumDiffCRs":1,"TotalCRs":1,"MetadataHash":"f077accb087abd9b771f530230261cf5de002fe007c62a2bae1c11e1a516cec8","patchedCRs":0},"Diffs":[{"DiffOutput":"diff -u -N TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper\n--- TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper\tDATE\n@@ -10,7 +10,7 @@\n revisionHistoryLimit: 10\n selector:\n matchLabels:\n- k8s-app: dashboard-metrics-scraper\n+ k8s-app: dashboard-metrics-scraper-diff\n template:\n metadata:\n labels:\n","CorrelatedTemplate":"deploymentMetrics.yaml","CRName":"apps/v1_Deployment_kubernetes-dashboard_dashboard-metrics-scraper"}]}
{"Summary":{"ValidationIssues":{"ExamplePart":{"Dashboard":{"Msg":"Missing CRs","CRs":["deploymentDashboard.yaml"]}}},"NumMissing":1,"UnmatchedCRS":[],"NumDiffCRs":1,"TotalCRs":1,"MetadataHash":"f077accb087abd9b771f530230261cf5de002fe007c62a2bae1c11e1a516cec8","patchedCRs":0},"Diffs":[{"DiffOutput":"diff -u -N TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper\n--- TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_dashboard-metrics-scraper\tDATE\n@@ -10,7 +10,7 @@\n revisionHistoryLimit: 10\n selector:\n matchLabels:\n- k8s-app: dashboard-metrics-scraper\n+ k8s-app: dashboard-metrics-scraper-diff\n template:\n metadata:\n labels:\n","CorrelatedTemplate":"deploymentMetrics.yaml","CRName":"apps/v1_Deployment_kubernetes-dashboard_dashboard-metrics-scraper"}]}
2 changes: 1 addition & 1 deletion pkg/compare/testdata/NoInput/localerr.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
error: usage error: "Reference config file is required"
error: usage error: Reference config file is required
See 'cluster-compare -h' for help and examples
error code:2
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: "Reference config file doesn't exist"
error: Reference config file doesn't exist
error code:2
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: error occurred while trying to process resources: template injection failed: failed to properly run inline diff functions for v1_configmap_kubernetes-dashboard_kubernetes-dashboard-settings some diff may be incorrect: failed to acces value in template of field spec.bigTextBloc that uses inline diff func: Not found
error: error occurred while trying to process resources: template injection failed: failed to properly run inline diff functions for v1_configmap_kubernetes-dashboard_kubernetes-dashboard-settings some diff may be incorrect: failed to access value in template of field spec.bigTextBloc that uses inline diff func: Not found
error code:2
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
error: failed to parse template apps.v1.DaemonSet.kube-system.kindnet.yaml with empty data: failed to constuct template: template: apps.v1.DaemonSet.kube-system.kindnet.yaml:5:61: executing "apps.v1.DaemonSet.kube-system.kindnet.yaml" at <len .spec.annotations>: error calling len: reflect: call of reflect.Value.Type on zero Value
error: failed to parse template apps.v1.DaemonSet.kube-system.kindnet.yaml with empty data: failed to construct template: template: apps.v1.DaemonSet.kube-system.kindnet.yaml:5:61: executing "apps.v1.DaemonSet.kube-system.kindnet.yaml" at <len .spec.annotations>: error calling len: reflect: call of reflect.Value.Type on zero Value
error code:2
8 changes: 4 additions & 4 deletions pkg/compare/testdata/YAMLOutput/localout.golden
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ Diffs:
CorrelatedTemplate: deploymentDashboard.yaml
DiffOutput: "diff -u -N TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard
TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\n---
TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n@@ -14,7 +14,7 @@\n template:\n metadata:\n labels:\n-
\ k8s-app: kubernetes-dashboard\n+ k8s-app: kubernetes-dashboard-diff\n
\ spec:\n containers:\n - args:\n"
TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n+++ TEMP/apps-v1_deployment_kubernetes-dashboard_kubernetes-dashboard\tDATE\n@@ -14,7 +14,7 @@\n template:\n metadata:\n labels:\n- k8s-app:
kubernetes-dashboard\n+ k8s-app: kubernetes-dashboard-diff\n spec:\n
\ containers:\n - args:\n"
Summary:
MetadataHash: f077accb087abd9b771f530230261cf5de002fe007c62a2bae1c11e1a516cec8
NumDiffCRs: 1
NumMissing: 1
TotalCRs: 1
UnmatchedCRS: []
ValidationIssuses:
ValidationIssues:
ExamplePart:
Dashboard:
CRs:
Expand Down