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

ssa: object normalization improvements #658

Merged
merged 4 commits into from
Oct 7, 2023
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
76 changes: 76 additions & 0 deletions ssa/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2023 The Flux 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 ssa

import (
"fmt"

apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
)

// DryRunErr is an error that occurs during a server-side dry-run apply.
type DryRunErr struct {
underlyingErr error
involvedObject *unstructured.Unstructured
}

// NewDryRunErr returns a new DryRunErr.
func NewDryRunErr(err error, involvedObject *unstructured.Unstructured) *DryRunErr {
return &DryRunErr{
underlyingErr: err,
involvedObject: involvedObject,
}
}

// InvolvedObject returns the involved object.
func (e *DryRunErr) InvolvedObject() *unstructured.Unstructured {
return e.involvedObject
}

// Error returns the error message.
func (e *DryRunErr) Error() string {
if e.involvedObject == nil {
return e.underlyingErr.Error()
}

if apierrors.IsNotFound(e.Unwrap()) {
if e.involvedObject.GetNamespace() != "" {
return fmt.Sprintf("%s namespace not specified: %s", FmtUnstructured(e.involvedObject), e.Unwrap().Error())
}
return fmt.Sprintf("%s not found: %s", FmtUnstructured(e.involvedObject), e.Unwrap().Error())
}

reason := fmt.Sprintf("%s", apierrors.ReasonForError(e.Unwrap()))

// Detect managed field conflict.
if status, ok := apierrors.StatusCause(e.Unwrap(), metav1.CauseTypeFieldManagerConflict); ok {
reason = fmt.Sprintf("%s", status.Type)
}

if reason != "" {
reason = fmt.Sprintf(" (%s)", reason)
}

return fmt.Sprintf("%s dry-run failed%s: %s", FmtUnstructured(e.involvedObject), reason, e.underlyingErr.Error())
}

// Unwrap returns the underlying error.
func (e *DryRunErr) Unwrap() error {
return e.underlyingErr
}
2 changes: 1 addition & 1 deletion ssa/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ require (
k8s.io/api v0.27.4
k8s.io/apimachinery v0.27.4
k8s.io/client-go v0.27.4
k8s.io/utils v0.0.0-20230209194617-a36077c30491
sigs.k8s.io/cli-utils v0.35.0
sigs.k8s.io/controller-runtime v0.15.1
sigs.k8s.io/structured-merge-diff/v4 v4.3.0
Expand Down Expand Up @@ -78,7 +79,6 @@ require (
k8s.io/klog/v2 v2.90.1 // indirect
k8s.io/kube-openapi v0.0.0-20230501164219-8b0f38b5fd1f // indirect
k8s.io/kubectl v0.26.0 // indirect
k8s.io/utils v0.0.0-20230209194617-a36077c30491 // indirect
sigs.k8s.io/json v0.0.0-20221116044647-bc3834ca7abd // indirect
sigs.k8s.io/kustomize/api v0.12.1 // indirect
sigs.k8s.io/kustomize/kyaml v0.13.9 // indirect
Expand Down
4 changes: 2 additions & 2 deletions ssa/manager_apply.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func (m *ResourceManager) Apply(ctx context.Context, object *unstructured.Unstru
return m.Apply(ctx, object, opts)
}

return nil, m.validationError(dryRunObject, err)
return nil, NewDryRunErr(err, dryRunObject)
}

patched, err := m.cleanupMetadata(ctx, object, existingObject, opts.Cleanup)
Expand Down Expand Up @@ -191,7 +191,7 @@ func (m *ResourceManager) ApplyAll(ctx context.Context, objects []*unstructured.
}

if err != nil {
return m.validationError(dryRunObject, err)
return NewDryRunErr(err, dryRunObject)
}
}

