@@ -17,6 +17,7 @@ limitations under the License.
1717package v4
1818
1919import (
20+ "errors"
2021 "fmt"
2122
2223 "github.com/spf13/pflag"
@@ -82,6 +83,14 @@ func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {
8283 "[DEPRECATED] Attempts to create resource under the API directory (legacy path). " +
8384 "This option will be removed in future versions." )
8485
86+ fs .StringVar (& p .options .ExternalAPIPath , "external-api-path" , "" ,
87+ "Specify the Go package import path for the external API. This is used to scaffold controllers for resources " +
88+ "defined outside this project (e.g., github.com/cert-manager/cert-manager/pkg/apis/certmanager/v1)." )
89+
90+ fs .StringVar (& p .options .ExternalAPIDomain , "external-api-domain" , "" ,
91+ "Specify the domain name for the external API. This domain is used to generate accurate RBAC " +
92+ "markers and permissions for the external resources (e.g., cert-manager.io)." )
93+
8594 fs .BoolVar (& p .force , "force" , false ,
8695 "attempt to create resource even if it already exists" )
8796}
@@ -94,6 +103,19 @@ func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
94103func (p * createWebhookSubcommand ) InjectResource (res * resource.Resource ) error {
95104 p .resource = res
96105
106+ // Ensure that if any external API flag is set, both must be provided.
107+ if len (p .options .ExternalAPIPath ) != 0 || len (p .options .ExternalAPIDomain ) != 0 {
108+ if len (p .options .ExternalAPIPath ) == 0 || len (p .options .ExternalAPIDomain ) == 0 {
109+ return errors .New ("Both '--external-api-path' and '--external-api-domain' must be " +
110+ "specified together when referencing an external API." )
111+ }
112+ }
113+
114+ if len (p .options .ExternalAPIPath ) != 0 && len (p .options .ExternalAPIDomain ) != 0 && p .isLegacyPath {
115+ return errors .New ("You cannot scaffold webhooks for external types " +
116+ "using the legacy path" )
117+ }
118+
97119 p .options .UpdateResource (p .resource , p .config )
98120
99121 if err := p .resource .Validate (); err != nil {
@@ -106,9 +128,13 @@ func (p *createWebhookSubcommand) InjectResource(res *resource.Resource) error {
106128 }
107129
108130 // check if resource exist to create webhook
109- if r , err := p .config .GetResource (p .resource .GVK ); err != nil {
110- return fmt .Errorf ("%s create webhook requires a previously created API " , p .commandName )
111- } else if r .Webhooks != nil && ! r .Webhooks .IsEmpty () && ! p .force {
131+ resValue , err := p .config .GetResource (p .resource .GVK )
132+ res = & resValue
133+ if err != nil {
134+ if ! p .resource .External {
135+ return fmt .Errorf ("%s create webhook requires a previously created API " , p .commandName )
136+ }
137+ } else if res .Webhooks != nil && ! res .Webhooks .IsEmpty () && ! p .force {
112138 return fmt .Errorf ("webhook resource already exists" )
113139 }
114140
0 commit comments