Skip to content

Commit

Permalink
cleanup: refactor to disable metrics
Browse files Browse the repository at this point in the history
Signed-off-by: PuneetPunamiya <[email protected]>
  • Loading branch information
PuneetPunamiya committed Mar 3, 2025
1 parent 2277f66 commit 54b6e02
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 64 deletions.
18 changes: 0 additions & 18 deletions pkg/apis/config/feature_flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,10 @@ limitations under the License.
package config

import (
"context"
"fmt"
"os"
"strconv"
"strings"

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

const (
Expand Down Expand Up @@ -448,21 +445,6 @@ func setVerificationNoMatchPolicy(cfgMap map[string]string, defaultValue string,
return nil
}

// NewFeatureFlagsFromConfigMap returns a Config for the given configmap
func NewFeatureFlagsFromConfigMap(config *corev1.ConfigMap) (*FeatureFlags, error) {
return NewFeatureFlagsFromMap(config.Data)
}

// GetVerificationNoMatchPolicy returns the "trusted-resources-verification-no-match-policy" value
func GetVerificationNoMatchPolicy(ctx context.Context) string {
return FromContextOrDefaults(ctx).FeatureFlags.VerificationNoMatchPolicy
}

// IsSpireEnabled checks if non-falsifiable provenance is enforced through SPIRE
func IsSpireEnabled(ctx context.Context) bool {
return FromContextOrDefaults(ctx).FeatureFlags.EnforceNonfalsifiability == EnforceNonfalsifiabilityWithSpire
}

type PerFeatureFlag struct {
// Name of the feature flag
Name string
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/config/featureflags_validation.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !disable_tls

/*
Copyright 2021 The Tekton Authors
Expand Down
7 changes: 0 additions & 7 deletions pkg/apis/config/metrics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package config

import (
corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/metrics"
)

const (
Expand Down Expand Up @@ -109,12 +108,6 @@ type Metrics struct {
ThrottleWithNamespace bool
}

// GetMetricsConfigName returns the name of the configmap containing all
// customizations for the storage bucket.
func GetMetricsConfigName() string {
return metrics.ConfigMapName()
}

// Equals returns true if two Configs are identical
func (cfg *Metrics) Equals(other *Metrics) bool {
if cfg == nil && other == nil {
Expand Down
7 changes: 7 additions & 0 deletions pkg/apis/config/metrics_notls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//go:build disable_tls

package config

// GetMetricsConfigName returns the name of the configmap containing all
// customizations for the storage bucket.
func GetMetricsConfigName() string { panic("not supported when tls is disabled") }
31 changes: 31 additions & 0 deletions pkg/apis/config/metrics_tls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
//go:build !disable_tls

package config

import (
"context"

corev1 "k8s.io/api/core/v1"
"knative.dev/pkg/metrics"
)

// GetMetricsConfigName returns the name of the configmap containing all
// customizations for the storage bucket.
func GetMetricsConfigName() string {
return metrics.ConfigMapName()
}

// NewFeatureFlagsFromConfigMap returns a Config for the given configmap
func NewFeatureFlagsFromConfigMap(config *corev1.ConfigMap) (*FeatureFlags, error) {
return NewFeatureFlagsFromMap(config.Data)
}

// GetVerificationNoMatchPolicy returns the "trusted-resources-verification-no-match-policy" value
func GetVerificationNoMatchPolicy(ctx context.Context) string {
return FromContextOrDefaults(ctx).FeatureFlags.VerificationNoMatchPolicy
}

// IsSpireEnabled checks if non-falsifiable provenance is enforced through SPIRE
func IsSpireEnabled(ctx context.Context) bool {
return FromContextOrDefaults(ctx).FeatureFlags.EnforceNonfalsifiability == EnforceNonfalsifiabilityWithSpire
}
2 changes: 2 additions & 0 deletions pkg/apis/config/resolver/store.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !disable_tls

/*
Copyright 2022 The Tekton Authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/config/spire_config.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !disable_tls

/*
Copyright 2022 The Tekton Authors
Expand Down
2 changes: 2 additions & 0 deletions pkg/apis/config/store.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !disable_tls

/*
Copyright 2019 The Tekton Authors
Expand Down
61 changes: 61 additions & 0 deletions pkg/substitution/replacements.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
Copyright 2025 The Tekton 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 substitution

import (
"fmt"
"strings"
)

// ApplyReplacements returns a string with references to parameters replaced,
// based on the mapping provided in replacements.
// For example, if the input string is "foo: $(params.foo)", and replacements maps "params.foo" to "bar",
// the output would be "foo: bar".
func ApplyReplacements(in string, replacements map[string]string) string {
replacementsList := []string{}
for k, v := range replacements {
replacementsList = append(replacementsList, fmt.Sprintf("$(%s)", k), v)
}
// strings.Replacer does all replacements in one pass, preventing multiple replacements
// See #2093 for an explanation on why we need to do this.
replacer := strings.NewReplacer(replacementsList...)
return replacer.Replace(in)
}

// ApplyArrayReplacements takes an input string, and output an array of strings related to possible arrayReplacements. If there aren't any
// areas where the input can be split up via arrayReplacements, then just return an array with a single element,
// which is ApplyReplacements(in, replacements).
func ApplyArrayReplacements(in string, stringReplacements map[string]string, arrayReplacements map[string][]string) []string {
for k, v := range arrayReplacements {
stringToReplace := fmt.Sprintf("$(%s)", k)

// If the input string matches a replacement's key (without padding characters), return the corresponding array.
// Note that the webhook should prevent all instances where this could evaluate to false.
if (strings.Count(in, stringToReplace) == 1) && len(in) == len(stringToReplace) {
return v
}

// same replace logic for star array expressions
starStringtoReplace := fmt.Sprintf("$(%s[*])", k)
if (strings.Count(in, starStringtoReplace) == 1) && len(in) == len(starStringtoReplace) {
return v
}
}

// Otherwise return a size-1 array containing the input string with standard stringReplacements applied.
return []string{ApplyReplacements(in, stringReplacements)}
}
41 changes: 2 additions & 39 deletions pkg/substitution/substitution.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !disable_tls

/*
Copyright 2019 The Tekton Authors
Expand Down Expand Up @@ -306,45 +308,6 @@ func matchGroups(matches []string, pattern *regexp.Regexp) map[string]string {
return groups
}

// ApplyReplacements returns a string with references to parameters replaced,
// based on the mapping provided in replacements.
// For example, if the input string is "foo: $(params.foo)", and replacements maps "params.foo" to "bar",
// the output would be "foo: bar".
func ApplyReplacements(in string, replacements map[string]string) string {
replacementsList := []string{}
for k, v := range replacements {
replacementsList = append(replacementsList, fmt.Sprintf("$(%s)", k), v)
}
// strings.Replacer does all replacements in one pass, preventing multiple replacements
// See #2093 for an explanation on why we need to do this.
replacer := strings.NewReplacer(replacementsList...)
return replacer.Replace(in)
}

// ApplyArrayReplacements takes an input string, and output an array of strings related to possible arrayReplacements. If there aren't any
// areas where the input can be split up via arrayReplacements, then just return an array with a single element,
// which is ApplyReplacements(in, replacements).
func ApplyArrayReplacements(in string, stringReplacements map[string]string, arrayReplacements map[string][]string) []string {
for k, v := range arrayReplacements {
stringToReplace := fmt.Sprintf("$(%s)", k)

// If the input string matches a replacement's key (without padding characters), return the corresponding array.
// Note that the webhook should prevent all instances where this could evaluate to false.
if (strings.Count(in, stringToReplace) == 1) && len(in) == len(stringToReplace) {
return v
}

// same replace logic for star array expressions
starStringtoReplace := fmt.Sprintf("$(%s[*])", k)
if (strings.Count(in, starStringtoReplace) == 1) && len(in) == len(starStringtoReplace) {
return v
}
}

// Otherwise return a size-1 array containing the input string with standard stringReplacements applied.
return []string{ApplyReplacements(in, stringReplacements)}
}

// TrimArrayIndex replaces all `[i]` and `[*]` to "".
func TrimArrayIndex(s string) string {
return arrayIndexingRegex.ReplaceAllString(s, "")
Expand Down
2 changes: 2 additions & 0 deletions pkg/termination/parse.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build !disable_tls

/*
Copyright 2019 The Tekton Authors
Expand Down

0 comments on commit 54b6e02

Please sign in to comment.