Skip to content

Commit e407784

Browse files
authored
fix: address a misconfigured default value in hub agent flag setup (#523)
1 parent 2bee163 commit e407784

2 files changed

Lines changed: 144 additions & 14 deletions

File tree

cmd/hubagent/options/options_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -706,6 +706,123 @@ func TestPlacementManagementOptions(t *testing.T) {
706706
}
707707
}
708708

709+
// TestWebhookOptions tests the parsing and validation logic of the webhook options defined in WebhookOptions.
710+
func TestWebhookOptions(t *testing.T) {
711+
testCases := []struct {
712+
name string
713+
flagSetName string
714+
args []string
715+
wantWebhookOpts WebhookOptions
716+
wantErred bool
717+
wantErrMsgSubStr string
718+
}{
719+
{
720+
name: "all default",
721+
flagSetName: "allDefault",
722+
args: []string{},
723+
wantWebhookOpts: WebhookOptions{
724+
EnableWebhooks: true,
725+
ClientConnectionType: "url",
726+
ServiceName: "fleetwebhook",
727+
EnableGuardRail: false,
728+
GuardRailWhitelistedUsers: "",
729+
GuardRailDenyModifyMemberClusterLabels: false,
730+
EnableWorkload: false,
731+
UseCertManager: false,
732+
},
733+
},
734+
{
735+
name: "all specified",
736+
flagSetName: "allSpecified",
737+
args: []string{
738+
"--enable-webhook=false",
739+
"--webhook-client-connection-type=service",
740+
"--webhook-service-name=customwebhook",
741+
"--enable-guard-rail=true",
742+
"--whitelisted-users=user1,user2",
743+
"--deny-modify-member-cluster-labels=true",
744+
"--enable-workload=true",
745+
"--use-cert-manager=true",
746+
},
747+
wantWebhookOpts: WebhookOptions{
748+
EnableWebhooks: false,
749+
ClientConnectionType: "service",
750+
ServiceName: "customwebhook",
751+
EnableGuardRail: true,
752+
GuardRailWhitelistedUsers: "user1,user2",
753+
GuardRailDenyModifyMemberClusterLabels: true,
754+
EnableWorkload: true,
755+
UseCertManager: true,
756+
},
757+
},
758+
{
759+
name: "webhook client connection type URL (case-insensitive)",
760+
flagSetName: "webhookClientConnTypeURL",
761+
args: []string{"--webhook-client-connection-type=URL"},
762+
wantWebhookOpts: WebhookOptions{
763+
EnableWebhooks: true,
764+
ClientConnectionType: "url",
765+
ServiceName: "fleetwebhook",
766+
EnableGuardRail: false,
767+
GuardRailWhitelistedUsers: "",
768+
GuardRailDenyModifyMemberClusterLabels: false,
769+
EnableWorkload: false,
770+
UseCertManager: false,
771+
},
772+
},
773+
{
774+
name: "webhook client connection type service (case-insensitive)",
775+
flagSetName: "webhookClientConnTypeService",
776+
args: []string{"--webhook-client-connection-type=Service"},
777+
wantWebhookOpts: WebhookOptions{
778+
EnableWebhooks: true,
779+
ClientConnectionType: "service",
780+
ServiceName: "fleetwebhook",
781+
EnableGuardRail: false,
782+
GuardRailWhitelistedUsers: "",
783+
GuardRailDenyModifyMemberClusterLabels: false,
784+
EnableWorkload: false,
785+
UseCertManager: false,
786+
},
787+
},
788+
{
789+
name: "invalid webhook client connection type",
790+
flagSetName: "webhookClientConnTypeInvalid",
791+
args: []string{"--webhook-client-connection-type=ftp"},
792+
wantErred: true,
793+
wantErrMsgSubStr: "invalid webhook client connection type",
794+
},
795+
}
796+
797+
for _, tc := range testCases {
798+
t.Run(tc.name, func(t *testing.T) {
799+
flags := flag.NewFlagSet(tc.flagSetName, flag.ContinueOnError)
800+
webhookOpts := WebhookOptions{}
801+
webhookOpts.AddFlags(flags)
802+
803+
err := flags.Parse(tc.args)
804+
if tc.wantErred {
805+
if err == nil {
806+
t.Fatalf("flag Parse() = nil, want erred")
807+
}
808+
809+
if !strings.Contains(err.Error(), tc.wantErrMsgSubStr) {
810+
t.Fatalf("flag Parse() error = %v, want error msg with sub-string %s", err, tc.wantErrMsgSubStr)
811+
}
812+
return
813+
}
814+
815+
if err != nil {
816+
t.Fatalf("flag Parse() = %v, want nil", err)
817+
}
818+
819+
if diff := cmp.Diff(webhookOpts, tc.wantWebhookOpts); diff != "" {
820+
t.Errorf("webhook options diff (-got, +want):\n%s", diff)
821+
}
822+
})
823+
}
824+
}
825+
709826
// TestRateLimitOptions tests the parsing and validation logic of the rate limit options defined in RateLimitOptions.
710827
func TestRateLimitOptions(t *testing.T) {
711828
testCases := []struct {

cmd/hubagent/options/webhooks.go

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -71,22 +71,10 @@ func (o *WebhookOptions) AddFlags(flags *flag.FlagSet) {
7171
"Enable the KubeFleet webhooks or not.",
7272
)
7373

74-
flags.Func(
74+
flags.Var(
75+
newWebhookClientConnTypeValueWithValidation(string(URL), &o.ClientConnectionType),
7576
"webhook-client-connection-type",
7677
"The connection type used by the webhook client. Valid values are `url` and `service`. Defaults to `url`. This option only applies if webhooks are enabled.",
77-
func(s string) error {
78-
if len(s) == 0 {
79-
o.ClientConnectionType = "url"
80-
return nil
81-
}
82-
83-
parsedStr, err := parseWebhookClientConnectionString(s)
84-
if err != nil {
85-
return fmt.Errorf("invalid webhook client connection type: %w", err)
86-
}
87-
o.ClientConnectionType = string(parsedStr)
88-
return nil
89-
},
9078
)
9179

9280
flags.StringVar(
@@ -131,3 +119,28 @@ func (o *WebhookOptions) AddFlags(flags *flag.FlagSet) {
131119
"Use the cert-manager project for managing KubeFleet webhook server certificates or not. If set to false, the system will use self-signed certificates. If set to true, the EnableWorkload option must be set to true as well. This option only applies if webhooks are enabled.",
132120
)
133121
}
122+
123+
type WebhookClientConnTypeValueWithValidation string
124+
125+
func (v *WebhookClientConnTypeValueWithValidation) String() string {
126+
return string(*v)
127+
}
128+
129+
func (v *WebhookClientConnTypeValueWithValidation) Set(s string) error {
130+
if len(s) == 0 {
131+
*v = "url"
132+
return nil
133+
}
134+
135+
parsedStr, err := parseWebhookClientConnectionString(s)
136+
if err != nil {
137+
return fmt.Errorf("invalid webhook client connection type: %w", err)
138+
}
139+
*v = WebhookClientConnTypeValueWithValidation(parsedStr)
140+
return nil
141+
}
142+
143+
func newWebhookClientConnTypeValueWithValidation(defaultVal string, p *string) *WebhookClientConnTypeValueWithValidation {
144+
*p = defaultVal
145+
return (*WebhookClientConnTypeValueWithValidation)(p)
146+
}

0 commit comments

Comments
 (0)