-
Notifications
You must be signed in to change notification settings - Fork 592
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'origin/main' into feat/udp-publish
- Loading branch information
Showing
12 changed files
with
273 additions
and
112 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
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 was deleted.
Oops, something went wrong.
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,29 @@ | ||
package manager | ||
|
||
import ( | ||
"errors" | ||
"strings" | ||
|
||
"k8s.io/apimachinery/pkg/types" | ||
) | ||
|
||
// This file contains a set of constructors that are used to validate and set validated values in Config. | ||
// They're meant to be used together with ValidatedValue[T] type. | ||
|
||
func namespacedNameFromFlagValue(flagValue string) (types.NamespacedName, error) { | ||
parts := strings.SplitN(flagValue, "/", 3) | ||
if len(parts) != 2 { | ||
return types.NamespacedName{}, errors.New("the expected format is namespace/name") | ||
} | ||
return types.NamespacedName{ | ||
Namespace: parts[0], | ||
Name: parts[1], | ||
}, nil | ||
} | ||
|
||
func gatewayAPIControllerNameFromFlagValue(flagValue string) (string, error) { | ||
if !isControllerNameValid(flagValue) { | ||
return "", errors.New("the expected format is example.com/controller-name") | ||
} | ||
return flagValue, 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 @@ | ||
package manager | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"k8s.io/apimachinery/pkg/types" | ||
|
||
"github.com/kong/kubernetes-ingress-controller/v2/internal/controllers/gateway" | ||
) | ||
|
||
func TestConfigValidatedVars(t *testing.T) { | ||
type testCase struct { | ||
Input string | ||
ExpectedValue any | ||
ExtractValueFn func(c Config) any | ||
ExpectedErrorContains string | ||
} | ||
|
||
testCasesGroupedByFlag := map[string][]testCase{ | ||
"--gateway-api-controller-name": { | ||
{ | ||
Input: "example.com/controller-name", | ||
ExtractValueFn: func(c Config) any { | ||
return c.GatewayAPIControllerName | ||
}, | ||
ExpectedValue: "example.com/controller-name", | ||
}, | ||
{ | ||
Input: "", | ||
ExtractValueFn: func(c Config) any { | ||
return c.GatewayAPIControllerName | ||
}, | ||
ExpectedValue: string(gateway.ControllerName), | ||
}, | ||
{ | ||
Input: "%invalid_controller_name$", | ||
ExpectedErrorContains: "the expected format is example.com/controller-name", | ||
}, | ||
}, | ||
"--publish-service": { | ||
{ | ||
Input: "namespace/servicename", | ||
ExtractValueFn: func(c Config) any { | ||
return c.PublishService | ||
}, | ||
ExpectedValue: types.NamespacedName{Namespace: "namespace", Name: "servicename"}, | ||
}, | ||
{ | ||
Input: "servicename", | ||
ExpectedErrorContains: "the expected format is namespace/name", | ||
}, | ||
}, | ||
} | ||
|
||
for flag, flagTestCases := range testCasesGroupedByFlag { | ||
for _, tc := range flagTestCases { | ||
t.Run(fmt.Sprintf("%s=%s", flag, tc.Input), func(t *testing.T) { | ||
var c Config | ||
var input []string | ||
if tc.Input != "" { | ||
input = []string{flag, tc.Input} | ||
} | ||
|
||
err := c.FlagSet().Parse(input) | ||
if tc.ExpectedErrorContains != "" { | ||
require.ErrorContains(t, err, tc.ExpectedErrorContains) | ||
return | ||
} | ||
|
||
require.NoError(t, err) | ||
require.Equal(t, tc.ExpectedValue, tc.ExtractValueFn(c)) | ||
}) | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,50 @@ | ||
package manager | ||
|
||
import "fmt" | ||
|
||
// ValidatedValue implements `pflag.Value` interface. It can be used for hooking up arbitrary validation logic to any type. | ||
// It should be passed to `pflag.FlagSet.Var()`. | ||
type ValidatedValue[T any] struct { | ||
origin string | ||
variable *T | ||
constructor func(string) (T, error) | ||
} | ||
|
||
// NewValidatedValue creates a validated variable of type T. Constructor should validate the input and return an error | ||
// in case of any failures. If validation passes, constructor should return a value that's to be set in the variable. | ||
// The constructor accepts a flagValue that is raw input from user's command line (or an env variable that was bind to | ||
// the flag, see: bindEnvVars). | ||
func NewValidatedValue[T any](variable *T, constructor func(flagValue string) (T, error)) ValidatedValue[T] { | ||
return ValidatedValue[T]{ | ||
constructor: constructor, | ||
variable: variable, | ||
} | ||
} | ||
|
||
// NewValidatedValueWithDefault creates a validated variable of type T with a default value. | ||
func NewValidatedValueWithDefault[T any](variable *T, constructor func(flagValue string) (T, error), value T) ValidatedValue[T] { | ||
*variable = value | ||
return ValidatedValue[T]{ | ||
constructor: constructor, | ||
variable: variable, | ||
} | ||
} | ||
|
||
func (v ValidatedValue[T]) String() string { | ||
return v.origin | ||
} | ||
|
||
func (v ValidatedValue[T]) Set(s string) error { | ||
value, err := v.constructor(s) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
*v.variable = value | ||
return nil | ||
} | ||
|
||
func (v ValidatedValue[T]) Type() string { | ||
var t T | ||
return fmt.Sprintf("Validated%T", t) | ||
} |
Oops, something went wrong.