Expand Down
10 changes: 5 additions & 5 deletions ssa/manager_apply_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ func TestApply_Force(t *testing.T) {

// verify that the error message does not contain sensitive information
expectedErr := fmt.Sprintf(
"%s dry-run failed, reason: Invalid: Secret \"%s\" is invalid: data: Forbidden: field is immutable when `immutable` is set",
"%s dry-run failed (Invalid): Secret \"%s\" is invalid: data: Forbidden: field is immutable when `immutable` is set",
FmtUnstructured(secret), secret.GetName())
if diff := cmp.Diff(expectedErr, err.Error()); diff != "" {
t.Errorf("Mismatch from expected value (-want +got):\n%s", diff)
Expand Down Expand Up @@ -377,7 +377,7 @@ func TestApply_SetNativeKindsDefaults(t *testing.T) {

manager.SetOwnerLabels(objects, "app1", "default")

if err := SetNativeKindsDefaults(objects); err != nil {
if err := NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -417,7 +417,7 @@ func TestApply_NoOp(t *testing.T) {

manager.SetOwnerLabels(objects, "app1", "default")

if err := SetNativeKindsDefaults(objects); err != nil {
if err := NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -639,7 +639,7 @@ func TestApply_Cleanup(t *testing.T) {

_, deployObject := getFirstObject(objects, "Deployment", id)

if err := SetNativeKindsDefaults(objects); err != nil {
if err = NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -897,7 +897,7 @@ func TestApply_Cleanup_Exclusions(t *testing.T) {

_, deployObject := getFirstObject(objects, "Deployment", id)

if err := SetNativeKindsDefaults(objects); err != nil {
if err = NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

Expand Down
31 changes: 2 additions & 29 deletions ssa/manager_diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,7 @@ package ssa

import (
"context"
"fmt"

apiequality "k8s.io/apimachinery/pkg/api/equality"
apierrors "k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"sigs.k8s.io/controller-runtime/pkg/client"
)
Expand Down Expand Up @@ -61,7 +57,7 @@ func (m *ResourceManager) Diff(ctx context.Context, object *unstructured.Unstruc

dryRunObject := object.DeepCopy()
if err := m.dryRunApply(ctx, dryRunObject); err != nil {
return nil, nil, nil, m.validationError(dryRunObject, err)
return nil, nil, nil, NewDryRunErr(err, dryRunObject)
}

if dryRunObject.GetResourceVersion() == "" {
Expand Down Expand Up @@ -116,31 +112,8 @@ func prepareObjectForDiff(object *unstructured.Unstructured) *unstructured.Unstr
deepCopy := object.DeepCopy()
unstructured.RemoveNestedField(deepCopy.Object, "metadata")
unstructured.RemoveNestedField(deepCopy.Object, "status")
if err := fixHorizontalPodAutoscaler(deepCopy); err != nil {
if err := NormalizeDryRunUnstructured(deepCopy); err != nil {
return object
}
return deepCopy
}

// validationError formats the given error and hides sensitive data
// if the error was caused by an invalid Kubernetes secrets.
func (m *ResourceManager) validationError(object *unstructured.Unstructured, err error) error {
if apierrors.IsNotFound(err) {
return fmt.Errorf("%s namespace not specified: %w", FmtUnstructured(object), err)
}

reason := fmt.Sprintf("%v", apierrors.ReasonForError(err))

// detect managed field conflict
if status, ok := apierrors.StatusCause(err, metav1.CauseTypeFieldManagerConflict); ok {
reason = fmt.Sprintf("%v", status.Type)
}

if reason != "" {
reason = fmt.Sprintf(", reason: %s", reason)
}

return fmt.Errorf("%s dry-run failed%s: %w",
FmtUnstructured(object), reason, err)

}
18 changes: 12 additions & 6 deletions ssa/manager_diff_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,15 +90,17 @@ func TestDiff(t *testing.T) {
t.Run("generates diff for replaced key in stringData secret", func(t *testing.T) {
// create a new stringData secret
sec := secret.DeepCopy()
if err := unstructured.SetNestedField(sec.Object, generateName("diff"), "metadata", "name"); err != nil {
if err = unstructured.SetNestedField(sec.Object, generateName("diff"), "metadata", "name"); err != nil {
t.Fatal(err)
}

// copy the secret to simulate a replace of key
diffSecret := sec.DeepCopy()

// apply stringData conversion
SetNativeKindsDefaults([]*unstructured.Unstructured{sec})
if err = NormalizeUnstructured(sec); err != nil {
t.Fatal(err)
}

if _, err = manager.Apply(ctx, sec, DefaultApplyOptions()); err != nil {
t.Fatal(err)
Expand All @@ -108,13 +110,14 @@ func TestDiff(t *testing.T) {
unstructured.RemoveNestedField(diffSecret.Object, "stringData", "key")

newKey := "key.new"
err = unstructured.SetNestedField(diffSecret.Object, newVal, "stringData", newKey)
if err != nil {
if err = unstructured.SetNestedField(diffSecret.Object, newVal, "stringData", newKey); err != nil {
t.Fatal(err)
}

// apply stringData conversion
SetNativeKindsDefaults([]*unstructured.Unstructured{diffSecret})
if err = NormalizeUnstructured(diffSecret); err != nil {
t.Fatal(err)
}

_, liveObj, mergedObj, err := manager.Diff(ctx, diffSecret, DefaultDiffOptions())
if err != nil {
Expand Down Expand Up @@ -234,7 +237,10 @@ func TestDiff_Removals(t *testing.T) {
if err != nil {
t.Fatal(err)
}
SetNativeKindsDefaults(objects)

if err = NormalizeUnstructuredList(objects); err != nil {
t.Fatal(err)
}

configMapName, configMap := getFirstObject(objects, "ConfigMap", id)

Expand Down
Loading
Loading