-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Mateusz Urbanek <[email protected]>
- Loading branch information
Showing
7 changed files
with
271 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
/* | ||
Copyright 2025 anza-labs contributors. | ||
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 controller | ||
|
||
import ( | ||
"github.com/prometheus/client_golang/prometheus" | ||
"sigs.k8s.io/controller-runtime/pkg/metrics" | ||
) | ||
|
||
var ( | ||
validationErrorsCounter = prometheus.NewCounterVec( | ||
prometheus.CounterOpts{ | ||
Name: "annotation_validation_errors_total", | ||
Help: "Total count of validation errors", | ||
}, | ||
[]string{"source_namespace"}, | ||
) | ||
) | ||
|
||
func init() { | ||
// Register custom metrics with the global prometheus registry | ||
metrics.Registry.MustRegister(validationErrorsCounter) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
Copyright 2025 anza-labs contributors. | ||
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 controller | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"maps" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/util/validation" | ||
) | ||
|
||
const AnnotationValidationFailure = "AnnotationValidationFailure" | ||
|
||
// ValidationErrors represents a collection of validation errors. | ||
type ValidationErrors struct { | ||
Items []*ValidationError | ||
} | ||
|
||
// Error formats all validation errors into a single string. | ||
func (ve *ValidationErrors) Error() string { | ||
if len(ve.Items) == 0 { | ||
return "no validation errors" | ||
} | ||
var parts []string | ||
for _, item := range ve.Items { | ||
parts = append(parts, item.Error()) | ||
} | ||
return strings.Join(parts, "; ") | ||
} | ||
|
||
// ValidationError represents an error associated with a specific key. | ||
type ValidationError struct { | ||
Key string | ||
Errs []error | ||
} | ||
|
||
// NewValidationError creates a new ValidationError or appends errors to an existing one. | ||
func NewValidationError(existing *ValidationError, key string, errs ...error) *ValidationError { | ||
if existing == nil { | ||
return &ValidationError{Key: key, Errs: errs} | ||
} | ||
existing.Key = key | ||
existing.Errs = append(existing.Errs, errs...) | ||
return existing | ||
} | ||
|
||
// AppendError adds an error to the ValidationError. | ||
func (ve *ValidationError) AppendError(err error) { | ||
ve.Errs = append(ve.Errs, err) | ||
} | ||
|
||
// Error formats the ValidationError into a readable string. | ||
func (ve *ValidationError) Error() string { | ||
if len(ve.Errs) == 0 { | ||
return fmt.Sprintf("validation error at key %q with no specific error details", ve.Key) | ||
} | ||
var errMessages []string | ||
for _, err := range ve.Errs { | ||
errMessages = append(errMessages, err.Error()) | ||
} | ||
return fmt.Sprintf("validation error at key %q: [%s]", ve.Key, strings.Join(errMessages, ", ")) | ||
} | ||
|
||
func errsFromStrs(strs []string) []error { | ||
var errs []error | ||
for _, s := range strs { | ||
errs = append(errs, errors.New(s)) | ||
} | ||
|
||
return errs | ||
} | ||
|
||
func ValidateAnnotations(annotations map[string]string) (map[string]string, *ValidationErrors) { | ||
result := maps.Clone(annotations) | ||
|
||
var validationErrs []*ValidationError | ||
for k := range annotations { | ||
var verr *ValidationError | ||
|
||
errStrs := validation.IsQualifiedName(k) | ||
if errStrs != nil { | ||
verr = NewValidationError(verr, k, errsFromStrs(errStrs)...) | ||
} | ||
|
||
if verr != nil { | ||
delete(result, k) | ||
validationErrs = append(validationErrs, verr) | ||
} | ||
} | ||
|
||
if len(validationErrs) > 0 { | ||
return result, &ValidationErrors{Items: validationErrs} | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
Copyright 2025 anza-labs contributors. | ||
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 controller | ||
|
||
import ( | ||
"io" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidationErrors(t *testing.T) { | ||
verr1 := &ValidationError{ | ||
Key: "test", | ||
Errs: []error{ErrSkipReconciliation}, | ||
} | ||
assert.Equal(t, "validation error at key \"test\": [skip reconciliation]", verr1.Error()) | ||
|
||
verr2 := &ValidationError{ | ||
Key: "other", | ||
Errs: []error{io.EOF}, | ||
} | ||
|
||
verrs := ValidationErrors{Items: []*ValidationError{verr1, verr2}} | ||
assert.Equal(t, "validation error at key \"test\": [skip reconciliation]; validation error at key \"other\": [EOF]", verrs.Error()) | ||
} | ||
|
||
func TestValidationError(t *testing.T) { | ||
t.Parallel() | ||
|
||
for name, tc := range map[string]struct { | ||
verr *ValidationError | ||
key string | ||
inputErrors []error | ||
}{ | ||
"success": { | ||
verr: &ValidationError{Key: "test"}, | ||
key: "test", | ||
inputErrors: []error{ErrSkipReconciliation}, | ||
}, | ||
"success - joined error": { | ||
verr: &ValidationError{Key: "test", Errs: []error{io.EOF}}, | ||
key: "test", | ||
inputErrors: []error{ErrSkipReconciliation}, | ||
}, | ||
"success - without input errors": { | ||
key: "test", | ||
}, | ||
} { | ||
t.Run(name, func(t *testing.T) { | ||
t.Parallel() | ||
|
||
var expectedErrors []error | ||
if tc.verr != nil { | ||
expectedErrors = append(tc.inputErrors, tc.verr.Errs...) | ||
} | ||
|
||
valErr := NewValidationError(tc.verr, tc.key, tc.inputErrors...) | ||
assert.Equal(t, valErr.Key, tc.key) | ||
assert.ElementsMatch(t, expectedErrors, valErr.Errs) | ||
}) | ||
} | ||
} |