Skip to content

Commit

Permalink
add secret mutator (#482)
Browse files Browse the repository at this point in the history
  • Loading branch information
airycanon authored and l-qing committed Sep 9, 2023
1 parent 79ecb3e commit c45b618
Show file tree
Hide file tree
Showing 10 changed files with 314 additions and 17 deletions.
2 changes: 1 addition & 1 deletion apis/applications/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

32 changes: 31 additions & 1 deletion apis/artifacts/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions apis/codequality/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/data/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 73 additions & 7 deletions apis/meta/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions apis/security/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion apis/selection/v1alpha1/zz_generated.deepcopy.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions registry/detect_scheme.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,8 @@ func (d *DefaultRegistrySchemeDetection) DetectScheme(ctx context.Context, regis

scheme, err := DefaultDetectImageRegistryScheme(ctx, registry, httpClient, d.Insecure)
if err != nil {
log.Errorw("failed to detect registry scheme", "error", err)
return "", err
log.Errorw("failed to detect registry scheme returning default HTTPS", "error", err)
return HTTPS, err
}
log.Debugw("detect registry scheme", "scheme", scheme)

Expand Down
78 changes: 78 additions & 0 deletions secret/mutate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
/*
Copyright 2023 The Katanomi 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 secret

import (
"context"

corev1 "k8s.io/api/core/v1"
)

// Mutator interface defines a method for modifying Secret objects.
type Mutator interface {
Mutate(context.Context, *corev1.Secret) error
}

// MutateFunc is a function type that implements the Mutator interface.
type MutateFunc func(context.Context, *corev1.Secret) error

func (m MutateFunc) Mutate(ctx context.Context, secret *corev1.Secret) error {
return m(ctx, secret)
}

// MutatorList is a slice of Mutators.
type MutatorList []Mutator

// Mutate iterates over the list of mutators and applies them to the provided Secret object.
func (m MutatorList) Mutate(ctx context.Context, secret *corev1.Secret) error {
for _, mutator := range m {
if err := mutator.Mutate(ctx, secret); err != nil {
return err
}
}
return nil
}

// DefaultMutator is a no-operation implementation of the Mutator interface.
// It is used as a default option when there is no Mutator associated with the context.
var DefaultMutator = &NoOpMutator{}

// NoOpMutator struct implements the Mutator interface but performs no actions.
type NoOpMutator struct {
}

// Mutate is the NoOpMutator's implementation that simply returns the passed-in Secret object without any modifications.
func (a *NoOpMutator) Mutate(_ context.Context, secret *corev1.Secret) error {
return nil
}

// mutatorKey is a key used for storing a Mutator in a context.
var mutatorKey = struct{}{}

// MutatorFromCtx retrieves a Mutator from the given context.
// If no Mutator is found, it returns the noOpMutator.
func MutatorFromCtx(ctx context.Context) Mutator {
if mutation, ok := ctx.Value(mutatorKey).(Mutator); ok {
return mutation
}
return DefaultMutator
}

// WithMutator attaches a Mutator to a context and returns the new context.
func WithMutator(ctx context.Context, mutator Mutator) context.Context {
return context.WithValue(ctx, mutatorKey, mutator)
}
Loading

0 comments on commit c45b618

Please sign in to comment.