@@ -17,6 +17,7 @@ limitations under the License.
17
17
package v4
18
18
19
19
import (
20
+ "errors"
20
21
"fmt"
21
22
22
23
"github.com/spf13/pflag"
@@ -82,6 +83,14 @@ func (p *createWebhookSubcommand) BindFlags(fs *pflag.FlagSet) {
82
83
"[DEPRECATED] Attempts to create resource under the API directory (legacy path). " +
83
84
"This option will be removed in future versions." )
84
85
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
+
85
94
fs .BoolVar (& p .force , "force" , false ,
86
95
"attempt to create resource even if it already exists" )
87
96
}
@@ -94,6 +103,19 @@ func (p *createWebhookSubcommand) InjectConfig(c config.Config) error {
94
103
func (p * createWebhookSubcommand ) InjectResource (res * resource.Resource ) error {
95
104
p .resource = res
96
105
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
+
97
119
p .options .UpdateResource (p .resource , p .config )
98
120
99
121
if err := p .resource .Validate (); err != nil {
@@ -106,9 +128,13 @@ func (p *createWebhookSubcommand) InjectResource(res *resource.Resource) error {
106
128
}
107
129
108
130
// 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 {
112
138
return fmt .Errorf ("webhook resource already exists" )
113
139
}
114
140
0 commit comments