Skip to content

Commit 56dab7c

Browse files
author
Per Goncalves da Silva
committed
Update webhook configuration naming
Signed-off-by: Per Goncalves da Silva <[email protected]>
1 parent 165086d commit 56dab7c

File tree

4 files changed

+218
-25
lines changed

4 files changed

+218
-25
lines changed

internal/operator-controller/rukpak/render/generators/generators.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,13 @@ func BundleValidatingWebhookResourceGenerator(rv1 *render.RegistryV1, opts rende
304304
continue
305305
}
306306
certProvisioner := render.CertProvisionerFor(wh.DeploymentName, opts)
307+
webhookName := strings.TrimSuffix(wh.GenerateName, "-")
307308
webhookResource := CreateValidatingWebhookConfigurationResource(
308-
wh.GenerateName,
309+
webhookName,
309310
opts.InstallNamespace,
310311
WithValidatingWebhooks(
311312
admissionregistrationv1.ValidatingWebhook{
312-
Name: wh.GenerateName,
313+
Name: webhookName,
313314
Rules: wh.Rules,
314315
FailurePolicy: wh.FailurePolicy,
315316
MatchPolicy: wh.MatchPolicy,
@@ -351,12 +352,13 @@ func BundleMutatingWebhookResourceGenerator(rv1 *render.RegistryV1, opts render.
351352
continue
352353
}
353354
certProvisioner := render.CertProvisionerFor(wh.DeploymentName, opts)
355+
webhookName := strings.TrimSuffix(wh.GenerateName, "-")
354356
webhookResource := CreateMutatingWebhookConfigurationResource(
355-
wh.GenerateName,
357+
webhookName,
356358
opts.InstallNamespace,
357359
WithMutatingWebhooks(
358360
admissionregistrationv1.MutatingWebhook{
359-
Name: wh.GenerateName,
361+
Name: webhookName,
360362
Rules: wh.Rules,
361363
FailurePolicy: wh.FailurePolicy,
362364
MatchPolicy: wh.MatchPolicy,

internal/operator-controller/rukpak/render/generators/generators_test.go

Lines changed: 199 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1547,8 +1547,101 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
15471547
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
15481548
},
15491549
ObjectMeta: metav1.ObjectMeta{
1550-
GenerateName: "my-webhook-",
1551-
Namespace: "install-namespace",
1550+
Name: "my-webhook",
1551+
Namespace: "install-namespace",
1552+
},
1553+
Webhooks: []admissionregistrationv1.ValidatingWebhook{
1554+
{
1555+
Name: "my-webhook",
1556+
Rules: []admissionregistrationv1.RuleWithOperations{
1557+
{
1558+
Operations: []admissionregistrationv1.OperationType{
1559+
admissionregistrationv1.OperationAll,
1560+
},
1561+
Rule: admissionregistrationv1.Rule{
1562+
APIGroups: []string{""},
1563+
APIVersions: []string{""},
1564+
Resources: []string{"namespaces"},
1565+
},
1566+
},
1567+
},
1568+
FailurePolicy: ptr.To(admissionregistrationv1.Fail),
1569+
ObjectSelector: &metav1.LabelSelector{
1570+
MatchLabels: map[string]string{
1571+
"foo": "bar",
1572+
},
1573+
},
1574+
SideEffects: ptr.To(admissionregistrationv1.SideEffectClassNone),
1575+
TimeoutSeconds: ptr.To(int32(1)),
1576+
AdmissionReviewVersions: []string{
1577+
"v1beta1",
1578+
"v1beta2",
1579+
},
1580+
ClientConfig: admissionregistrationv1.WebhookClientConfig{
1581+
Service: &admissionregistrationv1.ServiceReference{
1582+
Namespace: "install-namespace",
1583+
Name: "my-deployment-service",
1584+
Path: ptr.To("/webhook-path"),
1585+
Port: ptr.To(int32(443)),
1586+
},
1587+
},
1588+
},
1589+
},
1590+
},
1591+
},
1592+
},
1593+
{
1594+
name: "removes any - suffixes from the webhook name (v0 used GenerateName to allow multiple operator installations - we don't want that in v1)",
1595+
bundle: &render.RegistryV1{
1596+
CSV: MakeCSV(
1597+
WithWebhookDefinitions(
1598+
v1alpha1.WebhookDescription{
1599+
Type: v1alpha1.ValidatingAdmissionWebhook,
1600+
GenerateName: "my-webhook-",
1601+
DeploymentName: "my-deployment",
1602+
Rules: []admissionregistrationv1.RuleWithOperations{
1603+
{
1604+
Operations: []admissionregistrationv1.OperationType{
1605+
admissionregistrationv1.OperationAll,
1606+
},
1607+
Rule: admissionregistrationv1.Rule{
1608+
APIGroups: []string{""},
1609+
APIVersions: []string{""},
1610+
Resources: []string{"namespaces"},
1611+
},
1612+
},
1613+
},
1614+
FailurePolicy: ptr.To(admissionregistrationv1.Fail),
1615+
ObjectSelector: &metav1.LabelSelector{
1616+
MatchLabels: map[string]string{
1617+
"foo": "bar",
1618+
},
1619+
},
1620+
SideEffects: ptr.To(admissionregistrationv1.SideEffectClassNone),
1621+
TimeoutSeconds: ptr.To(int32(1)),
1622+
AdmissionReviewVersions: []string{
1623+
"v1beta1",
1624+
"v1beta2",
1625+
},
1626+
WebhookPath: ptr.To("/webhook-path"),
1627+
ContainerPort: 443,
1628+
},
1629+
),
1630+
),
1631+
},
1632+
opts: render.Options{
1633+
InstallNamespace: "install-namespace",
1634+
TargetNamespaces: []string{"watch-namespace-one", "watch-namespace-two"},
1635+
},
1636+
expectedResources: []client.Object{
1637+
&admissionregistrationv1.ValidatingWebhookConfiguration{
1638+
TypeMeta: metav1.TypeMeta{
1639+
Kind: "ValidatingWebhookConfiguration",
1640+
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
1641+
},
1642+
ObjectMeta: metav1.ObjectMeta{
1643+
Name: "my-webhook",
1644+
Namespace: "install-namespace",
15521645
},
15531646
Webhooks: []admissionregistrationv1.ValidatingWebhook{
15541647
{
@@ -1616,8 +1709,8 @@ func Test_BundleValidatingWebhookResourceGenerator_Succeeds(t *testing.T) {
16161709
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
16171710
},
16181711
ObjectMeta: metav1.ObjectMeta{
1619-
GenerateName: "my-webhook-",
1620-
Namespace: "install-namespace",
1712+
Name: "my-webhook",
1713+
Namespace: "install-namespace",
16211714
Annotations: map[string]string{
16221715
"cert-provider": "annotation",
16231716
},
@@ -1719,8 +1812,103 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
17191812
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
17201813
},
17211814
ObjectMeta: metav1.ObjectMeta{
1722-
GenerateName: "my-webhook-",
1723-
Namespace: "install-namespace",
1815+
Name: "my-webhook",
1816+
Namespace: "install-namespace",
1817+
},
1818+
Webhooks: []admissionregistrationv1.MutatingWebhook{
1819+
{
1820+
Name: "my-webhook",
1821+
Rules: []admissionregistrationv1.RuleWithOperations{
1822+
{
1823+
Operations: []admissionregistrationv1.OperationType{
1824+
admissionregistrationv1.OperationAll,
1825+
},
1826+
Rule: admissionregistrationv1.Rule{
1827+
APIGroups: []string{""},
1828+
APIVersions: []string{""},
1829+
Resources: []string{"namespaces"},
1830+
},
1831+
},
1832+
},
1833+
FailurePolicy: ptr.To(admissionregistrationv1.Fail),
1834+
ObjectSelector: &metav1.LabelSelector{
1835+
MatchLabels: map[string]string{
1836+
"foo": "bar",
1837+
},
1838+
},
1839+
SideEffects: ptr.To(admissionregistrationv1.SideEffectClassNone),
1840+
TimeoutSeconds: ptr.To(int32(1)),
1841+
AdmissionReviewVersions: []string{
1842+
"v1beta1",
1843+
"v1beta2",
1844+
},
1845+
ReinvocationPolicy: ptr.To(admissionregistrationv1.IfNeededReinvocationPolicy),
1846+
ClientConfig: admissionregistrationv1.WebhookClientConfig{
1847+
Service: &admissionregistrationv1.ServiceReference{
1848+
Namespace: "install-namespace",
1849+
Name: "my-deployment-service",
1850+
Path: ptr.To("/webhook-path"),
1851+
Port: ptr.To(int32(443)),
1852+
},
1853+
},
1854+
},
1855+
},
1856+
},
1857+
},
1858+
},
1859+
{
1860+
name: "removes any - suffixes from the webhook name (v0 used GenerateName to allow multiple operator installations - we don't want that in v1)",
1861+
bundle: &render.RegistryV1{
1862+
CSV: MakeCSV(
1863+
WithWebhookDefinitions(
1864+
v1alpha1.WebhookDescription{
1865+
Type: v1alpha1.MutatingAdmissionWebhook,
1866+
GenerateName: "my-webhook-",
1867+
DeploymentName: "my-deployment",
1868+
Rules: []admissionregistrationv1.RuleWithOperations{
1869+
{
1870+
Operations: []admissionregistrationv1.OperationType{
1871+
admissionregistrationv1.OperationAll,
1872+
},
1873+
Rule: admissionregistrationv1.Rule{
1874+
APIGroups: []string{""},
1875+
APIVersions: []string{""},
1876+
Resources: []string{"namespaces"},
1877+
},
1878+
},
1879+
},
1880+
FailurePolicy: ptr.To(admissionregistrationv1.Fail),
1881+
ObjectSelector: &metav1.LabelSelector{
1882+
MatchLabels: map[string]string{
1883+
"foo": "bar",
1884+
},
1885+
},
1886+
SideEffects: ptr.To(admissionregistrationv1.SideEffectClassNone),
1887+
TimeoutSeconds: ptr.To(int32(1)),
1888+
AdmissionReviewVersions: []string{
1889+
"v1beta1",
1890+
"v1beta2",
1891+
},
1892+
WebhookPath: ptr.To("/webhook-path"),
1893+
ContainerPort: 443,
1894+
ReinvocationPolicy: ptr.To(admissionregistrationv1.IfNeededReinvocationPolicy),
1895+
},
1896+
),
1897+
),
1898+
},
1899+
opts: render.Options{
1900+
InstallNamespace: "install-namespace",
1901+
TargetNamespaces: []string{"watch-namespace-one", "watch-namespace-two"},
1902+
},
1903+
expectedResources: []client.Object{
1904+
&admissionregistrationv1.MutatingWebhookConfiguration{
1905+
TypeMeta: metav1.TypeMeta{
1906+
Kind: "MutatingWebhookConfiguration",
1907+
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
1908+
},
1909+
ObjectMeta: metav1.ObjectMeta{
1910+
Name: "my-webhook",
1911+
Namespace: "install-namespace",
17241912
},
17251913
Webhooks: []admissionregistrationv1.MutatingWebhook{
17261914
{
@@ -1789,8 +1977,8 @@ func Test_BundleMutatingWebhookResourceGenerator_Succeeds(t *testing.T) {
17891977
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
17901978
},
17911979
ObjectMeta: metav1.ObjectMeta{
1792-
GenerateName: "my-webhook-",
1793-
Namespace: "install-namespace",
1980+
Name: "my-webhook",
1981+
Namespace: "install-namespace",
17941982
Annotations: map[string]string{
17951983
"cert-provider": "annotation",
17961984
},
@@ -2258,6 +2446,7 @@ func Test_CertProviderResourceGenerator_Succeeds(t *testing.T) {
22582446
fakeProvider := FakeCertProvider{
22592447
AdditionalObjectsFn: func(cfg render.CertificateProvisionerConfig) ([]unstructured.Unstructured, error) {
22602448
return []unstructured.Unstructured{*ToUnstructuredT(t, &corev1.Secret{
2449+
TypeMeta: metav1.TypeMeta{Kind: "Secret", APIVersion: corev1.SchemeGroupVersion.String()},
22612450
ObjectMeta: metav1.ObjectMeta{
22622451
Name: cfg.CertName,
22632452
},
@@ -2290,9 +2479,8 @@ func Test_CertProviderResourceGenerator_Succeeds(t *testing.T) {
22902479
require.NoError(t, err)
22912480
require.Equal(t, []client.Object{
22922481
ToUnstructuredT(t, &corev1.Secret{
2293-
ObjectMeta: metav1.ObjectMeta{
2294-
Name: "my-deployment-service-cert",
2295-
},
2482+
TypeMeta: metav1.TypeMeta{Kind: "Secret", APIVersion: corev1.SchemeGroupVersion.String()},
2483+
ObjectMeta: metav1.ObjectMeta{Name: "my-deployment-service-cert"},
22962484
}),
22972485
}, objs)
22982486
}

internal/operator-controller/rukpak/render/generators/resources.go

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package generators
22

33
import (
4-
"fmt"
5-
64
admissionregistrationv1 "k8s.io/api/admissionregistration/v1"
75
appsv1 "k8s.io/api/apps/v1"
86
corev1 "k8s.io/api/core/v1"
@@ -217,36 +215,41 @@ func CreateDeploymentResource(name string, namespace string, opts ...ResourceCre
217215
).(*appsv1.Deployment)
218216
}
219217

220-
func CreateValidatingWebhookConfigurationResource(generateName string, namespace string, opts ...ResourceCreatorOption) *admissionregistrationv1.ValidatingWebhookConfiguration {
218+
// CreateValidatingWebhookConfigurationResource creates a ValidatingWebhookConfiguration resource with name 'name',
219+
// namespace 'namespace', and applying any ValidatingWebhookConfiguration related options in opts
220+
func CreateValidatingWebhookConfigurationResource(name string, namespace string, opts ...ResourceCreatorOption) *admissionregistrationv1.ValidatingWebhookConfiguration {
221221
return ResourceCreatorOptions(opts).ApplyTo(
222222
&admissionregistrationv1.ValidatingWebhookConfiguration{
223223
TypeMeta: metav1.TypeMeta{
224224
Kind: "ValidatingWebhookConfiguration",
225225
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
226226
},
227227
ObjectMeta: metav1.ObjectMeta{
228-
GenerateName: fmt.Sprintf("%s-", generateName),
229-
Namespace: namespace,
228+
Name: name,
229+
Namespace: namespace,
230230
},
231231
},
232232
).(*admissionregistrationv1.ValidatingWebhookConfiguration)
233233
}
234234

235-
func CreateMutatingWebhookConfigurationResource(generateName string, namespace string, opts ...ResourceCreatorOption) *admissionregistrationv1.MutatingWebhookConfiguration {
235+
// CreateMutatingWebhookConfigurationResource creates a MutatingWebhookConfiguration resource with name 'name',
236+
// namespace 'namespace', and applying any MutatingWebhookConfiguration related options in opts
237+
func CreateMutatingWebhookConfigurationResource(name string, namespace string, opts ...ResourceCreatorOption) *admissionregistrationv1.MutatingWebhookConfiguration {
236238
return ResourceCreatorOptions(opts).ApplyTo(
237239
&admissionregistrationv1.MutatingWebhookConfiguration{
238240
TypeMeta: metav1.TypeMeta{
239241
Kind: "MutatingWebhookConfiguration",
240242
APIVersion: admissionregistrationv1.SchemeGroupVersion.String(),
241243
},
242244
ObjectMeta: metav1.ObjectMeta{
243-
GenerateName: fmt.Sprintf("%s-", generateName),
244-
Namespace: namespace,
245+
Name: name,
246+
Namespace: namespace,
245247
},
246248
},
247249
).(*admissionregistrationv1.MutatingWebhookConfiguration)
248250
}
249251

252+
// CreateServiceResource creates a Service resource with name 'name', namespace 'namespace', and applying any Service related options in opts
250253
func CreateServiceResource(name string, namespace string, opts ...ResourceCreatorOption) *corev1.Service {
251254
return ResourceCreatorOptions(opts).ApplyTo(&corev1.Service{
252255
TypeMeta: metav1.TypeMeta{

internal/operator-controller/rukpak/render/generators/resources_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,14 @@ func Test_CreateService(t *testing.T) {
8585
func Test_CreateValidatingWebhookConfiguration(t *testing.T) {
8686
wh := generators.CreateValidatingWebhookConfigurationResource("my-validating-webhook-configuration", "my-namespace")
8787
require.NotNil(t, wh)
88-
require.Equal(t, "my-validating-webhook-configuration-", wh.GenerateName)
88+
require.Equal(t, "my-validating-webhook-configuration", wh.Name)
8989
require.Equal(t, "my-namespace", wh.Namespace)
9090
}
9191

9292
func Test_CreateMutatingWebhookConfiguration(t *testing.T) {
9393
wh := generators.CreateMutatingWebhookConfigurationResource("my-mutating-webhook-configuration", "my-namespace")
9494
require.NotNil(t, wh)
95-
require.Equal(t, "my-mutating-webhook-configuration-", wh.GenerateName)
95+
require.Equal(t, "my-mutating-webhook-configuration", wh.Name)
9696
require.Equal(t, "my-namespace", wh.Namespace)
9797
}
9898

0 commit comments

Comments
 (0)